feat: ollama tool shim (#1448)

Co-authored-by: Alice Hau <ahau@squareup.com>
This commit is contained in:
Alice Hau
2025-03-12 11:01:28 -04:00
committed by GitHub
parent 6abc336484
commit 259ccd516e
7 changed files with 455 additions and 80 deletions
+23 -2
View File
@@ -19,6 +19,9 @@ use crate::message::{Message, ToolRequest};
use crate::providers::base::Provider;
use crate::providers::base::ProviderUsage;
use crate::providers::errors::ProviderError;
use crate::providers::toolshim::{
augment_message_with_tool_calls, modify_system_prompt_for_tool_json, OllamaInterpreter,
};
use crate::register_agent;
use crate::session;
use crate::token_counter::TokenCounter;
@@ -217,7 +220,17 @@ impl Agent for TruncateAgent {
tools.push(list_resources_tool);
}
let system_prompt = capabilities.get_system_prompt().await;
let config = capabilities.provider().get_model_config();
let mut system_prompt = capabilities.get_system_prompt().await;
let mut toolshim_tools = vec![];
if config.toolshim {
// If tool interpretation is enabled, modify the system prompt to instruct to return JSON tool requests
system_prompt = modify_system_prompt_for_tool_json(&system_prompt, &tools);
// make a copy of tools before empty
toolshim_tools = tools.clone();
// pass empty tools vector to provider completion since toolshim will handle tool calls instead
tools = vec![];
}
// Set the user_message field in the span instead of creating a new event
if let Some(content) = messages
@@ -236,7 +249,15 @@ impl Agent for TruncateAgent {
&messages,
&tools,
).await {
Ok((response, usage)) => {
Ok((mut response, usage)) => {
// Post-process / structure the response only if tool interpretation is enabled
if config.toolshim {
let interpreter = OllamaInterpreter::new()
.map_err(|e| anyhow::anyhow!("Failed to create OllamaInterpreter: {}", e))?;
response = augment_message_with_tool_calls(&interpreter, response, &toolshim_tools).await?;
}
capabilities.record_usage(usage.clone()).await;
// record usage for the session in the session file