utils.py 11.1 KB
Newer Older
1
2
# SPDX-License-Identifier: Apache-2.0

3
from copy import deepcopy
zhuwenwen's avatar
zhuwenwen committed
4

zhuwenwen's avatar
zhuwenwen committed
5
import os
6
from typing import Any, Optional
7
8
9
10
11
12

from openai.types.chat import (ChatCompletionMessageParam,
                               ChatCompletionToolParam)
from typing_extensions import TypedDict

from tests.utils import VLLM_PATH
zhuwenwen's avatar
zhuwenwen committed
13
from ..utils import models_path_prefix
14
15


16
class ServerConfig(TypedDict, total=False):
17
    model: str
18
    arguments: list[str]
19
20
    system_prompt: Optional[str]
    supports_parallel: Optional[bool]
21
    supports_rocm: Optional[bool]
22
    extended: Optional[bool]  # tests do not run in CI automatically
23
24


25
26
def patch_system_prompt(messages: list[dict[str, Any]],
                        system_prompt: str) -> list[dict[str, Any]]:
27
28
29
30
31
32
33
34
    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


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


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

51
CONFIGS: dict[str, ServerConfig] = {
52
53
    "hermes": {
        "model":
zhuwenwen's avatar
zhuwenwen committed
54
        os.path.join(models_path_prefix, "NousResearch/Hermes-3-Llama-3.1-8B"),
55
        "arguments": [
Robert Shaw's avatar
Robert Shaw committed
56
            "--enforce-eager", "--no-enable-prefix-caching",
57
58
            "--tool-call-parser", "hermes", "--chat-template",
            str(VLLM_PATH / "examples/tool_chat_template_hermes.jinja")
59
60
61
62
63
64
65
66
67
68
69
70
        ],
        "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."
    },
    "llama": {
        "model":
        "meta-llama/Meta-Llama-3.1-8B-Instruct",
        "arguments": [
Robert Shaw's avatar
Robert Shaw committed
71
            "--enforce-eager", "--no-enable-prefix-caching",
72
73
74
75
76
77
78
79
80
81
            "--tool-call-parser", "llama3_json", "--chat-template",
            str(VLLM_PATH / "examples/tool_chat_template_llama3.1_json.jinja")
        ],
        "supports_parallel":
        False,
    },
    "llama3.2": {
        "model":
        "meta-llama/Llama-3.2-3B-Instruct",
        "arguments": [
Robert Shaw's avatar
Robert Shaw committed
82
            "--enforce-eager", "--no-enable-prefix-caching",
83
84
85
86
87
            "--tool-call-parser", "llama3_json", "--chat-template",
            str(VLLM_PATH / "examples/tool_chat_template_llama3.2_json.jinja")
        ],
        "supports_parallel":
        False,
88
    },
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
    "llama4": {
        "model":
        "meta-llama/Llama-4-Scout-17B-16E-Instruct",
        "arguments": [
            "--enforce-eager", "--no-enable-prefix-caching",
            "--tool-call-parser", "pythonic", "--chat-template",
            str(VLLM_PATH /
                "examples/tool_chat_template_llama4_pythonic.jinja"), "-tp",
            "4"
        ],
        "supports_parallel":
        False,
        "extended":
        True
    },
104
105
106
107
108
109
110
111
112
113
114
115
116
117
    "llama4_json": {
        "model":
        "meta-llama/Llama-4-Scout-17B-16E-Instruct",
        "arguments": [
            "--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")
        ],
        "supports_parallel":
        True,
        "extended":
        True
    },
118
119
    "mistral": {
        "model":
zhuwenwen's avatar
zhuwenwen committed
120
        os.path.join(models_path_prefix, "mistralai/Mistral-7B-Instruct-v0.3"),
121
        "arguments": [
Robert Shaw's avatar
Robert Shaw committed
122
            "--enforce-eager", "--no-enable-prefix-caching",
123
124
125
            "--tool-call-parser", "mistral", "--chat-template",
            str(VLLM_PATH / "examples/tool_chat_template_mistral.jinja"),
            "--ignore-patterns=\"consolidated.safetensors\""
126
127
128
129
130
131
132
        ],
        "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."
133
    },
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
    # V1 Test: Passing locally but failing in CI. This runs the
    # V0 Engine because of CPU offloading. Need to debug why.
    # "granite20b": {
    #     "model":
    #     "mbayser/granite-20b-functioncalling-FP8-KV",
    #     "arguments": [
    #         "--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"
    #     ],
    #     "supports_parallel":
    #     False,
    #     "supports_rocm":
    #     False,
    # },
150
    "granite-3.0-8b": {
151
152
153
        "model":
        "ibm-granite/granite-3.0-8b-instruct",
        "arguments": [
Robert Shaw's avatar
Robert Shaw committed
154
            "--enforce-eager", "--no-enable-prefix-caching",
155
156
157
158
            "--tool-call-parser", "granite", "--chat-template",
            str(VLLM_PATH / "examples/tool_chat_template_granite.jinja")
        ],
    },
159
    "granite-3.1-8b": {
Robert Shaw's avatar
Robert Shaw committed
160
161
        "model":
        "ibm-granite/granite-3.1-8b-instruct",
162
        "arguments": [
Robert Shaw's avatar
Robert Shaw committed
163
164
            "--enforce-eager",
            "--no-enable-prefix-caching",
165
166
167
            "--tool-call-parser",
            "granite",
        ],
Robert Shaw's avatar
Robert Shaw committed
168
169
        "supports_parallel":
        True,
170
    },
171
172
173
174
    "internlm": {
        "model":
        "internlm/internlm2_5-7b-chat",
        "arguments": [
Robert Shaw's avatar
Robert Shaw committed
175
            "--enforce-eager", "--no-enable-prefix-caching",
176
177
178
179
180
181
182
            "--tool-call-parser", "internlm", "--chat-template",
            str(VLLM_PATH /
                "examples/tool_chat_template_internlm2_tool.jinja"),
            "--trust_remote_code"
        ],
        "supports_parallel":
        False,
183
184
185
186
187
    },
    "toolACE": {
        "model":
        "Team-ACE/ToolACE-8B",
        "arguments": [
Robert Shaw's avatar
Robert Shaw committed
188
            "--enforce-eager", "--no-enable-prefix-caching",
189
190
191
192
193
194
            "--tool-call-parser", "pythonic", "--chat-template",
            str(VLLM_PATH / "examples/tool_chat_template_toolace.jinja")
        ],
        "supports_parallel":
        True,
    },
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
}

WEATHER_TOOL: ChatCompletionToolParam = {
    "type": "function",
    "function": {
        "name": "get_current_weather",
        "description": "Get the current weather in a given location",
        "parameters": {
            "type": "object",
            "properties": {
                "city": {
                    "type":
                    "string",
                    "description":
                    "The city to find the weather for, "
                    "e.g. 'San Francisco'"
                },
                "state": {
                    "type":
                    "string",
                    "description":
216
                    "must the two-letter abbreviation for the state "
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
                    "that the city is in, e.g. 'CA' which would "
                    "mean 'California'"
                },
                "unit": {
                    "type": "string",
                    "description": "The unit to fetch the temperature in",
                    "enum": ["celsius", "fahrenheit"]
                }
            }
        }
    }
}

SEARCH_TOOL: ChatCompletionToolParam = {
    "type": "function",
    "function": {
        "name":
        "web_search",
        "description":
        "Search the internet and get a summary of the top "
        "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": {
                    "type":
                    "string",
                    "description":
                    "The term to use in the search. This should"
                    "ideally be keywords to search for, not a"
                    "natural-language question"
                }
            },
            "required": ["search_term"]
        }
    }
}

257
MESSAGES_WITHOUT_TOOLS: list[ChatCompletionMessageParam] = [{
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
    "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?"
}]

274
MESSAGES_ASKING_FOR_TOOLS: list[ChatCompletionMessageParam] = [{
275
276
277
278
279
280
    "role":
    "user",
    "content":
    "What is the weather in Dallas, Texas in Fahrenheit?"
}]

281
MESSAGES_WITH_TOOL_RESPONSE: list[ChatCompletionMessageParam] = [{
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
    "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."
}]

310
MESSAGES_ASKING_FOR_PARALLEL_TOOLS: list[ChatCompletionMessageParam] = [{
311
312
313
314
315
316
317
    "role":
    "user",
    "content":
    "What is the weather in Dallas, Texas and Orlando, Florida in "
    "Fahrenheit?"
}]

318
MESSAGES_WITH_PARALLEL_TOOL_RESPONSE: list[ChatCompletionMessageParam] = [{
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
    "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."
}]