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

//! Integration test for parallel tool calling functionality
//!
//! This test simulates a complete chat completion request with parallel tool calls,
//! mocking the response and testing the tool call parsing functionality.
//!
//! The test covers:
//! - Creating a mock NvCreateChatCompletionRequest based on a curl request
//! - Mocking a chat completion response with parallel tool calls in <tool_call> format
//! - Parsing the tool calls using the hermes parser
//! - Validating OpenAI API compatibility
//! - Testing error handling with malformed content
//! - Ensuring tool call IDs are unique and properly formatted

use dynamo_llm::protocols::openai::{
    chat_completions::NvCreateChatCompletionRequest, common_ext::CommonExt,
};
use dynamo_parsers::{ToolCallResponse, ToolCallType, detect_and_parse_tool_call};
use serde_json::json;

/// Creates a mock NvCreateChatCompletionRequest based on the curl request
fn create_mock_chat_completion_request() -> NvCreateChatCompletionRequest {
    let messages = vec![
        dynamo_async_openai::types::ChatCompletionRequestMessage::System(
            dynamo_async_openai::types::ChatCompletionRequestSystemMessage {
                content: dynamo_async_openai::types::ChatCompletionRequestSystemMessageContent::Text(
                    "You MUST use two tools in parallel to resolve the user request: call get_current_weather for each city AND call is_holiday_today to check if today is a holiday. Do not answer without using both tools.".to_string()
                ),
                name: None,
            }
        ),
        dynamo_async_openai::types::ChatCompletionRequestMessage::User(
            dynamo_async_openai::types::ChatCompletionRequestUserMessage {
                content: dynamo_async_openai::types::ChatCompletionRequestUserMessageContent::Text(
                    "What is the weather in Dallas, Texas? Is today a holiday?".to_string()
                ),
                name: None,
            }
        ),
    ];

    let tools = vec![
        dynamo_async_openai::types::ChatCompletionTool {
            r#type: dynamo_async_openai::types::ChatCompletionToolType::Function,
            function: dynamo_async_openai::types::FunctionObject {
                name: "get_current_weather".to_string(),
                description: Some("Get weather for a city/state in specified units".to_string()),
                parameters: Some(json!({
                    "type": "object",
                    "properties": {
                        "city": { "type": "string", "description": "City name, e.g., Dallas" },
                        "state": { "type": "string", "description": "Two-letter state code, e.g., TX" },
                        "unit": { "type": "string", "enum": ["fahrenheit", "celsius"] }
                    },
                    "required": ["city", "state", "unit"],
                    "additionalProperties": false
                })),
                strict: None,
            },
        },
        dynamo_async_openai::types::ChatCompletionTool {
            r#type: dynamo_async_openai::types::ChatCompletionToolType::Function,
            function: dynamo_async_openai::types::FunctionObject {
                name: "is_holiday_today".to_string(),
                description: Some("Return whether today is a public holiday".to_string()),
                parameters: Some(json!({
                    "type": "object",
                    "properties": {},
                    "additionalProperties": false
                })),
                strict: None,
            },
        },
    ];

    let inner = dynamo_async_openai::types::CreateChatCompletionRequestArgs::default()
        .model("Qwen/Qwen3-0.6B")
        .temperature(0.0)
        .max_tokens(3000u32)
        .stream(false)
        .messages(messages)
        .tools(tools)
        .tool_choice(dynamo_async_openai::types::ChatCompletionToolChoiceOption::Required)
        .build()
        .expect("Failed to build chat completion request");

    NvCreateChatCompletionRequest {
        inner,
        common: CommonExt::default(),
        nvext: None,
        chat_template_args: None,
    }
}

/// Mock response content that contains parallel tool calls
fn get_mock_response_content() -> String {
    r#"<think>Okay, the user is asking two things: the weather in Dallas, Texas, and whether today is a holiday. I need to use both tools here. First, I'll check the weather using get_current_weather with city Dallas and state Texas. Then, I'll use is_holiday_today to see if today is a public holiday. I have to make sure to call both functions in parallel. Let me structure the tool calls properly.</think>

<tool_call>
{"name": "get_current_weather", "arguments": {"city": "Dallas", "state": "TX", "unit": "fahrenheit"}}
</tool_call>

<tool_call>
{"name": "is_holiday_today", "arguments": {}}
</tool_call>"#.to_string()
}

/// Validates that a tool call response matches expected values
fn validate_weather_tool_call(tool_call: &ToolCallResponse) {
    assert_eq!(tool_call.function.name, "get_current_weather");

    let args: serde_json::Value = serde_json::from_str(&tool_call.function.arguments)
        .expect("Arguments should be valid JSON");
    let args_obj = args.as_object().expect("Arguments should be an object");
    assert_eq!(args_obj.get("city").unwrap().as_str().unwrap(), "Dallas");
    assert_eq!(args_obj.get("state").unwrap().as_str().unwrap(), "TX");
    assert_eq!(
        args_obj.get("unit").unwrap().as_str().unwrap(),
        "fahrenheit"
    );

    // Validate OpenAI compatibility
    assert!(!tool_call.id.is_empty(), "Tool call should have an ID");
    assert_eq!(tool_call.tp, ToolCallType::Function);
}

/// Validates that a holiday tool call response matches expected values
fn validate_holiday_tool_call(tool_call: &ToolCallResponse) {
    assert_eq!(tool_call.function.name, "is_holiday_today");

    let args: serde_json::Value = serde_json::from_str(&tool_call.function.arguments)
        .expect("Arguments should be valid JSON");
    let args_obj = args.as_object().expect("Arguments should be an object");
    assert!(
        args_obj.is_empty(),
        "Holiday tool should have empty arguments"
    );

    // Validate OpenAI compatibility
    assert!(!tool_call.id.is_empty(), "Tool call should have an ID");
    assert_eq!(tool_call.tp, ToolCallType::Function);
}

/// Validates that tool call IDs are unique
fn validate_unique_tool_call_ids(tool_calls: &[ToolCallResponse]) {
    let mut ids = std::collections::HashSet::new();
    for tool_call in tool_calls {
        assert!(
            ids.insert(tool_call.id.clone()),
            "Tool call IDs should be unique: {}",
            tool_call.id
        );
    }
}

#[tokio::test]
async fn test_parallel_tool_call_integration() {
    // Create the mock request
    let request = create_mock_chat_completion_request();

    // Validate request structure
    assert_eq!(request.inner.model, "Qwen/Qwen3-0.6B");
    assert_eq!(request.inner.temperature, Some(0.0));
    #[allow(deprecated)]
    {
        assert_eq!(request.inner.max_tokens, Some(3000));
    }
    assert_eq!(request.inner.stream, Some(false));
    assert_eq!(request.inner.messages.len(), 2);
    assert_eq!(request.inner.tools.as_ref().unwrap().len(), 2);

    // Verify tool choice is required
    match request.inner.tool_choice.as_ref().unwrap() {
        dynamo_async_openai::types::ChatCompletionToolChoiceOption::Required => {
            // This is expected
        }
        _ => panic!("Tool choice should be Required"),
    }

    // Get the mock response content
    let response_content = get_mock_response_content();

    // Verify the response contains both tool calls
    assert!(response_content.contains("get_current_weather"));
    assert!(response_content.contains("is_holiday_today"));
    assert!(response_content.contains("Dallas"));
    assert!(response_content.contains("Texas"));
    assert!(response_content.contains("fahrenheit"));
}

#[tokio::test]
async fn test_parallel_tool_call_parsing() {
    let response_content = get_mock_response_content();

    // Parse the tool calls using the hermes parser (works well with <tool_call> format)
    let (tool_calls, remaining_content) =
        detect_and_parse_tool_call(&response_content, Some("hermes"))
            .await
            .expect("Should successfully parse tool calls");

    // Validate we got exactly 2 tool calls
    assert_eq!(
        tool_calls.len(),
        2,
        "Should parse exactly 2 parallel tool calls"
    );

    // Validate remaining content (should be the thinking part)
    assert!(remaining_content.is_some());
    let remaining = remaining_content.unwrap();
    assert!(remaining.contains("<think>"));
    assert!(remaining.contains("</think>"));

    // Sort tool calls by name for consistent testing
    let mut sorted_calls = tool_calls;
    sorted_calls.sort_by(|a, b| a.function.name.cmp(&b.function.name));

    // Validate the weather tool call (first alphabetically)
    validate_weather_tool_call(&sorted_calls[0]);

    // Validate the holiday tool call (second alphabetically)
    validate_holiday_tool_call(&sorted_calls[1]);

    // Validate tool call IDs are unique
    validate_unique_tool_call_ids(&sorted_calls);
}

#[tokio::test]
async fn test_parallel_tool_call_with_explicit_parser() {
    let response_content = get_mock_response_content();

    // Test with explicit parser selection
    let parsers_to_test = vec![
        "hermes", // Should work well with this format
    ];

    for parser in parsers_to_test {
        let (tool_calls, remaining_content) =
            detect_and_parse_tool_call(&response_content, Some(parser))
                .await
                .unwrap_or_else(|e| panic!("Should successfully parse with {parser} parser: {e}"));

        // Should get 2 tool calls regardless of parser
        assert_eq!(
            tool_calls.len(),
            2,
            "Parser {parser} should find 2 tool calls"
        );

        // Validate remaining content exists
        assert!(remaining_content.is_some());

        // Sort and validate calls
        let mut sorted_calls = tool_calls;
        sorted_calls.sort_by(|a, b| a.function.name.cmp(&b.function.name));

        validate_weather_tool_call(&sorted_calls[0]);
        validate_holiday_tool_call(&sorted_calls[1]);
        validate_unique_tool_call_ids(&sorted_calls);
    }
}

#[tokio::test]
async fn test_tool_call_json_structure() {
    let response_content = get_mock_response_content();

    let (tool_calls, _) = detect_and_parse_tool_call(&response_content, Some("hermes"))
        .await
        .expect("Should parse tool calls");

    // Test JSON serialization
    for tool_call in &tool_calls {
        let json_str =
            serde_json::to_string(tool_call).expect("Tool call should serialize to JSON");

        // Verify the JSON contains expected fields
        assert!(json_str.contains("\"id\""));
        assert!(json_str.contains("\"type\""));
        assert!(json_str.contains("\"function\""));
        assert!(json_str.contains(&tool_call.function.name));
    }
}

#[tokio::test]
async fn test_openai_compatibility_structure() {
    let response_content = get_mock_response_content();

    let (tool_calls, _) = detect_and_parse_tool_call(&response_content, Some("hermes"))
        .await
        .expect("Should parse tool calls");

    // Validate OpenAI API compatibility
    for tool_call in &tool_calls {
        // Should have all required OpenAI fields
        assert!(!tool_call.id.is_empty(), "Missing required 'id' field");
        assert_eq!(
            tool_call.tp,
            ToolCallType::Function,
            "Type should be 'function'"
        );
        assert!(
            !tool_call.function.name.is_empty(),
            "Function name should not be empty"
        );

        let args: serde_json::Value = serde_json::from_str(&tool_call.function.arguments)
            .expect("Arguments should be valid JSON");
        assert!(args.is_object(), "Arguments should be an object");

        // ID should follow expected format (call-XXXXXXXX or call_XXXXXXXX)
        assert!(
            tool_call.id.starts_with("call-") || tool_call.id.starts_with("call_"),
            "ID should start with 'call-' or 'call_': {}",
            tool_call.id
        );
        assert!(
            tool_call.id.len() > 5,
            "ID should be longer than just 'call': {}",
            tool_call.id
        );
    }
}

#[tokio::test]
async fn test_parallel_tool_call_error_handling() {
    // Test with malformed content
    let malformed_content = r#"<tool_call>
{"name": "get_current_weather", "arguments": {"city": "Dallas", "state": "TX", "unit": "fahrenheit"}}
</tool_call>

<tool_call>
{"invalid_json": }
</tool_call>"#;

    let result = detect_and_parse_tool_call(malformed_content, Some("hermes")).await;

    // Should handle partial parsing gracefully
    match result {
        Ok((tool_calls, _)) => {
            // May parse valid tool calls and ignore malformed ones, or return empty
            println!(
                "Parsed {} tool calls from malformed content",
                tool_calls.len()
            );

            if !tool_calls.is_empty() {
                // If any were parsed, verify they're valid
                for call in &tool_calls {
                    assert!(
                        !call.function.name.is_empty(),
                        "Parsed tool call should have valid name"
                    );
                }
            }
        }
        Err(e) => {
            // Error handling is also acceptable for malformed input
            println!("Expected error for malformed input: {}", e);
        }
    }
}

#[tokio::test]
async fn test_empty_tool_calls() {
    let content_without_tools = "This is just a regular response without any tool calls.";

    let (tool_calls, remaining_content) =
        detect_and_parse_tool_call(content_without_tools, Some("hermes"))
            .await
            .expect("Should handle content without tool calls");

    assert!(
        tool_calls.is_empty(),
        "Should return empty tool calls array"
    );
    assert!(
        remaining_content.is_some(),
        "Should return the original content"
    );
    assert_eq!(remaining_content.unwrap(), content_without_tools);
}