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

4
# imports for structured outputs tests
5
6
7
8
9
import json

import jsonschema
import openai  # use the official client for correctness check
import pytest
10
import pytest_asyncio
11
import regex as re
12
import requests
13
import torch
14
from openai import BadRequestError
15

16
from ...utils import RemoteOpenAIServer
17
18
19
20
21

# any model with a chat template should work here
MODEL_NAME = "HuggingFaceH4/zephyr-7b-beta"


22
23
24
25
26
27
28
29
@pytest.fixture(scope="module")
def zephyr_lora_files():
    """Download zephyr LoRA files once per test session."""
    from huggingface_hub import snapshot_download

    return snapshot_download(repo_id="typeof/zephyr-7b-beta-lora")


30
@pytest.fixture(scope="module")
31
def server(zephyr_lora_files):  # noqa: F811
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
    args = [
        # use half precision for speed and memory savings in CI environment
        "--dtype",
        "bfloat16",
        "--max-model-len",
        "8192",
        "--enforce-eager",
        # lora config below
        "--enable-lora",
        "--lora-modules",
        f"zephyr-lora={zephyr_lora_files}",
        "--max-lora-rank",
        "64",
        "--max-cpu-loras",
        "2",
        "--max-num-seqs",
        "128",
    ]

    with RemoteOpenAIServer(MODEL_NAME, args) as remote_server:
52
        yield remote_server
53
54


55
56
57
58
@pytest_asyncio.fixture
async def client(server):
    async with server.get_async_client() as async_client:
        yield async_client
59
60
61
62
63
64


@pytest.mark.asyncio
@pytest.mark.parametrize(
    # first test base model, then test loras
    "model_name",
65
    [MODEL_NAME, "zephyr-lora"],
66
67
)
async def test_no_logprobs_chat(client: openai.AsyncOpenAI, model_name: str):
68
69
70
71
    messages = [
        {"role": "system", "content": "you are a helpful assistant"},
        {"role": "user", "content": "what is 1+1?"},
    ]
72

73
74
75
76
77
    chat_completion = await client.chat.completions.create(
        model=model_name,
        messages=messages,
        max_completion_tokens=5,
        temperature=0.0,
78
79
        logprobs=False,
    )
80
81
82
83
84
85
86
87
88
89
90
91

    choice = chat_completion.choices[0]
    assert choice.logprobs is None


@pytest.mark.asyncio
@pytest.mark.parametrize(
    # just test 1 lora hereafter
    "model_name",
    [MODEL_NAME, "zephyr-lora"],
)
async def test_zero_logprobs_chat(client: openai.AsyncOpenAI, model_name: str):
92
93
94
95
    messages = [
        {"role": "system", "content": "you are a helpful assistant"},
        {"role": "user", "content": "what is 1+1?"},
    ]
96

97
98
99
100
101
102
    chat_completion = await client.chat.completions.create(
        model=model_name,
        messages=messages,
        max_completion_tokens=5,
        temperature=0.0,
        logprobs=True,
103
104
        top_logprobs=0,
    )
105
106
107
108
109
110
111
112
113
114
115
116
117

    choice = chat_completion.choices[0]
    assert choice.logprobs is not None
    assert choice.logprobs.content is not None
    assert len(choice.logprobs.content[0].top_logprobs) == 0


@pytest.mark.asyncio
@pytest.mark.parametrize(
    "model_name",
    [MODEL_NAME, "zephyr-lora"],
)
async def test_some_logprobs_chat(client: openai.AsyncOpenAI, model_name: str):
118
119
120
121
    messages = [
        {"role": "system", "content": "you are a helpful assistant"},
        {"role": "user", "content": "what is 1+1?"},
    ]
122

123
124
125
126
127
128
    chat_completion = await client.chat.completions.create(
        model=model_name,
        messages=messages,
        max_completion_tokens=5,
        temperature=0.0,
        logprobs=True,
129
130
        top_logprobs=5,
    )
131
132
133
134
135
136
137
138
139
140
141
142

    choice = chat_completion.choices[0]
    assert choice.logprobs is not None
    assert choice.logprobs.content is not None
    assert len(choice.logprobs.content[0].top_logprobs) == 5


@pytest.mark.asyncio
@pytest.mark.parametrize(
    "model_name",
    [MODEL_NAME, "zephyr-lora"],
)
143
144
145
146
147
async def test_too_many_chat_logprobs(client: openai.AsyncOpenAI, model_name: str):
    messages = [
        {"role": "system", "content": "you are a helpful assistant"},
        {"role": "user", "content": "what is 1+1?"},
    ]
148
149
150

    # Default max_logprobs is 20, so this should raise an error
    with pytest.raises((openai.BadRequestError, openai.APIError)):
151
152
153
154
155
156
157
158
        stream = await client.chat.completions.create(
            model=model_name,
            messages=messages,
            max_completion_tokens=10,
            logprobs=True,
            top_logprobs=21,
            stream=True,
        )
159
160
161
162
        async for chunk in stream:
            ...

    with pytest.raises(openai.BadRequestError):
163
164
165
166
167
168
169
170
        await client.chat.completions.create(
            model=model_name,
            messages=messages,
            max_completion_tokens=10,
            logprobs=True,
            top_logprobs=30,
            stream=False,
        )
171
172

    # the server should still work afterwards
173
    chat_completion = await client.chat.completions.create(
174
175
        model=model_name, messages=messages, max_completion_tokens=10, stream=False
    )
176
177
178
179
    message = chat_completion.choices[0].message
    assert message.content is not None and len(message.content) >= 0


180
181
182
183
184
@pytest.mark.asyncio
@pytest.mark.parametrize(
    "model_name, prompt_logprobs",
    [(MODEL_NAME, 1), (MODEL_NAME, 0), (MODEL_NAME, -1), (MODEL_NAME, None)],
)
185
async def test_prompt_logprobs_chat(
186
    client: openai.AsyncOpenAI, model_name: str, prompt_logprobs: int | None
187
):
188
    params: dict = {
189
190
191
192
193
194
195
196
197
198
        "messages": [
            {"role": "system", "content": "You are a helpful assistant."},
            {"role": "user", "content": "Who won the world series in 2020?"},
            {
                "role": "assistant",
                "content": "The Los Angeles Dodgers won the World Series in 2020.",
            },
            {"role": "user", "content": "Where was it played?"},
        ],
        "model": model_name,
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
    }

    if prompt_logprobs is not None:
        params["extra_body"] = {"prompt_logprobs": prompt_logprobs}

    if prompt_logprobs is not None and prompt_logprobs < 0:
        with pytest.raises(BadRequestError):
            await client.chat.completions.create(**params)
    else:
        completion = await client.chat.completions.create(**params)
        if prompt_logprobs is not None:
            assert completion.prompt_logprobs is not None
            assert len(completion.prompt_logprobs) > 0
        else:
            assert completion.prompt_logprobs is None


@pytest.mark.asyncio
@pytest.mark.parametrize(
    "model_name",
    [MODEL_NAME],
)
221
222
223
async def test_more_than_one_prompt_logprobs_chat(
    client: openai.AsyncOpenAI, model_name: str
):
224
    params: dict = {
225
226
227
228
229
230
231
232
233
234
235
        "messages": [
            {"role": "system", "content": "You are a helpful assistant."},
            {"role": "user", "content": "Who won the world series in 2020?"},
            {
                "role": "assistant",
                "content": "The Los Angeles Dodgers won the World Series in 2020.",
            },
            {"role": "user", "content": "Where was it played?"},
        ],
        "model": model_name,
        "extra_body": {"prompt_logprobs": 1},
236
237
238
239
240
241
242
243
244
245
246
    }

    completion_1 = await client.chat.completions.create(**params)

    params["extra_body"] = {"prompt_logprobs": 2}
    completion_2 = await client.chat.completions.create(**params)

    assert len(completion_1.prompt_logprobs[3]) == 1
    assert len(completion_2.prompt_logprobs[3]) == 2


247
248
249
250
251
@pytest.mark.asyncio
@pytest.mark.parametrize(
    "model_name",
    [MODEL_NAME, "zephyr-lora"],
)
252
253
254
255
256
async def test_single_chat_session(client: openai.AsyncOpenAI, model_name: str):
    messages = [
        {"role": "system", "content": "you are a helpful assistant"},
        {"role": "user", "content": "what is 1+1?"},
    ]
257
258

    # test single completion
259
260
261
262
263
    chat_completion = await client.chat.completions.create(
        model=model_name,
        messages=messages,
        max_completion_tokens=10,
        logprobs=True,
264
265
        top_logprobs=5,
    )
266
267
268
269
270
271
    assert chat_completion.id is not None
    assert len(chat_completion.choices) == 1

    choice = chat_completion.choices[0]
    assert choice.finish_reason == "length"
    assert chat_completion.usage == openai.types.CompletionUsage(
272
273
        completion_tokens=10, prompt_tokens=37, total_tokens=47
    )
274
275
276
277
278
279
280
281
282
283
284

    message = choice.message
    assert message.content is not None and len(message.content) >= 10
    assert message.role == "assistant"
    messages.append({"role": "assistant", "content": message.content})

    # test multi-turn dialogue
    messages.append({"role": "user", "content": "express your result in json"})
    chat_completion = await client.chat.completions.create(
        model=model_name,
        messages=messages,
285
        max_completion_tokens=10,
286
287
288
289
290
291
292
293
294
295
296
297
    )
    message = chat_completion.choices[0].message
    assert message.content is not None and len(message.content) >= 0


@pytest.mark.asyncio
@pytest.mark.parametrize(
    # just test 1 lora hereafter
    "model_name",
    [MODEL_NAME, "zephyr-lora"],
)
async def test_chat_streaming(client: openai.AsyncOpenAI, model_name: str):
298
299
300
301
    messages = [
        {"role": "system", "content": "you are a helpful assistant"},
        {"role": "user", "content": "what is 1+1?"},
    ]
302
303
304
305
306

    # test single completion
    chat_completion = await client.chat.completions.create(
        model=model_name,
        messages=messages,
307
        max_completion_tokens=10,
308
309
310
311
312
313
314
315
316
        temperature=0.0,
    )
    output = chat_completion.choices[0].message.content
    stop_reason = chat_completion.choices[0].finish_reason

    # test streaming
    stream = await client.chat.completions.create(
        model=model_name,
        messages=messages,
317
        max_completion_tokens=10,
318
319
320
        temperature=0.0,
        stream=True,
    )
321
    chunks: list[str] = []
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
    finish_reason_count = 0
    async for chunk in stream:
        delta = chunk.choices[0].delta
        if delta.role:
            assert delta.role == "assistant"
        if delta.content:
            chunks.append(delta.content)
        if chunk.choices[0].finish_reason is not None:
            finish_reason_count += 1
    # finish reason should only return in last block
    assert finish_reason_count == 1
    assert chunk.choices[0].finish_reason == stop_reason
    assert delta.content
    assert "".join(chunks) == output


@pytest.mark.asyncio
@pytest.mark.parametrize(
    "model_name",
    ["HuggingFaceH4/zephyr-7b-beta", "zephyr-lora"],
)
343
344
345
346
347
348
349
async def test_chat_completion_stream_options(
    client: openai.AsyncOpenAI, model_name: str
):
    messages = [
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "What is the capital of France?"},
    ]
350
351
352
353
354

    # Test stream=True, stream_options={"include_usage": False}
    stream = await client.chat.completions.create(
        model=model_name,
        messages=messages,
355
        max_completion_tokens=10,
356
357
        temperature=0.0,
        stream=True,
358
359
        stream_options={"include_usage": False},
    )
360
361
362
    async for chunk in stream:
        assert chunk.usage is None

363
364
    # Test stream=True, stream_options={"include_usage": True,
    #                                   "continuous_usage_stats": False}}
365
366
367
368
369
370
371
372
    stream = await client.chat.completions.create(
        model=model_name,
        messages=messages,
        max_completion_tokens=10,
        temperature=0.0,
        stream=True,
        stream_options={"include_usage": True, "continuous_usage_stats": False},
    )
373
374
375
376
377
378

    async for chunk in stream:
        if chunk.choices[0].finish_reason is None:
            assert chunk.usage is None
        else:
            assert chunk.usage is None
379
            final_chunk = await anext(stream)
380
381
382
383
            assert final_chunk.usage is not None
            assert final_chunk.usage.prompt_tokens > 0
            assert final_chunk.usage.completion_tokens > 0
            assert final_chunk.usage.total_tokens == (
384
385
                final_chunk.usage.prompt_tokens + final_chunk.usage.completion_tokens
            )
386
387
388
389
390
391
392
            assert final_chunk.choices == []

    # Test stream=False, stream_options={"include_usage": None}
    with pytest.raises(BadRequestError):
        await client.chat.completions.create(
            model=model_name,
            messages=messages,
393
            max_completion_tokens=10,
394
395
            temperature=0.0,
            stream=False,
396
397
            stream_options={"include_usage": None},
        )
398
399
400
401
402
403

    # Test stream=False, stream_options={"include_usage": True}
    with pytest.raises(BadRequestError):
        await client.chat.completions.create(
            model=model_name,
            messages=messages,
404
            max_completion_tokens=10,
405
406
            temperature=0.0,
            stream=False,
407
408
            stream_options={"include_usage": True},
        )
409

410
411
412
413
414
    # Test stream=True, stream_options={"include_usage": True,
    #                           "continuous_usage_stats": True}
    stream = await client.chat.completions.create(
        model=model_name,
        messages=messages,
415
        max_completion_tokens=10,
416
        extra_body=dict(min_tokens=10),
417
418
419
420
        temperature=0.0,
        stream=True,
        stream_options={
            "include_usage": True,
421
            "continuous_usage_stats": True,
422
423
        },
    )
424
    last_completion_tokens = 0
425
426
    async for chunk in stream:
        assert chunk.usage.prompt_tokens >= 0
427
428
429
430
431
432
433
434
435
436
437
        assert (
            last_completion_tokens == 0
            or chunk.usage.completion_tokens > last_completion_tokens
            or (
                not chunk.choices
                and chunk.usage.completion_tokens == last_completion_tokens
            )
        )
        assert chunk.usage.total_tokens == (
            chunk.usage.prompt_tokens + chunk.usage.completion_tokens
        )
438
439
440
        last_completion_tokens = chunk.usage.completion_tokens

    assert last_completion_tokens == 10
441

442
443

@pytest.mark.asyncio
444
async def test_structured_outputs_choice_chat(
445
446
447
    client: openai.AsyncOpenAI,
    sample_structured_outputs_choices,
):
448
449
450
451
452
453
454
    messages = [
        {"role": "system", "content": "you are a helpful assistant"},
        {
            "role": "user",
            "content": "The best language for type-safe systems programming is ",
        },
    ]
455
456
457
    chat_completion = await client.chat.completions.create(
        model=MODEL_NAME,
        messages=messages,
458
        max_completion_tokens=10,
459
        temperature=0.7,
460
        extra_body=dict(
461
462
463
            structured_outputs={"choice": sample_structured_outputs_choices}
        ),
    )
464
    choice1 = chat_completion.choices[0].message.content
465
    assert choice1 in sample_structured_outputs_choices
466
467

    messages.append({"role": "assistant", "content": choice1})
468
    messages.append({"role": "user", "content": "I disagree, pick another one"})
469
470
471
    chat_completion = await client.chat.completions.create(
        model=MODEL_NAME,
        messages=messages,
472
        max_completion_tokens=10,
473
        temperature=0.7,
474
        extra_body=dict(
475
476
477
            structured_outputs={"choice": sample_structured_outputs_choices}
        ),
    )
478
    choice2 = chat_completion.choices[0].message.content
479
    assert choice2 in sample_structured_outputs_choices
480
481
482
483
    assert choice1 != choice2


@pytest.mark.asyncio
484
485
486
487
async def test_structured_outputs_json_chat(
    client: openai.AsyncOpenAI,
    sample_json_schema,
):
488
489
490
491
492
493
494
495
    messages = [
        {"role": "system", "content": "you are a helpful assistant"},
        {
            "role": "user",
            "content": f"Give an example JSON for an employee profile that "
            f"fits this schema: {sample_json_schema}",
        },
    ]
496
497
498
    chat_completion = await client.chat.completions.create(
        model=MODEL_NAME,
        messages=messages,
499
        max_completion_tokens=1000,
500
501
        extra_body=dict(structured_outputs={"json": sample_json_schema}),
    )
502
503
504
    message = chat_completion.choices[0].message
    assert message.content is not None
    json1 = json.loads(message.content)
505
    jsonschema.validate(instance=json1, schema=sample_json_schema)
506
507

    messages.append({"role": "assistant", "content": message.content})
508
509
510
    messages.append(
        {"role": "user", "content": "Give me another one with a different name and age"}
    )
511
512
513
    chat_completion = await client.chat.completions.create(
        model=MODEL_NAME,
        messages=messages,
514
        max_completion_tokens=1000,
515
516
        extra_body=dict(structured_outputs={"json": sample_json_schema}),
    )
517
518
519
    message = chat_completion.choices[0].message
    assert message.content is not None
    json2 = json.loads(message.content)
520
    jsonschema.validate(instance=json2, schema=sample_json_schema)
521
522
523
524
525
    assert json1["name"] != json2["name"]
    assert json1["age"] != json2["age"]


@pytest.mark.asyncio
526
527
528
529
async def test_structured_outputs_regex_chat(
    client: openai.AsyncOpenAI,
    sample_regex,
):
530
531
532
533
534
535
536
    messages = [
        {"role": "system", "content": "you are a helpful assistant"},
        {
            "role": "user",
            "content": f"Give an example IP address with this regex: {sample_regex}",
        },
    ]
537
538
539
    chat_completion = await client.chat.completions.create(
        model=MODEL_NAME,
        messages=messages,
540
        max_completion_tokens=20,
541
542
        extra_body=dict(structured_outputs={"regex": sample_regex}),
    )
543
544
    ip1 = chat_completion.choices[0].message.content
    assert ip1 is not None
545
    assert re.fullmatch(sample_regex, ip1) is not None
546
547
548
549
550
551

    messages.append({"role": "assistant", "content": ip1})
    messages.append({"role": "user", "content": "Give me a different one"})
    chat_completion = await client.chat.completions.create(
        model=MODEL_NAME,
        messages=messages,
552
        max_completion_tokens=20,
553
554
        extra_body=dict(structured_outputs={"regex": sample_regex}),
    )
555
556
    ip2 = chat_completion.choices[0].message.content
    assert ip2 is not None
557
    assert re.fullmatch(sample_regex, ip2) is not None
558
559
560
561
    assert ip1 != ip2


@pytest.mark.asyncio
562
async def test_structured_outputs_type_error(client: openai.AsyncOpenAI):
563
564
565
566
567
568
569
    messages = [
        {"role": "system", "content": "you are a helpful assistant"},
        {
            "role": "user",
            "content": "The best language for type-safe systems programming is ",
        },
    ]
570
571

    with pytest.raises(openai.BadRequestError):
572
573
574
        _ = await client.chat.completions.create(
            model=MODEL_NAME,
            messages=messages,
575
576
            extra_body=dict(structured_outputs={"regex": {1: "Python", 2: "C++"}}),
        )
577
578
579


@pytest.mark.asyncio
580
async def test_structured_outputs_choice_chat_logprobs(
581
582
583
584
585
586
587
588
589
    client: openai.AsyncOpenAI, sample_structured_outputs_choices
):
    messages = [
        {"role": "system", "content": "you are a helpful assistant"},
        {
            "role": "user",
            "content": "The best language for type-safe systems programming is ",
        },
    ]
590
591
592
    chat_completion = await client.chat.completions.create(
        model=MODEL_NAME,
        messages=messages,
593
        max_completion_tokens=10,
594
595
        logprobs=True,
        top_logprobs=5,
596
        extra_body=dict(
597
598
599
            structured_outputs={"choice": sample_structured_outputs_choices}
        ),
    )
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614

    assert chat_completion.choices[0].logprobs is not None
    assert chat_completion.choices[0].logprobs.content is not None
    top_logprobs = chat_completion.choices[0].logprobs.content[0].top_logprobs

    # -9999.0 is the minimum logprob returned by OpenAI
    for item in top_logprobs:
        assert item.logprob >= -9999.0, f"Failed (top_logprobs={top_logprobs})"


@pytest.mark.asyncio
async def test_response_format_json_object(client: openai.AsyncOpenAI):
    for _ in range(2):
        resp = await client.chat.completions.create(
            model=MODEL_NAME,
615
616
617
618
619
620
621
622
623
624
625
            messages=[
                {
                    "role": "user",
                    "content": (
                        "what is 1+1? please respond with a JSON object, "
                        'the format is {"result": 2}'
                    ),
                }
            ],
            response_format={"type": "json_object"},
        )
626
627
628
629
630
631
632
633

        content = resp.choices[0].message.content
        assert content is not None

        loaded = json.loads(content)
        assert loaded == {"result": 2}, loaded


634
@pytest.mark.asyncio
635
async def test_response_format_json_schema(client: openai.AsyncOpenAI):
636
637
    prompt = 'what is 1+1? The format is "result": 2'
    # Check that this prompt cannot lead to a valid JSON without json_schema
638
639
640
    for _ in range(2):
        resp = await client.chat.completions.create(
            model=MODEL_NAME,
641
            messages=[{"role": "user", "content": prompt}],
642
643
644
645
646
647
648
649
650
651
        )
        content = resp.choices[0].message.content
        assert content is not None
        with pytest.raises((json.JSONDecodeError, AssertionError)):
            loaded = json.loads(content)
            assert loaded == {"result": 2}, loaded

    for _ in range(2):
        resp = await client.chat.completions.create(
            model=MODEL_NAME,
652
            messages=[{"role": "user", "content": prompt}],
653
654
655
656
657
658
659
            response_format={
                "type": "json_schema",
                "json_schema": {
                    "name": "foo_test",
                    "schema": {
                        "type": "object",
                        "properties": {
660
                            "result": {"type": "integer"},
661
662
                        },
                    },
663
664
665
                },
            },
        )
666
667
668
669
670
671
672
673

        content = resp.choices[0].message.content
        assert content is not None

        loaded = json.loads(content)
        assert loaded == {"result": 2}, loaded


674
@pytest.mark.asyncio
675
676
677
async def test_extra_fields_allowed(client: openai.AsyncOpenAI):
    resp = await client.chat.completions.create(
        model=MODEL_NAME,
678
679
680
681
682
683
684
        messages=[
            {
                "role": "user",
                "content": "what is 1+1?",
                "extra_field": "0",
            }
        ],  # type: ignore
685
        temperature=0,
686
687
        seed=0,
    )
688
689
690

    content = resp.choices[0].message.content
    assert content is not None
691
692
693
694


@pytest.mark.asyncio
async def test_complex_message_content(client: openai.AsyncOpenAI):
695
696
697
698
699
700
    content = [
        {
            "type": "text",
            "text": "what is 1+1? please provide the result without any other text.",
        }
    ]
701
702
    resp = await client.chat.completions.create(
        model=MODEL_NAME,
703
704
705
        messages=[
            {
                "role": "user",
706
                "content": content,
707
708
            }
        ],
709
        temperature=0,
710
711
        seed=0,
    )
712
713
714
715
716
717
718
719
720
721
722
    content = resp.choices[0].message.content
    assert content == "2"


@pytest.mark.asyncio
async def test_custom_role(client: openai.AsyncOpenAI):
    # Not sure how the model handles custom roles so we just check that
    # both string and complex message content are handled in the same way

    resp1 = await client.chat.completions.create(
        model=MODEL_NAME,
723
724
725
726
727
728
        messages=[
            {
                "role": "my-custom-role",
                "content": "what is 1+1?",
            }
        ],  # type: ignore
729
        temperature=0,
730
731
        seed=0,
    )
732
733
734

    resp2 = await client.chat.completions.create(
        model=MODEL_NAME,
735
736
737
738
739
740
        messages=[
            {
                "role": "my-custom-role",
                "content": [{"type": "text", "text": "what is 1+1?"}],
            }
        ],  # type: ignore
741
        temperature=0,
742
743
        seed=0,
    )
744
745
746
747
748
749
750
751

    content1 = resp1.choices[0].message.content
    content2 = resp2.choices[0].message.content
    assert content1 == content2


@pytest.mark.asyncio
async def test_long_seed(client: openai.AsyncOpenAI):
752
    for seed in [torch.iinfo(torch.long).min - 1, torch.iinfo(torch.long).max + 1]:
753
754
755
        with pytest.raises(BadRequestError) as exc_info:
            await client.chat.completions.create(
                model=MODEL_NAME,
756
757
758
759
760
761
                messages=[
                    {
                        "role": "system",
                        "content": "You are a helpful assistant.",
                    }
                ],
762
                temperature=0,
763
764
                seed=seed,
            )
765

766
767
768
769
        assert (
            "greater_than_equal" in exc_info.value.message
            or "less_than_equal" in exc_info.value.message
        )
770
771


772
@pytest.mark.asyncio
773
774
775
776
777
async def test_invocations(server: RemoteOpenAIServer, client: openai.AsyncOpenAI):
    messages = [
        {"role": "system", "content": "you are a helpful assistant"},
        {"role": "user", "content": "what is 1+1?"},
    ]
778
779
780
781
782
783
784
785
786
787
788

    request_args = {
        "model": MODEL_NAME,
        "messages": messages,
        "max_completion_tokens": 5,
        "temperature": 0.0,
        "logprobs": False,
    }

    chat_completion = await client.chat.completions.create(**request_args)

789
790
791
    invocation_response = requests.post(
        server.url_for("invocations"), json=request_args
    )
792
793
794
795
796
797
798
    invocation_response.raise_for_status()

    chat_output = chat_completion.model_dump()
    invocation_output = invocation_response.json()

    assert chat_output.keys() == invocation_output.keys()
    assert chat_output["choices"] == invocation_output["choices"]