add OpenAI like tool_choice for named choice

This commit is contained in:
Linus Bierhoff 2024-10-10 18:50:32 +02:00
parent 3dbdf63ec5
commit 2285b0d63e
2 changed files with 30 additions and 0 deletions

View File

@ -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.",

View File

@ -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,
@ -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 {
@ -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)),
}
}