LM) struct is a configurable client for calling LLM providers, with built-in caching and history tracking.
This page explains what an LM is in DSRs, how it’s structured in Rust terms, and how it cooperates with other building blocks.
What is an LM?
TheLM struct is a thin wrapper around OpenAI-compatible API clients, with built-in support for multiple providers.
It handles three core responsibilities:
- Configuration - Stores provider credentials, model selection, and inference parameters (eg: temperature)
-
API Execution - Takes pre-formatted
Chatmessages and executes HTTP calls to the LLM provider - Response Caching - Optionally stores input/output pairs to avoid duplicate API calls
Structure
LM is built using the builder pattern and holds:
model- Model identifier (e.g., “gpt-4o-mini” or “openai:gpt-4o-mini”)api_key- Provider API credentials (optional for local servers)base_url- API endpoint URL (optional, inferred from model provider)temperature- Sampling temperature (default: 0.7)max_tokens- Maximum completion tokens (default: 512)cache- Enable response caching (default: true)client- Internal HTTP client (initialized during build)cache_handler- Optional response cache (initialized during build if enabled)
LM is cheap - clones share the same HTTP client and cache via Arc, making them ideal for concurrent use.
Where it fits
You rarely callLM directly—It’s the lowest-level DSRs primitive. Instead, a Predictor uses an Adapter to format a Signature and call the LM. This keeps business logic (your task) separate from transport (the model client).
Construction and configuration
TheLM::builder() must be awaited with .build().await because client initialization is async.
Basic usage
Local server usage
For local OpenAI-compatible servers (vLLM, Ollama, etc.), providebase_url without an api_key:
Custom OpenAI-compatible endpoints
For custom endpoints requiring authentication, provide bothbase_url and api_key:
- Clone semantics:
LMimplementsClone; clones share the underlying client and cache viaArc, so they see the same history while carrying their own config copy.
API Reference
You can browse the fullLM module reference on docs.rs.
Global vs explicit usage
- Global:
configure(lm, ChatAdapter)sets the process-wide default used by predictors. - Explicit: Wrap the model in an
Arcwhen you want to override the global instance:let shared = Arc::new(lm); predictor.forward_with_config(inputs, Arc::clone(&shared)).await.
Async execution and sync entry
- Async: LM building and calls are
async; prefer using an async runtime (Tokio). - Sync-style: If you need a plain
fn main, create a runtime andblock_onthe async work.
- Async (Tokio)
- Sync
Inspecting history
inspect_history requires caching to be enabled on the LM; otherwise no history is recorded.
Configuration options
AllLM builder parameters have sensible defaults, so you only need to override what you need.
| Parameter | Type | Default | Notes |
|---|---|---|---|
model | String | "openai:gpt-4o-mini" | Supports “provider:model” format or bare model name (defaults to OpenAI) |
api_key | Option<String> | None | Provider API key; omit for local servers |
base_url | Option<String> | None | Custom endpoint URL; auto-detected from model provider if not provided |
temperature | f32 | 0.7 | Higher values increase randomness |
max_tokens | u32 | 512 | Upper bound on completion tokens |
cache | bool | true | Enables response caching and inspect_history support |
Example with custom settings
Provider Support
DSRs supports multiple LLM providers through Rig. Use theprovider:model format to specify which provider to use. Bare model names default to OpenAI.
Supported providers:
openai- OpenAI models (requiresOPENAI_API_KEY)anthropic- Anthropic models (requiresANTHROPIC_API_KEY)gemini- Google Gemini models (requiresGEMINI_API_KEY)groq- Groq models (requiresGROQ_API_KEY)openrouter- OpenRouter (requiresOPENROUTER_API_KEY)ollama- Local Ollama models (no API key required)
.api_key() if you want to override the default environment variable.
You can also use base_url to connect to any OpenAI-compatible server (vLLM, LiteLLM, etc.).
