pub struct ChatCompletion<N, M, S = StreamOff>where
N: ChatRequestModel + Chat,
M: Serialize,
(N, M): Bounded,
ChatBody<N, M>: Serialize,
S: StreamState,{ /* private fields */ }Expand description
Typed chat-completion request builder sent through a ZaiClient.
Generic over the model N, the message type M, and a stream type-state
S (StreamOff by default, StreamOn after
enable_stream).
Credentials, endpoint selection and transport policy are owned by the
ZaiClient supplied to send_via.
Audio chat deliberately has no tool surface:
use zai_rs::model::{
chat::ChatCompletion,
chat_message_types::VoiceMessage,
chat_models::GLM4_voice,
tools::Function,
};
ChatCompletion::new(GLM4_voice {}, VoiceMessage::new_user()).add_tool(
Function::new("lookup", "Lookup", serde_json::json!({"type": "object"})),
);use zai_rs::model::{
chat::ChatCompletion,
chat_message_types::VoiceMessage,
chat_models::GLM4_voice,
tools::ToolChoice,
};
ChatCompletion::new(GLM4_voice {}, VoiceMessage::new_user())
.with_tool_choice(ToolChoice::auto());use zai_rs::model::{
chat::ChatCompletion,
chat_message_types::VoiceMessage,
chat_models::GLM4_voice,
tools::ResponseFormat,
};
ChatCompletion::new(GLM4_voice {}, VoiceMessage::new_user())
.with_response_format(ResponseFormat::JsonObject);Vision chat accepts a bare Function, not
the text schema’s retrieval, web-search, or MCP tool variants:
use zai_rs::model::{
chat::ChatCompletion,
chat_message_types::VisionMessage,
chat_models::GLM4_6v,
tools::{Retrieval, Tools},
};
ChatCompletion::new(GLM4_6v {}, VisionMessage::new_user()).add_tool(
Tools::Retrieval { retrieval: Retrieval::new("kb") },
);use zai_rs::model::{
chat::ChatCompletion,
chat_message_types::VisionMessage,
chat_models::GLM4_6v,
tools::ResponseFormat,
};
ChatCompletion::new(GLM4_6v {}, VisionMessage::new_user())
.with_response_format(ResponseFormat::JsonObject);Implementations§
Source§impl<N, M, S> ChatCompletion<N, M, S>where
N: ChatRequestModel + Chat,
M: Serialize,
(N, M): Bounded,
ChatBody<N, M>: Serialize,
S: StreamState,
impl<N, M, S> ChatCompletion<N, M, S>where
N: ChatRequestModel + Chat,
M: Serialize,
(N, M): Bounded,
ChatBody<N, M>: Serialize,
S: StreamState,
Source§impl<N, M> ChatCompletion<N, M, StreamOff>
impl<N, M> ChatCompletion<N, M, StreamOff>
Sourcepub fn new(model: N, messages: M) -> ChatCompletion<N, M, StreamOff>
pub fn new(model: N, messages: M) -> ChatCompletion<N, M, StreamOff>
Create a new chat request from a model and the first message batch.
Sourcepub fn add_message(self, message: M) -> Self
pub fn add_message(self, message: M) -> Self
Append one message to the conversation.
Sourcepub fn extend_messages(self, messages: impl IntoIterator<Item = M>) -> Self
pub fn extend_messages(self, messages: impl IntoIterator<Item = M>) -> Self
Append multiple messages to the conversation.
Sourcepub fn with_request_id(self, request_id: impl Into<String>) -> Self
pub fn with_request_id(self, request_id: impl Into<String>) -> Self
Set the client-side request id.
Sourcepub fn with_do_sample(self, do_sample: bool) -> Self
pub fn with_do_sample(self, do_sample: bool) -> Self
Enable/disable sampling (do_sample).
Sourcepub fn with_temperature(self, temperature: f64) -> Self
pub fn with_temperature(self, temperature: f64) -> Self
Set the sampling temperature.
Sourcepub fn with_top_p(self, top_p: f64) -> Self
pub fn with_top_p(self, top_p: f64) -> Self
Set the nucleus-sampling probability (top_p).
Sourcepub fn with_max_tokens(self, max_tokens: u32) -> Self
pub fn with_max_tokens(self, max_tokens: u32) -> Self
Set the maximum number of tokens to generate.
Sourcepub fn add_tool(self, tool: N::Tool) -> Selfwhere
N: ChatToolSupport,
pub fn add_tool(self, tool: N::Tool) -> Selfwhere
N: ChatToolSupport,
Add a single tool to the request.
Sourcepub fn add_tools(self, tools: impl IntoIterator<Item = N::Tool>) -> Selfwhere
N: ChatToolSupport,
pub fn add_tools(self, tools: impl IntoIterator<Item = N::Tool>) -> Selfwhere
N: ChatToolSupport,
Add multiple tools to the request at once.
Sourcepub fn with_tool_choice(self, tool_choice: ToolChoice) -> Selfwhere
N: ChatToolSupport,
pub fn with_tool_choice(self, tool_choice: ToolChoice) -> Selfwhere
N: ChatToolSupport,
Set automatic tool selection.
Sourcepub fn clear_tools(self) -> Selfwhere
N: ChatToolSupport,
pub fn clear_tools(self) -> Selfwhere
N: ChatToolSupport,
Remove all tools and their selection policy.
Sourcepub fn with_response_format(self, format: ResponseFormat) -> Selfwhere
N: ResponseFormatEnable,
pub fn with_response_format(self, format: ResponseFormat) -> Selfwhere
N: ResponseFormatEnable,
Set the text response format.
Sourcepub fn with_watermark_enabled(self, enabled: bool) -> Selfwhere
N: WatermarkEnable,
pub fn with_watermark_enabled(self, enabled: bool) -> Selfwhere
N: WatermarkEnable,
Enable or disable audio-output watermarking.
Sourcepub fn with_user_id(self, user_id: impl Into<String>) -> Self
pub fn with_user_id(self, user_id: impl Into<String>) -> Self
Set the end-user id (used for abuse monitoring).
Sourcepub fn with_stop(self, stop: impl Into<String>) -> Self
pub fn with_stop(self, stop: impl Into<String>) -> Self
Add a stop sequence that halts generation when encountered.
Sourcepub fn with_thinking(self, thinking: ThinkingType) -> Selfwhere
N: ThinkEnable,
pub fn with_thinking(self, thinking: ThinkingType) -> Selfwhere
N: ThinkEnable,
Enable thinking mode (requires a model that supports it).
Sourcepub fn with_reasoning_effort(self, effort: ReasoningEffort) -> Selfwhere
N: ReasoningEffortEnable,
pub fn with_reasoning_effort(self, effort: ReasoningEffort) -> Selfwhere
N: ReasoningEffortEnable,
Set the reasoning effort (GLM-5.2+ only).
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.
Sourcepub fn validate(&self) -> ZaiResult<()>
pub fn validate(&self) -> ZaiResult<()>
Validate request parameters for non-stream chat (StreamOff)
Sourcepub async fn send_via(
&self,
client: &ZaiClient,
) -> ZaiResult<ChatCompletionResponse>
pub async fn send_via( &self, client: &ZaiClient, ) -> ZaiResult<ChatCompletionResponse>
Submit the request via a ZaiClient and await the (non-streaming)
response.
Sourcepub async fn send_via_coding_plan(
&self,
client: &ZaiClient,
) -> ZaiResult<ChatCompletionResponse>
pub async fn send_via_coding_plan( &self, client: &ZaiClient, ) -> ZaiResult<ChatCompletionResponse>
Submit using a coding-plan endpoint via a ZaiClient.
Source§impl<N, M> ChatCompletion<N, M, StreamOn>
impl<N, M> ChatCompletion<N, M, StreamOn>
Sourcepub fn with_tool_stream(self, tool_stream: bool) -> Selfwhere
N: ToolStreamEnable,
pub fn with_tool_stream(self, tool_stream: bool) -> Selfwhere
N: ToolStreamEnable,
Enable/disable tool-call streaming (requires a model that supports it).
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.
Sourcepub async fn stream_via(&self, client: &ZaiClient) -> ZaiResult<ChatStream>
pub async fn stream_via(&self, client: &ZaiClient) -> ZaiResult<ChatStream>
Submit the request and yield parsed chat chunks from its SSE response.
Authentication, response content-type validation, timeouts, and body
limits remain inside ZaiClient; the API secret is never exposed to
the caller. Streaming POST requests are not retried or redirected.