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
//! Tests for the chat module types.

mod common;

use pretty_assertions::assert_eq;
use serde_json::json;
use xai_openapi::chat::{
    ChatRequest, ChatResponse, ChatResponseChunk, Choice, ChoiceChunk, ChoiceMessage, Delta,
    LogProbs, Message, TokenLogProb, TopLogProb,
};
use xai_openapi::common::Content;

#[test]
fn test_chat_request_minimal() {
    let json = json!({
        "messages": []
    });
    let request: ChatRequest = serde_json::from_value(json).unwrap();
    assert!(request.messages.is_empty());
    assert!(request.model.is_none());
}

#[test]
fn test_chat_request_full() {
    let json = json!({
        "model": "grok-3",
        "messages": [
            {
                "role": "system",
                "content": "You are a helpful assistant."
            },
            {
                "role": "user",
                "content": "Hello!"
            }
        ],
        "temperature": 0.7,
        "top_p": 0.9,
        "n": 1,
        "stream": false,
        "max_completion_tokens": 1000,
        "presence_penalty": 0.0,
        "frequency_penalty": 0.0,
        "seed": 42
    });

    let request: ChatRequest = serde_json::from_value(json.clone()).unwrap();
    assert_eq!(request.model, Some("grok-3".to_string()));
    assert_eq!(request.messages.len(), 2);
    assert_eq!(request.temperature, Some(0.7));
    assert_eq!(request.seed, Some(42));

    // Round-trip
    let serialized = serde_json::to_value(&request).unwrap();
    let deserialized: ChatRequest = serde_json::from_value(serialized).unwrap();
    assert_eq!(request, deserialized);
}

#[test]
fn test_chat_request_default_roundtrip() {
    common::test_default_roundtrip::<ChatRequest>();
}

#[test]
fn test_message_system() {
    let json = json!({
        "role": "system",
        "content": "You are a helpful assistant.",
        "name": "system_prompt"
    });

    let message: Message = serde_json::from_value(json).unwrap();
    match message {
        Message::System { content, name } => {
            assert_eq!(
                content,
                Content::Text("You are a helpful assistant.".to_string())
            );
            assert_eq!(name, Some("system_prompt".to_string()));
        }
        _ => panic!("Expected System message"),
    }
}

#[test]
fn test_message_user() {
    let json = json!({
        "role": "user",
        "content": "What is the weather?"
    });

    let message: Message = serde_json::from_value(json).unwrap();
    match message {
        Message::User { content, name } => {
            assert_eq!(content, Content::Text("What is the weather?".to_string()));
            assert!(name.is_none());
        }
        _ => panic!("Expected User message"),
    }
}

#[test]
fn test_message_assistant() {
    let json = json!({
        "role": "assistant",
        "content": "The weather is sunny.",
        "reasoning_content": "Based on the forecast..."
    });

    let message: Message = serde_json::from_value(json).unwrap();
    match message {
        Message::Assistant {
            content,
            reasoning_content,
            tool_calls,
            ..
        } => {
            assert_eq!(
                content,
                Some(Content::Text("The weather is sunny.".to_string()))
            );
            assert_eq!(
                reasoning_content,
                Some("Based on the forecast...".to_string())
            );
            assert!(tool_calls.is_none());
        }
        _ => panic!("Expected Assistant message"),
    }
}

#[test]
fn test_message_assistant_with_tool_calls() {
    let json = json!({
        "role": "assistant",
        "content": null,
        "tool_calls": [
            {
                "id": "call_123",
                "function": {
                    "name": "get_weather",
                    "arguments": "{\"location\": \"San Francisco\"}"
                }
            }
        ]
    });

    let message: Message = serde_json::from_value(json).unwrap();
    match message {
        Message::Assistant { tool_calls, .. } => {
            let calls = tool_calls.unwrap();
            assert_eq!(calls.len(), 1);
            assert_eq!(calls[0].id, "call_123");
            assert_eq!(calls[0].function.name, "get_weather");
        }
        _ => panic!("Expected Assistant message"),
    }
}

#[test]
fn test_message_tool() {
    let json = json!({
        "role": "tool",
        "content": "{\"temperature\": 72}",
        "tool_call_id": "call_123"
    });

    let message: Message = serde_json::from_value(json).unwrap();
    match message {
        Message::Tool {
            content,
            tool_call_id,
        } => {
            assert_eq!(content, Content::Text("{\"temperature\": 72}".to_string()));
            assert_eq!(tool_call_id, Some("call_123".to_string()));
        }
        _ => panic!("Expected Tool message"),
    }
}

#[test]
fn test_message_function_deprecated() {
    let json = json!({
        "role": "function",
        "content": "result",
        "name": "my_function"
    });

    let message: Message = serde_json::from_value(json).unwrap();
    match message {
        Message::Function { content, name } => {
            assert_eq!(content, Content::Text("result".to_string()));
            assert_eq!(name, Some("my_function".to_string()));
        }
        _ => panic!("Expected Function message"),
    }
}

#[test]
fn test_chat_response() {
    let json = json!({
        "id": "chatcmpl-123",
        "object": "chat.completion",
        "created": 1677652288,
        "model": "grok-3",
        "choices": [
            {
                "index": 0,
                "message": {
                    "role": "assistant",
                    "content": "Hello! How can I help you today?"
                },
                "finish_reason": "stop"
            }
        ],
        "usage": {
            "prompt_tokens": 10,
            "completion_tokens": 20,
            "total_tokens": 30,
            "prompt_tokens_details": {
                "text_tokens": 10,
                "audio_tokens": 0,
                "image_tokens": 0,
                "cached_tokens": 0
            },
            "completion_tokens_details": {
                "reasoning_tokens": 0,
                "audio_tokens": 0,
                "accepted_prediction_tokens": 0,
                "rejected_prediction_tokens": 0
            },
            "num_sources_used": 0
        }
    });

    let response: ChatResponse = serde_json::from_value(json).unwrap();
    assert_eq!(response.id, "chatcmpl-123");
    assert_eq!(response.object, "chat.completion");
    assert_eq!(response.model, "grok-3");
    assert_eq!(response.choices.len(), 1);
    assert_eq!(response.choices[0].finish_reason, Some("stop".to_string()));
}

#[test]
fn test_chat_response_default_roundtrip() {
    common::test_default_roundtrip::<ChatResponse>();
}

#[test]
fn test_choice() {
    let json = json!({
        "index": 0,
        "message": {
            "role": "assistant",
            "content": "Test response"
        },
        "finish_reason": "stop"
    });

    let choice: Choice = common::test_roundtrip(json);
    assert_eq!(choice.index, 0);
    assert_eq!(choice.finish_reason, Some("stop".to_string()));
}

#[test]
fn test_choice_message() {
    let json = json!({
        "role": "assistant",
        "content": "Hello!",
        "reasoning_content": "thinking...",
        "refusal": null
    });

    let message: ChoiceMessage = common::test_roundtrip(json);
    assert_eq!(message.role, "assistant");
    assert_eq!(message.content, Some("Hello!".to_string()));
    assert_eq!(message.reasoning_content, Some("thinking...".to_string()));
}

#[test]
fn test_chat_response_chunk() {
    let json = json!({
        "id": "chatcmpl-123",
        "object": "chat.completion.chunk",
        "created": 1677652288,
        "model": "grok-3",
        "choices": [
            {
                "index": 0,
                "delta": {
                    "role": "assistant",
                    "content": "Hello"
                },
                "finish_reason": null
            }
        ]
    });

    let chunk: ChatResponseChunk = common::test_roundtrip(json);
    assert_eq!(chunk.object, "chat.completion.chunk");
    assert_eq!(chunk.choices.len(), 1);
}

#[test]
fn test_choice_chunk() {
    let json = json!({
        "index": 0,
        "delta": {
            "content": " world"
        },
        "finish_reason": null
    });

    let chunk: ChoiceChunk = common::test_roundtrip(json);
    assert_eq!(chunk.delta.content, Some(" world".to_string()));
}

#[test]
fn test_delta() {
    let json = json!({
        "role": "assistant",
        "content": "Hello",
        "reasoning_content": "thinking"
    });

    let delta: Delta = common::test_roundtrip(json);
    assert_eq!(delta.role, Some("assistant".to_string()));
    assert_eq!(delta.content, Some("Hello".to_string()));
}

#[test]
fn test_logprobs() {
    let json = json!({
        "content": [
            {
                "token": "Hello",
                "logprob": -0.5,
                "top_logprobs": [
                    {"token": "Hello", "logprob": -0.5},
                    {"token": "Hi", "logprob": -1.2}
                ]
            }
        ]
    });

    let logprobs: LogProbs = common::test_roundtrip(json);
    let content = logprobs.content.unwrap();
    assert_eq!(content.len(), 1);
    assert_eq!(content[0].token, "Hello");
}

#[test]
fn test_token_logprob() {
    let json = json!({
        "token": "test",
        "logprob": -0.123,
        "top_logprobs": [],
        "bytes": [116, 101, 115, 116]
    });

    let token: TokenLogProb = common::test_roundtrip(json);
    assert_eq!(token.token, "test");
    assert_eq!(token.bytes, Some(vec![116, 101, 115, 116]));
}

#[test]
fn test_top_logprob() {
    let json = json!({
        "token": "the",
        "logprob": -0.01
    });

    let top: TopLogProb = common::test_roundtrip(json);
    assert_eq!(top.token, "the");
}

#[test]
fn test_message_default_roundtrip() {
    common::test_default_roundtrip::<Message>();
}

#[test]
fn test_all_message_variants_roundtrip() {
    // System
    let system = Message::System {
        content: "Be helpful".into(),
        name: Some("sys".to_string()),
    };
    let json = serde_json::to_value(&system).unwrap();
    let deserialized: Message = serde_json::from_value(json).unwrap();
    assert_eq!(system, deserialized);

    // User
    let user = Message::User {
        content: "Hello".into(),
        name: None,
    };
    let json = serde_json::to_value(&user).unwrap();
    let deserialized: Message = serde_json::from_value(json).unwrap();
    assert_eq!(user, deserialized);

    // Assistant
    let assistant = Message::Assistant {
        content: Some("Hi there!".into()),
        name: None,
        reasoning_content: None,
        tool_calls: None,
    };
    let json = serde_json::to_value(&assistant).unwrap();
    let deserialized: Message = serde_json::from_value(json).unwrap();
    assert_eq!(assistant, deserialized);

    // Tool
    let tool = Message::Tool {
        content: "result".into(),
        tool_call_id: Some("call_1".to_string()),
    };
    let json = serde_json::to_value(&tool).unwrap();
    let deserialized: Message = serde_json::from_value(json).unwrap();
    assert_eq!(tool, deserialized);

    // Function (deprecated)
    let function = Message::Function {
        content: "output".into(),
        name: Some("func".to_string()),
    };
    let json = serde_json::to_value(&function).unwrap();
    let deserialized: Message = serde_json::from_value(json).unwrap();
    assert_eq!(function, deserialized);
}