Expand description
Core language model types, traits, and provider implementations.
Provides the foundational types used across the predict engine and
any other crate that communicates with language models. Includes
concrete implementations for Anthropic and OpenAI APIs, plus a
spec-compliant SSE parser for streaming responses.
§Providers
AnthropicLanguageModel(anthropic) — Anthropic Messages APIOpenAiLanguageModel(openai) —OpenAIChat Completions API, including compatible endpoints throughOpenAiConfig::base_urlOllamaLanguageModel(ollama) — local Ollama daemonBedrockProvider(bedrock) — AWS Bedrock via Converse / ConverseStreamDummyLM— test mock
§OpenAI-Compatible Providers
With the openai feature, OpenAiLanguageModel works with any provider that
implements the OpenAI Chat Completions API:
ⓘ
use std::sync::Arc;
use modelplease::{ApiKey, OpenAiConfig, OpenAiDeps, OpenAiLanguageModel, RetryConfig};
let lm = OpenAiLanguageModel::new(
OpenAiDeps { client: Arc::new(reqwest::Client::new()) },
OpenAiConfig {
api_key: ApiKey::parse("local")?,
base_url: "http://localhost:8080/v1".to_owned(),
retry_config: RetryConfig::default(),
},
);§Quick Start
use modelplease::{ContentPart, HttpsUrl, MediaSource, Message, Role};
// Simple text message
let _msg = Message::user("What is the capital of France?");
// Message with mixed content parts
let url = HttpsUrl::parse("https://example.com/photo.jpg").unwrap();
let _msg = Message::with_parts(
Role::User,
vec![
ContentPart::text("Describe this image:"),
ContentPart::image(MediaSource::Url { url }),
],
);§Modules
- Messages —
Message,Role,ContentPart - Providers —
LanguageModelProvider,GenerateRequest - Configuration —
LanguageModelConfig,ResponseFormat - Responses —
LanguageModelResponse,StreamDelta,Usage - Errors —
LanguageModelError - Retry —
RetryConfig,with_retry - SSE —
SseEvent,parse_sse_stream - Provider implementations —
AnthropicLanguageModel,OpenAiLanguageModel, andDummyLM
Structs§
- Anthropic
Config - Configuration for
AnthropicLanguageModel. - Anthropic
Deps - Injected dependencies for
AnthropicLanguageModel. The HTTP client is constructed once at the application composition root and shared across HTTP-based providers. - Anthropic
Language Model - Anthropic Messages API language model.
- ApiKey
- Provider API key. Validated at parse time; redacted on
Display. - AwsAccount
Id - A 12-digit AWS account id. Used as the optional
bucket_owneronMediaSource::S3for cross-accountS3Locationreferences. - Bedrock
Mantle Provider - AWS Bedrock Mantle language-model provider.
- Bedrock
Mantle Provider Config - Per-family region routing + retry knobs for
BedrockMantleProvider. - Bedrock
Mantle Provider Deps - Injected dependencies for
BedrockMantleProvider. - Bedrock
Provider - AWS Bedrock language model provider.
- Bedrock
Provider Config - Configuration for
BedrockProvider. Pure value-shaped data — AWS SDK client construction (credential chain probing, profile resolution) happens in the application composition root, not here. - Bedrock
Provider Deps - Injected AWS SDK clients for
BedrockProvider. - Chat
Model Info - Catalog metadata for one model offered by a provider.
- DummyLM
- A test mock that implements
LanguageModelProvider. - Generate
Request - Per-call inputs for
LanguageModelProvider::generateandLanguageModelProvider::generate_stream. - Https
Url - A URL pinned to the
https://scheme. - Invalid
Reasoning Effort - Error returned when parsing a
ReasoningEffortfrom a string that doesn’t match one of the six accepted names. - Language
Model Config - Configuration for a language model generation call.
- Language
Model Response - A response from a language model generation call.
- Media
Support - What a model accepts for one
MediaKind. - Media
Type - An RFC 6838 media type / MIME (
type/subtype[;parameters]). - Message
- A single message in an LM conversation.
- Model
Capabilities - What a single model can carry across every
MediaKind. - ModelId
- Provider model identifier (e.g.
"claude-sonnet-4-6","gpt-4o-mini"). Wraps an arbitraryStringso it cannot be passed where anApiKeyis expected. - Ollama
Config - Configuration for
OllamaLanguageModel. - Ollama
Deps - Injected dependencies for
OllamaLanguageModel. The HTTP client is constructed once at the application composition root and shared across HTTP-based providers. - Ollama
Language Model - Ollama language model.
- Open
AiConfig - Configuration for
OpenAiLanguageModel. - Open
AiDeps - Injected dependencies for
OpenAiLanguageModel. - Open
AiLanguage Model - OpenAI Chat Completions API language model.
- Provider
File Id - An opaque, provider-issued file identifier (Anthropic Files API,
OpenAIFiles API). - Reasoning
Capability - What reasoning a single model supports.
- Reasoning
Param Conflicts - Sampling-parameter restrictions that apply when reasoning is on.
- Retry
Config - Configuration for retry behavior.
- S3Uri
- A canonical AWS S3 URI:
s3://<bucket>/<key>. - SseEvent
- A parsed Server-Sent Event.
- SseStream
- Stream adapter that transforms a byte stream into SSE events.
- Stream
Delta - An incremental chunk from a streaming language model response.
- Usage
- Token usage statistics for a generation call.
Enums§
- ApiKey
Error - Failure mode returned by
ApiKey::parse. - AwsAccount
IdError - Failure mode returned by
AwsAccountId::parse. - Bedrock
Mantle Auth - Authentication for
BedrockMantleProvider. - Cache
Ttl - Prompt-cache time-to-live for cache breakpoints.
- Capability
Error - Reasons a request can fail capability validation, by precision.
- Content
Part - A single content part within a message.
- Https
UrlError - Failure mode returned by
HttpsUrl::parse. - Language
Model Error - Errors that can occur when calling a language model.
- Latency
Mode - Latency tier for the generation call.
- Media
Kind - Bucket for media content parts.
- Media
Source - Where the bytes of one media content part come from.
- Media
Type Error - Failure mode returned by
MediaType::parse. - Prompt
Caching - Prompt-caching mode for the generation call.
- Provider
File IdError - Failure mode returned by
ProviderFileId::parse. - Reasoning
Config - Resolved reasoning intent passed to providers via
LanguageModelConfig::reasoning. - Reasoning
Effort - Reasoning effort hint for thinking models.
- Reasoning
Mode - Provider wire-mode classes a model accepts.
- Reasoning
Validation Error - Reasons a
ReasoningConfigcan failReasoningCapability::validate. - Response
Format - Constrains the output format of a language model response.
- Response
Format Kind - Mirror of
ResponseFormatwithout the per-variant payload. - Role
- Message role in a conversation.
- S3Uri
Error - Failure mode returned by
S3Uri::parse. - Source
Kind - Discriminant for
MediaSourcevariants, suitable for use inenumset::EnumSet-backed capability masks (e.g.MediaSupport.sources). - Stop
Reason - Normalized reason the model stopped generating.
Traits§
- Accepts
Audio Bytes - Implementor accepts at least one model that takes
MediaSource::InlineBytesforMediaKind::Audio. (OpenAIgpt-audio family, Bedrock Voxtral.) - Accepts
Document Bytes - Implementor accepts at least one model that takes
MediaSource::InlineBytesforMediaKind::Document. (Anthropic,OpenAIfile API, Bedrock.) - Accepts
Image Bytes - Implementor accepts at least one model that takes
MediaSource::InlineBytesforMediaKind::Image. (All providers except text-only ones.) - Accepts
Image S3 - Implementor accepts at least one model that takes
MediaSource::S3forMediaKind::Image. (Bedrock only.) - Accepts
Image Url - Implementor accepts at least one model that takes
MediaSource::UrlforMediaKind::Image. (OpenAI, Anthropic.) - Accepts
Video Bytes - Implementor accepts at least one model that takes
MediaSource::InlineBytesforMediaKind::Video. (Bedrock Nova Pro/Lite only.) - Accepts
Video S3 - Implementor accepts at least one model that takes
MediaSource::S3forMediaKind::Video. (Bedrock only.) - Language
Model Provider - A chat / completion provider — one impl per backend.
Functions§
- all_
source_ kinds EnumSet<SourceKind>containing every variant — useful as a defaultaccepted_sourcesfor callers that haven’t narrowed the slot.- parse_
sse_ stream - Parse a byte stream (e.g. from
reqwest::Response::bytes_stream()) into a stream ofSseEvents following the WHATWG SSE specification. - with_
retry - Execute an async operation with retry and exponential backoff.