ollama: Fix tool calling (#42275)
Closes #42303 Ollama added tool call identifiers (https://github.com/ollama/ollama/pull/12956) in its latest version [v0.12.10](https://github.com/ollama/ollama/releases/tag/v0.12.10). This broke our json schema and made all tool calls fail. This PR fixes the schema and uses the Ollama provided tool call identifier when available. We remain backwards compatible and still use our own identifier with older versions of Ollama. I added a `TODO` to remove the `Option` around the new field when most users have updated their installations to v0.12.10 or above. Note to reviewer: The fix to this issue should likely get cherry-picked into the next release, since Ollama becomes unusable as an agent without it. Release Notes: - Fixed tool calling when using the latest version of Ollama
This commit is contained in:
@@ -381,10 +381,13 @@ impl OllamaLanguageModel {
|
||||
thinking = Some(text)
|
||||
}
|
||||
MessageContent::ToolUse(tool_use) => {
|
||||
tool_calls.push(OllamaToolCall::Function(OllamaFunctionCall {
|
||||
name: tool_use.name.to_string(),
|
||||
arguments: tool_use.input,
|
||||
}));
|
||||
tool_calls.push(OllamaToolCall {
|
||||
id: Some(tool_use.id.to_string()),
|
||||
function: OllamaFunctionCall {
|
||||
name: tool_use.name.to_string(),
|
||||
arguments: tool_use.input,
|
||||
},
|
||||
});
|
||||
}
|
||||
_ => (),
|
||||
}
|
||||
@@ -575,25 +578,23 @@ fn map_to_language_model_completion_events(
|
||||
}
|
||||
|
||||
if let Some(tool_call) = tool_calls.and_then(|v| v.into_iter().next()) {
|
||||
match tool_call {
|
||||
OllamaToolCall::Function(function) => {
|
||||
let tool_id = format!(
|
||||
"{}-{}",
|
||||
&function.name,
|
||||
TOOL_CALL_COUNTER.fetch_add(1, Ordering::Relaxed)
|
||||
);
|
||||
let event =
|
||||
LanguageModelCompletionEvent::ToolUse(LanguageModelToolUse {
|
||||
id: LanguageModelToolUseId::from(tool_id),
|
||||
name: Arc::from(function.name),
|
||||
raw_input: function.arguments.to_string(),
|
||||
input: function.arguments,
|
||||
is_input_complete: true,
|
||||
});
|
||||
events.push(Ok(event));
|
||||
state.used_tools = true;
|
||||
}
|
||||
}
|
||||
let OllamaToolCall { id, function } = tool_call;
|
||||
let id = id.unwrap_or_else(|| {
|
||||
format!(
|
||||
"{}-{}",
|
||||
&function.name,
|
||||
TOOL_CALL_COUNTER.fetch_add(1, Ordering::Relaxed)
|
||||
)
|
||||
});
|
||||
let event = LanguageModelCompletionEvent::ToolUse(LanguageModelToolUse {
|
||||
id: LanguageModelToolUseId::from(id),
|
||||
name: Arc::from(function.name),
|
||||
raw_input: function.arguments.to_string(),
|
||||
input: function.arguments,
|
||||
is_input_complete: true,
|
||||
});
|
||||
events.push(Ok(event));
|
||||
state.used_tools = true;
|
||||
} else if !content.is_empty() {
|
||||
events.push(Ok(LanguageModelCompletionEvent::Text(content)));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user