utils.py 12.4 KB
Newer Older
1
# SPDX-License-Identifier: Apache-2.0
2
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
3

4
from copy import deepcopy
5
from typing import Any
6

7
from openai.types.chat import ChatCompletionMessageParam, ChatCompletionToolParam
8
9
10
11
12
from typing_extensions import TypedDict

from tests.utils import VLLM_PATH


13
class ServerConfig(TypedDict, total=False):
14
    model: str
15
    arguments: list[str]
16
17
18
19
    system_prompt: str | None
    supports_parallel: bool | None
    supports_rocm: bool | None
    extended: bool | None  # tests do not run in CI automatically
20
21


22
23
24
def patch_system_prompt(
    messages: list[dict[str, Any]], system_prompt: str
) -> list[dict[str, Any]]:
25
26
27
28
29
30
31
32
    new_messages = deepcopy(messages)
    if new_messages[0]["role"] == "system":
        new_messages[0]["content"] = system_prompt
    else:
        new_messages.insert(0, {"role": "system", "content": system_prompt})
    return new_messages


33
34
35
def ensure_system_prompt(
    messages: list[dict[str, Any]], config: ServerConfig
) -> list[dict[str, Any]]:
36
37
38
39
40
    prompt = config.get("system_prompt")
    if prompt:
        return patch_system_prompt(messages, prompt)
    else:
        return messages
41
42
43
44


# universal args for all models go here. also good if you need to test locally
# and change type or KV cache quantization or something.
45
ARGS: list[str] = [
46
47
48
49
50
    "--enable-auto-tool-choice",
    "--max-model-len",
    "1024",
    "--max-num-seqs",
    "256",
51
]
52

53
CONFIGS: dict[str, ServerConfig] = {
54
    "hermes": {
55
        "model": "NousResearch/Hermes-3-Llama-3.1-8B",
56
        "arguments": [
57
58
59
60
61
62
            "--enforce-eager",
            "--no-enable-prefix-caching",
            "--tool-call-parser",
            "hermes",
            "--chat-template",
            str(VLLM_PATH / "examples/tool_chat_template_hermes.jinja"),
63
        ],
64
        "system_prompt": "You are a helpful assistant with access to tools. If a tool"
65
66
67
        " that you have would be helpful to answer a user query, "
        "call the tool. Otherwise, answer the user's query directly "
        "without calling a tool. DO NOT CALL A TOOL THAT IS IRRELEVANT "
68
        "to the user's question - just respond to it normally.",
69
70
    },
    "llama": {
71
        "model": "meta-llama/Meta-Llama-3.1-8B-Instruct",
72
        "arguments": [
73
74
75
76
77
78
            "--enforce-eager",
            "--no-enable-prefix-caching",
            "--tool-call-parser",
            "llama3_json",
            "--chat-template",
            str(VLLM_PATH / "examples/tool_chat_template_llama3.1_json.jinja"),
79
        ],
80
        "supports_parallel": False,
81
82
    },
    "llama3.2": {
83
        "model": "meta-llama/Llama-3.2-3B-Instruct",
84
        "arguments": [
85
86
87
88
89
90
            "--enforce-eager",
            "--no-enable-prefix-caching",
            "--tool-call-parser",
            "llama3_json",
            "--chat-template",
            str(VLLM_PATH / "examples/tool_chat_template_llama3.2_json.jinja"),
91
        ],
92
        "supports_parallel": False,
93
    },
94
    "llama4": {
95
        "model": "meta-llama/Llama-4-Scout-17B-16E-Instruct",
96
        "arguments": [
97
98
99
100
101
102
103
104
            "--enforce-eager",
            "--no-enable-prefix-caching",
            "--tool-call-parser",
            "llama4_pythonic",
            "--chat-template",
            str(VLLM_PATH / "examples/tool_chat_template_llama4_pythonic.jinja"),
            "-tp",
            "4",
105
        ],
106
107
        "supports_parallel": False,
        "extended": True,
108
    },
109
    "llama4_json": {
110
        "model": "meta-llama/Llama-4-Scout-17B-16E-Instruct",
111
        "arguments": [
112
113
114
115
116
117
118
119
120
121
            "--enforce-eager",
            "--no-enable-prefix-caching",
            "-tp",
            "4",
            "--distributed-executor-backend",
            "mp",
            "--tool-call-parser",
            "llama4_json",
            "--chat-template",
            str(VLLM_PATH / "examples/tool_chat_template_llama4_json.jinja"),
122
        ],
123
124
        "supports_parallel": True,
        "extended": True,
125
    },
126
    "mistral-7b": {
127
        "model": "mistralai/Mistral-7B-Instruct-v0.3",
128
        "arguments": [
129
130
            "--enforce-eager",
            "--no-enable-prefix-caching",
131
132
133
134
135
136
            "--tokenizer_mode",
            "hf",
            "--load_format",
            "hf",
            "--config_format",
            "hf",
137
138
139
            "--tool-call-parser",
            "mistral",
            "--chat-template",
140
            str(VLLM_PATH / "examples/tool_chat_template_mistral.jinja"),
141
            '--ignore-patterns="consolidated.safetensors"',
142
        ],
143
        "system_prompt": "You are a helpful assistant with access to tools. If a tool"
144
145
146
        " that you have would be helpful to answer a user query, "
        "call the tool. Otherwise, answer the user's query directly "
        "without calling a tool. DO NOT CALL A TOOL THAT IS IRRELEVANT "
147
        "to the user's question - just respond to it normally.",
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
        "supports_parallel": True,
    },
    "mistral-small-3.2": {
        "model": "mistralai/Mistral-Small-3.2-24B-Instruct-2506",
        "arguments": [
            "--enforce-eager",
            "--no-enable-prefix-caching",
            "--tool-call-parser",
            "mistral",
            "--tokenizer-mode",
            "mistral",
            "--config-format",
            "mistral",
            "--load-format",
            "mistral",
            "--tensor-parallel-size",
            "4",
            '--ignore-patterns="consolidated.safetensors"',
        ],
        "system_prompt": "You are a helpful assistant with access to tools. If a tool"
        " that you have would be helpful to answer a user query, "
        "call the tool. Otherwise, answer the user's query directly "
        "without calling a tool. DO NOT CALL A TOOL THAT IS IRRELEVANT "
        "to the user's question - just respond to it normally.",
        "supports_parallel": True,
        "extended": True,
174
    },
175
    # FIXME: This test currently fails, need to debug why.
176
    # "granite20b": {
177
    #     "model": "mbayser/granite-20b-functioncalling-FP8-KV",
178
    #     "arguments": [
179
180
181
182
183
184
185
186
187
    #         "--tool-call-parser",
    #         "granite-20b-fc",
    #         "--chat-template",
    #         str(VLLM_PATH / "examples/tool_chat_template_granite_20b_fc.jinja"),
    #         "--max_num_seqs",
    #         "1",
    #         "--enforce-eager",
    #         "--cpu-offload-gb",
    #         "20",
188
    #     ],
189
190
    #     "supports_parallel": False,
    #     "supports_rocm": False,
191
    # },
192
    "granite-3.0-8b": {
193
        "model": "ibm-granite/granite-3.0-8b-instruct",
194
        "arguments": [
195
196
197
198
199
200
            "--enforce-eager",
            "--no-enable-prefix-caching",
            "--tool-call-parser",
            "granite",
            "--chat-template",
            str(VLLM_PATH / "examples/tool_chat_template_granite.jinja"),
201
202
        ],
    },
203
    "granite-3.1-8b": {
204
        "model": "ibm-granite/granite-3.1-8b-instruct",
205
        "arguments": [
Robert Shaw's avatar
Robert Shaw committed
206
207
            "--enforce-eager",
            "--no-enable-prefix-caching",
208
209
210
            "--tool-call-parser",
            "granite",
        ],
211
        "supports_parallel": True,
212
    },
213
    "internlm": {
214
        "model": "internlm/internlm2_5-7b-chat",
215
        "arguments": [
216
217
218
219
220
221
222
            "--enforce-eager",
            "--no-enable-prefix-caching",
            "--tool-call-parser",
            "internlm",
            "--chat-template",
            str(VLLM_PATH / "examples/tool_chat_template_internlm2_tool.jinja"),
            "--trust_remote_code",
223
        ],
224
        "supports_parallel": False,
225
226
    },
    "toolACE": {
227
        "model": "Team-ACE/ToolACE-8B",
228
        "arguments": [
229
230
231
232
233
234
            "--enforce-eager",
            "--no-enable-prefix-caching",
            "--tool-call-parser",
            "pythonic",
            "--chat-template",
            str(VLLM_PATH / "examples/tool_chat_template_toolace.jinja"),
235
        ],
236
        "supports_parallel": True,
237
    },
238
239
240
241
242
243
244
245
246
247
248
}

WEATHER_TOOL: ChatCompletionToolParam = {
    "type": "function",
    "function": {
        "name": "get_current_weather",
        "description": "Get the current weather in a given location",
        "parameters": {
            "type": "object",
            "properties": {
                "city": {
249
250
251
                    "type": "string",
                    "description": "The city to find the weather for, "
                    "e.g. 'San Francisco'",
252
253
                },
                "state": {
254
255
                    "type": "string",
                    "description": "must the two-letter abbreviation for the state "
256
                    "that the city is in, e.g. 'CA' which would "
257
                    "mean 'California'",
258
259
260
261
                },
                "unit": {
                    "type": "string",
                    "description": "The unit to fetch the temperature in",
262
263
264
265
266
                    "enum": ["celsius", "fahrenheit"],
                },
            },
        },
    },
267
268
269
270
271
}

SEARCH_TOOL: ChatCompletionToolParam = {
    "type": "function",
    "function": {
272
273
        "name": "web_search",
        "description": "Search the internet and get a summary of the top "
274
275
276
277
278
279
280
        "10 webpages. Should only be used if you don't know "
        "the answer to a user query, and the results are likely"
        "to be able to be found with a web search",
        "parameters": {
            "type": "object",
            "properties": {
                "search_term": {
281
282
                    "type": "string",
                    "description": "The term to use in the search. This should"
283
                    "ideally be keywords to search for, not a"
284
                    "natural-language question",
285
286
                }
            },
287
288
289
            "required": ["search_term"],
        },
    },
290
291
}

292
293
294
295
296
MESSAGES_WITHOUT_TOOLS: list[ChatCompletionMessageParam] = [
    {"role": "user", "content": "Hi! How are you?"},
    {"role": "assistant", "content": "I'm doing great! How can I assist you?"},
    {"role": "user", "content": "Can you tell me a joke please?"},
]
297

298
299
300
MESSAGES_ASKING_FOR_TOOLS: list[ChatCompletionMessageParam] = [
    {"role": "user", "content": "What is the weather in Dallas, Texas in Fahrenheit?"}
]
301

302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
MESSAGES_WITH_TOOL_RESPONSE: list[ChatCompletionMessageParam] = [
    {"role": "user", "content": "What is the weather in Dallas, Texas in Fahrenheit?"},
    {
        "role": "assistant",
        "tool_calls": [
            {
                "id": "chatcmpl-tool-03e6481b146e408e9523d9c956696295",
                "type": "function",
                "function": {
                    "name": WEATHER_TOOL["function"]["name"],
                    "arguments": '{"city": "Dallas", "state": "TX", '
                    '"unit": "fahrenheit"}',
                },
            }
        ],
    },
    {
        "role": "tool",
        "tool_call_id": "chatcmpl-tool-03e6481b146e408e9523d9c956696295",
        "content": "The weather in Dallas is 98 degrees fahrenheit, with partly"
        "cloudy skies and a low chance of rain.",
    },
]
325

326
327
328
329
330
331
332
MESSAGES_ASKING_FOR_PARALLEL_TOOLS: list[ChatCompletionMessageParam] = [
    {
        "role": "user",
        "content": "What is the weather in Dallas, Texas and Orlando, Florida in "
        "Fahrenheit?",
    }
]
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
MESSAGES_WITH_PARALLEL_TOOL_RESPONSE: list[ChatCompletionMessageParam] = [
    {
        "role": "user",
        "content": "What is the weather in Dallas, Texas and Orlando, Florida in "
        "Fahrenheit?",
    },
    {
        "role": "assistant",
        "tool_calls": [
            {
                "id": "chatcmpl-tool-03e6481b146e408e9523d9c956696295",
                "type": "function",
                "function": {
                    "name": WEATHER_TOOL["function"]["name"],
                    "arguments": '{"city": "Dallas", "state": "TX", '
                    '"unit": "fahrenheit"}',
                },
            },
            {
                "id": "chatcmpl-tool-d027061e1bd21cda48bee7da829c1f5b",
                "type": "function",
                "function": {
                    "name": WEATHER_TOOL["function"]["name"],
                    "arguments": '{"city": "Orlando", "state": "Fl", '
                    '"unit": "fahrenheit"}',
                },
            },
        ],
    },
    {
        "role": "tool",
        "tool_call_id": "chatcmpl-tool-03e6481b146e408e9523d9c956696295",
        "content": "The weather in Dallas TX is 98 degrees fahrenheit with mostly "
        "cloudy skies and a chance of rain in the evening.",
    },
    {
        "role": "tool",
        "tool_call_id": "chatcmpl-tool-d027061e1bd21cda48bee7da829c1f5b",
        "content": "The weather in Orlando FL is 78 degrees fahrenheit with clear"
        "skies.",
    },
]