use modelbridge::agent::{Agent, Step, Tools};
use modelbridge::{Client, ToolCall, ToolSchema};
use serde_json::json;
struct Calculator;
impl Tools for Calculator {
fn schemas(&self) -> Vec<ToolSchema> {
vec![ToolSchema::new(
"add",
"Add two numbers and return the sum.",
json!({
"type": "object",
"properties": {
"a": { "type": "number" },
"b": { "type": "number" },
},
"required": ["a", "b"],
}),
)]
}
fn run(&mut self, call: &ToolCall) -> Result<String, String> {
let number = |name: &str| {
call.args[name]
.as_f64()
.ok_or_else(|| format!("`{name}` must be a number"))
};
Ok((number("a")? + number("b")?).to_string())
}
}
fn main() -> Result<(), String> {
let mut args = std::env::args().skip(1);
let provider = args.next().unwrap_or_else(|| "openai".into());
let question = args
.next()
.unwrap_or_else(|| "What is 2354 plus 8811?".into());
let client = Client::default_for(&provider)?;
let mut agent = Agent::new(&client, Calculator)
.instructions("Use the tools for arithmetic. Answer in one sentence.")
.max_turns(4);
let answer = agent.ask(&question, &mut |step| match step {
Step::Token(text) => print!("{text}"),
Step::Calling(call) => eprintln!("\n[calling {} with {}]", call.name, call.args),
Step::Planning(_) => print!("\r"),
_ => {}
})?;
println!("\n\n{}", answer.text);
eprintln!("{} turns, {} tokens", answer.turns, answer.usage.total());
Ok(())
}