test_common_ext.rs 12.1 KB
Newer Older
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
// SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

use dynamo_llm::protocols::{
    common::StopConditionsProvider,
    openai::{
        chat_completions::NvCreateChatCompletionRequest,
        common_ext::{CommonExt, CommonExtProvider},
        completions::NvCreateCompletionRequest,
        nvext::NvExt,
    },
};

#[test]
fn test_chat_completions_ignore_eos_from_common() {
    // Test that ignore_eos can be specified at root level
    let json_str = r#"{
        "model": "test-model",
        "messages": [{"role": "user", "content": "Hello"}],
        "ignore_eos": true,
        "min_tokens": 100
    }"#;

    let request: NvCreateChatCompletionRequest = serde_json::from_str(json_str).unwrap();

    assert_eq!(request.common.ignore_eos, Some(true));
    assert_eq!(request.common.min_tokens, Some(100));
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
    assert_eq!(request.common.include_stop_str_in_output, None);
}

#[test]
fn test_chat_completions_include_stop_str_in_output_from_common() {
    let json_str = r#"{
        "model": "test-model",
        "messages": [{"role": "user", "content": "Hello"}],
        "include_stop_str_in_output": true
    }"#;

    let request: NvCreateChatCompletionRequest = serde_json::from_str(json_str).unwrap();

    assert_eq!(request.common.include_stop_str_in_output, Some(true));
    assert_eq!(request.get_include_stop_str_in_output(), Some(true));
}

#[test]
fn test_completions_include_stop_str_in_output_from_common() {
    let json_str = r#"{
        "model": "test-model",
        "prompt": "Hello world",
        "include_stop_str_in_output": true
    }"#;

    let request: NvCreateCompletionRequest = serde_json::from_str(json_str).unwrap();
    assert_eq!(request.common.include_stop_str_in_output, Some(true));
    // When exposed on completions, this should also be available via the provider
    assert_eq!(request.get_include_stop_str_in_output(), Some(true));
}

#[test]
fn test_sampling_parameters_include_stop_str_in_output_extraction() {
    use dynamo_llm::protocols::common::SamplingOptionsProvider;

    let request = NvCreateChatCompletionRequest {
        inner: Default::default(),
        common: CommonExt::builder()
            .include_stop_str_in_output(true)
            .build()
            .unwrap(),
        nvext: None,
70
        chat_template_args: None,
71
        unsupported_fields: Default::default(),
72
73
74
75
    };

    let sampling = request.extract_sampling_options().unwrap();
    assert_eq!(sampling.include_stop_str_in_output, Some(true));
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
}

#[test]
fn test_chat_completions_guided_decoding_from_common() {
    // Test that guided_json can be specified at root level
    let json_str = r#"{
        "model": "test-model",
        "messages": [{"role": "user", "content": "Hello"}],
        "guided_json": {"key": "value"}
    }"#;

    let request: NvCreateChatCompletionRequest = serde_json::from_str(json_str).unwrap();

    assert_eq!(
        request.common.guided_json,
        Some(serde_json::json!({"key": "value"}))
    );
    assert_eq!(
        request.get_guided_json(),
95
        Some(serde_json::json!({"key": "value"}))
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
    );

    // Test guided_regex can be specified at root level
    let json_str = r#"{
        "model": "test-model",
        "messages": [{"role": "user", "content": "Hello"}],
        "guided_regex": "*"
    }"#;

    let request: NvCreateChatCompletionRequest = serde_json::from_str(json_str).unwrap();

    assert_eq!(request.common.guided_regex, Some("*".to_string()));
    assert_eq!(request.get_guided_regex(), Some("*".to_string()));

    // Test guided_grammar can be specified at root level
    let json_str = r#"{
        "model": "test-model",
        "messages": [{"role": "user", "content": "Hello"}],
        "guided_grammar": "::=[1-9]"
    }"#;

    let request: NvCreateChatCompletionRequest = serde_json::from_str(json_str).unwrap();

    assert_eq!(request.common.guided_grammar, Some("::=[1-9]".to_string()));
    assert_eq!(request.get_guided_grammar(), Some("::=[1-9]".to_string()));

    // Test guided_choice can be specified at root level
    let json_str = r#"{
        "model": "test-model",
        "messages": [{"role": "user", "content": "Hello"}],
        "guided_choice": ["choice1", "choice2"]
    }"#;

    let request: NvCreateChatCompletionRequest = serde_json::from_str(json_str).unwrap();

    assert_eq!(
        request.common.guided_choice,
        Some(vec!["choice1".to_string(), "choice2".to_string()])
    );
    assert_eq!(
        request.get_guided_choice(),
        Some(vec!["choice1".to_string(), "choice2".to_string()])
    );

    // Test guided_decoding_backend can be specified at root level
    let json_str = r#"{
        "model": "test-model",
        "messages": [{"role": "user", "content": "Hello"}],
        "guided_decoding_backend": "backend"
    }"#;

    let request: NvCreateChatCompletionRequest = serde_json::from_str(json_str).unwrap();

    assert_eq!(
        request.common.guided_decoding_backend,
        Some("backend".to_string())
    );
    assert_eq!(
        request.get_guided_decoding_backend(),
        Some("backend".to_string())
    );
}

#[test]
160
161
fn test_chat_completions_common_values() {
    // Test that ignore_eos and guided_regex are read from common (root level)
162
163
164
165
166
    let json_str = r#"{
        "model": "test-model",
        "messages": [{"role": "user", "content": "Hello"}],
        "ignore_eos": false,
        "guided_regex": ".*",
167
        "min_tokens": 50
168
169
170
171
172
173
    }"#;

    let request: NvCreateChatCompletionRequest = serde_json::from_str(json_str).unwrap();

    assert_eq!(request.common.ignore_eos, Some(false));
    assert_eq!(request.common.guided_regex, Some(".*".to_string()));
174
175
    assert_eq!(request.get_guided_regex(), Some(".*".to_string()));
    // Verify extraction through stop conditions
176
    let stop_conditions = request.extract_stop_conditions().unwrap();
177
    assert_eq!(stop_conditions.ignore_eos, Some(false));
178
179
180
    assert_eq!(stop_conditions.min_tokens, Some(50));
}

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
#[test]
fn test_max_thinking_tokens_extraction() {
    // Test that max_thinking_tokens is extracted from nvext to StopConditions
    let json_str = r#"{
        "model": "test-model",
        "messages": [{"role": "user", "content": "Hello"}],
        "nvext": {
            "max_thinking_tokens": 1024
        }
    }"#;

    let request: NvCreateChatCompletionRequest = serde_json::from_str(json_str).unwrap();

    // Verify nvext parsing
    assert_eq!(
        request.nvext.as_ref().unwrap().max_thinking_tokens,
        Some(1024)
    );

    // Verify extraction to StopConditions
    let stop_conditions = request.extract_stop_conditions().unwrap();
    assert_eq!(stop_conditions.max_thinking_tokens, Some(1024));

    // Test with None value
    let json_str_none = r#"{
        "model": "test-model",
        "messages": [{"role": "user", "content": "Hello"}]
    }"#;

    let request_none: NvCreateChatCompletionRequest = serde_json::from_str(json_str_none).unwrap();
    let stop_conditions_none = request_none.extract_stop_conditions().unwrap();
    assert_eq!(stop_conditions_none.max_thinking_tokens, None);
}

215
#[test]
216
217
fn test_chat_completions_no_common_values() {
    // Test that when no common values are set, we get None
218
219
    let json_str = r#"{
        "model": "test-model",
220
        "messages": [{"role": "user", "content": "Hello"}]
221
222
223
224
225
226
    }"#;

    let request: NvCreateChatCompletionRequest = serde_json::from_str(json_str).unwrap();

    assert_eq!(request.common.ignore_eos, None);
    assert_eq!(request.common.guided_json, None);
227
    assert_eq!(request.get_guided_json(), None);
228
229
    // Verify through stop conditions extraction
    let stop_conditions = request.extract_stop_conditions().unwrap();
230
    assert_eq!(stop_conditions.ignore_eos, None);
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
    assert_eq!(stop_conditions.min_tokens, None);
}

#[test]
fn test_completions_ignore_eos_from_common() {
    // Test that ignore_eos can be specified at root level for completions
    let json_str = r#"{
        "model": "test-model",
        "prompt": "Hello world",
        "ignore_eos": true,
        "min_tokens": 200
    }"#;

    let request: NvCreateCompletionRequest = serde_json::from_str(json_str).unwrap();

    assert_eq!(request.common.ignore_eos, Some(true));
    assert_eq!(request.common.min_tokens, Some(200));

    // Verify through stop conditions extraction
    let stop_conditions = request.extract_stop_conditions().unwrap();
    assert_eq!(stop_conditions.ignore_eos, Some(true));
    assert_eq!(stop_conditions.min_tokens, Some(200));
}

#[test]
256
257
fn test_completions_common_values() {
    // Test that root-level ignore_eos is read from common for completions
258
259
260
261
    let json_str = r#"{
        "model": "test-model",
        "prompt": "Hello world",
        "ignore_eos": false,
262
        "min_tokens": 75
263
264
265
266
267
    }"#;

    let request: NvCreateCompletionRequest = serde_json::from_str(json_str).unwrap();

    assert_eq!(request.common.ignore_eos, Some(false));
268
    // Verify extraction through stop conditions
269
    let stop_conditions = request.extract_stop_conditions().unwrap();
270
    assert_eq!(stop_conditions.ignore_eos, Some(false));
271
272
273
274
275
276
277
    assert_eq!(stop_conditions.min_tokens, Some(75));
}

#[test]
fn test_serialization_preserves_structure() {
    // Test that serialization preserves the flattened structure
    let request = NvCreateChatCompletionRequest {
278
        inner: dynamo_async_openai::types::CreateChatCompletionRequest {
279
            model: "test-model".to_string(),
280
281
282
            messages: vec![dynamo_async_openai::types::ChatCompletionRequestMessage::User(
                dynamo_async_openai::types::ChatCompletionRequestUserMessage {
                    content: dynamo_async_openai::types::ChatCompletionRequestUserMessageContent::Text(
283
284
285
286
287
288
289
290
291
292
293
294
295
                        "Hello".to_string(),
                    ),
                    ..Default::default()
                },
            )],
            ..Default::default()
        },
        common: CommonExt {
            ignore_eos: Some(true),
            min_tokens: Some(100),
            ..Default::default()
        },
        nvext: Some(NvExt {
296
            greed_sampling: Some(false),
297
298
            ..Default::default()
        }),
299
        chat_template_args: None,
300
        unsupported_fields: Default::default(),
301
302
303
304
305
306
307
308
    };

    let json = serde_json::to_value(&request).unwrap();

    // Check that fields are at the expected levels
    assert_eq!(json["model"], "test-model");
    assert_eq!(json["ignore_eos"], true); // From common (flattened)
    assert_eq!(json["min_tokens"], 100); // From common (flattened)
309
    assert_eq!(json["nvext"]["greed_sampling"], false); // From nvext
310

311
    // Verify extraction through stop conditions
312
    let stop_conditions = request.extract_stop_conditions().unwrap();
313
    assert_eq!(stop_conditions.ignore_eos, Some(true));
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
    assert_eq!(stop_conditions.min_tokens, Some(100));
}

#[test]
fn test_min_tokens_only_at_root_level() {
    // Test that min_tokens is only available at root level, not in nvext
    let json_str = r#"{
        "model": "test-model",
        "messages": [{"role": "user", "content": "Hello"}],
        "min_tokens": 150
    }"#;

    let request: NvCreateChatCompletionRequest = serde_json::from_str(json_str).unwrap();

    assert_eq!(request.common.min_tokens, Some(150));

    // Verify through stop conditions extraction
    let stop_conditions = request.extract_stop_conditions().unwrap();
    assert_eq!(stop_conditions.min_tokens, Some(150));
}
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349

#[test]
fn test_sampling_parameters_extraction() {
    use dynamo_llm::protocols::common::SamplingOptionsProvider;
    use dynamo_llm::protocols::openai::chat_completions::NvCreateChatCompletionRequest;
    use dynamo_llm::protocols::openai::common_ext::CommonExt;

    // Test that top_k and repetition_penalty are extracted in sampling options when passed a top level
    let request = NvCreateChatCompletionRequest {
        inner: Default::default(),
        common: CommonExt::builder()
            .top_k(42)
            .repetition_penalty(1.3)
            .build()
            .unwrap(),
        nvext: None,
350
        chat_template_args: None,
351
        unsupported_fields: Default::default(),
352
353
354
355
356
357
358
    };

    let sampling_options = request.extract_sampling_options().unwrap();

    assert_eq!(sampling_options.top_k, Some(42));
    assert_eq!(sampling_options.repetition_penalty, Some(1.3));
}