1use modelbridge::{Client, Message, Request, ToolCall, ToolResult, ToolSchema};
12use serde_json::json;
13
14fn schemas() -> Vec<ToolSchema> {
15 vec![ToolSchema::new(
16 "distance_km",
17 "Straight-line distance between two cities, in kilometres.",
18 json!({
19 "type": "object",
20 "properties": {
21 "from": { "type": "string" },
22 "to": { "type": "string" },
23 },
24 "required": ["from", "to"],
25 }),
26 )]
27}
28
29fn run(call: &ToolCall) -> Result<String, String> {
32 match call.name.as_str() {
33 "distance_km" => {
34 let from = call.arg("from").ok_or("`from` is required")?;
35 let to = call.arg("to").ok_or("`to` is required")?;
36 Ok(format!("{from} to {to}: 1,213 km"))
37 }
38 other => Err(format!("no tool named `{other}`")),
39 }
40}
41
42fn main() -> Result<(), String> {
43 let provider = std::env::args().nth(1).unwrap_or_else(|| "openai".into());
44 let client = Client::default_for(&provider)?;
45
46 let question = "How far is Lisbon from Paris, and how long would that take at 90 km/h?";
47 let mut conversation = vec![Message::user(question)];
48
49 let first = client.complete(
51 &Request::new("Use the tools for anything factual. Answer in one sentence.")
52 .messages(conversation.clone())
53 .tools(schemas())
54 .max_output_tokens(400),
55 )?;
56
57 if first.tool_calls.is_empty() {
58 println!("{}", first.text);
59 return Ok(());
60 }
61
62 conversation.push(first.as_message());
66
67 let mut results = Vec::new();
68 for (index, call) in first.tool_calls.iter().enumerate() {
69 eprintln!("[calling {}({})]", call.name, call.args);
70 results.push(match run(call) {
71 Ok(output) => ToolResult::new(call, index, output),
72 Err(message) => ToolResult::failure(call, index, message),
73 });
74 }
75 conversation.push(Message::tool_results(results));
76
77 let second = client.complete(
80 &Request::new("Use the tools for anything factual. Answer in one sentence.")
81 .messages(conversation)
82 .max_output_tokens(400),
83 )?;
84
85 println!("{}", second.text);
86 let usage = first.usage + second.usage;
87 eprintln!(
88 "2 turns, {} tokens, about ${:.4}",
89 usage.total(),
90 usage.dollars()
91 );
92 Ok(())
93}