summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorsigoden <sigoden@gmail.com>2024-06-28 07:11:20 +0800
committerGitHub <noreply@github.com>2024-06-28 07:11:20 +0800
commit8b8f8b37c35479f747a70a65f069c0d8d4b01dcf (patch)
tree35a235d09289357373e4235789fcbda1da57a404
parent4fbbbd2d991b37ac04b77151ef862de9649bbfec (diff)
downloadaichat-8b8f8b37c35479f747a70a65f069c0d8d4b01dcf.tar.gz
fix: gemini with functions that have empty parameters (#666)
-rw-r--r--src/client/vertexai.rs13
-rw-r--r--src/function.rs9
2 files changed, 21 insertions, 1 deletions
diff --git a/src/client/vertexai.rs b/src/client/vertexai.rs
index cc7e464..a4c4050 100644
--- a/src/client/vertexai.rs
+++ b/src/client/vertexai.rs
@@ -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)
diff --git a/src/function.rs b/src/function.rs
index 8528af5..e99d2c6 100644
--- a/src/function.rs
+++ b/src/function.rs
@@ -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,