dynamo-llm 1.1.1

Dynamo LLM Library
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
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
// SPDX-FileCopyrightText: Copyright (c) 2024-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

//! Tests for DeepSeek V3.2 encoding against official test data
//!
//! These tests use the official test files from:
//! https://huggingface.co/deepseek-ai/DeepSeek-V3.2/tree/main/encoding

use dynamo_llm::preprocessor::prompt::deepseek_v32::{ThinkingMode, encode_messages};
use dynamo_llm::preprocessor::prompt::{OAIChatLikeRequest, OAIPromptFormatter};
use dynamo_llm::protocols::openai::chat_completions::NvCreateChatCompletionRequest;
use serde_json::Value as JsonValue;
use std::fs;
use std::path::PathBuf;

fn get_test_data_path() -> PathBuf {
    PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests/data/deepseek-v3.2")
}

fn run_official_test(input_file: &str, output_file: &str) {
    let test_dir = get_test_data_path();

    // Load test input
    let input_path = test_dir.join(input_file);
    let input_data: JsonValue = serde_json::from_str(
        &fs::read_to_string(&input_path)
            .unwrap_or_else(|_| panic!("Failed to read {}", input_file)),
    )
    .unwrap_or_else(|_| panic!("Failed to parse {}", input_file));

    // Load expected output
    let output_path = test_dir.join(output_file);
    let expected_output = fs::read_to_string(&output_path)
        .unwrap_or_else(|_| panic!("Failed to read {}", output_file));

    // Extract messages and tools
    let mut messages = input_data["messages"]
        .as_array()
        .expect("Missing messages")
        .clone();

    // Add tools to first message (system) if present
    if let Some(tools) = input_data.get("tools")
        && let Some(first_msg) = messages.get_mut(0)
    {
        first_msg
            .as_object_mut()
            .unwrap()
            .insert("tools".to_string(), tools.clone());
    }

    // Encode messages
    let result = encode_messages(
        &messages,
        ThinkingMode::Thinking,
        true, // add_bos_token
    )
    .expect("Failed to encode messages");

    // Compare outputs
    let expected = expected_output.trim();
    let actual = result.trim();

    if expected != actual {
        println!("=== Test: {} ===", input_file);

        // Show first difference
        let exp_lines: Vec<&str> = expected.lines().collect();
        let act_lines: Vec<&str> = actual.lines().collect();

        for (i, (exp_line, act_line)) in exp_lines.iter().zip(act_lines.iter()).enumerate() {
            if exp_line != act_line {
                println!("Line {} differs:", i + 1);
                println!("  Expected: {}", exp_line);
                println!("  Actual:   {}", act_line);
                break;
            }
        }

        if exp_lines.len() != act_lines.len() {
            println!("\nLine count mismatch:");
            println!("  Expected: {} lines", exp_lines.len());
            println!("  Actual:   {} lines", act_lines.len());
        }

        panic!("Output does not match expected for {}", input_file);
    }
}

#[test]
fn test_official_basic_example() {
    run_official_test("test_input.json", "test_output.txt");
}

#[test]
fn test_official_search_without_date() {
    run_official_test(
        "test_input_search_wo_date.json",
        "test_output_search_wo_date.txt",
    );
}

#[test]
fn test_official_search_with_date() {
    run_official_test(
        "test_input_search_w_date.json",
        "test_output_search_w_date.txt",
    );
}

#[test]
fn test_simple_conversation_no_tools() {
    // Simple test without tools
    let messages = serde_json::json!([
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "Hello!"},
        {"role": "assistant", "content": "Hi! How can I help you today?"},
        {"role": "user", "content": "What is 2+2?"}
    ]);

    let result = encode_messages(messages.as_array().unwrap(), ThinkingMode::Thinking, true)
        .expect("Failed to encode");

    // Check basic structure
    assert!(result.starts_with("<|begin▁of▁sentence|>"));
    assert!(result.contains("<|User|>Hello!<|Assistant|>"));
    assert!(result.contains("Hi! How can I help you today?"));
    assert!(result.contains("<|end▁of▁sentence|>"));
}

#[test]
fn test_comprehensive_conversation_with_tools() {
    // Comprehensive test covering all features with English text
    let messages = serde_json::json!([
        {
            "role": "system",
            "content": "You are a helpful weather assistant.",
            "tools": [
                {
                    "type": "function",
                    "function": {
                        "name": "get_datetime",
                        "description": "Get the current date and time",
                        "parameters": {
                            "type": "object",
                            "properties": {
                                "timezone": {
                                    "type": "string",
                                    "description": "The timezone, e.g. America/New_York, UTC"
                                }
                            },
                            "required": ["timezone"]
                        }
                    }
                },
                {
                    "type": "function",
                    "function": {
                        "name": "get_weather",
                        "description": "Get the weather for a specific date and location",
                        "parameters": {
                            "type": "object",
                            "properties": {
                                "location": {
                                    "type": "string",
                                    "description": "The city name, e.g. New York, San Francisco"
                                },
                                "date": {
                                    "type": "string",
                                    "description": "The date in YYYY-MM-DD format"
                                }
                            },
                            "required": ["location", "date"]
                        }
                    }
                }
            ]
        },
        {"role": "user", "content": "What's the weather tomorrow in San Francisco and New York?"},
        {
            "role": "assistant",
            "reasoning_content": "User is asking about tomorrow's weather. I need to first get the current date to calculate tomorrow's date.",
            "tool_calls": [{
                "id": "call_1",
                "type": "function",
                "function": {
                    "name": "get_datetime",
                    "arguments": "{\"timezone\": \"America/New_York\"}"
                }
            }]
        },
        {
            "role": "tool",
            "tool_call_id": "call_1",
            "content": "{\"current_date\": \"2024-01-15\", \"current_time\": \"14:30:00\", \"timezone\": \"America/New_York\"}"
        },
        {
            "role": "assistant",
            "reasoning_content": "Now I know today is 2024-01-15, so tomorrow is 2024-01-16. Let me query the weather for both cities.",
            "tool_calls": [
                {
                    "id": "call_2",
                    "type": "function",
                    "function": {
                        "name": "get_weather",
                        "arguments": "{\"location\": \"San Francisco\", \"date\": \"2024-01-16\"}"
                    }
                },
                {
                    "id": "call_3",
                    "type": "function",
                    "function": {
                        "name": "get_weather",
                        "arguments": "{\"location\": \"New York\", \"date\": \"2024-01-16\"}"
                    }
                }
            ]
        },
        {
            "role": "tool",
            "tool_call_id": "call_2",
            "content": "{\"location\": \"San Francisco\", \"date\": \"2024-01-16\", \"temperature_high\": \"65\", \"temperature_low\": \"55\", \"weather\": \"sunny\", \"humidity\": \"70%\"}"
        },
        {
            "role": "tool",
            "tool_call_id": "call_3",
            "content": "{\"location\": \"New York\", \"date\": \"2024-01-16\", \"temperature_high\": \"30\", \"temperature_low\": \"20\", \"weather\": \"cloudy\", \"humidity\": \"45%\"}"
        },
        {
            "role": "assistant",
            "reasoning_content": "Got the weather data for both cities. Let me format a nice response for the user.",
            "content": "Here's the weather forecast for tomorrow (January 16, 2024):\n\n**San Francisco**:\n- Weather: Sunny\n- High: 65°F\n- Low: 55°F\n- Humidity: 70%\n\n**New York**:\n- Weather: Cloudy\n- High: 30°F\n- Low: 20°F\n- Humidity: 45%\n\nSan Francisco will be warm and sunny, while New York will be cold and cloudy. Dress warmly if you're in New York!"
        }
    ]);

    let result = encode_messages(messages.as_array().unwrap(), ThinkingMode::Thinking, true)
        .expect("Failed to encode");

    // Check all major components are present
    assert!(result.starts_with("<|begin▁of▁sentence|>"));
    assert!(result.contains("## Tools"));
    assert!(result.contains("get_datetime"));
    assert!(result.contains("get_weather"));
    assert!(result.contains("<|User|>What's the weather tomorrow"));
    assert!(result.contains("<|Assistant|><think>"));
    assert!(result.contains("User is asking about tomorrow's weather"));
    assert!(result.contains("</think>"));
    assert!(result.contains("<|DSML|function_calls>"));
    assert!(result.contains("<|DSML|invoke name=\"get_datetime\">"));
    assert!(result.contains(
        "<|DSML|parameter name=\"timezone\" string=\"true\">America/New_York</|DSML|parameter>"
    ));
    assert!(result.contains("</|DSML|function_calls>"));
    assert!(result.contains("<function_results>"));
    assert!(result.contains("<result>"));
    assert!(result.contains("</function_results>"));
    assert!(result.contains("San Francisco"));
    assert!(result.contains("New York"));
    assert!(result.contains("<|end▁of▁sentence|>"));
}

#[test]
fn test_with_reasoning_content() {
    let messages = serde_json::json!([
        {"role": "user", "content": "Calculate 15 * 23"},
        {
            "role": "assistant",
            "content": "The answer is 345.",
            "reasoning_content": "Let me compute this step by step: 15 * 23 = 15 * 20 + 15 * 3 = 300 + 45 = 345"
        }
    ]);

    let result = encode_messages(messages.as_array().unwrap(), ThinkingMode::Thinking, true)
        .expect("Failed to encode");

    // Should contain thinking tags with reasoning
    assert!(result.contains("<think>"));
    assert!(result.contains("</think>"));
    assert!(result.contains("Let me compute this step by step"));
}

#[test]
fn test_reasoning_content_survives_chat_request_parsing_and_rendering() {
    let json = r#"{
        "model": "deepseek-v3.2",
        "messages": [
            {"role": "user", "content": "weather tomorrow?"},
            {
                "role": "assistant",
                "reasoning_content": "need date first",
                "tool_calls": [{
                    "id": "call_1",
                    "type": "function",
                    "function": {
                        "name": "get_datetime",
                        "arguments": "{\"timezone\":\"UTC\"}"
                    }
                }]
            },
            {
                "role": "tool",
                "tool_call_id": "call_1",
                "content": "{\"current_date\":\"2024-01-15\"}"
            }
        ]
    }"#;

    let request: NvCreateChatCompletionRequest = serde_json::from_str(json).unwrap();
    let messages = serde_json::to_value(request.messages()).unwrap();
    assert_eq!(
        messages[1]["reasoning_content"],
        serde_json::Value::String("need date first".to_string())
    );

    let formatter =
        dynamo_llm::preprocessor::prompt::deepseek_v32::DeepSeekV32Formatter::new_thinking();
    let rendered = formatter.render(&request).unwrap();

    assert!(rendered.contains("need date first"));
    assert!(rendered.contains("<think>"));
    assert!(rendered.contains("</think>"));
}

// Regression test for the KV-cache flattening bug.
//
// Models like GLM-5 and Qwen3 (Pattern A) emit interleaved thinking:
//
//   <think>A</think> <call>t1</call> <think>B</think> <call>t2</call>
//
// `convert_assistant_blocks` now serialises this as a JSON *array*:
//
//   "reasoning_content": ["A", "B", ""]
//
// OLD CODE stored `reasoning_content: Option<String>` — a JSON array would fail
// to deserialise into that type, so this test panics at `.unwrap()` on old code.
// NEW CODE stores `Option<ReasoningContent>` which accepts both string and array,
// and round-trips the array form faithfully.
#[test]
fn test_reasoning_segments_roundtrip_through_parse_and_render() {
    // Simulate what convert_assistant_blocks produces for an interleaved GLM-5 turn:
    //   [Think("A"), Tool(t1), Think("B"), Tool(t2)]  →  segments = ["A", "B", ""]
    let json = r#"{
        "model": "glm-5",
        "messages": [
            {"role": "user", "content": "call two tools"},
            {
                "role": "assistant",
                "reasoning_content": ["A", "B", ""],
                "tool_calls": [
                    {"id": "t1", "type": "function", "function": {"name": "f1", "arguments": "{}"}},
                    {"id": "t2", "type": "function", "function": {"name": "f2", "arguments": "{}"}}
                ]
            },
            {"role": "tool", "tool_call_id": "t1", "content": "r1"},
            {"role": "tool", "tool_call_id": "t2", "content": "r2"}
        ]
    }"#;

    // OLD CODE: serde_json::from_str fails here because Option<String> can't
    // deserialise a JSON array.  NEW CODE: succeeds.
    let request: NvCreateChatCompletionRequest = serde_json::from_str(json).unwrap();

    // Segments must survive the round-trip through serde_json
    let messages_json = serde_json::to_value(request.messages()).unwrap();
    assert!(
        messages_json[1]["reasoning_content"].is_array(),
        "reasoning_content must serialise as a JSON array to preserve positional info; \
         a string would lose which reasoning preceded which tool call"
    );
    let segs = messages_json[1]["reasoning_content"].as_array().unwrap();
    assert_eq!(segs.len(), 3);
    assert_eq!(segs[0].as_str().unwrap(), "A"); // precedes t1
    assert_eq!(segs[1].as_str().unwrap(), "B"); // precedes t2
    assert_eq!(segs[2].as_str().unwrap(), ""); // no trailing reasoning

    // The formatter must not drop the reasoning content when segments are used.
    // (DeepSeek V3.2 joins segments into one <think> block; this confirms the
    // content is not silently discarded.)
    let formatter =
        dynamo_llm::preprocessor::prompt::deepseek_v32::DeepSeekV32Formatter::new_thinking();
    let rendered = formatter.render(&request).unwrap();
    assert!(
        rendered.contains("A"),
        "segment A must appear in rendered output"
    );
    assert!(
        rendered.contains("B"),
        "segment B must appear in rendered output"
    );
    assert!(rendered.contains("<think>"));
    assert!(rendered.contains("</think>"));
}

#[test]
fn test_tool_call_formatting() {
    let messages = serde_json::json!([
        {"role": "user", "content": "What's the weather in Beijing?"},
        {
            "role": "assistant",
            "content": "",
            "tool_calls": [{
                "id": "call_123",
                "type": "function",
                "function": {
                    "name": "get_weather",
                    "arguments": "{\"location\": \"Beijing\", \"unit\": \"celsius\"}"
                }
            }]
        }
    ]);

    let result = encode_messages(messages.as_array().unwrap(), ThinkingMode::Thinking, true)
        .expect("Failed to encode");

    // Check DSML format
    assert!(result.contains("<|DSML|function_calls>"));
    assert!(result.contains("<|DSML|invoke name=\"get_weather\">"));
    assert!(result.contains(
        "<|DSML|parameter name=\"location\" string=\"true\">Beijing</|DSML|parameter>"
    ));
    assert!(
        result.contains(
            "<|DSML|parameter name=\"unit\" string=\"true\">celsius</|DSML|parameter>"
        )
    );
    assert!(result.contains("</|DSML|invoke>"));
    assert!(result.contains("</|DSML|function_calls>"));
}

#[test]
fn test_tool_results() {
    let messages = serde_json::json!([
        {"role": "user", "content": "Check weather"},
        {
            "role": "assistant",
            "content": "",
            "tool_calls": [{
                "id": "call_123",
                "type": "function",
                "function": {
                    "name": "get_weather",
                    "arguments": "{\"location\": \"Beijing\"}"
                }
            }]
        },
        {
            "role": "tool",
            "tool_call_id": "call_123",
            "content": "{\"temperature\": \"20C\", \"condition\": \"sunny\"}"
        }
    ]);

    let result = encode_messages(messages.as_array().unwrap(), ThinkingMode::Thinking, true)
        .expect("Failed to encode");

    // Check function_results wrapper
    assert!(result.contains("<function_results>"));
    assert!(result.contains("<result>"));
    assert!(result.contains("{\"temperature\": \"20C\", \"condition\": \"sunny\"}"));
    assert!(result.contains("</result>"));
    assert!(result.contains("</function_results>"));
}

#[test]
fn test_multiple_tool_calls() {
    let messages = serde_json::json!([
        {"role": "user", "content": "Weather in Beijing and Shanghai"},
        {
            "role": "assistant",
            "content": "",
            "tool_calls": [
                {
                    "id": "call_1",
                    "type": "function",
                    "function": {
                        "name": "get_weather",
                        "arguments": "{\"location\": \"Beijing\"}"
                    }
                },
                {
                    "id": "call_2",
                    "type": "function",
                    "function": {
                        "name": "get_weather",
                        "arguments": "{\"location\": \"Shanghai\"}"
                    }
                }
            ]
        }
    ]);

    let result = encode_messages(messages.as_array().unwrap(), ThinkingMode::Thinking, true)
        .expect("Failed to encode");

    // Should contain both tool calls
    assert!(result.contains("Beijing"));
    assert!(result.contains("Shanghai"));
    // Should be in same function_calls block
    assert_eq!(result.matches("<|DSML|function_calls>").count(), 1);
    assert_eq!(result.matches("</|DSML|function_calls>").count(), 1);
    // But two invocations
    assert_eq!(result.matches("<|DSML|invoke").count(), 2);
}

#[test]
fn test_chat_mode_vs_thinking_mode() {
    let messages = serde_json::json!([
        {"role": "user", "content": "Hello"}
    ]);

    let chat_result = encode_messages(messages.as_array().unwrap(), ThinkingMode::Chat, true)
        .expect("Failed to encode");

    let thinking_result =
        encode_messages(messages.as_array().unwrap(), ThinkingMode::Thinking, true)
            .expect("Failed to encode");

    // Chat mode should have </think>, thinking mode should have <think>
    assert!(chat_result.contains("</think>"));
    assert!(!chat_result.contains("<think>"));

    assert!(thinking_result.contains("<think>"));
}