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
//! What to ask the model, in terms no provider owns.

use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;

use serde_json::Value;

use crate::message::Message;

/// A function the model may call, described with JSON Schema.
#[derive(Clone, Debug, PartialEq)]
pub struct ToolSchema {
    pub name: String,
    pub description: String,
    /// A JSON Schema object describing the arguments.
    pub parameters: Value,
}

impl ToolSchema {
    pub fn new(name: impl Into<String>, description: impl Into<String>, parameters: Value) -> Self {
        Self {
            name: name.into(),
            description: description.into(),
            parameters,
        }
    }
}

/// A raised flag that stops a stream at the next event boundary.
pub type Cancel = Arc<AtomicBool>;

/// One request, whatever the provider underneath.
///
/// Build it with [`Request::new`] and the chaining setters. The fields stay
/// public to read; the struct is `#[non_exhaustive]` so that adding a knob
/// later is not a breaking change.
#[derive(Clone, Debug, Default)]
#[non_exhaustive]
pub struct Request {
    /// The system prompt, by whatever name the provider gives it.
    pub instructions: String,
    pub messages: Vec<Message>,
    pub tools: Vec<ToolSchema>,
    pub max_output_tokens: Option<u32>,
    pub temperature: Option<f32>,
    /// Route to the client's cheaper side model, for background work the user
    /// never sees.
    pub side_call: bool,
    /// Override the client's model for this request alone.
    pub model: Option<String>,
    /// Raised by the caller to abandon a stream in flight.
    pub cancel: Option<Cancel>,
}

impl Request {
    pub fn new(instructions: impl Into<String>) -> Self {
        Self {
            instructions: instructions.into(),
            ..Self::default()
        }
    }

    pub fn instructions(mut self, instructions: impl Into<String>) -> Self {
        self.instructions = instructions.into();
        self
    }

    pub fn message(mut self, message: Message) -> Self {
        self.messages.push(message);
        self
    }

    pub fn user(self, text: impl Into<String>) -> Self {
        self.message(Message::user(text))
    }

    pub fn assistant(self, text: impl Into<String>) -> Self {
        self.message(Message::assistant(text))
    }

    pub fn messages(mut self, messages: impl IntoIterator<Item = Message>) -> Self {
        self.messages.extend(messages);
        self
    }

    pub fn tool(mut self, tool: ToolSchema) -> Self {
        self.tools.push(tool);
        self
    }

    pub fn tools(mut self, tools: impl IntoIterator<Item = ToolSchema>) -> Self {
        self.tools.extend(tools);
        self
    }

    pub fn max_output_tokens(mut self, tokens: u32) -> Self {
        self.max_output_tokens = Some(tokens);
        self
    }

    pub fn temperature(mut self, temperature: f32) -> Self {
        self.temperature = Some(temperature);
        self
    }

    pub fn side_call(mut self, side: bool) -> Self {
        self.side_call = side;
        self
    }

    pub fn model(mut self, model: impl Into<String>) -> Self {
        self.model = Some(model.into());
        self
    }

    pub fn cancel(mut self, cancel: Cancel) -> Self {
        self.cancel = Some(cancel);
        self
    }

    /// Whether the caller has asked to stop.
    pub fn cancelled(&self) -> bool {
        self.cancel
            .as_ref()
            .is_some_and(|flag| flag.load(Ordering::Relaxed))
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;

    #[test]
    fn a_request_reads_back_what_was_chained_onto_it() {
        let request = Request::new("Be brief")
            .user("hello")
            .assistant("hi")
            .tool(ToolSchema::new(
                "now",
                "the time",
                json!({"type": "object"}),
            ))
            .max_output_tokens(64)
            .temperature(0.2)
            .model("some-model")
            .side_call(true);

        assert_eq!(request.instructions, "Be brief");
        assert_eq!(request.messages.len(), 2);
        assert_eq!(request.tools[0].name, "now");
        assert_eq!(request.max_output_tokens, Some(64));
        assert_eq!(request.temperature, Some(0.2));
        assert_eq!(request.model.as_deref(), Some("some-model"));
        assert!(request.side_call);
    }

    #[test]
    fn cancellation_is_off_until_the_caller_raises_it() {
        let flag = Cancel::default();
        let request = Request::new("x").cancel(flag.clone());
        assert!(!request.cancelled());
        flag.store(true, Ordering::Relaxed);
        assert!(request.cancelled());
        assert!(!Request::new("x").cancelled());
    }
}