Skip to main content

Crate modelbridge

Crate modelbridge 

Source
Expand description

One small Rust API for OpenAI, Anthropic, OpenRouter, Groq, DeepSeek, Gemini, xAI, Mistral, Ollama, and any OpenAI-compatible endpoint.

use modelbridge::{Client, Request};

let client = Client::default_for("anthropic")?;
let answer = client.complete(&Request::new("Be brief").user("Why is the sky blue?"))?;
println!("{} ({} tokens)", answer.text, answer.usage.total());

Everything is synchronous and runtime-free. Provider::stream blocks and pushes Chunks down an mpsc channel, which is what a TUI or GUI wants: run it on a worker thread and drain the channel each frame.

  • PROVIDERS and MODELS know the endpoint, credential variable, wire format, and price of the providers people actually use, without ever restricting the model id you pass.
  • agent runs the call-tools-until-answered loop, feeding results back in each provider’s native format.
  • oauth is the whole browser sign-in flow for the ChatGPT and Claude subscription routes, including refresh and storage. On by default; turn it off with default-features = false and three dependencies go with it.

Modules§

agent
The loop: ask, run whatever tools the model asked for, hand the results back, repeat until it answers.
oauth
Browser sign-in for the two providers people reach with a subscription rather than a key.

Structs§

Client
A configured route to one provider.
ClientBuilder
Message
One turn of the conversation.
ModelSpec
ProviderSpec
Request
One request, whatever the provider underneath.
Response
A finished reply.
Retry
How many times a transient failure is worth repeating, and how long to wait first. A server that sends Retry-After overrides the delay.
ToolCall
A tool invocation the model asked for.
ToolResult
What a tool produced, on its way back to the model.
ToolSchema
A function the model may call, described with JSON Schema.
Usage
What a client has spent, in tokens and in millionths of a dollar.

Enums§

ApiStyle
Auth
Chunk
One piece of a reply, as it arrives.
Error
Why a request failed.
Role

Constants§

MODELS
PROVIDERS

Traits§

OAuthTokenProvider
Supplies OAuth access tokens while the host application owns login, persistence, expiry, scopes, and refresh-token security.
Provider
Anything that can answer a Request.

Functions§

collect_text
complete, keeping only the text.
complete
Consume a streaming provider into one Response.
extract_json
The first well-formed JSON object in a string, tolerating code fences and whatever prose a model wrapped around it.
model
models_for
provider

Type Aliases§

Cancel
A raised flag that stops a stream at the next event boundary.
Result
A short-hand for the crate’s fallible operations.