forked from sgl-project/sglang
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_tool_choice.py
More file actions
563 lines (480 loc) · 22.4 KB
/
test_tool_choice.py
File metadata and controls
563 lines (480 loc) · 22.4 KB
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
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
#!/usr/bin/env python3
"""
Test script for tool_choice functionality in SGLang
Tests: required, auto, and specific function choices in both streaming and non-streaming modes
"""
import openai
def setup_client():
"""Setup OpenAI client pointing to SGLang server"""
client = openai.Client(base_url="http://localhost:8080/v1", api_key="xxxxxx")
model_name = client.models.list().data[0].id
return client, model_name
def get_test_tools():
"""Get the test tools for function calling"""
return [
{
"type": "function",
"function": {
"name": "get_weather",
"description": "use this to get latest weather information for a city given its name",
"parameters": {
"type": "object",
"properties": {
"city": {
"type": "string",
"description": "name of the city to get weather for",
}
},
"required": ["city"],
},
},
},
{
"type": "function",
"function": {
"name": "get_pokemon_info",
"description": "get detailed information about a pokemon given its name",
"parameters": {
"type": "object",
"properties": {
"name": {
"type": "string",
"description": "name of the pokemon to get info for",
}
},
"required": ["name"],
},
},
},
{
"type": "function",
"function": {
"name": "make_next_step_decision",
"description": "You will be given a trace of thinking process in the following format.\n\nQuestion: the input question you must answer\nTOOL: think about what to do, and choose a tool to use ONLY IF there are defined tools. \n You should never call the same tool with the same input twice in a row.\n If the previous conversation history already contains the information that can be retrieved from the tool, you should not call the tool again.\nOBSERVATION: the result of the tool call, NEVER include this in your response, this information will be provided\n... (this TOOL/OBSERVATION can repeat N times)\nANSWER: If you know the answer to the original question, require for more information,\n or you don't know the answer and there are no defined tools or all available tools are not helpful, respond with the answer without mentioning anything else.\n If the previous conversation history already contains the answer, respond with the answer right away.\n\n If no tools are configured, naturally mention this limitation while still being helpful. Briefly note that adding tools in the agent configuration would expand capabilities.\n\nYour task is to respond with the next step to take, based on the traces, \nor answer the question if you have enough information.",
"parameters": {
"type": "object",
"properties": {
"decision": {
"type": "string",
"description": 'The next step to take, it must be either "TOOL" or "ANSWER". If the previous conversation history already contains the information that can be retrieved from the tool, you should not call the tool again. If there are no defined tools, you should not return "TOOL" in your response.',
},
"content": {
"type": "string",
"description": 'The content of the next step. If the decision is "TOOL", this should be a short and concise reasoning of why you chose the tool, MUST include the tool name. If the decision is "ANSWER", this should be the answer to the question. If no tools are available, integrate this limitation conversationally without sounding scripted.',
},
},
"required": ["decision", "content"],
},
},
},
]
def get_test_messages():
"""Get test messages that should trigger tool usage"""
return [
{
"role": "user",
"content": "Answer the following questions as best you can:\n\nYou will be given a trace of thinking process in the following format.\n\nQuestion: the input question you must answer\nTOOL: think about what to do, and choose a tool to use ONLY IF there are defined tools\nOBSERVATION: the result of the tool call or the observation of the current task, NEVER include this in your response, this information will be provided\n... (this TOOL/OBSERVATION can repeat N times)\nANSWER: If you know the answer to the original question, require for more information, \nif the previous conversation history already contains the answer, \nor you don't know the answer and there are no defined tools or all available tools are not helpful, respond with the answer without mentioning anything else.\nYou may use light Markdown formatting to improve clarity (e.g. lists, **bold**, *italics*), but keep it minimal and unobtrusive.\n\nYour task is to respond with the next step to take, based on the traces, \nor answer the question if you have enough information.\n\nQuestion: what is the weather in top 5 populated cities in the US?\n\nTraces:\n\n\nThese are some additional instructions that you should follow:",
}
]
def get_travel_tools():
"""Get tools for travel assistant scenario that should trigger multiple tool calls"""
return [
{
"type": "function",
"function": {
"name": "get_weather",
"description": "Get the current weather for a given location.",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The name of the city or location.",
},
"unit": {"type": "string", "enum": ["celsius", "fahrenheit"]},
},
"required": ["location", "unit"],
},
},
},
{
"type": "function",
"function": {
"name": "get_tourist_attractions",
"description": "Get a list of top tourist attractions for a given city.",
"parameters": {
"type": "object",
"properties": {
"city": {
"type": "string",
"description": "The name of the city to find attractions for.",
}
},
"required": ["city"],
},
},
},
]
def get_travel_messages():
"""Get travel assistant messages that should trigger multiple tool calls"""
return [
{
"content": "You are a travel assistant providing real-time weather updates and top tourist attractions.",
"role": "system",
},
{
"content": "I'm planning a trip to Tokyo next week. What's the weather like? What are the most amazing sights?",
"role": "user",
},
]
def test_non_streaming(client, model_name, tools, messages, tool_choice, test_name):
"""Test non-streaming mode with given tool_choice"""
print(f"\n{'=' * 60}")
print(f"Testing NON-STREAMING: {test_name}")
print(f"tool_choice: {tool_choice}")
print(f"{'=' * 60}")
try:
response = client.chat.completions.create(
model=model_name,
messages=messages,
max_tokens=200,
tools=tools,
tool_choice=tool_choice,
stream=False,
)
print("✅ Request successful")
print(f"Content: {response.choices[0].message.content}")
print(f"Tool calls: {response.choices[0].message.tool_calls}")
# Validate based on tool_choice
tool_calls = response.choices[0].message.tool_calls
if tool_choice == "required":
assert (
tool_calls is not None and len(tool_calls) > 0
), "Expected tool calls when tool_choice='required'"
print("✅ PASS: Tool calls present when required")
elif isinstance(tool_choice, dict) and tool_choice.get("type") == "function":
expected_name = tool_choice["function"]["name"]
assert (
tool_calls is not None and len(tool_calls) > 0
), f"Expected tool calls when tool_choice specifies {expected_name}"
actual_name = tool_calls[0].function.name
assert (
actual_name == expected_name
), f"Expected function {expected_name}, got {actual_name}"
print(f"✅ PASS: Correct function called: {actual_name}")
elif tool_choice == "auto":
print(f"ℹ️ INFO: Auto mode - tool calls: {tool_calls is not None}")
return True
except Exception as e:
print(f"❌ FAIL: {str(e)}")
return False
def test_streaming(client, model_name, tools, messages, tool_choice, test_name):
"""Test streaming mode with given tool_choice"""
print(f"\n{'=' * 60}")
print(f"Testing STREAMING: {test_name}")
print(f"tool_choice: {tool_choice}")
print(f"{'=' * 60}")
try:
response = client.chat.completions.create(
model=model_name,
messages=messages,
max_tokens=200,
tools=tools,
tool_choice=tool_choice,
stream=True,
)
print("✅ Request successful")
# Collect streaming response
content_chunks = []
tool_call_chunks = []
for chunk in response:
if chunk.choices[0].delta.content:
content_chunks.append(chunk.choices[0].delta.content)
elif chunk.choices[0].delta.tool_calls:
tool_call_chunks.extend(chunk.choices[0].delta.tool_calls)
full_content = "".join(content_chunks)
print(f"Content: {full_content}")
print(f"Tool call chunks: {len(tool_call_chunks)}")
# Validate based on tool_choice
if tool_choice == "required":
assert (
len(tool_call_chunks) > 0
), "Expected tool call chunks when tool_choice='required'"
print("✅ PASS: Tool call chunks present when required")
elif isinstance(tool_choice, dict) and tool_choice.get("type") == "function":
expected_name = tool_choice["function"]["name"]
assert (
len(tool_call_chunks) > 0
), f"Expected tool call chunks when tool_choice specifies {expected_name}"
# Find function name in chunks
found_name = None
for chunk in tool_call_chunks:
if chunk.function and chunk.function.name:
found_name = chunk.function.name
break
assert (
found_name == expected_name
), f"Expected function {expected_name}, got {found_name}"
print(f"✅ PASS: Correct function called: {found_name}")
elif tool_choice == "auto":
print(f"ℹ️ INFO: Auto mode - tool call chunks: {len(tool_call_chunks)}")
return True
except Exception as e:
print(f"❌ FAIL: {str(e)}")
return False
def test_multi_tool_non_streaming(
client, model_name, tools, messages, tool_choice, test_name
):
"""Test non-streaming mode expecting multiple tool calls"""
print(f"\n{'=' * 60}")
print(f"Testing MULTI-TOOL NON-STREAMING: {test_name}")
print(f"tool_choice: {tool_choice}")
print(f"{'=' * 60}")
try:
response = client.chat.completions.create(
model=model_name,
messages=messages,
max_tokens=400,
temperature=0.8,
top_p=0.8,
tools=tools,
tool_choice=tool_choice,
stream=False,
)
print("✅ Request successful")
print(f"Content: {response.choices[0].message.content}")
print(f"Tool calls: {response.choices[0].message.tool_calls}")
# Validate based on tool_choice and expected functions
tool_calls = response.choices[0].message.tool_calls
expected_functions = {"get_weather", "get_tourist_attractions"}
is_flaky = False
if tool_choice in ["required", "auto"]:
if tool_choice == "auto" and (tool_calls is None or len(tool_calls) == 0):
print(
"ℹ️ INFO: Auto mode got 0 tool calls - this is flaky but acceptable"
)
is_flaky = True
return {"success": True, "flaky": is_flaky}
assert (
tool_calls is not None and len(tool_calls) > 0
), f"Expected tool calls when tool_choice='{tool_choice}'"
called_functions = set()
for call in tool_calls:
called_functions.add(call.function.name)
print(
f" - Called: {call.function.name} with args: {call.function.arguments}"
)
# For this specific travel scenario, we expect both functions to be called
if tool_choice == "required":
# With required, we should get at least one tool call, ideally both
print(
f"✅ PASS: Tool calls present when required ({len(tool_calls)} calls)"
)
if expected_functions.issubset(called_functions):
print("✅ BONUS: Both expected functions called!")
else:
print(
f"ℹ️ INFO: Called {called_functions}, expected both {expected_functions}"
)
elif tool_choice == "auto":
print(f"ℹ️ INFO: Auto mode - got {len(tool_calls)} tool calls")
if expected_functions.issubset(called_functions):
print("✅ BONUS: Both expected functions called!")
return {"success": True, "flaky": is_flaky}
except Exception as e:
print(f"❌ FAIL: {str(e)}")
return {"success": False, "flaky": False}
def test_multi_tool_streaming(
client, model_name, tools, messages, tool_choice, test_name
):
"""Test streaming mode expecting multiple tool calls"""
print(f"\n{'=' * 60}")
print(f"Testing MULTI-TOOL STREAMING: {test_name}")
print(f"tool_choice: {tool_choice}")
print(f"{'=' * 60}")
try:
response = client.chat.completions.create(
model=model_name,
messages=messages,
max_tokens=400,
temperature=0.8,
top_p=0.8,
tools=tools,
tool_choice=tool_choice,
stream=True,
)
print("✅ Request successful")
# Collect streaming response
content_chunks = []
tool_call_chunks = []
for chunk in response:
if chunk.choices[0].delta.content:
content_chunks.append(chunk.choices[0].delta.content)
elif chunk.choices[0].delta.tool_calls:
tool_call_chunks.extend(chunk.choices[0].delta.tool_calls)
full_content = "".join(content_chunks)
print(f"Content: {full_content}")
print(f"Tool call chunks: {len(tool_call_chunks)}")
# Collect function names from chunks
called_functions = set()
for chunk in tool_call_chunks:
if chunk.function and chunk.function.name:
called_functions.add(chunk.function.name)
print(f" - Found function: {chunk.function.name}")
expected_functions = {"get_weather", "get_tourist_attractions"}
is_flaky = False
# Validate based on tool_choice
if tool_choice in ["required", "auto"]:
if tool_choice == "auto" and len(tool_call_chunks) == 0:
print(
"ℹ️ INFO: Auto mode got 0 tool call chunks - this is flaky but acceptable"
)
is_flaky = True
return {"success": True, "flaky": is_flaky}
assert (
len(tool_call_chunks) > 0
), f"Expected tool call chunks when tool_choice='{tool_choice}'"
if tool_choice == "required":
print(
f"✅ PASS: Tool call chunks present when required ({len(tool_call_chunks)} chunks)"
)
if expected_functions.issubset(called_functions):
print("✅ BONUS: Both expected functions found in chunks!")
else:
print(
f"ℹ️ INFO: Found {called_functions}, expected both {expected_functions}"
)
elif tool_choice == "auto":
print(
f"ℹ️ INFO: Auto mode - got {len(tool_call_chunks)} tool call chunks"
)
if expected_functions.issubset(called_functions):
print("✅ BONUS: Both expected functions found in chunks!")
return {"success": True, "flaky": is_flaky}
except Exception as e:
print(f"❌ FAIL: {str(e)}")
return {"success": False, "flaky": False}
def main():
"""Main test function"""
print("🧪 Starting tool_choice functionality tests...")
# Setup
client, model_name = setup_client()
# =================================================================
# Test Suite 1: Single Tool Choice Tests
# =================================================================
print(f"\n{'=' * 80}")
print("📋 TEST SUITE 1: Single Tool Choice Tests")
print(f"{'=' * 80}")
tools = get_test_tools()
messages = get_test_messages()
# Test cases for single tool scenarios
single_tool_test_cases = [
("auto", "Tool choice AUTO"),
("required", "Tool choice REQUIRED"),
(
{"type": "function", "function": {"name": "get_weather"}},
"Tool choice SPECIFIC: get_weather",
),
(
{"type": "function", "function": {"name": "get_pokemon_info"}},
"Tool choice SPECIFIC: get_pokemon_info",
),
(
{"type": "function", "function": {"name": "make_next_step_decision"}},
"Tool choice SPECIFIC: make_next_step_decision",
),
]
single_tool_results = []
# Run single tool tests
for tool_choice, test_name in single_tool_test_cases:
# Test non-streaming
non_stream_result = test_non_streaming(
client, model_name, tools, messages, tool_choice, test_name
)
# Test streaming
stream_result = test_streaming(
client, model_name, tools, messages, tool_choice, test_name
)
single_tool_results.append(
{
"test_name": test_name,
"tool_choice": tool_choice,
"non_streaming": non_stream_result,
"streaming": stream_result,
}
)
# =================================================================
# Test Suite 2: Multi-Tool Travel Assistant Tests
# =================================================================
print(f"\n{'=' * 80}")
print("📋 TEST SUITE 2: Multi-Tool Travel Assistant Tests")
print(f"{'=' * 80}")
travel_tools = get_travel_tools()
travel_messages = get_travel_messages()
# Test cases for multi-tool scenarios (expecting both get_weather and get_tourist_attractions)
multi_tool_test_cases = [
("auto", "Multi-Tool AUTO (Travel Assistant)"),
("required", "Multi-Tool REQUIRED (Travel Assistant)"),
]
multi_tool_results = []
# Run multi-tool tests
for tool_choice, test_name in multi_tool_test_cases:
# Test non-streaming
non_stream_result = test_multi_tool_non_streaming(
client, model_name, travel_tools, travel_messages, tool_choice, test_name
)
# Test streaming
stream_result = test_multi_tool_streaming(
client, model_name, travel_tools, travel_messages, tool_choice, test_name
)
multi_tool_results.append(
{
"test_name": test_name,
"tool_choice": tool_choice,
"non_streaming": non_stream_result["success"],
"streaming": stream_result["success"],
"non_streaming_flaky": non_stream_result.get("flaky", False),
"streaming_flaky": stream_result.get("flaky", False),
}
)
# =================================================================
# Summary
# =================================================================
all_results = single_tool_results + multi_tool_results
print(f"\n{'=' * 80}")
print("📊 COMPREHENSIVE TEST SUMMARY")
print(f"{'=' * 80}")
print(f"\n🗳️ Model: {model_name}")
total_tests = len(all_results) * 2 # non-streaming + streaming
passed_tests = sum(
[1 for r in all_results if r["non_streaming"]]
+ [1 for r in all_results if r["streaming"]]
)
print("\n🔸 Single Tool Choice Tests:")
for result in single_tool_results:
ns_status = "✅ PASS" if result["non_streaming"] else "❌ FAIL"
s_status = "✅ PASS" if result["streaming"] else "❌ FAIL"
print(f" {result['test_name']}")
print(f" Non-streaming: {ns_status}")
print(f" Streaming: {s_status}")
print("\n🔸 Multi-Tool Travel Assistant Tests:")
for result in multi_tool_results:
ns_status = "✅ PASS" if result["non_streaming"] else "❌ FAIL"
s_status = "✅ PASS" if result["streaming"] else "❌ FAIL"
# Add flaky indicators
if result.get("non_streaming_flaky", False):
ns_status += " (FLAKY)"
if result.get("streaming_flaky", False):
s_status += " (FLAKY)"
print(f" {result['test_name']}")
print(f" Non-streaming: {ns_status}")
print(f" Streaming: {s_status}")
print(f"\nOverall: {passed_tests}/{total_tests} tests passed")
if passed_tests == total_tests:
print("🎉 All tests passed!")
return 0
else:
print("❌ Some tests failed!")
return 1
if __name__ == "__main__":
exit(main())