From c55abac3846776abdf31e4a7ddef5fb4b0347635 Mon Sep 17 00:00:00 2001 From: Aaron Mihalik Date: Thu, 15 Feb 2024 13:30:31 -0500 Subject: [PATCH] Added `name` field to OpenAI compatible API Messages (#1563) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit # What does this PR do? Literally just adds the name field to the Message class. I verified this change by building a new docker container (using the `Dockerfile` in the repo) and trialing with a `chat_template` that uses the `name` field. Here's the previous behavior: Input messages: ``` { "messages": [ {"role": "system", "content": "You are a succinct but helpful AI Assistant listening to a chat server. Address everyone by @"}, {"role": "user", "name": "Aaron", "content": "Hello There!"}, {"role": "assistant", "content": " Hello @Aaron! How can I assist you today?"}, {"role": "user", "name": "Sally", "content": "Hiya everyone. Is @Aaron is this room?"} ], "model": "meta-llama/Llama-2-7b-chat-hf" } ``` Response before the modification: ``` Hello @Aaron! Yes, you are in the chat room. How can I assist you today? 😊 Hiya everyone! *waves* It's great to see you all here. Is there something on your mind that you'd like to talk about or ask? I'm here to listen and help in any way I can. 🤖 ``` Response after my modification: ``` Hello @Sally! Yes, @Aaron is currently in the chat room. How may I assist you today? ``` Fixes #1558 ## Before submitting - [ ] This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case). - [x] Did you read the [contributor guideline](https://github.com/huggingface/transformers/blob/main/CONTRIBUTING.md#start-contributing-pull-requests), Pull Request section? - [ ] Was this discussed/approved via a Github issue or the [forum](https://discuss.huggingface.co/)? Please add a link to it if that's the case. - [ ] Did you make sure to update the documentation with your changes? Here are the [documentation guidelines](https://github.com/huggingface/transformers/tree/main/docs), and [here are tips on formatting docstrings](https://github.com/huggingface/transformers/tree/main/docs#writing-source-documentation). - [ ] Did you write any new necessary tests? ## Who can review? @Narsil --------- Co-authored-by: Aaron Mihalik Co-authored-by: drbh --- router/src/infer.rs | 17 +++++++++++++++++ router/src/lib.rs | 4 ++++ 2 files changed, 21 insertions(+) diff --git a/router/src/infer.rs b/router/src/infer.rs index d7b9b52b..a45685cb 100644 --- a/router/src/infer.rs +++ b/router/src/infer.rs @@ -800,18 +800,22 @@ mod tests { Message { role: "user".to_string(), content: "Hi!".to_string(), + name: None, }, Message { role: "assistant".to_string(), content: "Hello how can I help?".to_string(), + name: None, }, Message { role: "user".to_string(), content: "What is Deep Learning?".to_string(), + name: None, }, Message { role: "assistant".to_string(), content: "magic!".to_string(), + name: None, }, ], bos_token: Some("[BOS]"), @@ -861,22 +865,27 @@ mod tests { Message { role: "user".to_string(), content: "Hi!".to_string(), + name: None, }, Message { role: "user".to_string(), content: "Hi again!".to_string(), + name: None, }, Message { role: "assistant".to_string(), content: "Hello how can I help?".to_string(), + name: None, }, Message { role: "user".to_string(), content: "What is Deep Learning?".to_string(), + name: None, }, Message { role: "assistant".to_string(), content: "magic!".to_string(), + name: None, }, ], bos_token: Some("[BOS]"), @@ -931,18 +940,22 @@ mod tests { Message { role: "user".to_string(), content: "Hi!".to_string(), + name: None, }, Message { role: "assistant".to_string(), content: "Hello how can I help?".to_string(), + name: None, }, Message { role: "user".to_string(), content: "What is Deep Learning?".to_string(), + name: None, }, Message { role: "assistant".to_string(), content: "magic!".to_string(), + name: None, }, ], bos_token: Some("[BOS]"), @@ -981,18 +994,22 @@ mod tests { Message { role: "user".to_string(), content: "Hi!".to_string(), + name: None, }, Message { role: "assistant".to_string(), content: "Hello how can I help?".to_string(), + name: None, }, Message { role: "user".to_string(), content: "What is Deep Learning?".to_string(), + name: None, }, Message { role: "assistant".to_string(), content: "magic!".to_string(), + name: None, }, ], bos_token: Some("[BOS]"), diff --git a/router/src/lib.rs b/router/src/lib.rs index 87873821..8c7ca74b 100644 --- a/router/src/lib.rs +++ b/router/src/lib.rs @@ -378,6 +378,7 @@ impl ChatCompletion { message: Message { role: "assistant".into(), content: output, + name: None, }, logprobs: return_logprobs .then(|| ChatCompletionLogprobs::from((details.tokens, details.top_tokens))), @@ -453,6 +454,7 @@ fn default_request_messages() -> Vec { vec![Message { role: "user".to_string(), content: "My name is David and I".to_string(), + name: None, }] } @@ -547,6 +549,8 @@ pub(crate) struct Message { pub role: String, #[schema(example = "My name is David and I")] pub content: String, + #[schema(example = "\"David\"")] + pub name: Option, } #[derive(Clone, Debug, Deserialize, ToSchema)]