Skip to main content

Crate llm_universal

Crate llm_universal 

Source
Expand description

One client interface for OpenAI-compatible APIs and the Anthropic Messages API.

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(OpenAiProvider::new("").with_base_url("http://localhost:11434/v1")),
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());

Errors follow the official SDKs: see LlmError and ApiError.

Structs§

AnthropicProvider
Client for the Anthropic Messages API (POST {base_url}/v1/messages).
ApiError
A response with a non-2xx status (APIStatusError in the SDKs).
ChatRequest
Provider-neutral chat request.
ChatResponse
CohereProvider
Client for the Cohere Chat API v2 (POST {base_url}/chat).
GeminiProvider
Client for the Google Gemini API (POST {base_url}/models/{model}:generateContent, key in x-goog-api-key).
Message
OpenAiProvider
Client for the OpenAI Chat Completions API (POST {base_url}/chat/completions).
Tool
A function the model may call. parameters is a JSON Schema with "type": "object".
Usage

Enums§

ApiErrorKind
Error class by HTTP status, matching the APIError subclasses in the official SDKs.
LlmError
Every error this crate returns.
Part
One piece of a message.
Role
Author of a message.
StopReason
Why generation stopped, normalized across providers.

Constants§

DEFAULT_MAX_TOKENS
max_tokens sent to Anthropic when the request does not set one.

Traits§

LlmProvider
Common contract implemented by every provider.

Type Aliases§

Result