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

4
5
from __future__ import annotations

6
import asyncio
7
from contextlib import suppress
8
from dataclasses import dataclass, field
9
from typing import TYPE_CHECKING, Any, Optional
10
from unittest.mock import MagicMock
11

12
import pytest
13
import pytest_asyncio
14

15
from vllm.config.multimodal import MultiModalConfig
16
from vllm.entrypoints.openai.protocol import ChatCompletionRequest
17
from vllm.entrypoints.openai.serving_chat import OpenAIServingChat
18
19
from vllm.entrypoints.openai.serving_models import (BaseModelPath,
                                                    OpenAIServingModels)
20
from vllm.transformers_utils.tokenizer import get_tokenizer
21
from vllm.v1.engine.async_llm import AsyncLLM
22

23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
from ...utils import RemoteOpenAIServer

if TYPE_CHECKING:
    from openai import OpenAI

GPT_OSS_MODEL_NAME = "openai/gpt-oss-20b"


@pytest.fixture(scope="module")
def monkeypatch_module():
    from _pytest.monkeypatch import MonkeyPatch
    mpatch = MonkeyPatch()
    yield mpatch
    mpatch.undo()


39
40
41
42
43
44
45
@pytest.fixture(scope="module",
                params=[True, False],
                ids=["with_tool_parser", "without_tool_parser"])
def with_tool_parser(request) -> bool:
    return request.param


46
@pytest.fixture(scope="module")
47
48
49
50
51
52
53
54
55
56
57
58
59
def default_server_args(with_tool_parser: bool):
    args = [
        # use half precision for speed and memory savings in CI environment
        "--enforce-eager",
        "--max-model-len",
        "4096",
        "--reasoning-parser",
        "openai_gptoss",
        "--gpu-memory-utilization",
        "0.8",
    ]
    if with_tool_parser:
        args.extend([
60
61
62
            "--tool-call-parser",
            "openai",
            "--enable-auto-tool-choice",
63
64
65
66
67
68
69
70
        ])
    return args


@pytest.fixture(scope="module")
def gptoss_server(monkeypatch_module: pytest.MonkeyPatch,
                  default_server_args: list[str]):
    with monkeypatch_module.context() as m:
71
        m.setenv("VLLM_ATTENTION_BACKEND", "TRITON_ATTN")
72
73
        with RemoteOpenAIServer(GPT_OSS_MODEL_NAME,
                                default_server_args) as remote_server:
74
75
76
77
78
79
80
81
82
83
            yield remote_server


@pytest_asyncio.fixture
async def gptoss_client(gptoss_server):
    async with gptoss_server.get_async_client() as async_client:
        yield async_client


@pytest.mark.asyncio
84
85
async def test_gpt_oss_chat_tool_call_streaming(gptoss_client: OpenAI,
                                                with_tool_parser: bool):
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
    tools = [{
        "type": "function",
        "function": {
            "name": "get_current_weather",
            "description": "Get the current weather in a given location",
            "parameters": {
                "type": "object",
                "properties": {
                    "city": {
                        "type": "string"
                    },
                    "state": {
                        "type": "string"
                    },
                    "unit": {
                        "type": "string",
                        "enum": ["celsius", "fahrenheit"],
                    },
                },
                "required": ["city", "state", "unit"],
            },
        },
    }]

    messages = [
        {
            "role": "user",
            "content": "What is the weather in Dallas, TX?"
        },
    ]

    stream = await gptoss_client.chat.completions.create(
118
119
120
121
        model=GPT_OSS_MODEL_NAME,
        messages=messages,
        tools=tools if with_tool_parser else None,
        stream=True)
122
123
124

    name = None
    args_buf = ""
125
    content_buf = ""
126
127
128
129
130
131
132
133
    async for chunk in stream:
        delta = chunk.choices[0].delta
        if delta.tool_calls:
            tc = delta.tool_calls[0]
            if tc.function and tc.function.name:
                name = tc.function.name
            if tc.function and tc.function.arguments:
                args_buf += tc.function.arguments
134
135
136
137
138
139
140
141
142
        if getattr(delta, "content", None):
            content_buf += delta.content
    if with_tool_parser:
        assert name is not None
        assert len(args_buf) > 0
    else:
        assert name is None
        assert len(args_buf) == 0
        assert len(content_buf) > 0
143
144
145


@pytest.mark.asyncio
146
147
148
149
async def test_gpt_oss_multi_turn_chat(gptoss_client: OpenAI,
                                       with_tool_parser: bool):
    if not with_tool_parser:
        pytest.skip("skip non-tool for multi-turn tests")
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
    tools = [{
        "type": "function",
        "function": {
            "name": "get_current_weather",
            "description": "Get the current weather in a given location",
            "parameters": {
                "type": "object",
                "properties": {
                    "city": {
                        "type": "string"
                    },
                    "state": {
                        "type": "string"
                    },
                    "unit": {
                        "type": "string",
                        "enum": ["celsius", "fahrenheit"],
                    },
                },
                "required": ["city", "state", "unit"],
            },
        },
    }]

    messages = [
        {
            "role": "system",
            "content": "you are a helpful assistant"
        },
        {
            "role": "user",
181
            "content": "What is the weather in Dallas, TX with celsius?"
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
        },
    ]

    first = await gptoss_client.chat.completions.create(
        model=GPT_OSS_MODEL_NAME,
        messages=messages,
        tools=tools,
        temperature=0.0,
    )
    first_msg = first.choices[0].message
    assert first_msg.tool_calls is not None and len(first_msg.tool_calls) > 0
    tc = first_msg.tool_calls[0]
    assert tc.function is not None and tc.function.name == "get_current_weather"
    args1 = tc.function.arguments
    assert args1 is not None and len(args1) > 0
197
    assert not first_msg.content
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212

    messages.append({"role": "assistant", "content": args1})
    messages.append({
        "role": "user",
        "content": "Now convert to celsius and return JSON only"
    })

    second = await gptoss_client.chat.completions.create(
        model=GPT_OSS_MODEL_NAME,
        messages=messages,
        tools=tools,
        temperature=0.0,
    )
    second_msg = second.choices[0].message
    assert (second_msg.content is not None and len(second_msg.content) > 0) or \
213
        (second_msg.tool_calls is not None and len(second_msg.tool_calls) > 0)
214
215


216
MODEL_NAME = "openai-community/gpt2"
217
MODEL_NAME_SHORT = "gpt2"
218
CHAT_TEMPLATE = "Dummy chat template for testing {}"
219
220
221
222
BASE_MODEL_PATHS = [
    BaseModelPath(name=MODEL_NAME, model_path=MODEL_NAME),
    BaseModelPath(name=MODEL_NAME_SHORT, model_path=MODEL_NAME_SHORT)
]
223
224


225
226
227
228
229
@dataclass
class MockHFConfig:
    model_type: str = "any"


230
231
@dataclass
class MockModelConfig:
232
    task = "generate"
233
234
235
236
237
    tokenizer = MODEL_NAME
    trust_remote_code = False
    tokenizer_mode = "auto"
    max_model_len = 100
    tokenizer_revision = None
238
    multimodal_config = MultiModalConfig()
239
    hf_config = MockHFConfig()
240
    logits_processor_pattern = None
241
    diff_sampling_param: Optional[dict] = None
242
    allowed_local_media_path: str = ""
243
    allowed_media_domains: Optional[list[str]] = None
244
    encoder_config = None
245
    generation_config: str = "auto"
246
    media_io_kwargs: dict[str, dict[str, Any]] = field(default_factory=dict)
247
248
249

    def get_diff_sampling_param(self):
        return self.diff_sampling_param or {}
250
251
252
253
254
255


@dataclass
class MockEngine:

    async def get_model_config(self):
256
        return MockModelConfig()
257
258
259


async def _async_serving_chat_init():
260
261
262
    engine = MockEngine()
    model_config = await engine.get_model_config()

263
    models = OpenAIServingModels(engine, model_config, BASE_MODEL_PATHS)
264
265
    serving_completion = OpenAIServingChat(engine,
                                           model_config,
266
                                           models,
267
                                           response_role="assistant",
268
                                           chat_template=CHAT_TEMPLATE,
269
                                           chat_template_content_format="auto",
270
                                           request_logger=None)
271
272
273
274
275
    return serving_completion


def test_async_serving_chat_init():
    serving_completion = asyncio.run(_async_serving_chat_init())
276
    assert serving_completion.chat_template == CHAT_TEMPLATE
277
278


279
280
@pytest.mark.asyncio
async def test_serving_chat_returns_correct_model_name():
281
    mock_engine = MagicMock(spec=AsyncLLM)
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
    mock_engine.get_tokenizer.return_value = get_tokenizer(MODEL_NAME)
    mock_engine.errored = False

    models = OpenAIServingModels(engine_client=mock_engine,
                                 base_model_paths=BASE_MODEL_PATHS,
                                 model_config=MockModelConfig())
    serving_chat = OpenAIServingChat(mock_engine,
                                     MockModelConfig(),
                                     models,
                                     response_role="assistant",
                                     chat_template=CHAT_TEMPLATE,
                                     chat_template_content_format="auto",
                                     request_logger=None)
    messages = [{"role": "user", "content": "what is 1+1?"}]

    async def return_model_name(*args):
        return args[3]

    serving_chat.chat_completion_full_generator = return_model_name

    # Test that full name is returned when short name is requested
    req = ChatCompletionRequest(model=MODEL_NAME_SHORT, messages=messages)
    assert await serving_chat.create_chat_completion(req) == MODEL_NAME

    # Test that full name is returned when empty string is specified
    req = ChatCompletionRequest(model="", messages=messages)
    assert await serving_chat.create_chat_completion(req) == MODEL_NAME

    # Test that full name is returned when no model is specified
    req = ChatCompletionRequest(messages=messages)
    assert await serving_chat.create_chat_completion(req) == MODEL_NAME


315
316
@pytest.mark.asyncio
async def test_serving_chat_should_set_correct_max_tokens():
317
    mock_engine = MagicMock(spec=AsyncLLM)
318
    mock_engine.get_tokenizer.return_value = get_tokenizer(MODEL_NAME)
319
    mock_engine.errored = False
320

321
322
    models = OpenAIServingModels(engine_client=mock_engine,
                                 base_model_paths=BASE_MODEL_PATHS,
323
                                 model_config=MockModelConfig())
324
325
    serving_chat = OpenAIServingChat(mock_engine,
                                     MockModelConfig(),
326
                                     models,
327
328
                                     response_role="assistant",
                                     chat_template=CHAT_TEMPLATE,
329
                                     chat_template_content_format="auto",
330
                                     request_logger=None)
331

332
333
334
335
336
337
338
339
340
    req = ChatCompletionRequest(
        model=MODEL_NAME,
        messages=[{
            "role": "user",
            "content": "what is 1+1?"
        }],
    )

    with suppress(Exception):
341
        await serving_chat.create_chat_completion(req)
342
343
344
345
346

    assert mock_engine.generate.call_args.args[1].max_tokens == 93

    req.max_tokens = 10
    with suppress(Exception):
347
        await serving_chat.create_chat_completion(req)
348
349

    assert mock_engine.generate.call_args.args[1].max_tokens == 10
350

351
352
353
354
355
356
357
358
    # Setting server's max_tokens in the generation_config.json
    # lower than context_window - prompt_tokens
    mock_model_config = MockModelConfig()
    mock_model_config.diff_sampling_param = {
        "max_tokens": 10  # Setting server-side max_tokens limit
    }

    # Reinitialize the engine with new settings
359
    mock_engine = MagicMock(spec=AsyncLLM)
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
    mock_engine.get_tokenizer.return_value = get_tokenizer(MODEL_NAME)
    mock_engine.errored = False

    # Initialize the serving chat
    models = OpenAIServingModels(engine_client=mock_engine,
                                 base_model_paths=BASE_MODEL_PATHS,
                                 model_config=mock_model_config)
    serving_chat = OpenAIServingChat(mock_engine,
                                     mock_model_config,
                                     models,
                                     response_role="assistant",
                                     chat_template=CHAT_TEMPLATE,
                                     chat_template_content_format="auto",
                                     request_logger=None)

    # Test Case 1: No max_tokens specified in request
    req = ChatCompletionRequest(
        model=MODEL_NAME,
        messages=[{
            "role": "user",
            "content": "what is 1+1?"
        }],
    )

    with suppress(Exception):
385
        await serving_chat.create_chat_completion(req)
386
387
388
389
390
391
392

    assert mock_engine.generate.call_args.args[1].max_tokens == 10

    # Test Case 2: Request's max_tokens set higher than server accepts
    req.max_tokens = 15

    with suppress(Exception):
393
        await serving_chat.create_chat_completion(req)
394
395
396
397
398
399
400

    assert mock_engine.generate.call_args.args[1].max_tokens == 10

    # Test Case 3: Request's max_tokens set lower than server accepts
    req.max_tokens = 5

    with suppress(Exception):
401
        await serving_chat.create_chat_completion(req)
402
403
404
405
406
407
408
409
410
411
412

    assert mock_engine.generate.call_args.args[1].max_tokens == 5

    # Setting server's max_tokens in the generation_config.json
    # higher than context_window - prompt_tokens
    mock_model_config = MockModelConfig()
    mock_model_config.diff_sampling_param = {
        "max_tokens": 200  # Setting server-side max_tokens limit
    }

    # Reinitialize the engine with new settings
413
    mock_engine = MagicMock(spec=AsyncLLM)
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
    mock_engine.get_tokenizer.return_value = get_tokenizer(MODEL_NAME)
    mock_engine.errored = False

    # Initialize the serving chat
    models = OpenAIServingModels(engine_client=mock_engine,
                                 base_model_paths=BASE_MODEL_PATHS,
                                 model_config=mock_model_config)
    serving_chat = OpenAIServingChat(mock_engine,
                                     mock_model_config,
                                     models,
                                     response_role="assistant",
                                     chat_template=CHAT_TEMPLATE,
                                     chat_template_content_format="auto",
                                     request_logger=None)

    # Test case 1: No max_tokens specified, defaults to context_window
    req = ChatCompletionRequest(
        model=MODEL_NAME,
        messages=[{
            "role": "user",
            "content": "what is 1+1?"
        }],
    )

    with suppress(Exception):
439
        await serving_chat.create_chat_completion(req)
440
441
442
443
444
445
446

    assert mock_engine.generate.call_args.args[1].max_tokens == 93

    # Test Case 2: Request's max_tokens set higher than server accepts
    req.max_tokens = 100

    with suppress(Exception):
447
        await serving_chat.create_chat_completion(req)
448
449
450
451
452
453
454

    assert mock_engine.generate.call_args.args[1].max_tokens == 93

    # Test Case 3: Request's max_tokens set lower than server accepts
    req.max_tokens = 5

    with suppress(Exception):
455
        await serving_chat.create_chat_completion(req)
456
457
458

    assert mock_engine.generate.call_args.args[1].max_tokens == 5

459

460
461
@pytest.mark.asyncio
async def test_serving_chat_could_load_correct_generation_config():
462
463
464
465
466
467
468

    mock_model_config = MockModelConfig()
    mock_model_config.diff_sampling_param = {
        "temperature": 0.5,
        "repetition_penalty": 1.05
    }

469
    mock_engine = MagicMock(spec=AsyncLLM)
470
471
472
473
    mock_engine.get_tokenizer.return_value = get_tokenizer(MODEL_NAME)
    mock_engine.errored = False

    # Initialize the serving chat
474
475
    models = OpenAIServingModels(engine_client=mock_engine,
                                 base_model_paths=BASE_MODEL_PATHS,
476
                                 model_config=mock_model_config)
477
478
    serving_chat = OpenAIServingChat(mock_engine,
                                     mock_model_config,
479
                                     models,
480
481
482
483
                                     response_role="assistant",
                                     chat_template=CHAT_TEMPLATE,
                                     chat_template_content_format="auto",
                                     request_logger=None)
484

485
486
487
488
489
490
491
492
493
    req = ChatCompletionRequest(
        model=MODEL_NAME,
        messages=[{
            "role": "user",
            "content": "what is 1+1?"
        }],
    )

    with suppress(Exception):
494
        await serving_chat.create_chat_completion(req)
495
496
497
498
499
500
501
502

    assert mock_engine.generate.call_args.args[1].temperature == 0.5
    assert mock_engine.generate.call_args.args[1].repetition_penalty == 1.05

    # Test the param when user set it
    req.temperature = 0.1

    with suppress(Exception):
503
        await serving_chat.create_chat_completion(req)
504
505
506
507
508
509
510
511

    assert mock_engine.generate.call_args.args[1].temperature == 0.1
    assert mock_engine.generate.call_args.args[1].repetition_penalty == 1.05

    # Test When temperature==0.0
    req.temperature = 0.0

    with suppress(Exception):
512
        await serving_chat.create_chat_completion(req)
513
514
515

    assert mock_engine.generate.call_args.args[1].temperature == 0.0
    assert mock_engine.generate.call_args.args[1].repetition_penalty == 1.05
516
517


518
@pytest.mark.parametrize("model_type", ["gpt_oss", "any"])
519
@pytest.mark.asyncio
520
async def test_serving_chat_did_set_correct_cache_salt(model_type):
521
    mock_model_config = MockModelConfig()
522
    mock_model_config.hf_config.model_type = model_type
523

524
    mock_engine = MagicMock(spec=AsyncLLM)
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
    mock_engine.get_tokenizer.return_value = get_tokenizer(MODEL_NAME)
    mock_engine.errored = False

    # Initialize the serving chat
    models = OpenAIServingModels(engine_client=mock_engine,
                                 base_model_paths=BASE_MODEL_PATHS,
                                 model_config=mock_model_config)
    serving_chat = OpenAIServingChat(mock_engine,
                                     mock_model_config,
                                     models,
                                     response_role="assistant",
                                     chat_template=CHAT_TEMPLATE,
                                     chat_template_content_format="auto",
                                     request_logger=None)

    # Test cache_salt
    req = ChatCompletionRequest(
        model=MODEL_NAME,
        messages=[{
            "role": "user",
            "content": "what is 1+1?"
        }],
    )

549
    # By default, cache_salt in the engine prompt is not set
550
    with suppress(Exception):
551
        await serving_chat.create_chat_completion(req)
552
553
554
555
556
    assert "cache_salt" not in mock_engine.generate.call_args.args[0]

    # Test with certain cache_salt
    req.cache_salt = "test_salt"
    with suppress(Exception):
557
        await serving_chat.create_chat_completion(req)
558
    assert mock_engine.generate.call_args.args[0]["cache_salt"] == "test_salt"