1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
//! Chat completions API types for `/v1/chat/completions` endpoint.
use serde::{Deserialize, Serialize};
use crate::prelude::*;
use crate::common::{Content, DebugOutput, ResponseFormat, StreamOptions};
use crate::search::SearchParameters;
use crate::tools::{Tool, ToolCall, ToolChoice};
use crate::usage::Usage;
/// The chat request body for `/v1/chat/completions` endpoint.
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
pub struct ChatRequest {
/// A list of messages that make up the chat conversation.
#[serde(default)]
pub messages: Vec<Message>,
/// Model name for the model to use.
#[serde(skip_serializing_if = "Option::is_none")]
pub model: Option<String>,
/// What sampling temperature to use, between 0 and 2.
#[serde(skip_serializing_if = "Option::is_none")]
pub temperature: Option<f32>,
/// An alternative to sampling with temperature, called nucleus sampling.
#[serde(skip_serializing_if = "Option::is_none")]
pub top_p: Option<f32>,
/// How many chat completion choices to generate for each input message.
#[serde(skip_serializing_if = "Option::is_none")]
pub n: Option<i32>,
/// If set, partial message deltas will be sent as server-sent events.
#[serde(skip_serializing_if = "Option::is_none")]
pub stream: Option<bool>,
/// Options for streaming response.
#[serde(skip_serializing_if = "Option::is_none")]
pub stream_options: Option<StreamOptions>,
/// (Not supported by reasoning models) Up to 4 sequences where the API will stop generating.
#[serde(skip_serializing_if = "Option::is_none")]
pub stop: Option<Vec<String>>,
/// An upper bound for the number of tokens that can be generated for a completion.
#[serde(skip_serializing_if = "Option::is_none")]
pub max_completion_tokens: Option<i32>,
/// \[DEPRECATED\] The maximum number of tokens that can be generated.
#[serde(skip_serializing_if = "Option::is_none")]
pub max_tokens: Option<i32>,
/// (Not supported by `grok-3` and reasoning models) Presence penalty.
#[serde(skip_serializing_if = "Option::is_none")]
pub presence_penalty: Option<f32>,
/// (Not supported by reasoning models) Frequency penalty.
#[serde(skip_serializing_if = "Option::is_none")]
pub frequency_penalty: Option<f32>,
/// (Unsupported) A JSON object that maps tokens to an associated bias value.
#[serde(skip_serializing_if = "Option::is_none")]
pub logit_bias: Option<serde_json::Value>,
/// Whether to return log probabilities of the output tokens.
#[serde(skip_serializing_if = "Option::is_none")]
pub logprobs: Option<bool>,
/// Number of most likely tokens to return at each token position.
#[serde(skip_serializing_if = "Option::is_none")]
pub top_logprobs: Option<i32>,
/// A unique identifier representing your end-user.
#[serde(skip_serializing_if = "Option::is_none")]
pub user: Option<String>,
/// An object specifying the format that the model must output.
#[serde(skip_serializing_if = "Option::is_none")]
pub response_format: Option<ResponseFormat>,
/// If specified, system will make a best effort to sample deterministically.
#[serde(skip_serializing_if = "Option::is_none")]
pub seed: Option<i32>,
/// A list of tools the model may call.
#[serde(skip_serializing_if = "Option::is_none")]
pub tools: Option<Vec<Tool>>,
/// Controls which (if any) tool is called by the model.
#[serde(skip_serializing_if = "Option::is_none")]
pub tool_choice: Option<ToolChoice>,
/// If set to false, the model can perform maximum one tool call.
#[serde(skip_serializing_if = "Option::is_none")]
pub parallel_tool_calls: Option<bool>,
/// Set the parameters to be used for searched data.
#[serde(skip_serializing_if = "Option::is_none")]
pub search_parameters: Option<SearchParameters>,
/// Options to control the web search.
#[serde(skip_serializing_if = "Option::is_none")]
pub web_search_options: Option<crate::tools::WebSearchOptions>,
/// Constrains how hard a reasoning model thinks before responding.
#[serde(skip_serializing_if = "Option::is_none")]
pub reasoning_effort: Option<String>,
/// If set to `true`, the request returns a `request_id` for deferred completion.
#[serde(skip_serializing_if = "Option::is_none")]
pub deferred: Option<bool>,
/// (Internal) Bootstrap host address for disaggregated prefill/decode.
#[serde(skip_serializing_if = "Option::is_none")]
pub bootstrap_host: Option<String>,
/// (Internal) Bootstrap room ID for disaggregated prefill/decode.
#[serde(skip_serializing_if = "Option::is_none")]
pub bootstrap_room: Option<i64>,
}
/// Chat message objects.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(tag = "role", rename_all = "lowercase")]
pub enum Message {
/// System message, usually instructions for the model to respond in a certain way.
System {
/// System prompt content.
content: Content,
/// A unique identifier representing your end-user.
#[serde(skip_serializing_if = "Option::is_none")]
name: Option<String>,
},
/// User message, typically request from user for the model to answer.
User {
/// User prompt content.
content: Content,
/// A unique identifier representing your end-user.
#[serde(skip_serializing_if = "Option::is_none")]
name: Option<String>,
},
/// Assistant role message, previous chat messages from the model.
Assistant {
/// Assistant prompt content.
#[serde(skip_serializing_if = "Option::is_none")]
content: Option<Content>,
/// A unique identifier representing your end-user.
#[serde(skip_serializing_if = "Option::is_none")]
name: Option<String>,
/// Assistant reasoning content.
#[serde(skip_serializing_if = "Option::is_none")]
reasoning_content: Option<String>,
/// An array of tool calls available to the model.
#[serde(skip_serializing_if = "Option::is_none")]
tool_calls: Option<Vec<ToolCall>>,
},
/// Tool call role message, used to return function call result to the model.
Tool {
/// Content of the tool call result.
content: Content,
/// The ID of the tool call received from assistant message response.
#[serde(skip_serializing_if = "Option::is_none")]
tool_call_id: Option<String>,
},
/// Function call role message. Deprecated in favor of `tool`.
Function {
/// Content of the tool call result.
content: Content,
/// A unique identifier representing your end-user.
#[serde(skip_serializing_if = "Option::is_none")]
name: Option<String>,
},
}
impl Default for Message {
fn default() -> Self {
Message::User {
content: Content::default(),
name: None,
}
}
}
/// The chat response body for `/v1/chat/completions` endpoint.
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
pub struct ChatResponse {
/// A unique ID for the chat response.
pub id: String,
/// The object type, which is always `"chat.completion"`.
pub object: String,
/// The chat completion creation time in Unix timestamp.
pub created: i64,
/// Model ID used to create chat completion.
pub model: String,
/// A list of response choices from the model.
pub choices: Vec<Choice>,
/// Token usage information.
#[serde(skip_serializing_if = "Option::is_none")]
pub usage: Option<Usage>,
/// System fingerprint, used to indicate xAI system configuration changes.
#[serde(skip_serializing_if = "Option::is_none")]
pub system_fingerprint: Option<String>,
/// List of all the external pages used by the model to answer.
#[serde(skip_serializing_if = "Option::is_none")]
pub citations: Option<Vec<String>>,
/// Debug output. Only available to trusted testers.
#[serde(skip_serializing_if = "Option::is_none")]
pub debug_output: Option<DebugOutput>,
}
/// A response choice from the model.
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
pub struct Choice {
/// Index of the choice within the response choices, starting from 0.
pub index: i32,
/// The generated chat completion message.
pub message: ChoiceMessage,
/// Finish reason.
#[serde(skip_serializing_if = "Option::is_none")]
pub finish_reason: Option<String>,
/// The log probabilities of each output token.
#[serde(skip_serializing_if = "Option::is_none")]
pub logprobs: Option<LogProbs>,
}
/// The generated chat completion message.
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
pub struct ChoiceMessage {
/// The role that the message belongs to, the response from model is always `"assistant"`.
pub role: String,
/// The content of the message.
#[serde(skip_serializing_if = "Option::is_none")]
pub content: Option<String>,
/// The reasoning trace generated by the model.
#[serde(skip_serializing_if = "Option::is_none")]
pub reasoning_content: Option<String>,
/// The reason given by model if unable to generate a response.
#[serde(skip_serializing_if = "Option::is_none")]
pub refusal: Option<String>,
/// A list of tool calls asked by model for user to perform.
#[serde(skip_serializing_if = "Option::is_none")]
pub tool_calls: Option<Vec<ToolCall>>,
}
/// Streaming chat response chunk.
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
pub struct ChatResponseChunk {
/// A unique ID for the chat response chunk.
pub id: String,
/// The object type, which is always `"chat.completion.chunk"`.
pub object: String,
/// The chat completion creation time in Unix timestamp.
pub created: i64,
/// The model ID used to create chat completion.
pub model: String,
/// A list of response choices from the model.
pub choices: Vec<ChoiceChunk>,
/// Token usage information.
#[serde(skip_serializing_if = "Option::is_none")]
pub usage: Option<Usage>,
/// System fingerprint, used to indicate xAI system configuration changes.
#[serde(skip_serializing_if = "Option::is_none")]
pub system_fingerprint: Option<String>,
/// List of all the external pages used by the model to answer.
#[serde(skip_serializing_if = "Option::is_none")]
pub citations: Option<Vec<String>>,
/// Debug output. Only available to trusted testers.
#[serde(skip_serializing_if = "Option::is_none")]
pub debug_output: Option<DebugOutput>,
}
/// A streaming response choice chunk.
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
pub struct ChoiceChunk {
/// Index of the choice.
pub index: i32,
/// Additional difference (delta) of the result.
pub delta: Delta,
/// Finish reason.
#[serde(skip_serializing_if = "Option::is_none")]
pub finish_reason: Option<String>,
/// The log probabilities of each output token.
#[serde(skip_serializing_if = "Option::is_none")]
pub logprobs: Option<LogProbs>,
}
/// Delta content in streaming response.
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
pub struct Delta {
/// The role of the message.
#[serde(skip_serializing_if = "Option::is_none")]
pub role: Option<String>,
/// The content delta.
#[serde(skip_serializing_if = "Option::is_none")]
pub content: Option<String>,
/// The reasoning content delta.
#[serde(skip_serializing_if = "Option::is_none")]
pub reasoning_content: Option<String>,
/// Tool calls delta.
#[serde(skip_serializing_if = "Option::is_none")]
pub tool_calls: Option<Vec<ToolCall>>,
/// Images generated by the model.
#[serde(skip_serializing_if = "Option::is_none")]
pub images: Option<Vec<String>>,
}
/// Log probabilities.
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
pub struct LogProbs {
/// An array of the log probabilities of each output token returned.
#[serde(skip_serializing_if = "Option::is_none")]
pub content: Option<Vec<TokenLogProb>>,
}
/// Token log probability.
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
pub struct TokenLogProb {
/// The token.
pub token: String,
/// The log probability of returning this token.
pub logprob: f32,
/// An array of the most likely tokens to return at this token position.
pub top_logprobs: Vec<TopLogProb>,
/// The ASCII encoding of the output character.
#[serde(skip_serializing_if = "Option::is_none")]
pub bytes: Option<Vec<u32>>,
}
/// Top log probability.
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
pub struct TopLogProb {
/// The token.
pub token: String,
/// The log probability of returning this token.
pub logprob: f32,
/// The ASCII encoding of the output character.
#[serde(skip_serializing_if = "Option::is_none")]
pub bytes: Option<Vec<u32>>,
}