fix: gemini empty content (#1425)
This commit is contained in:
@@ -63,6 +63,7 @@ pub fn format_messages(messages: &[Message]) -> Vec<Value> {
|
|||||||
.map(|content| content.unannotated())
|
.map(|content| content.unannotated())
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
|
let mut tool_content = Vec::new();
|
||||||
for content in abridged {
|
for content in abridged {
|
||||||
match content {
|
match content {
|
||||||
Content::Image(image) => {
|
Content::Image(image) => {
|
||||||
@@ -74,15 +75,29 @@ pub fn format_messages(messages: &[Message]) -> Vec<Value> {
|
|||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
parts.push(json!({
|
tool_content.push(content);
|
||||||
"functionResponse": {
|
|
||||||
"name": response.id,
|
|
||||||
"response": {"content": content},
|
|
||||||
}}
|
|
||||||
));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
let mut text = tool_content
|
||||||
|
.iter()
|
||||||
|
.filter_map(|c| match c {
|
||||||
|
Content::Text(t) => Some(t.text.clone()),
|
||||||
|
_ => None,
|
||||||
|
})
|
||||||
|
.collect::<Vec<_>>()
|
||||||
|
.join("\n");
|
||||||
|
if !tool_content.is_empty() {
|
||||||
|
if text.is_empty() {
|
||||||
|
text = "Tool call is done.".to_string();
|
||||||
|
}
|
||||||
|
parts.push(json!({
|
||||||
|
"functionResponse": {
|
||||||
|
"name": response.id,
|
||||||
|
"response": {"content": {"text": text}},
|
||||||
|
}}
|
||||||
|
));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
parts.push(json!({"text":format!("Error: {}", e)}));
|
parts.push(json!({"text":format!("Error: {}", e)}));
|
||||||
@@ -400,6 +415,36 @@ mod tests {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_message_to_google_spec_tool_result_multiple_texts() {
|
||||||
|
let tool_result: Vec<Content> = vec![
|
||||||
|
Content::text("Hello"),
|
||||||
|
Content::text("World"),
|
||||||
|
Content::text("This is a test."),
|
||||||
|
];
|
||||||
|
|
||||||
|
let messages = vec![set_up_tool_response_message("response_id", tool_result)];
|
||||||
|
let payload = format_messages(&messages);
|
||||||
|
|
||||||
|
let expected_payload = vec![json!({
|
||||||
|
"role": "model",
|
||||||
|
"parts": [
|
||||||
|
{
|
||||||
|
"functionResponse": {
|
||||||
|
"name": "response_id",
|
||||||
|
"response": {
|
||||||
|
"content": {
|
||||||
|
"text": "Hello\nWorld\nThis is a test."
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
})];
|
||||||
|
|
||||||
|
assert_eq!(payload, expected_payload);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_tools_to_google_spec_with_valid_tools() {
|
fn test_tools_to_google_spec_with_valid_tools() {
|
||||||
let params1 = json!({
|
let params1 = json!({
|
||||||
|
|||||||
Reference in New Issue
Block a user