modelbridge 0.5.2

One provider-neutral Rust client for OpenAI, Anthropic, Gemini, Groq, Ollama and friends: streaming, tool calling, an agent loop, usage accounting, and subscription sign-in
Documentation
//! A tool round trip by hand, without the agent loop.
//!
//! `cargo run --example tools -- anthropic`
//!
//! [`modelbridge::agent`] does all of this for you and you should usually use
//! it. This is the primitive underneath, for when you want to own the loop:
//! ask, receive tool calls, run them, hand the results back as real
//! conversation turns, and get an answer. The same code runs unchanged against
//! Responses, Anthropic Messages, and Chat Completions.

use modelbridge::{Client, Message, Request, ToolCall, ToolResult, ToolSchema};
use serde_json::json;

fn schemas() -> Vec<ToolSchema> {
    vec![ToolSchema::new(
        "distance_km",
        "Straight-line distance between two cities, in kilometres.",
        json!({
            "type": "object",
            "properties": {
                "from": { "type": "string" },
                "to": { "type": "string" },
            },
            "required": ["from", "to"],
        }),
    )]
}

/// Stands in for whatever your tool actually does. Returning `Err` is how you
/// tell the model a call failed in a way it could work around.
fn run(call: &ToolCall) -> Result<String, String> {
    match call.name.as_str() {
        "distance_km" => {
            let from = call.arg("from").ok_or("`from` is required")?;
            let to = call.arg("to").ok_or("`to` is required")?;
            Ok(format!("{from} to {to}: 1,213 km"))
        }
        other => Err(format!("no tool named `{other}`")),
    }
}

fn main() -> Result<(), String> {
    let provider = std::env::args().nth(1).unwrap_or_else(|| "openai".into());
    let client = Client::default_for(&provider)?;

    let question = "How far is Lisbon from Paris, and how long would that take at 90 km/h?";
    let mut conversation = vec![Message::user(question)];

    // Turn one: offer the tools and see what comes back.
    let first = client.complete(
        &Request::new("Use the tools for anything factual. Answer in one sentence.")
            .messages(conversation.clone())
            .tools(schemas())
            .max_output_tokens(400),
    )?;

    if first.tool_calls.is_empty() {
        println!("{}", first.text);
        return Ok(());
    }

    // `as_message` is the assistant turn that made those calls, ids and all.
    // Skipping it, or replaying the calls as prose, is what makes a hand-rolled
    // loop confuse the model on turn two.
    conversation.push(first.as_message());

    let mut results = Vec::new();
    for (index, call) in first.tool_calls.iter().enumerate() {
        eprintln!("[calling {}({})]", call.name, call.args);
        results.push(match run(call) {
            Ok(output) => ToolResult::new(call, index, output),
            Err(message) => ToolResult::failure(call, index, message),
        });
    }
    conversation.push(Message::tool_results(results));

    // Turn two: same conversation, results attached, no tools offered so it
    // has to answer.
    let second = client.complete(
        &Request::new("Use the tools for anything factual. Answer in one sentence.")
            .messages(conversation)
            .max_output_tokens(400),
    )?;

    println!("{}", second.text);
    let usage = first.usage + second.usage;
    eprintln!(
        "2 turns, {} tokens, about ${:.4}",
        usage.total(),
        usage.dollars()
    );
    Ok(())
}