Expand description
One async client for many LLM APIs: Anthropic, OpenAI, Google Gemini, Cohere, Azure OpenAI, Amazon Bedrock, Vertex AI and every OpenAI-compatible server.
The same code (ChatRequest -> ChatResponse) runs against any provider; switching
providers means switching the constructor:
let provider: Box<dyn LlmProvider> = Box::new(AnthropicProvider::new("sk-ant-..."));
// or: Box::new(OpenAiProvider::new("sk-...")),
// or: Box::new(GeminiProvider::new("AIza...")),
// or: Box::new(OpenAiProvider::ollama()),
let req = ChatRequest::new("claude-opus-5-5")
.system("Be concise.")
.max_tokens(512)
.message(Message::user("Hello!"));
let resp = provider.chat(&req).await?;
println!("{}", resp.text());
// Streaming:
let mut stream = provider.chat_stream(&req).await?;
while let Some(event) = stream.next().await {
if let StreamEvent::TextDelta(text) = event? {
print!("{text}");
}
}Errors follow the official SDKs: see LlmError and ApiError. Failed requests are
retried like the SDKs do (2 retries by default; see with_max_retries).
Structs§
- Anthropic
Provider - Client for the Anthropic Messages API (
POST {base_url}/v1/messages), or for Claude on Google Cloud Vertex AI viavertex. - ApiError
- A response with a non-2xx status (
APIStatusErrorin the SDKs). - Chat
Request - Provider-neutral chat request.
- Chat
Response - Cohere
Provider - Client for the Cohere Chat API v2 (
POST {base_url}/chat). - Embeddings
- Result of an embeddings call: one vector per input, in input order.
- Gemini
Provider - Client for the Google Gemini API
(
POST {base_url}/models/{model}:generateContent, key inx-goog-api-key), or for Gemini on Vertex AI viavertex. - Message
- Model
Info - A model available to the key.
- Open
AiProvider - Client for the OpenAI Chat Completions API (
POST {base_url}/chat/completions). - Response
Format - Asks the model to answer with JSON matching
schema. - Tool
- A function the model may call.
parametersis a JSON Schema with"type": "object". - Tool
Call - A tool call taken out of a response.
- Usage
Enums§
- ApiError
Kind - Error class by HTTP status, matching the
APIErrorsubclasses in the official SDKs. - Image
Source - Where an image comes from.
- LlmError
- Every error this crate returns.
- Part
- One piece of a message.
- Reasoning
Effort - How hard the model should think, on models that support reasoning.
- Role
- Author of a message.
- Stop
Reason - Why generation stopped, normalized across providers.
- Stream
Event - What a stream yields. The last item is always
StreamEvent::Done(or an error).
Constants§
- DEFAULT_
MAX_ RETRIES - Retries by default, like the official SDKs.
- DEFAULT_
MAX_ TOKENS max_tokenssent to Anthropic when the request does not set one.
Traits§
- Embedding
Provider - Providers that can turn text into vectors (OpenAI and compatible servers, Gemini, Cohere).
- LlmProvider
- Common contract implemented by every provider.
- Stream
Ext - An extension trait for
Streams that provides a variety of convenient combinator functions.
Functions§
- run_
tools - Runs the tool loop until the model gives a final answer.
Type Aliases§
- Chat
Stream - A stream of
StreamEvents. Read it withStreamExt::next(re-exported by this crate). - Result