Protocol

Trait Protocol 

Source
pub trait Protocol:
    Send
    + Sync
    + Clone
    + 'static {
    type Request: Serialize + Send + Sync;
    type Response: for<'de> Deserialize<'de> + Send + Sync;

    // Required methods
    fn name(&self) -> &str;
    fn chat_endpoint(&self, base_url: &str) -> String;
    fn build_request(
        &self,
        request: &ChatRequest,
    ) -> Result<Self::Request, LlmConnectorError>;
    fn parse_response(
        &self,
        response: &str,
    ) -> Result<ChatResponse, LlmConnectorError>;
    fn map_error(&self, status: u16, body: &str) -> LlmConnectorError;

    // Provided methods
    fn models_endpoint(&self, _base_url: &str) -> Option<String> { ... }
    fn parse_models(
        &self,
        _response: &str,
    ) -> Result<Vec<String>, LlmConnectorError> { ... }
    fn auth_headers(&self) -> Vec<(String, String)> { ... }
}
Expand description

协议trait - 定义纯API规范

这个trait代表一个LLM API的协议规范,如OpenAI API、Anthropic API等。 它只关注API的格式转换,不涉及具体的网络通信。

Required Associated Types§

Source

type Request: Serialize + Send + Sync

协议特定的请求类型

Source

type Response: for<'de> Deserialize<'de> + Send + Sync

协议特定的响应类型

Required Methods§

Source

fn name(&self) -> &str

协议名称 (如 “openai”, “anthropic”)

Source

fn chat_endpoint(&self, base_url: &str) -> String

获取聊天完成的端点URL

Source

fn build_request( &self, request: &ChatRequest, ) -> Result<Self::Request, LlmConnectorError>

构建协议特定的请求

Source

fn parse_response( &self, response: &str, ) -> Result<ChatResponse, LlmConnectorError>

解析协议特定的响应

Source

fn map_error(&self, status: u16, body: &str) -> LlmConnectorError

映射HTTP错误到统一错误类型

Provided Methods§

Source

fn models_endpoint(&self, _base_url: &str) -> Option<String>

获取模型列表的端点URL (可选)

Source

fn parse_models( &self, _response: &str, ) -> Result<Vec<String>, LlmConnectorError>

解析模型列表响应

Source

fn auth_headers(&self) -> Vec<(String, String)>

获取认证头 (可选)

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§