Skip to main content

Provider

Trait Provider 

Source
pub trait Provider:
    Send
    + Sync
    + 'static {
    // Required method
    fn name(&self) -> &'static str;

    // Provided methods
    fn display_name(&self) -> &str { ... }
    fn normalize_model(&self, model: String) -> String { ... }
    fn chat_completions_path(&self) -> String { ... }
    fn transform_request(&self, _request: &mut ChatRequest) { ... }
    fn transform_response(&self, _response: &mut ChatResponse) { ... }
    fn transform_stream_chunk(&self, _chunk: &mut ChatStreamChunk) { ... }
}
Expand description

Provider trait for LLM provider-specific transformations.

Each Chinese LLM provider may have slightly different API requirements or model name formats that need to be normalized.

Implementations are expected to be stateless so a single instance can be shared across all requests via Arc<dyn Provider>.

Required Methods§

Source

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

Get provider type identifier.

Returns a static string identifying the provider type, e.g. "glm", "kimi", "default". This is used for programmatic dispatch (e.g. provider.name() == "minimax") and should not be used for user-facing logs.

Provided Methods§

Source

fn display_name(&self) -> &str

Get the display name for logging and diagnostics.

For named providers (GLM, Kimi, etc.) this returns the same value as name(). For [DefaultProvider] this returns the original backend name from config (e.g. "qwen", "yi-lightning"), making it easy to identify which backend a request was routed to.

Use this in log messages, metrics labels, and diagnostics.

Source

fn normalize_model(&self, model: String) -> String

Normalize model name from Responses API to provider’s format.

Source

fn chat_completions_path(&self) -> String

Get the chat completions path for this provider.

Returns the endpoint path without the version prefix, e.g. "/chat/completions". The version prefix (e.g., "/v1") comes from the backend URL’s base_path configured in config.json and is prepended automatically during path rewriting in upstream_request_filter.

§Example

Config URL https://api.moonshot.cn/v1base_path = "/v1" chat_completions_path() = "/chat/completions" → final path: /v1/chat/completions

Source

fn transform_request(&self, _request: &mut ChatRequest)

Transform request before sending to provider.

This is called after the standard conversion but before sending to the upstream provider. Providers can modify the request to handle API differences.

Source

fn transform_response(&self, _response: &mut ChatResponse)

Transform response after receiving from provider.

This is called after receiving the response but before converting to Responses API format. Providers can normalize response format.

Source

fn transform_stream_chunk(&self, _chunk: &mut ChatStreamChunk)

Transform streaming chunk in real-time.

This is called for each SSE chunk received from the provider. Providers can modify chunk content before event conversion.

Implementors§