main
py 93 lines 2.83 KB
Raw
1 # SPDX-License-Identifier: Apache-2.0
2 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project
3
4 import openai # use the official client for correctness check
5 import openai.types.responses as openai_responses_types
6 import pytest
7
8
9 @pytest.mark.asyncio
10 async def test_simple_input(client: openai.AsyncOpenAI):
11 response = await client.responses.create(input="What is 13 * 24?")
12 print(response)
13
14 outputs = response.output
15 # Whether the output contains the answer.
16 assert outputs[-1].type == "message"
17 assert "312" in outputs[-1].content[0].text
18
19 # Whether the output contains the reasoning.
20 assert outputs[0].type == "reasoning"
21 assert outputs[0].content[0].text != ""
22
23
24 @pytest.mark.asyncio
25 async def test_instructions(client: openai.AsyncOpenAI):
26 response = await client.responses.create(
27 instructions="Finish the answer with QED.",
28 input="What is 13 * 24?",
29 )
30 print(response)
31
32 output_text = response.output[-1].content[0].text
33 assert "312" in output_text
34 assert "QED" in output_text
35
36
37 @pytest.mark.asyncio
38 async def test_chat(client: openai.AsyncOpenAI):
39 response = await client.responses.create(
40 input=[
41 {"role": "system", "content": "Finish the answer with QED."},
42 {"role": "user", "content": "What is 5 * 3?"},
43 {"role": "assistant", "content": "15. QED."},
44 {"role": "user", "content": "Multiply the result by 2."},
45 ],
46 )
47 print(response)
48
49 output_text = response.output[-1].content[0].text
50 assert "30" in output_text
51 assert "QED" in output_text
52
53
54 @pytest.mark.asyncio
55 async def test_chat_with_input_type(client: openai.AsyncOpenAI):
56 response = await client.responses.create(
57 input=[
58 {
59 "role": "user",
60 "content": [{"type": "input_text", "text": "Hello!"}],
61 },
62 ],
63 )
64 print(response)
65 assert response.status == "completed"
66
67
68 @pytest.mark.asyncio
69 async def test_logprobs(client: openai.AsyncOpenAI):
70 response = await client.responses.create(
71 include=["message.output_text.logprobs"],
72 input="What is 13 * 24?",
73 top_logprobs=5,
74 )
75 print(response)
76 outputs = response.output
77 assert outputs[-1].content[-1].logprobs
78 assert len(outputs[-1].content[-1].logprobs[0].top_logprobs) == 5
79
80
81 @pytest.mark.asyncio
82 async def test_streaming(client: openai.AsyncOpenAI):
83 stream = await client.responses.create(
84 input="What is 13 * 24?",
85 stream=True,
86 )
87 events = [event async for event in stream]
88 assert isinstance(events[0], openai_responses_types.ResponseCreatedEvent)
89 assert any(
90 isinstance(event, openai_responses_types.ResponseTextDeltaEvent)
91 for event in events
92 )
93 assert isinstance(events[-1], openai_responses_types.ResponseCompletedEvent)