Skip to main content

LanguageModel

Trait LanguageModel 

Source
pub trait LanguageModel: Send + Sync {
    // Required methods
    fn provider(&self) -> &str;
    fn model_id(&self) -> &str;
    fn do_generate<'life0, 'life1, 'async_trait>(
        &'life0 self,
        options: &'life1 CallOptions,
    ) -> Pin<Box<dyn Future<Output = Result<GenerateResult, AiMuxError>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait;
    fn do_stream<'life0, 'life1, 'async_trait>(
        &'life0 self,
        options: &'life1 CallOptions,
    ) -> Pin<Box<dyn Future<Output = Result<StreamResult, AiMuxError>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait;

    // Provided methods
    fn specification_version(&self) -> &'static str { ... }
    fn retry_config(&self) -> RetryConfig { ... }
    fn config_snapshot(&self) -> ProviderRecord { ... }
}
Expand description

The unified language model trait (provider-facing).

Aligned with Vercel AI SDK LanguageModelV4.

§Implementation notes

  • do_generate / do_stream are not user-facing API. Users call generate_text / stream_text (free functions in crate::generate).
  • Providers must emit StreamStart as the first stream part, and Finish as the last (before any Error).
  • Unsupported CallOptions fields must produce a Warning::Unsupported rather than being silently dropped.

Required Methods§

Source

fn provider(&self) -> &str

Provider name, e.g. "openai".

Source

fn model_id(&self) -> &str

Model identifier, e.g. "gpt-4o".

Source

fn do_generate<'life0, 'life1, 'async_trait>( &'life0 self, options: &'life1 CallOptions, ) -> Pin<Box<dyn Future<Output = Result<GenerateResult, AiMuxError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Generate a complete (non-streaming) response.

Source

fn do_stream<'life0, 'life1, 'async_trait>( &'life0 self, options: &'life1 CallOptions, ) -> Pin<Box<dyn Future<Output = Result<StreamResult, AiMuxError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Generate a streaming response.

Provided Methods§

Source

fn specification_version(&self) -> &'static str

Specification version (always "v4").

Source

fn retry_config(&self) -> RetryConfig

Provider/model retry settings used when the caller does not override the retry count.

Source

fn config_snapshot(&self) -> ProviderRecord

RFC-0023: 配置侧快照供录制。默认返回最小信息(provider/model_id); 各 provider 覆盖以提供 base_url/profile/api_key 来源等完整配置。 透明 decorator(TraceLayer 等)必须覆盖并转发 inner 快照。

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§