xai-openapi 0.1.1

Rust types for the xAI API (Grok models)
Documentation
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
//! Common types shared across multiple xAI API endpoints.

use serde::{Deserialize, Serialize};

use crate::prelude::*;

/// Content of each chat message.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(untagged)]
pub enum Content {
    /// Text prompt.
    Text(String),
    /// An array of content parts of different types, such as image, text or text file.
    Parts(Vec<ContentPart>),
}

impl Default for Content {
    fn default() -> Self {
        Content::Text(String::new())
    }
}

impl From<&str> for Content {
    fn from(s: &str) -> Self {
        Content::Text(s.to_string())
    }
}

impl From<String> for Content {
    fn from(s: String) -> Self {
        Content::Text(s)
    }
}

/// A part of content in a message.
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
pub struct ContentPart {
    /// The type of the content part.
    #[serde(rename = "type")]
    pub content_type: String,

    /// Text prompt.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub text: Option<String>,

    /// A public URL of image prompt, only available for vision models.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub image_url: Option<ImageUrl>,

    /// Specifies the detail level of the image.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub detail: Option<String>,

    /// File path to a text file to be used as prompt.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub text_file: Option<String>,

    /// File reference for file attachments (OpenAI-compatible nesting).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub file: Option<FileRef>,
}

/// Image URL object of image prompt.
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
pub struct ImageUrl {
    /// URL of the image.
    pub url: String,

    /// Specifies the detail level of the image.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub detail: Option<String>,
}

/// File reference for file attachments.
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
pub struct FileRef {
    /// The file ID from the Files API.
    pub file_id: String,
}

/// Options available when using streaming response.
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
pub struct StreamOptions {
    /// Set an additional chunk to be streamed before the `data: [DONE]` message.
    /// The other chunks will return `null` in `usage` field.
    pub include_usage: bool,
}

/// Response format parameter for structured outputs.
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum ResponseFormat {
    /// Specify text response format, always `"text"`.
    #[default]
    Text,
    /// Specify `json_object` response format, always `json_object`.
    /// Used for backward compatibility. Prefer to use `"json_schema"` instead.
    JsonObject,
    /// Specify `json_schema` response format with a given schema.
    JsonSchema {
        /// A json schema representing the desired response schema.
        json_schema: serde_json::Value,
    },
}

/// Annotation on text output.
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
pub struct Annotation {
    /// The type of the annotation. Only supported type currently is `url_citation`.
    #[serde(rename = "type")]
    pub annotation_type: String,

    /// The URL of the web resource.
    pub url: String,

    /// The title of the annotation.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub title: Option<String>,

    /// The start index of the annotation.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub start_index: Option<i32>,

    /// The end index of the annotation.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub end_index: Option<i32>,
}

/// Details about why a response is incomplete.
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
pub struct IncompleteDetails {
    /// The reason why the response is incomplete.
    pub reason: String,
}

/// API key information.
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
pub struct ApiKey {
    /// The redacted API key.
    pub redacted_api_key: String,

    /// User ID the API key belongs to.
    pub user_id: String,

    /// The name of the API key specified by user.
    pub name: String,

    /// Creation time of the API key in Unix timestamp.
    pub create_time: String,

    /// Last modification time of the API key in Unix timestamp.
    pub modify_time: String,

    /// User ID of the user who last modified the API key.
    pub modified_by: String,

    /// The team ID of the team that owns the API key.
    pub team_id: String,

    /// A list of ACLs authorized with the API key.
    pub acls: Vec<String>,

    /// ID of the API key.
    pub api_key_id: String,

    /// Indicates whether the team that owns the API key is blocked.
    pub team_blocked: bool,

    /// Indicates whether the API key is blocked.
    pub api_key_blocked: bool,

    /// Indicates whether the API key is disabled.
    pub api_key_disabled: bool,
}

/// (Legacy) Anthropic compatible complete request on `/v1/complete` endpoint.
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
pub struct CompleteRequest {
    /// Model to use for completion.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub model: Option<String>,

    /// Prompt for the model to perform completion on.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub prompt: Option<String>,

    /// The maximum number of tokens to generate before stopping.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub max_tokens_to_sample: Option<i32>,

    /// 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>,

    /// (Unsupported) When generating next tokens, randomly selecting from the k most likely options.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub top_k: Option<i32>,

    /// (Not supported by reasoning models) Up to 4 sequences where the API will stop generating.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub stop_sequences: Option<Vec<String>>,

    /// (Unsupported) If set, partial message deltas will be sent.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub stream: Option<bool>,

    /// An object describing metadata about the request.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub metadata: Option<MessageMetadata>,
}

/// (Legacy) Anthropic compatible complete response on `/v1/complete` endpoint.
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
pub struct CompleteResponse {
    /// Completion response object type. This is always `"completion"`.
    #[serde(rename = "type")]
    pub response_type: String,

    /// ID of the completion response.
    pub id: String,

    /// The completion content up to and excluding stop sequences.
    pub completion: String,

    /// The model that handled the request.
    pub model: String,

    /// The reason to stop completion.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub stop_reason: Option<String>,
}

/// Message metadata.
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
pub struct MessageMetadata {
    /// A unique identifier representing your end-user.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub user_id: Option<String>,
}

/// (Legacy) Request for `/v1/completions` endpoint.
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
pub struct SampleRequest {
    /// Specifies the model to be used for the request.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub model: Option<String>,

    /// Input for generating completions.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub prompt: Option<SampleContent>,

    /// Limits the number of tokens that can be produced in the output.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub max_tokens: Option<i32>,

    /// 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>,

    /// Determines how many completion sequences to produce for each prompt.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub n: Option<i32>,

    /// Whether to stream back partial progress.
    #[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>>,

    /// (Not supported by `grok-3` and reasoning models) Presence penalty.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub presence_penalty: Option<f32>,

    /// (Unsupported) Frequency penalty.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub frequency_penalty: Option<f32>,

    /// Option to include the original prompt in the response.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub echo: Option<bool>,

    /// Include the log probabilities on the most likely output tokens.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub logprobs: Option<bool>,

    /// (Unsupported) Generates multiple completions internally.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub best_of: Option<i32>,

    /// (Unsupported) Logit bias.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub logit_bias: Option<serde_json::Value>,

    /// If specified, system will make a best effort to sample deterministically.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub seed: Option<i32>,

    /// (Unsupported) Optional string to append after the generated text.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub suffix: Option<String>,

    /// A unique identifier representing your end-user.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub user: Option<String>,
}

/// Sample content input.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(untagged)]
pub enum SampleContent {
    /// Text prompt.
    Text(String),
    /// An array of strings, a token list, or an array of token lists.
    Array(Vec<String>),
}

impl Default for SampleContent {
    fn default() -> Self {
        SampleContent::Text(String::new())
    }
}

/// (Legacy) Response for `/v1/completions` endpoint.
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
pub struct SampleResponse {
    /// ID of the request.
    pub id: String,

    /// Object type of the response. This is always `"text_completion"`.
    pub object: String,

    /// The chat completion creation time in Unix timestamp.
    pub created: i64,

    /// Model used.
    pub model: String,

    /// A list of response choices from the model.
    pub choices: Vec<SampleChoice>,

    /// System fingerprint, used to indicate xAI system configuration changes.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub system_fingerprint: Option<String>,

    /// Token usage information.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub usage: Option<crate::usage::Usage>,
}

/// A choice in a sample response.
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
pub struct SampleChoice {
    /// Index of the choice.
    pub index: i32,

    /// Text response.
    pub text: String,

    /// Finish reason.
    pub finish_reason: String,
}

/// A unique request ID for deferred chat response.
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
pub struct StartDeferredChatResponse {
    /// A unique request ID for the chat response.
    pub request_id: String,
}

/// `DocumentsSource` defines the source of documents to search over.
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
pub struct DocumentsSource {
    /// The collection IDs to search in.
    pub collection_ids: Vec<String>,
}

/// Deprecated: Metric now comes from collection creation.
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
pub enum RankingMetric {
    #[default]
    #[serde(rename = "RANKING_METRIC_UNKNOWN")]
    Unknown,
    #[serde(rename = "RANKING_METRIC_L2_DISTANCE")]
    L2Distance,
    #[serde(rename = "RANKING_METRIC_COSINE_SIMILARITY")]
    CosineSimilarity,
}

/// System message content.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(untagged)]
pub enum SystemMessageContent {
    /// Text content of system prompt.
    Text(String),
    /// An array of system prompt parts.
    Parts(Vec<SystemMessagePart>),
}

impl Default for SystemMessageContent {
    fn default() -> Self {
        SystemMessageContent::Text(String::new())
    }
}

/// A part of system message content.
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
pub struct SystemMessagePart {
    /// Type of the object. This is always `"text"`.
    #[serde(rename = "type")]
    pub part_type: String,

    /// System prompt text.
    pub text: String,

    /// (Unsupported) Cache control.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub cache_control: Option<serde_json::Value>,
}

/// Debug output. Only available to trusted testers.
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
pub struct DebugOutput {
    /// Number of attempts made to the model.
    pub attempts: i32,

    /// The request received from the user.
    pub request: String,

    /// The prompt sent to the model in text form.
    pub prompt: String,

    /// JSON-serialized request sent to the inference engine.
    pub engine_request: String,

    /// The response(s) received from the model.
    pub responses: Vec<String>,

    /// The individual chunks returned from the pipeline of samplers.
    pub chunks: Vec<String>,

    /// Number of cache reads.
    pub cache_read_count: i32,

    /// Size of cache read.
    pub cache_read_input_bytes: i64,

    /// Number of cache writes.
    pub cache_write_count: i32,

    /// Size of cache write.
    pub cache_write_input_bytes: i64,

    /// The load balancer address.
    pub lb_address: String,

    /// The tag of the actual engines sitting behind the GTP address.
    pub sampler_tag: String,

    /// The underlying checkpoint mount path for the sampler that served this request.
    pub sampler_checkpoint_mount: String,
}