fix: gemini with functions that have empty parameters (#666)

This commit is contained in:
sigoden 2024-06-28 07:11:20 +08:00 committed by GitHub
parent 4fbbbd2d99
commit 8b8f8b37c3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 21 additions and 1 deletions

View File

@ -353,7 +353,18 @@ pub fn gemini_build_chat_completions_body(
}
if let Some(functions) = functions {
body["tools"] = json!([{ "functionDeclarations": *functions }]);
// Gemini doesn't support functions with parameters that have empty properties, so we need to patch it.
let function_declarations: Vec<_> = functions.into_iter().map(|function| {
if function.parameters.is_empty_properties() {
json!({
"name": function.name,
"description": function.description,
})
} else {
json!(function)
}
}).collect();
body["tools"] = json!([{ "functionDeclarations": function_declarations }]);
}
Ok(body)

View File

@ -125,6 +125,15 @@ pub struct JsonSchema {
pub required: Option<Vec<String>>,
}
impl JsonSchema {
pub fn is_empty_properties(&self) -> bool {
match &self.properties {
Some(v) => v.is_empty(),
None => true,
}
}
}
#[derive(Debug, Clone, Default, Deserialize, Serialize)]
pub struct ToolCall {
pub name: String,