tool_parser_kimik2.rs 5.35 KB
Newer Older
1
2
//! Kimi K2 Parser Integration Tests

3
4
5
6
use sglang_router_rs::tool_parser::{KimiK2Parser, ToolParser};

mod common;
use common::create_test_tools;
7
8
9
10
11
12
13
14
15
16
17

#[tokio::test]
async fn test_kimik2_complete_parsing() {
    let parser = KimiK2Parser::new();

    let input = r#"Let me help you with that.
<|tool_calls_section_begin|>
<|tool_call_begin|>functions.get_weather:0<|tool_call_argument_begin|>{"location": "Tokyo", "units": "celsius"}<|tool_call_end|>
<|tool_calls_section_end|>
The weather in Tokyo is..."#;

18
    let (normal_text, tools) = parser.parse_complete(input).await.unwrap();
19
    assert_eq!(tools.len(), 1);
20
    assert_eq!(normal_text, "Let me help you with that.\n");
21
    assert_eq!(tools[0].function.name, "get_weather");
22

23
    let args: serde_json::Value = serde_json::from_str(&tools[0].function.arguments).unwrap();
24
25
26
27
28
29
30
31
32
33
34
35
36
    assert_eq!(args["location"], "Tokyo");
    assert_eq!(args["units"], "celsius");
}

#[tokio::test]
async fn test_kimik2_multiple_tools() {
    let parser = KimiK2Parser::new();

    let input = r#"<|tool_calls_section_begin|>
<|tool_call_begin|>functions.search:0<|tool_call_argument_begin|>{"query": "rust tutorials"}<|tool_call_end|>
<|tool_call_begin|>functions.translate:1<|tool_call_argument_begin|>{"text": "Hello", "to": "ja"}<|tool_call_end|>
<|tool_calls_section_end|>"#;

37
    let (normal_text, tools) = parser.parse_complete(input).await.unwrap();
38
    assert_eq!(tools.len(), 2);
39
    assert_eq!(normal_text, "");
40
41
    assert_eq!(tools[0].function.name, "search");
    assert_eq!(tools[1].function.name, "translate");
42
43
44
45
46
47
48
49
50
51
}

#[tokio::test]
async fn test_kimik2_with_whitespace() {
    let parser = KimiK2Parser::new();

    let input = r#"<|tool_calls_section_begin|>
<|tool_call_begin|> functions.test:0 <|tool_call_argument_begin|> {"key": "value", "num": 42} <|tool_call_end|>
<|tool_calls_section_end|>"#;

52
    let (normal_text, tools) = parser.parse_complete(input).await.unwrap();
53
    assert_eq!(tools.len(), 1);
54
    assert_eq!(normal_text, "");
55
    assert_eq!(tools[0].function.name, "test");
56

57
    let args: serde_json::Value = serde_json::from_str(&tools[0].function.arguments).unwrap();
58
59
60
61
62
63
    assert_eq!(args["key"], "value");
    assert_eq!(args["num"], 42);
}

#[tokio::test]
async fn test_kimik2_streaming() {
64
65
66
    let tools = create_test_tools();

    let mut parser = KimiK2Parser::new();
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82

    // Simulate streaming chunks
    let chunks = vec![
        "<|tool_calls_section_begin|>\n",
        "<|tool_call_begin|>functions.",
        "calculate:0",
        "<|tool_call_argument_begin|>",
        r#"{"x": 10, "#,
        r#""y": 20}"#,
        "<|tool_call_end|>\n",
        "<|tool_calls_section_end|>",
    ];

    let mut found_name = false;

    for chunk in chunks {
83
        let result = parser.parse_incremental(chunk, &tools).await.unwrap();
84

85
86
        for call in result.calls {
            if let Some(name) = call.name {
87
88
89
90
91
92
                assert_eq!(name, "calculate");
                found_name = true;
            }
        }
    }

93
    assert!(found_name, "Should have found tool name during streaming");
94
95
96
97
98
99
100
}

#[test]
fn test_kimik2_format_detection() {
    let parser = KimiK2Parser::new();

    // Should detect Kimi K2 format
101
102
    assert!(parser.has_tool_markers("<|tool_calls_section_begin|>"));
    assert!(parser.has_tool_markers("text with <|tool_calls_section_begin|> marker"));
103
104

    // Should not detect other formats
105
106
107
    assert!(!parser.has_tool_markers("[TOOL_CALLS]"));
    assert!(!parser.has_tool_markers("<tool_call>"));
    assert!(!parser.has_tool_markers("plain text"));
108
109
110
111
112
113
114
115
116
117
118
119
}

#[tokio::test]
async fn test_kimik2_sequential_indices() {
    let parser = KimiK2Parser::new();

    let input = r#"<|tool_calls_section_begin|>
<|tool_call_begin|>functions.first:0<|tool_call_argument_begin|>{"param": "a"}<|tool_call_end|>
<|tool_call_begin|>functions.second:1<|tool_call_argument_begin|>{"param": "b"}<|tool_call_end|>
<|tool_call_begin|>functions.third:2<|tool_call_argument_begin|>{"param": "c"}<|tool_call_end|>
<|tool_calls_section_end|>"#;

120
    let (normal_text, tools) = parser.parse_complete(input).await.unwrap();
121
    assert_eq!(tools.len(), 3);
122
    assert_eq!(normal_text, "");
123
124
125
    assert_eq!(tools[0].function.name, "first");
    assert_eq!(tools[1].function.name, "second");
    assert_eq!(tools[2].function.name, "third");
126
127
128
129
130
131
132
133
134
135
136
137
}

#[tokio::test]
async fn test_function_index_extraction() {
    let parser = KimiK2Parser::new();

    let input = r#"Text before tool calls.
<|tool_calls_section_begin|>
<|tool_call_begin|>functions.search:0<|tool_call_argument_begin|>{"query": "rust"}<|tool_call_end|>
<|tool_call_begin|>functions.calc:1<|tool_call_argument_begin|>{"x": 10}<|tool_call_end|>
<|tool_calls_section_end|>"#;

138
    let (normal_text, tools) = parser.parse_complete(input).await.unwrap();
139
    assert_eq!(tools.len(), 2);
140
    assert_eq!(normal_text, "Text before tool calls.\n");
141
142
    assert_eq!(tools[0].function.name, "search");
    assert_eq!(tools[1].function.name, "calc");
143
144
145
146
147
148
149
150
151
152
153
    // TODO: Verify indices are preserved: 0 and 1
}

#[tokio::test]
async fn test_namespace_extraction() {
    let parser = KimiK2Parser::new();

    let input = r#"<|tool_calls_section_begin|>
<|tool_call_begin|>api.tools.search:0<|tool_call_argument_begin|>{"q": "test"}<|tool_call_end|>
<|tool_calls_section_end|>"#;

154
155
    let (_normal_text, tools) = parser.parse_complete(input).await.unwrap();
    assert_eq!(tools.len(), 1);
156
    assert_eq!(tools[0].function.name, "api.tools.search"); // Includes full namespace
157
}