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

Fix tool call response to adhere to OpenAI spec #2949

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
10 changes: 5 additions & 5 deletions router/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -727,7 +727,7 @@ pub(crate) struct ChatCompletionChoice {
pub struct ToolCallDelta {
#[schema(example = "assistant")]
role: String,
tool_calls: DeltaToolCall,
tool_calls: Vec<DeltaToolCall>, // Changed to Vec<DeltaToolCall>
}

#[derive(Clone, Debug, Serialize, ToSchema)]
Expand Down Expand Up @@ -770,15 +770,15 @@ impl ChatCompletionChunk {
}),
(None, Some(tool_calls)) => ChatCompletionDelta::Tool(ToolCallDelta {
role: "assistant".to_string(),
tool_calls: DeltaToolCall {
index: 0,
tool_calls: tool_calls.iter().enumerate().map(|(index, tool_call)| DeltaToolCall {
index: index as u32,
id: String::new(),
r#type: "function".to_string(),
function: Function {
name: None,
arguments: tool_calls[0].to_string(),
arguments: tool_call.to_string(),
},
},
}).collect(),
}),
(None, None) => ChatCompletionDelta::Chat(TextMessage {
role: "assistant".to_string(),
Expand Down
35 changes: 31 additions & 4 deletions router/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1138,7 +1138,27 @@ fn create_event_from_stream_token(

// replace the content with the tool calls if grammar is present
let (content, tool_calls) = if inner_using_tools {
(None, Some(vec![stream_token.token.text.clone()]))
// Create a DeltaToolCall object
let delta_tool_call = DeltaToolCall {
index: 0, // Assuming this is the first tool call
id: "0".to_string(), // Generate a unique ID here
r#type: "function".to_string(),
function: Function {
name: Some(stream_token.token.text.clone()), // Wrap in Some
arguments: stream_token.token.text.clone(), // Assuming the arguments are in the token text
},
};

// Serialize the DeltaToolCall into a JSON string
let tool_call_string = serde_json::to_string(&delta_tool_call).unwrap_or_else(|e| {
println!("Failed to serialize DeltaToolCall: {:?}", e);
String::new()
});

// Wrap the serialized tool call in a Vec and Option
let tool_calls = Some(vec![tool_call_string]);

(None, tool_calls)
} else {
let content = if !stream_token.token.special {
Some(stream_token.token.text.clone())
Expand Down Expand Up @@ -1371,7 +1391,7 @@ pub(crate) async fn chat_completions(
system_fingerprint.clone(),
model_id.clone(),
);

yield Ok::<Event, Infallible>(event);
}
}
Expand Down Expand Up @@ -1432,13 +1452,20 @@ pub(crate) async fn chat_completions(
(None, Some(content_message))
}
_ => {
let arguments_string = serde_json::to_string(&arguments).map_err(|e| {
InferError::ToolError(format!(
"Failed to serialize arguments to string: {}",
e
))
})?;

let tool_calls = vec![ToolCall {
id: "0".to_string(),
id: format!("{:09}", 0),
r#type: "function".to_string(),
function: FunctionDefinition {
description: None,
name,
arguments,
arguments: Value::String(arguments_string), // Serialize to string here
},
}];
(Some(tool_calls), None)
Expand Down