ChatBody

Struct ChatBody 

Source
pub struct ChatBody<N, M>{
Show 14 fields pub model: N, pub messages: Vec<M>, pub request_id: Option<String>, pub thinking: Option<ThinkingType>, pub do_sample: Option<bool>, pub stream: Option<bool>, pub tool_stream: Option<bool>, pub temperature: Option<f32>, pub top_p: Option<f32>, pub max_tokens: Option<u32>, pub tools: Option<Vec<Tools>>, pub user_id: Option<String>, pub stop: Option<Vec<String>>, pub response_format: Option<ResponseFormat>,
}
Expand description

Main request body structure for chat API calls.

This structure represents a complete chat request with all possible configuration options. It uses generic types to support different model names and message types while maintaining type safety through trait bounds.

§Type Parameters

  • N - The model name type, must implement ModelName
  • M - The message type, must form a Bounded pair with N

§Examples

use crate::model::base::{ChatBody, TextMessage};

// Create a basic chat request
let chat_body = ChatBody {
    model: "gpt-4".to_string(),
    messages: vec![
        TextMessage::user("Hello, how are you?"),
        TextMessage::assistant("I'm doing well, thank you!")
    ],
    temperature: Some(0.7),
    max_tokens: Some(1000),
    ..Default::default()
};

Fields§

§model: N

The model to use for the chat completion.

§messages: Vec<M>

A list of messages comprising the conversation so far.

§request_id: Option<String>

A unique identifier for the request. Optional field that will be omitted from serialization if not provided.

§thinking: Option<ThinkingType>

Optional thinking prompt or reasoning text that can guide the model’s response. Only available for models that support thinking capabilities.

§do_sample: Option<bool>

Whether to use sampling during generation. When true, the model will use probabilistic sampling; when false, it will use deterministic generation.

§stream: Option<bool>

Whether to stream back partial message deltas as they are generated. When true, responses will be sent as server-sent events.

§tool_stream: Option<bool>

Whether to enable streaming of tool calls (streaming function call parameters). Only supported by GLM-4.6 models. Defaults to false when omitted.

§temperature: Option<f32>

Controls randomness in the output. Higher values (closer to 1.0) make the output more random, while lower values (closer to 0.0) make it more deterministic. Must be between 0.0 and 1.0.

§top_p: Option<f32>

Controls diversity via nucleus sampling. Only tokens with cumulative probability up to top_p are considered. Must be between 0.0 and 1.0.

§max_tokens: Option<u32>

The maximum number of tokens to generate in the completion. Must be between 1 and 98304.

§tools: Option<Vec<Tools>>

A list of tools the model may call. Currently supports function calling, web search, and retrieval tools. Note: server expects an array; we model this as a vector of tool items.

§user_id: Option<String>

A unique identifier representing your end-user, which can help monitor and detect abuse. Must be between 6 and 128 characters long.

§stop: Option<Vec<String>>

Up to 1 sequence where the API will stop generating further tokens.

§response_format: Option<ResponseFormat>

An object specifying the format that the model must output. Can be either text or JSON object format.

Implementations§

Source§

impl<N, M> ChatBody<N, M>

Source

pub fn new(model: N, messages: M) -> Self

Source

pub fn add_messages(self, messages: M) -> Self

Source

pub fn with_request_id(self, request_id: impl Into<String>) -> Self

Source

pub fn with_do_sample(self, do_sample: bool) -> Self

Source

pub fn with_stream(self, stream: bool) -> Self

Source

pub fn with_temperature(self, temperature: f32) -> Self

Source

pub fn with_top_p(self, top_p: f32) -> Self

Source

pub fn with_max_tokens(self, max_tokens: u32) -> Self

Source

pub fn with_tools(self, tools: impl Into<Vec<Tools>>) -> Self

👎Deprecated: with_tools is deprecated; use add_tool/add_tools instead

Deprecated: use add_tools (single) or extend_tools (Vec) on ChatBody, or prefer ChatCompletion::add_tool / add_tools at the client layer.

Source

pub fn add_tools(self, tools: Tools) -> Self

Source

pub fn extend_tools(self, tools: Vec<Tools>) -> Self

Source

pub fn with_user_id(self, user_id: impl Into<String>) -> Self

Source

pub fn with_stop(self, stop: String) -> Self

Source§

impl<N, M> ChatBody<N, M>

Source

pub fn with_thinking(self, thinking: ThinkingType) -> Self

Adds thinking text to the chat body for models that support thinking capabilities.

This method is only available for models that implement the ThinkEnable trait, ensuring type safety for thinking-enabled models.

§Arguments
  • thinking - The thinking prompt or reasoning text to add
§Returns

Returns self with the thinking field set, allowing for method chaining.

§Examples
let chat_body = ChatBody::new(model, messages)
    .with_thinking("Let me think step by step about this problem...");
Source§

impl<N, M> ChatBody<N, M>

Source

pub fn with_tool_stream(self, tool_stream: bool) -> Self

Enables streaming tool calls (GLM-4.6 only). Default is false when omitted.

Trait Implementations§

Source§

impl<N, M: Clone> Clone for ChatBody<N, M>

Source§

fn clone(&self) -> ChatBody<N, M>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<N, M: Debug> Debug for ChatBody<N, M>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<N, M> Serialize for ChatBody<N, M>

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl<N, M> Validate for ChatBody<N, M>

Source§

impl<'v_a, N, M> ValidateArgs<'v_a> for ChatBody<N, M>

Auto Trait Implementations§

§

impl<N, M> Freeze for ChatBody<N, M>
where N: Freeze,

§

impl<N, M> RefUnwindSafe for ChatBody<N, M>

§

impl<N, M> Send for ChatBody<N, M>
where N: Send, M: Send,

§

impl<N, M> Sync for ChatBody<N, M>
where N: Sync, M: Sync,

§

impl<N, M> Unpin for ChatBody<N, M>
where N: Unpin, M: Unpin,

§

impl<N, M> UnwindSafe for ChatBody<N, M>
where N: UnwindSafe, M: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> ErasedDestructor for T
where T: 'static,