aquafire / tool_agent.rs

Last active 6 months ago

Like 0

Revision 1d755dc10f3f03cb2a5b1ff7c979ed11d4b97fc7

tool_agent.rs Raw
1// use open_agent::{AgentOptions, Client, ContentBlock, tool};
2// use serde_json::json;
3// use std::collections::HashMap;
4
5// #[tokio::main]
6// async fn main() -> Result<(), Box<dyn std::error::Error>> {
7// // Define calculator tools
8// let add_tool = tool("add", "Add two numbers")
9// .param("a", "number")
10// .param("b", "number")
11// .build(|args| async move {
12// let a = args["a"].as_f64().unwrap_or(0.0);
13// let b = args["b"].as_f64().unwrap_or(0.0);
14// Ok(json!({"result": a + b}))
15// });
16
17// let subtract_tool = tool("subtract", "Subtract two numbers")
18// .param("a", "number")
19// .param("b", "number")
20// .build(|args| async move {
21// let a = args["a"].as_f64().unwrap_or(0.0);
22// let b = args["b"].as_f64().unwrap_or(0.0);
23// Ok(json!({"result": a - b}))
24// });
25
26// let multiply_tool = tool("multiply", "Multiply two numbers")
27// .param("a", "number")
28// .param("b", "number")
29// .build(|args| async move {
30// let a = args["a"].as_f64().unwrap_or(0.0);
31// let b = args["b"].as_f64().unwrap_or(0.0);
32// Ok(json!({"result": a * b}))
33// });
34
35// let divide_tool = tool("divide", "Divide two numbers")
36// .param("a", "number")
37// .param("b", "number")
38// .build(|args| async move {
39// let a = args["a"].as_f64().unwrap_or(0.0);
40// let b = args["b"].as_f64().unwrap_or(0.0);
41// if b == 0.0 {
42// return Ok(json!({"error": "Cannot divide by zero"}));
43// }
44// Ok(json!({"result": a / b}))
45// });
46
47// // Create tool registry for lookup
48// let mut tool_registry = HashMap::new();
49// tool_registry.insert("add".to_string(), add_tool.clone());
50// tool_registry.insert("subtract".to_string(), subtract_tool.clone());
51// tool_registry.insert("multiply".to_string(), multiply_tool.clone());
52// tool_registry.insert("divide".to_string(), divide_tool.clone());
53
54// // Configure agent with tools
55// let options = AgentOptions::builder()
56// .system_prompt(
57// "You are a helpful calculator assistant. \
58// Use the provided tools to perform calculations. \
59// Always show your work and explain the result.",
60// )
61// .model("qwen/qwen3-vl-30b")
62// .base_url("http://localhost:1234/v1")
63// .tools(vec![add_tool, subtract_tool, multiply_tool, divide_tool])
64// .auto_execute_tools(false) // Manual mode for this example
65// .max_turns(5)
66// .temperature(0.1)
67// .build()?;
68
69// // Example calculations
70// let queries = vec![
71// "What is 25 plus 17?",
72// "Calculate 144 divided by 12",
73// "What's 7 times 8, then add 5?",
74// ];
75
76// println!("{}", "=".repeat(70));
77// println!("CALCULATOR TOOLS EXAMPLE");
78// println!("{}", "=".repeat(70));
79// println!();
80
81// for query in queries {
82// println!("User: {}", query);
83// println!("{}", "-".repeat(50));
84
85// let mut client = Client::new(options.clone())?;
86// client.send(query).await?;
87
88// // Process responses and handle tool calls
89// while let Some(block) = client.receive().await? {
90// match block {
91// ContentBlock::Text(text) => {
92// if !text.text.trim().is_empty() {
93// println!("Assistant: {}", text.text);
94// }
95// }
96// ContentBlock::ToolUse(tool_use) => {
97// println!("🔧 Tool call: {}", tool_use.name);
98// println!(" Arguments: {}", tool_use.input);
99
100// // Look up and execute tool
101// if let Some(tool) = tool_registry.get(&tool_use.name) {
102// match tool.execute(tool_use.input.clone()).await {
103// Ok(result) => {
104// println!(" Result: {}", result);
105
106// // Add result to conversation
107// client.add_tool_result(&tool_use.id, result)?;
108
109// // Continue conversation to get assistant's response
110// client.send("").await?;
111// }
112// Err(e) => {
113// eprintln!(" Error executing tool: {}", e);
114// }
115// }
116// } else {
117// eprintln!(" Tool not found: {}", tool_use.name);
118// }
119// }
120// ContentBlock::ToolResult(_) => {
121// // Tool results handled manually in this example
122// }
123// }
124// }
125
126// println!();
127// }
128
129// println!("{}", "=".repeat(70));
130// println!("Example complete!");
131
132// Ok(())
133// }