pub struct ChatCompletionRequest {
pub model: String,
pub messages: Vec<Message>,
pub temperature: Option<f32>,
pub max_tokens: Option<u32>,
pub top_p: Option<f32>,
pub frequency_penalty: Option<f32>,
pub presence_penalty: Option<f32>,
pub stream: Option<bool>,
pub tools: Option<Vec<Tool>>,
}Expand description
Request to create a chat completion.
This is the primary request type for the chat completions endpoint.
Uses the Former builder pattern for fluent construction.
§Required Fields
model: Model ID (e.g., “grok-2-1212”)messages: Conversation messages
§Optional Fields
temperature: Randomness (0.0-2.0, default varies by model)max_tokens: Maximum tokens to generatetop_p: Nucleus sampling thresholdfrequency_penalty: Repetition reductionpresence_penalty: Topic diversitystream: Enable SSE streamingtools: Available functions for tool calling
§Examples
use api_xai::{ ChatCompletionRequest, Message };
let request = ChatCompletionRequest::former()
.model( "grok-2-1212".to_string() )
.messages( vec![ Message::user( "Hello!" ) ] )
.temperature( 0.7 )
.max_tokens( 100u32 )
.form();Fields§
§model: StringModel ID (e.g., “grok-2-1212”, “grok-4”).
messages: Vec<Message>Conversation messages.
temperature: Option<f32>Sampling temperature (0.0-2.0).
Higher values (0.8-2.0) make output more random. Lower values (0.0-0.4) make output more focused and deterministic.
max_tokens: Option<u32>Maximum tokens to generate.
Limits the length of the completion. Does not include prompt tokens.
top_p: Option<f32>Nucleus sampling threshold (0.0-1.0).
Only tokens with cumulative probability up to top_p are considered.
Recommended : 0.1-0.9. Do not use with temperature.
frequency_penalty: Option<f32>Frequency penalty (0.0-2.0).
Reduces repetition of tokens based on their frequency. Typical range : 0.1-0.8.
presence_penalty: Option<f32>Presence penalty (0.0-2.0).
Encourages the model to talk about new topics. Typical range : 0.1-0.8.
stream: Option<bool>Enable Server-Sent Events streaming.
When true, the response is streamed as SSE chunks.
tools: Option<Vec<Tool>>Tools (functions) available for the model to call.
Implementations§
Source§impl ChatCompletionRequest
impl ChatCompletionRequest
Sourcepub fn former() -> ChatCompletionRequestFormer<ChatCompletionRequestFormerDefinition<(), ChatCompletionRequest, ReturnPreformed>>
pub fn former() -> ChatCompletionRequestFormer<ChatCompletionRequestFormerDefinition<(), ChatCompletionRequest, ReturnPreformed>>
Provides a mechanism to initiate the formation process with a default completion behavior.
Trait Implementations§
Source§impl Clone for ChatCompletionRequest
impl Clone for ChatCompletionRequest
Source§fn clone(&self) -> ChatCompletionRequest
fn clone(&self) -> ChatCompletionRequest
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for ChatCompletionRequest
impl Debug for ChatCompletionRequest
Source§impl<'de> Deserialize<'de> for ChatCompletionRequest
impl<'de> Deserialize<'de> for ChatCompletionRequest
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Source§impl<__Context, __Formed, __End> EntityToDefinition<__Context, __Formed, __End> for ChatCompletionRequestwhere
__End: FormingEnd<ChatCompletionRequestFormerDefinitionTypes<__Context, __Formed>>,
impl<__Context, __Formed, __End> EntityToDefinition<__Context, __Formed, __End> for ChatCompletionRequestwhere
__End: FormingEnd<ChatCompletionRequestFormerDefinitionTypes<__Context, __Formed>>,
Source§type Definition = ChatCompletionRequestFormerDefinition<__Context, __Formed, __End>
type Definition = ChatCompletionRequestFormerDefinition<__Context, __Formed, __End>
FormerDefinition associated with this entity. Read moreSource§type Types = ChatCompletionRequestFormerDefinitionTypes<__Context, __Formed>
type Types = ChatCompletionRequestFormerDefinitionTypes<__Context, __Formed>
FormerDefinitionTypes associated with this entity. Read moreSource§impl<__Context, __Formed> EntityToDefinitionTypes<__Context, __Formed> for ChatCompletionRequest
impl<__Context, __Formed> EntityToDefinitionTypes<__Context, __Formed> for ChatCompletionRequest
Source§impl<Definition> EntityToFormer<Definition> for ChatCompletionRequestwhere
Definition: FormerDefinition<Storage = ChatCompletionRequestFormerStorage>,
impl<Definition> EntityToFormer<Definition> for ChatCompletionRequestwhere
Definition: FormerDefinition<Storage = ChatCompletionRequestFormerStorage>,
Source§impl PartialEq for ChatCompletionRequest
impl PartialEq for ChatCompletionRequest
Source§impl Serialize for ChatCompletionRequest
impl Serialize for ChatCompletionRequest
impl StructuralPartialEq for ChatCompletionRequest
Auto Trait Implementations§
impl Freeze for ChatCompletionRequest
impl RefUnwindSafe for ChatCompletionRequest
impl Send for ChatCompletionRequest
impl Sync for ChatCompletionRequest
impl Unpin for ChatCompletionRequest
impl UnwindSafe for ChatCompletionRequest
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<C, E> EntryToVal<C> for Ewhere
C: Collection<Entry = E>,
impl<C, E> EntryToVal<C> for Ewhere
C: Collection<Entry = E>,
Source§type Val = <C as Collection>::Val
type Val = <C as Collection>::Val
Entry in complex collections.
For example, in a HashMap, while Entry might be a ( key, value ) tuple, Val might only be the value part.Source§fn entry_to_val(self) -> <E as EntryToVal<C>>::Val
fn entry_to_val(self) -> <E as EntryToVal<C>>::Val
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
Source§impl<C, Val> ValToEntry<C> for Valwhere
C: CollectionValToEntry<Val>,
impl<C, Val> ValToEntry<C> for Valwhere
C: CollectionValToEntry<Val>,
Source§fn val_to_entry(self) -> <C as CollectionValToEntry<Val>>::Entry
fn val_to_entry(self) -> <C as CollectionValToEntry<Val>>::Entry
Invokes the val_to_entry function of the CollectionValToEntry trait to convert the value to an entry.
Source§type Entry = <C as CollectionValToEntry<Val>>::Entry
type Entry = <C as CollectionValToEntry<Val>>::Entry
Entry is defined by the Collection trait.