refactoring
This commit is contained in:
parent
04ece6afb1
commit
13e17770ca
12 changed files with 634 additions and 598 deletions
83
src/domain/agent/tools.rs
Normal file
83
src/domain/agent/tools.rs
Normal file
|
|
@ -0,0 +1,83 @@
|
|||
use super::api::{self, FunctionDefinition, Message, Tool, ToolCall};
|
||||
use std::collections::HashMap;
|
||||
|
||||
pub fn get_tools() -> Vec<Tool> {
|
||||
vec![
|
||||
Tool {
|
||||
tool_type: "function".to_string(),
|
||||
function: FunctionDefinition {
|
||||
name: "google_search".to_string(),
|
||||
description: "Search the web for information".to_string(),
|
||||
parameters: serde_json::json!({
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"query": {
|
||||
"type": "string",
|
||||
"description": "The search query"
|
||||
}
|
||||
},
|
||||
"required": ["query"]
|
||||
}),
|
||||
},
|
||||
},
|
||||
Tool {
|
||||
tool_type: "function".to_string(),
|
||||
function: FunctionDefinition {
|
||||
name: "finish".to_string(),
|
||||
description: "Finish the task".to_string(),
|
||||
parameters: serde_json::json!({
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"result": {
|
||||
"type": "string",
|
||||
"description": "The result of the task"
|
||||
}
|
||||
},
|
||||
"required": ["result"]
|
||||
}),
|
||||
},
|
||||
},
|
||||
]
|
||||
}
|
||||
|
||||
pub async fn handle_tool_call(
|
||||
tool_call: &ToolCall,
|
||||
tavily_api_key: &Option<String>,
|
||||
) -> Result<(Message, bool, Option<String>), Box<dyn std::error::Error>> {
|
||||
let mut answer = None;
|
||||
let name = &tool_call.function.name;
|
||||
|
||||
let (content, written) = if name == "google_search" {
|
||||
let args: HashMap<String, String> = serde_json::from_str(&tool_call.function.arguments)?;
|
||||
let query = args.get("query").ok_or("Missing query argument")?;
|
||||
|
||||
let search_result = if let Some(key) = tavily_api_key {
|
||||
match api::perform_search(query, key).await {
|
||||
Ok(results) => results,
|
||||
Err(e) => format!("Search error: {}", e),
|
||||
}
|
||||
} else {
|
||||
"Error: TAVILY_API_KEY is not set. Cannot perform real search.".to_string()
|
||||
};
|
||||
(search_result, false)
|
||||
} else if name == "finish" {
|
||||
let args: HashMap<String, String> = serde_json::from_str(&tool_call.function.arguments)?;
|
||||
let result = args.get("result").ok_or("Missing result argument")?;
|
||||
|
||||
answer = Some(result.clone());
|
||||
(result.clone(), true)
|
||||
} else {
|
||||
(format!("Error: Unknown tool {}", name), false)
|
||||
};
|
||||
|
||||
Ok((
|
||||
Message {
|
||||
role: "tool".to_string(),
|
||||
content: Some(content),
|
||||
tool_calls: None,
|
||||
tool_call_id: Some(tool_call.id.clone()),
|
||||
},
|
||||
written,
|
||||
answer,
|
||||
))
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue