Skip to main content

ModelProvider

Trait ModelProvider 

Source
pub trait ModelProvider: Send + Sync {
    // Required methods
    fn provider_id(&self) -> String;
    fn list_models<'life0, 'async_trait>(
        &'life0 self,
    ) -> Pin<Box<dyn Future<Output = Result<Vec<ModelInfo>, ModelError>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait;
    fn chat<'life0, 'async_trait>(
        &'life0 self,
        req: ModelChatRequest,
    ) -> Pin<Box<dyn Future<Output = Result<ModelChatResponse, ModelError>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait;
    fn stream_chat<'life0, 'async_trait>(
        &'life0 self,
        req: ModelChatRequest,
        tx: Sender<ModelStreamChunk>,
    ) -> Pin<Box<dyn Future<Output = Result<(), ModelError>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait;
    fn embedding<'life0, 'async_trait>(
        &'life0 self,
        req: ModelEmbeddingRequest,
    ) -> Pin<Box<dyn Future<Output = Result<ModelEmbeddingResponse, ModelError>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait;

    // Provided methods
    fn cache_namespace(&self) -> String { ... }
    fn is_available<'life0, 'async_trait>(
        &'life0 self,
    ) -> Pin<Box<dyn Future<Output = bool> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait { ... }
}
Expand description

模型提供方:声明模型清单并提供模型调用能力。

宿主内置提供方(OpenAI 兼容 / Ollama)与第三方插件后端(插件注册为 Provider 组件)均实现本接口;宿主 ModelRegistry 聚合清单并按 provider_id 前缀路由调用。提供方只实现自己支持的模型种类, 不支持的调用返回 ModelError::NotSupported。

Required Methods§

Source

fn provider_id(&self) -> String

提供方标识(如 “openai”、“ollama”、插件 id)。 与 model_id 的 {provider}/ 前缀一致,宿主按此前缀路由。

Source

fn list_models<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Result<Vec<ModelInfo>, ModelError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

当前支持的模型清单(可能随环境/配置变化)。

Source

fn chat<'life0, 'async_trait>( &'life0 self, req: ModelChatRequest, ) -> Pin<Box<dyn Future<Output = Result<ModelChatResponse, ModelError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

调用文本生成模型;仅支持 chat 模型的提供方实现。

Source

fn stream_chat<'life0, 'async_trait>( &'life0 self, req: ModelChatRequest, tx: Sender<ModelStreamChunk>, ) -> Pin<Box<dyn Future<Output = Result<(), ModelError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

调用流式文本生成模型:增量经 tx 推送,完成后关闭通道。

Source

fn embedding<'life0, 'async_trait>( &'life0 self, req: ModelEmbeddingRequest, ) -> Pin<Box<dyn Future<Output = Result<ModelEmbeddingResponse, ModelError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

调用文本向量化模型;仅支持 embedding 模型的提供方实现(task_type 必填)。

Provided Methods§

Source

fn cache_namespace(&self) -> String

返回影响 embedding 结果的 provider 配置命名空间,供宿主缓存隔离。 默认使用 provider_id;配置化 provider 应覆盖并包含 endpoint/模型配置版本。

Source

fn is_available<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = bool> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

提供方可用性探测(如 Ollama 未启动);默认恒可用。

Dyn Compatibility§

This trait is dyn compatible.

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

Implementors§