Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OpenAI function calling compatible support #1888

Merged
merged 3 commits into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions router/src/infer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -979,24 +979,28 @@ mod tests {
content: Some("Hi!".to_string()),
name: None,
tool_calls: None,
tool_call_id: None,
},
Message {
role: "assistant".to_string(),
content: Some("Hello how can I help?".to_string()),
name: None,
tool_calls: None,
tool_call_id: None,
},
Message {
role: "user".to_string(),
content: Some("What is Deep Learning?".to_string()),
name: None,
tool_calls: None,
tool_call_id: None,
},
Message {
role: "assistant".to_string(),
content: Some("magic!".to_string()),
name: None,
tool_calls: None,
tool_call_id: None,
},
],
bos_token: Some("[BOS]"),
Expand Down Expand Up @@ -1049,30 +1053,35 @@ mod tests {
content: Some("Hi!".to_string()),
name: None,
tool_calls: None,
tool_call_id: None,
},
Message {
role: "user".to_string(),
content: Some("Hi again!".to_string()),
name: None,
tool_calls: None,
tool_call_id: None,
},
Message {
role: "assistant".to_string(),
content: Some("Hello how can I help?".to_string()),
name: None,
tool_calls: None,
tool_call_id: None,
},
Message {
role: "user".to_string(),
content: Some("What is Deep Learning?".to_string()),
name: None,
tool_calls: None,
tool_call_id: None,
},
Message {
role: "assistant".to_string(),
content: Some("magic!".to_string()),
name: None,
tool_calls: None,
tool_call_id: None,
},
],
bos_token: Some("[BOS]"),
Expand Down Expand Up @@ -1130,24 +1139,28 @@ mod tests {
content: Some("Hi!".to_string()),
name: None,
tool_calls: None,
tool_call_id: None,
},
Message {
role: "assistant".to_string(),
content: Some("Hello how can I help?".to_string()),
name: None,
tool_calls: None,
tool_call_id: None,
},
Message {
role: "user".to_string(),
content: Some("What is Deep Learning?".to_string()),
name: None,
tool_calls: None,
tool_call_id: None,
},
Message {
role: "assistant".to_string(),
content: Some("magic!".to_string()),
name: None,
tool_calls: None,
tool_call_id: None,
},
],
bos_token: Some("[BOS]"),
Expand Down Expand Up @@ -1189,24 +1202,28 @@ mod tests {
content: Some("Hi!".to_string()),
name: None,
tool_calls: None,
tool_call_id: None,
},
Message {
role: "assistant".to_string(),
content: Some("Hello how can I help?".to_string()),
name: None,
tool_calls: None,
tool_call_id: None,
},
Message {
role: "user".to_string(),
content: Some("What is Deep Learning?".to_string()),
name: None,
tool_calls: None,
tool_call_id: None,
},
Message {
role: "assistant".to_string(),
content: Some("magic!".to_string()),
name: None,
tool_calls: None,
tool_call_id: None,
},
],
bos_token: Some("[BOS]"),
Expand Down Expand Up @@ -1234,18 +1251,21 @@ mod tests {
content: Some("Hello, how are you?".to_string()),
name: None,
tool_calls: None,
tool_call_id: None,
},
Message {
role: "assistant".to_string(),
content: Some("I'm doing great. How can I help you today?".to_string()),
name: None,
tool_calls: None,
tool_call_id: None,
},
Message {
role: "user".to_string(),
content: Some("I'd like to show off how chat templating works!".to_string()),
name: None,
tool_calls: None,
tool_call_id: None,
},
];

Expand All @@ -1257,6 +1277,7 @@ mod tests {
),
name: None,
tool_calls: None,
tool_call_id: None,
}]
.iter()
.chain(&example_chat)
Expand Down Expand Up @@ -1401,12 +1422,14 @@ mod tests {
content: Some("You are a friendly chatbot who always responds in the style of a pirate".to_string()),
name: None,
tool_calls: None,
tool_call_id: None,
},
Message {
role: "user".to_string(),
content: Some("How many helicopters can a human eat in one sitting?".to_string()),
name: None,
tool_calls: None,
tool_call_id: None,
},
],
add_generation_prompt: true,
Expand Down
8 changes: 6 additions & 2 deletions router/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,7 @@ impl ChatCompletion {
content: output,
name: None,
tool_calls,
tool_call_id: None,
},
logprobs: return_logprobs
.then(|| ChatCompletionLogprobs::from((details.tokens, details.top_tokens))),
Expand Down Expand Up @@ -881,7 +882,7 @@ pub(crate) struct ChatTemplateInputs<'a> {

#[derive(Clone, Deserialize, Serialize, ToSchema, Default, Debug)]
pub(crate) struct ToolCall {
pub id: u32,
pub id: String,
pub r#type: String,
pub function: FunctionDefinition,
}
Expand Down Expand Up @@ -954,13 +955,16 @@ pub(crate) struct Message {
pub role: String,
#[serde(skip_serializing_if = "Option::is_none")]
#[schema(example = "My name is David and I")]
#[serde(deserialize_with = "message_content_serde::deserialize")]
#[serde(default, deserialize_with = "message_content_serde::deserialize")]
pub content: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
#[schema(example = "\"David\"")]
pub name: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub tool_calls: Option<Vec<ToolCall>>,
#[serde(default, skip_serializing_if = "Option::is_none")]
#[schema(example = "\"get_weather\"")]
pub tool_call_id: Option<String>,
}

#[derive(Clone, Debug, Deserialize, ToSchema)]
Expand Down
3 changes: 1 addition & 2 deletions router/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -988,7 +988,6 @@ async fn chat_completions(
) -> Result<Response, (StatusCode, Json<ErrorResponse>)> {
let span = tracing::Span::current();
metrics::increment_counter!("tgi_request_count");

let ChatRequest {
logprobs,
max_tokens,
Expand Down Expand Up @@ -1160,7 +1159,7 @@ async fn chat_completions(
)
})?;
let tool_calls = vec![ToolCall {
id: 0,
id: "0".to_string(),
r#type: "function".to_string(),
function: FunctionDefinition {
description: None,
Expand Down