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 the tool_choice format for named choice by adapting OpenAIs scheme #2634

Closed
wants to merge 2 commits into from
Closed
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
18 changes: 18 additions & 0 deletions docs/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -2137,6 +2137,24 @@
"$ref": "#/components/schemas/FunctionName"
}
}
},
{
"type": "object",
"required": [
"type",
"function"
],
"properties": {
"type": {
"type": "string",
"enum": [
"function"
]
},
"function": {
"$ref": "#/components/schemas/FunctionName"
}
}
}
],
"description": "Controls which (if any) tool is called by the model.",
Expand Down
44 changes: 44 additions & 0 deletions router/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -968,9 +968,18 @@ pub enum ToolType {
NoTool,
/// Forces the model to call a specific tool.
#[schema(rename = "function")]
#[serde(alias = "function")]
Function(FunctionName),
}


#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
#[serde(tag = "type")]
pub enum TypedChoice {
#[serde(rename = "function")]
Function{function: FunctionName},
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, ToSchema)]
pub struct FunctionName {
pub name: String,
Expand All @@ -986,8 +995,10 @@ enum ToolTypeDeserializer {
Null,
String(String),
ToolType(ToolType),
TypedChoice(TypedChoice) //this is the OpenAI schema
}


impl From<ToolTypeDeserializer> for ToolChoice {
fn from(value: ToolTypeDeserializer) -> Self {
match value {
Expand All @@ -997,6 +1008,7 @@ impl From<ToolTypeDeserializer> for ToolChoice {
"auto" => ToolChoice(Some(ToolType::OneOf)),
_ => ToolChoice(Some(ToolType::Function(FunctionName { name: s }))),
},
ToolTypeDeserializer::TypedChoice(TypedChoice::Function{function}) => ToolChoice(Some(ToolType::Function(function))),
ToolTypeDeserializer::ToolType(tool_type) => ToolChoice(Some(tool_type)),
}
}
Expand Down Expand Up @@ -1604,4 +1616,36 @@ mod tests {
r#"{"role":"assistant","tool_calls":[{"id":"0","type":"function","function":{"description":null,"name":"myfn","arguments":{"format":"csv"}}}]}"#
);
}

#[test]
fn tool_choice_formats() {

#[derive(Deserialize)]
struct TestRequest {
tool_choice: ToolChoice,
}

let none = r#"{"tool_choice":"none"}"#;
let de_none: TestRequest = serde_json::from_str(none).unwrap();
assert_eq!(de_none.tool_choice, ToolChoice(Some(ToolType::NoTool)));

let auto = r#"{"tool_choice":"auto"}"#;
let de_auto: TestRequest = serde_json::from_str(auto).unwrap();
assert_eq!(de_auto.tool_choice, ToolChoice(Some(ToolType::OneOf)));

let ref_choice = ToolChoice(Some(ToolType::Function(FunctionName { name: "myfn".to_string() })));

let named = r#"{"tool_choice":"myfn"}"#;
let de_named: TestRequest = serde_json::from_str(named).unwrap();
assert_eq!(de_named.tool_choice, ref_choice);

let old_named = r#"{"tool_choice":{"function":{"name":"myfn"}}}"#;
let de_old_named: TestRequest = serde_json::from_str(old_named).unwrap();
assert_eq!(de_old_named.tool_choice, ref_choice);

let openai_named = r#"{"tool_choice":{"type":"function","function":{"name":"myfn"}}}"#;
let de_openai_named: TestRequest = serde_json::from_str(openai_named).unwrap();

assert_eq!(de_openai_named.tool_choice, ref_choice);
}
}
Loading