forked from waybarrios/vllm-mlx
-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathtest_tool_call_e2e.py
More file actions
594 lines (522 loc) · 18.7 KB
/
test_tool_call_e2e.py
File metadata and controls
594 lines (522 loc) · 18.7 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
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
# SPDX-License-Identifier: Apache-2.0
"""
End-to-end tool calling integration test.
Simulates a multi-round agent (like OpenClaw) that sends streaming requests
with 14 tools, executes tool results, and feeds them back. Verifies that
the server correctly parses tool calls (including text-format fallback)
and returns structured SSE responses.
Usage:
# Requires a running vllm-mlx server on localhost:8000
python3.12 -m pytest tests/test_tool_call_e2e.py -v -s
# Or run directly as a script for interactive debugging:
python3.12 tests/test_tool_call_e2e.py ["custom prompt"]
Skip condition: Tests are skipped if no server is running on localhost:8000.
"""
import json
import subprocess
import sys
import time
import pytest
try:
import httpx
_HTTPX = True
except ImportError:
_HTTPX = False
BASE_URL = "http://localhost:8000/v1/chat/completions"
SYSTEM_PROMPT = """You are Claw, a helpful AI assistant running on the user's local machine.
Current time: Thursday, February 26, 2026 8:40 PM PST
Location: Palo Alto, CA
## Rules
- Use exec or web_search for real-time info (weather, stocks, news).
- After getting tool results, give a FINAL TEXT ANSWER immediately.
- Do NOT call more tools unless absolutely necessary.
- Keep responses concise (under 200 words).
- If a command returns "still running", poll once then give best answer with available info.
"""
# 14 tools — same as OpenClaw's real tool set
TOOLS = [
{
"type": "function",
"function": {
"name": "web_search",
"description": "Search the web",
"parameters": {
"type": "object",
"properties": {"query": {"type": "string"}},
"required": ["query"],
},
},
},
{
"type": "function",
"function": {
"name": "exec",
"description": "Execute shell command",
"parameters": {
"type": "object",
"properties": {"command": {"type": "string"}},
"required": ["command"],
},
},
},
{
"type": "function",
"function": {
"name": "read",
"description": "Read a file",
"parameters": {
"type": "object",
"properties": {"path": {"type": "string"}},
"required": ["path"],
},
},
},
{
"type": "function",
"function": {
"name": "write",
"description": "Write to a file",
"parameters": {
"type": "object",
"properties": {
"path": {"type": "string"},
"content": {"type": "string"},
},
"required": ["path", "content"],
},
},
},
{
"type": "function",
"function": {
"name": "process",
"description": "Process management: list/poll/log/write/kill/clear/remove",
"parameters": {
"type": "object",
"properties": {
"action": {
"type": "string",
"enum": [
"list",
"poll",
"log",
"write",
"kill",
"clear",
"remove",
],
},
"sessionId": {"type": "string"},
"timeout": {"type": "integer"},
},
"required": ["action"],
},
},
},
{
"type": "function",
"function": {
"name": "memory_store",
"description": "Store key-value pair",
"parameters": {
"type": "object",
"properties": {"key": {"type": "string"}, "value": {"type": "string"}},
"required": ["key", "value"],
},
},
},
{
"type": "function",
"function": {
"name": "memory_get",
"description": "Get value by key",
"parameters": {
"type": "object",
"properties": {"key": {"type": "string"}},
"required": ["key"],
},
},
},
{
"type": "function",
"function": {
"name": "send_message",
"description": "Send a message",
"parameters": {
"type": "object",
"properties": {"to": {"type": "string"}, "text": {"type": "string"}},
"required": ["to", "text"],
},
},
},
{
"type": "function",
"function": {
"name": "create_reminder",
"description": "Create a reminder",
"parameters": {
"type": "object",
"properties": {"text": {"type": "string"}, "time": {"type": "string"}},
"required": ["text", "time"],
},
},
},
{
"type": "function",
"function": {
"name": "calendar_create",
"description": "Create calendar event",
"parameters": {
"type": "object",
"properties": {
"title": {"type": "string"},
"start": {"type": "string"},
},
"required": ["title", "start"],
},
},
},
{
"type": "function",
"function": {
"name": "browse",
"description": "Browse a URL",
"parameters": {
"type": "object",
"properties": {"url": {"type": "string"}},
"required": ["url"],
},
},
},
{
"type": "function",
"function": {
"name": "code_run",
"description": "Run code snippet",
"parameters": {
"type": "object",
"properties": {
"language": {"type": "string"},
"code": {"type": "string"},
},
"required": ["language", "code"],
},
},
},
{
"type": "function",
"function": {
"name": "image_gen",
"description": "Generate an image",
"parameters": {
"type": "object",
"properties": {"prompt": {"type": "string"}},
"required": ["prompt"],
},
},
},
{
"type": "function",
"function": {
"name": "translate",
"description": "Translate text",
"parameters": {
"type": "object",
"properties": {"text": {"type": "string"}, "to": {"type": "string"}},
"required": ["text", "to"],
},
},
},
]
MAX_ROUNDS = 8
def _server_available() -> bool:
"""Check if vllm-mlx server is running."""
if not _HTTPX:
return False
try:
r = httpx.get("http://localhost:8000/health", timeout=2.0)
return r.status_code == 200
except Exception:
return False
def stream_request(messages):
"""Stream a request and return (content, tool_calls, raw_chunks, elapsed)."""
content = ""
tool_calls = []
raw_chunks = []
start = time.time()
with httpx.stream(
"POST",
BASE_URL,
json={
"model": "default",
"stream": True,
"messages": messages,
"tools": TOOLS,
},
timeout=120.0,
) as resp:
for line in resp.iter_lines():
if not line.startswith("data: "):
continue
data = line[6:]
if data == "[DONE]":
raw_chunks.append("[DONE]")
break
try:
chunk = json.loads(data)
except json.JSONDecodeError:
continue
raw_chunks.append(chunk)
if "choices" not in chunk or not chunk["choices"]:
continue
choice = chunk["choices"][0]
delta = choice.get("delta", {})
if "content" in delta and delta["content"]:
content += delta["content"]
if "tool_calls" in delta and delta["tool_calls"]:
tool_calls.extend(delta["tool_calls"])
elapsed = time.time() - start
return content, tool_calls, raw_chunks, elapsed
def execute_tool(name, arguments):
"""Simulate tool execution with deterministic results."""
args = json.loads(arguments) if isinstance(arguments, str) else arguments
if name == "read":
path = args.get("path", "")
try:
with open(path) as f:
return f.read()[:2000]
except Exception as e:
return f"Error reading {path}: {e}"
elif name == "exec":
cmd = args.get("command", "")
try:
result = subprocess.run(
cmd, shell=True, capture_output=True, text=True, timeout=10
)
output = result.stdout + result.stderr
return output[:2000] if output else "(no output)"
except subprocess.TimeoutExpired:
return "Command still running. Use process tool to check status."
except Exception as e:
return f"Error: {e}"
elif name == "process":
action = args.get("action", "")
sid = args.get("sessionId", "unknown")
if action == "poll":
return "(no new output)\n\nProcess still running."
elif action == "list":
return "No active processes."
return f"Process {action} on {sid}: OK"
elif name == "web_search":
query = args.get("query", "")
return (
f"Search results for '{query}': "
"Palo Alto tonight: Clear skies, 54F (12C), wind 3mph NW, "
"humidity 58%. Sunset was at 6:05 PM."
)
else:
return f"Tool {name} executed with args: {json.dumps(args)}"
def run_agent_loop(user_msg, max_rounds=MAX_ROUNDS):
"""Run the full agent loop and return (rounds, final_content, tool_history).
Returns:
rounds: number of rounds completed
final_content: the final text response, or None
tool_history: list of (tool_name, tool_args) tuples
"""
messages = [
{"role": "system", "content": SYSTEM_PROMPT},
{"role": "user", "content": f"[Thu 2026-02-26 20:40 PST] {user_msg}"},
]
tool_history = []
for round_num in range(1, max_rounds + 1):
content, tool_calls, raw_chunks, elapsed = stream_request(messages)
if tool_calls:
tc = tool_calls[0]
fn = tc["function"]
tool_history.append((fn["name"], fn["arguments"]))
result = execute_tool(fn["name"], fn["arguments"])
messages.append(
{
"role": "assistant",
"content": None,
"tool_calls": [
{"id": tc["id"], "type": "function", "function": fn}
],
}
)
messages.append(
{
"role": "tool",
"content": result,
"tool_call_id": tc["id"],
}
)
continue
if content:
return round_num, content, tool_history
# No content and no tool calls — check for error
for c in raw_chunks:
if isinstance(c, dict) and "error" in c:
raise RuntimeError(f"Server error: {c['error']}")
return round_num, None, tool_history
return max_rounds, None, tool_history
# ---------------------------------------------------------------------------
# Pytest tests (skipped if server not running)
# ---------------------------------------------------------------------------
server_required = pytest.mark.skipif(
not _server_available(),
reason="vllm-mlx server not running on localhost:8000",
)
@server_required
class TestToolCallE2E:
"""End-to-end streaming tool call tests against a live server."""
def test_simple_exec(self):
"""Model should call exec and return a text answer."""
rounds, content, tools = run_agent_loop("帮我查看 ~/Desktop 下有什么文件")
assert content is not None, "Expected text response"
assert rounds <= 4, f"Should complete in <=4 rounds, got {rounds}"
assert any(t[0] == "exec" for t in tools), "Should have called exec"
def test_weather_with_fallback(self):
"""Model may call exec (curl), get 'still running', then web_search."""
rounds, content, tools = run_agent_loop("今晚出去跑步合适吗")
assert content is not None, "Expected text response"
assert rounds <= 6, f"Should complete in <=6 rounds, got {rounds}"
tool_names = [t[0] for t in tools]
assert any(n in tool_names for n in ("exec", "web_search")), (
f"Should use exec or web_search, got {tool_names}"
)
def test_no_tool_needed(self):
"""Pure reasoning should return text without tool calls."""
rounds, content, tools = run_agent_loop("解释一下什么是MoE模型")
assert content is not None, "Expected text response"
# Model may or may not use tools — but should produce content
assert len(content) > 20, "Response too short"
def test_multi_step_tool_chain(self):
"""Multi-step: exec + create_reminder."""
rounds, content, tools = run_agent_loop(
"帮我看下我电脑的 python 版本,然后创建一个提醒明天下午3点升级 python"
)
assert content is not None, "Expected text response"
assert rounds <= 6, f"Should complete in <=6 rounds, got {rounds}"
tool_names = [t[0] for t in tools]
assert "exec" in tool_names, "Should call exec for python version"
def test_sse_format_valid(self):
"""Every SSE chunk should be valid JSON with expected structure."""
messages = [
{"role": "system", "content": SYSTEM_PROMPT},
{"role": "user", "content": "[Thu 2026-02-26 20:40 PST] what time is it"},
]
content, tool_calls, raw_chunks, elapsed = stream_request(messages)
assert len(raw_chunks) > 0, "Should have chunks"
assert raw_chunks[-1] == "[DONE]", "Last chunk should be [DONE]"
for chunk in raw_chunks:
if isinstance(chunk, str):
assert chunk == "[DONE]"
continue
# Must have standard OpenAI fields
assert "id" in chunk
assert "object" in chunk
assert chunk["object"] == "chat.completion.chunk"
def test_tool_call_has_valid_id(self):
"""Tool call chunks should have a call ID starting with 'call_'."""
messages = [
{"role": "system", "content": SYSTEM_PROMPT},
{
"role": "user",
"content": "[Thu 2026-02-26 20:40 PST] list files in /tmp",
},
]
content, tool_calls, raw_chunks, elapsed = stream_request(messages)
if tool_calls:
tc = tool_calls[0]
assert "id" in tc, "Tool call should have id"
assert tc["id"].startswith("call_"), (
f"ID should start with call_, got {tc['id']}"
)
assert "function" in tc, "Tool call should have function"
assert "name" in tc["function"], "Function should have name"
assert "arguments" in tc["function"], "Function should have arguments"
# Arguments should be valid JSON
args = json.loads(tc["function"]["arguments"])
assert isinstance(args, dict), "Arguments should be a dict"
# ---------------------------------------------------------------------------
# CLI mode for interactive debugging
# ---------------------------------------------------------------------------
def main():
user_msg = (
sys.argv[1] if len(sys.argv) > 1 else "你帮我看下 我今晚出去跑步是不是合适"
)
print("=" * 70)
print(f"OpenClaw Simulation: '{user_msg}'")
print(f"Tools: {len(TOOLS)}, Max rounds: {MAX_ROUNDS}")
print("=" * 70)
messages = [
{"role": "system", "content": SYSTEM_PROMPT},
{"role": "user", "content": f"[Thu 2026-02-26 20:40 PST] {user_msg}"},
]
for round_num in range(1, MAX_ROUNDS + 1):
print(f"\n--- Round {round_num}: msgs={len(messages)} ---")
content, tool_calls, raw_chunks, elapsed = stream_request(messages)
# Analyze chunks
chunk_types = []
for c in raw_chunks:
if isinstance(c, str):
chunk_types.append("DONE")
continue
if "choices" not in c or not c["choices"]:
chunk_types.append("?")
continue
ch = c["choices"][0]
d = ch.get("delta", {})
fr = ch.get("finish_reason")
if "role" in d:
chunk_types.append("role")
elif "tool_calls" in d:
chunk_types.append("tc")
elif "content" in d and d["content"]:
chunk_types.append("txt")
elif fr:
chunk_types.append(f"fin:{fr}")
else:
chunk_types.append("?")
print(
f" {len(raw_chunks)} chunks [{', '.join(chunk_types[:15])}] {elapsed:.1f}s"
)
if tool_calls:
tc = tool_calls[0]
fn = tc["function"]
print(f" TOOL: {fn['name']}({fn['arguments'][:120]})")
result = execute_tool(fn["name"], fn["arguments"])
print(f" RESULT: {result[:150]}")
messages.append(
{
"role": "assistant",
"content": None,
"tool_calls": [
{"id": tc["id"], "type": "function", "function": fn}
],
}
)
messages.append(
{
"role": "tool",
"content": result,
"tool_call_id": tc["id"],
}
)
continue
if content:
print(f" TEXT ({len(content)} chars): {content[:300]}")
print(f"\n SUCCESS in {round_num} rounds")
return
print(" EMPTY — no content, no tool_calls")
for i, c in enumerate(raw_chunks[:5]):
if isinstance(c, str):
print(f" [{i}] {c}")
else:
print(f" [{i}] {json.dumps(c)[:200]}")
print("\n FAIL")
return
print(f"\n FAIL — exceeded {MAX_ROUNDS} rounds")
if __name__ == "__main__":
main()