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§
Sourcetype Response: for<'de> Deserialize<'de> + Send + Sync
type Response: for<'de> Deserialize<'de> + Send + Sync
协议特定的响应类型
Required Methods§
Sourcefn chat_endpoint(&self, base_url: &str) -> String
fn chat_endpoint(&self, base_url: &str) -> String
获取聊天完成的端点URL
Sourcefn build_request(
&self,
request: &ChatRequest,
) -> Result<Self::Request, LlmConnectorError>
fn build_request( &self, request: &ChatRequest, ) -> Result<Self::Request, LlmConnectorError>
构建协议特定的请求
Sourcefn parse_response(
&self,
response: &str,
) -> Result<ChatResponse, LlmConnectorError>
fn parse_response( &self, response: &str, ) -> Result<ChatResponse, LlmConnectorError>
解析协议特定的响应
Sourcefn map_error(&self, status: u16, body: &str) -> LlmConnectorError
fn map_error(&self, status: u16, body: &str) -> LlmConnectorError
映射HTTP错误到统一错误类型
Provided Methods§
Sourcefn models_endpoint(&self, _base_url: &str) -> Option<String>
fn models_endpoint(&self, _base_url: &str) -> Option<String>
获取模型列表的端点URL (可选)
Sourcefn parse_models(
&self,
_response: &str,
) -> Result<Vec<String>, LlmConnectorError>
fn parse_models( &self, _response: &str, ) -> Result<Vec<String>, LlmConnectorError>
解析模型列表响应
Sourcefn auth_headers(&self) -> Vec<(String, String)>
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.