File size: 8,527 Bytes
634c038
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""
VLLM Tool Calling Smoke Test
=============================

Quick verification that your VLLM instance supports tool calling.
Tests single tool calls, parallel tool calls, and multi-turn conversations.

Usage:
    python test_tool_calling.py --url http://localhost:8000 --model NousResearch/Hermes-3-Llama-3.1-70B-FP8
"""

import argparse
import json
import sys
import requests


def test_single_tool_call(url: str, model: str) -> bool:
    """Test basic single tool call."""
    print("Test 1: Single Tool Call")
    print("-" * 40)

    payload = {
        "model": model,
        "messages": [
            {"role": "user", "content": "What's the weather in San Francisco? Use the get_weather tool."}
        ],
        "tools": [{
            "type": "function",
            "function": {
                "name": "get_weather",
                "description": "Get the current weather for a location",
                "parameters": {
                    "type": "object",
                    "properties": {
                        "location": {"type": "string", "description": "City name"},
                        "unit": {"type": "string", "enum": ["celsius", "fahrenheit"]}
                    },
                    "required": ["location"]
                }
            }
        }],
        "tool_choice": "auto",
        "temperature": 0.1,
        "max_tokens": 500
    }

    try:
        response = requests.post(f"{url}/v1/chat/completions", json=payload, timeout=60)
        response.raise_for_status()
        result = response.json()
        message = result["choices"][0]["message"]

        if message.get("tool_calls"):
            tc = message["tool_calls"][0]
            print(f"  Tool:      {tc['function']['name']}")
            print(f"  Arguments: {tc['function']['arguments']}")
            print("  PASSED\n")
            return True
        else:
            print(f"  Model returned text: {message.get('content', '(empty)')[:100]}")
            print("  FAILED\n")
            return False

    except Exception as e:
        print(f"  ERROR: {e}")
        print("  FAILED\n")
        return False


def test_parallel_tool_calls(url: str, model: str) -> bool:
    """Test if model can make multiple tool calls in one turn."""
    print("Test 2: Parallel Tool Calls")
    print("-" * 40)

    payload = {
        "model": model,
        "messages": [
            {"role": "user", "content": "Get the weather in Tokyo AND London."}
        ],
        "tools": [{
            "type": "function",
            "function": {
                "name": "get_weather",
                "description": "Get the current weather for a location",
                "parameters": {
                    "type": "object",
                    "properties": {
                        "location": {"type": "string"}
                    },
                    "required": ["location"]
                }
            }
        }],
        "tool_choice": "auto",
        "temperature": 0.1,
        "max_tokens": 500
    }

    try:
        response = requests.post(f"{url}/v1/chat/completions", json=payload, timeout=60)
        response.raise_for_status()
        result = response.json()
        message = result["choices"][0]["message"]

        if message.get("tool_calls") and len(message["tool_calls"]) >= 2:
            for tc in message["tool_calls"]:
                print(f"  Tool: {tc['function']['name']}({tc['function']['arguments']})")
            print("  PASSED\n")
            return True
        elif message.get("tool_calls"):
            print(f"  Only {len(message['tool_calls'])} tool call(s) — expected 2+")
            print("  PARTIAL (some models serialize parallel calls differently)\n")
            return True  # Still counts as working
        else:
            print(f"  Model returned text: {message.get('content', '(empty)')[:100]}")
            print("  FAILED\n")
            return False

    except Exception as e:
        print(f"  ERROR: {e}")
        print("  FAILED\n")
        return False


def test_multi_turn(url: str, model: str) -> bool:
    """Test multi-turn: tool call -> tool result -> final answer."""
    print("Test 3: Multi-Turn Conversation")
    print("-" * 40)

    tools = [{
        "type": "function",
        "function": {
            "name": "get_weather",
            "description": "Get weather for a location",
            "parameters": {
                "type": "object",
                "properties": {"location": {"type": "string"}},
                "required": ["location"]
            }
        }
    }]

    messages = [
        {"role": "user", "content": "What's the weather in Paris?"}
    ]

    try:
        # Turn 1: Get tool call
        response = requests.post(
            f"{url}/v1/chat/completions",
            json={"model": model, "messages": messages, "tools": tools, "tool_choice": "auto",
                  "temperature": 0.1, "max_tokens": 500},
            timeout=60
        )
        response.raise_for_status()
        msg1 = response.json()["choices"][0]["message"]

        if not msg1.get("tool_calls"):
            print("  Turn 1: No tool calls")
            print("  FAILED\n")
            return False

        print(f"  Turn 1: {msg1['tool_calls'][0]['function']['name']}() called")
        messages.append(msg1)

        # Add tool result
        messages.append({
            "role": "tool",
            "tool_call_id": msg1["tool_calls"][0]["id"],
            "content": json.dumps({"temperature": 18, "condition": "Sunny", "unit": "celsius"})
        })

        # Turn 2: Get final answer
        response = requests.post(
            f"{url}/v1/chat/completions",
            json={"model": model, "messages": messages, "tools": tools,
                  "temperature": 0.1, "max_tokens": 500},
            timeout=60
        )
        response.raise_for_status()
        msg2 = response.json()["choices"][0]["message"]

        if msg2.get("content"):
            print(f"  Turn 2: {msg2['content'][:100]}")
            print("  PASSED\n")
            return True
        else:
            print("  Turn 2: Empty response")
            print("  FAILED\n")
            return False

    except Exception as e:
        print(f"  ERROR: {e}")
        print("  FAILED\n")
        return False


def test_connection(url: str) -> bool:
    """Test basic connectivity to VLLM."""
    print("Test 0: Connection")
    print("-" * 40)
    try:
        response = requests.get(f"{url}/v1/models", timeout=10)
        response.raise_for_status()
        models = response.json()
        model_ids = [m["id"] for m in models.get("data", [])]
        print(f"  Available models: {', '.join(model_ids)}")
        print("  PASSED\n")
        return True
    except Exception as e:
        print(f"  Cannot connect to {url}: {e}")
        print("  FAILED\n")
        return False


if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="VLLM tool calling smoke test")
    parser.add_argument("--url", default="http://localhost:8000", help="VLLM server URL")
    parser.add_argument("--model", default="NousResearch/Hermes-3-Llama-3.1-70B-FP8")
    args = parser.parse_args()

    print("=" * 60)
    print(f"VLLM Tool Calling Smoke Test")
    print(f"Server: {args.url}")
    print(f"Model:  {args.model}")
    print("=" * 60 + "\n")

    results = {}

    # Test connectivity first
    if not test_connection(args.url):
        print("Cannot connect to VLLM server. Is it running?")
        sys.exit(1)

    # Run tests
    results["Single tool call"] = test_single_tool_call(args.url, args.model)
    results["Parallel tool calls"] = test_parallel_tool_calls(args.url, args.model)
    results["Multi-turn"] = test_multi_turn(args.url, args.model)

    # Summary
    print("=" * 60)
    print("SUMMARY")
    print("=" * 60)
    for name, passed in results.items():
        status = "PASSED" if passed else "FAILED"
        print(f"  {name}: {status}")

    all_passed = all(results.values())
    print(f"\n{'All tests passed!' if all_passed else 'Some tests failed.'}")

    if not results.get("Single tool call"):
        print("\nTroubleshooting:")
        print("  1. Is --enable-auto-tool-choice set in VLLM launch?")
        print("  2. Is --tool-call-parser set correctly? (hermes/llama3_json/mistral)")
        print("  3. Is --max-model-len large enough? (128K recommended)")
        print("  4. Check VLLM server logs for errors")

    sys.exit(0 if all_passed else 1)