pub struct ChatCompletion<N, M, S = StreamOff>{
pub key: String,
pub url: String,
/* private fields */
}Expand description
Type-safe chat completion request structure.
This struct represents a chat completion request with compile-time guarantees for model compatibility and streaming capabilities.
§Type Parameters
N- The AI model type (must implementModelName + Chat)M- The message type (must form a valid bound with the model)S- Stream state (StreamOnorStreamOff, defaults toStreamOff)
§Examples
let model = GLM4_5_flash {};
let messages = TextMessage::user("Hello, how are you?");
let request = ChatCompletion::new(model, messages, api_key);Fields§
§key: StringAPI key for authentication with the Zhipu AI service.
url: StringAPI endpoint URL for chat completions.
Defaults to “https://open.bigmodel.cn/api/paas/v4/chat/completions”
but can be customized using the with_url() method.
Implementations§
Source§impl<N, M> ChatCompletion<N, M, StreamOff>
impl<N, M> ChatCompletion<N, M, StreamOff>
Sourcepub fn new(
model: N,
messages: M,
key: String,
) -> ChatCompletion<N, M, StreamOff>
pub fn new( model: N, messages: M, key: String, ) -> ChatCompletion<N, M, StreamOff>
Sourcepub fn body_mut(&mut self) -> &mut ChatBody<N, M>
pub fn body_mut(&mut self) -> &mut ChatBody<N, M>
Gets mutable access to the request body for further customization.
This method allows modification of request parameters after initial creation.
Sourcepub fn add_messages(self, messages: M) -> Self
pub fn add_messages(self, messages: M) -> Self
pub fn with_request_id(self, request_id: impl Into<String>) -> Self
pub fn with_do_sample(self, do_sample: bool) -> Self
pub fn with_temperature(self, temperature: f32) -> Self
pub fn with_top_p(self, top_p: f32) -> Self
pub fn with_max_tokens(self, max_tokens: u32) -> Self
pub fn add_tool(self, tool: Tools) -> Self
pub fn add_tools(self, tools: Vec<Tools>) -> Self
pub fn with_user_id(self, user_id: impl Into<String>) -> Self
pub fn with_stop(self, stop: String) -> Self
Sourcepub fn with_url(self, url: impl Into<String>) -> Self
pub fn with_url(self, url: impl Into<String>) -> Self
Sets a custom API endpoint URL for this chat completion request.
This method allows overriding the default API endpoint with a custom URL, enabling support for different deployment environments or proxy configurations.
§Arguments
url- The custom API endpoint URL
§Returns
Self with the updated URL, enabling method chaining.
§Examples
let request = ChatCompletion::new(model, messages, api_key)
.with_url("https://custom-api.example.com/v1/chat/completions");Sourcepub fn with_coding_plan(self) -> Self
pub fn with_coding_plan(self) -> Self
Sets the URL to the coding plan endpoint.
This method configures the chat completion request to use the coding-specific API endpoint “https://open.bigmodel.cn/api/coding/paas/v4/chat/completions”.
§Returns
Self with the coding plan URL, enabling method chaining.
§Examples
let request = ChatCompletion::new(model, messages, api_key)
.with_coding_plan();pub fn with_thinking(self, thinking: ThinkingType) -> Selfwhere
N: ThinkEnable,
Sourcepub fn enable_stream(self) -> ChatCompletion<N, M, StreamOn>
pub fn enable_stream(self) -> ChatCompletion<N, M, StreamOn>
Enables streaming for this chat completion request.
This method transitions the request to streaming mode, allowing real-time response processing through Server-Sent Events (SSE).
§Returns
A new ChatCompletion instance with streaming enabled (StreamOn).
Sourcepub fn validate(&self) -> ZaiResult<()>
pub fn validate(&self) -> ZaiResult<()>
Validate request parameters for non-stream chat (StreamOff)
pub async fn send(&self) -> ZaiResult<ChatCompletionResponse>
Source§impl<N, M> ChatCompletion<N, M, StreamOn>
impl<N, M> ChatCompletion<N, M, StreamOn>
pub fn with_tool_stream(self, tool_stream: bool) -> Selfwhere
N: ToolStreamEnable,
Sourcepub fn disable_stream(self) -> ChatCompletion<N, M, StreamOff>
pub fn disable_stream(self) -> ChatCompletion<N, M, StreamOff>
Disables streaming for this chat completion request.
This method ensures the request will receive a complete response rather than streaming chunks.
§Returns
A new ChatCompletion instance with streaming disabled (StreamOff).
Trait Implementations§
Source§impl<N, M, S> HttpClient for ChatCompletion<N, M, S>
impl<N, M, S> HttpClient for ChatCompletion<N, M, S>
type Body = ChatBody<N, M>
type ApiUrl = String
type ApiKey = String
fn api_key(&self) -> &Self::ApiKey
fn body(&self) -> &Self::Body
Source§fn http_config(&self) -> Arc<HttpClientConfig>
fn http_config(&self) -> Arc<HttpClientConfig>
Source§impl<N, M> SseStreamable for ChatCompletion<N, M, StreamOn>
Enables Server-Sent Events (SSE) streaming for streaming-enabled chat
completions.
impl<N, M> SseStreamable for ChatCompletion<N, M, StreamOn>
Enables Server-Sent Events (SSE) streaming for streaming-enabled chat completions.
This implementation allows streaming chat completions to be processed incrementally as responses arrive from the API.
Source§impl<N, M> StreamChatLikeExt for ChatCompletion<N, M, StreamOn>
Provides streaming extension methods for streaming-enabled chat completions.
impl<N, M> StreamChatLikeExt for ChatCompletion<N, M, StreamOn>
Provides streaming extension methods for streaming-enabled chat completions.
This implementation enables the use of streaming-specific methods for processing chat responses in real-time.