1use modelbridge::agent::{Agent, Step, Tools};
6use modelbridge::{Client, ToolCall, ToolSchema};
7use serde_json::json;
8
9struct Calculator;
11
12impl Tools for Calculator {
13 fn schemas(&self) -> Vec<ToolSchema> {
14 vec![ToolSchema::new(
15 "add",
16 "Add two numbers and return the sum.",
17 json!({
18 "type": "object",
19 "properties": {
20 "a": { "type": "number" },
21 "b": { "type": "number" },
22 },
23 "required": ["a", "b"],
24 }),
25 )]
26 }
27
28 fn run(&mut self, call: &ToolCall) -> Result<String, String> {
29 let number = |name: &str| {
30 call.args[name]
31 .as_f64()
32 .ok_or_else(|| format!("`{name}` must be a number"))
33 };
34 Ok((number("a")? + number("b")?).to_string())
35 }
36}
37
38fn main() -> Result<(), String> {
39 let mut args = std::env::args().skip(1);
40 let provider = args.next().unwrap_or_else(|| "openai".into());
41 let question = args
42 .next()
43 .unwrap_or_else(|| "What is 2354 plus 8811?".into());
44
45 let client = Client::default_for(&provider)?;
46 let mut agent = Agent::new(&client, Calculator)
47 .instructions("Use the tools for arithmetic. Answer in one sentence.")
48 .max_turns(4);
49
50 let answer = agent.ask(&question, &mut |step| match step {
51 Step::Token(text) => print!("{text}"),
52 Step::Calling(call) => eprintln!("\n[calling {} with {}]", call.name, call.args),
53 Step::Planning(_) => print!("\r"),
54 _ => {}
55 })?;
56
57 println!("\n\n{}", answer.text);
58 eprintln!("{} turns, {} tokens", answer.turns, answer.usage.total());
59 Ok(())
60}