main
py 152 lines 4.9 KB
Raw
1 # SPDX-License-Identifier: Apache-2.0
2 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project
3 import asyncio
4
5 import openai
6 import pytest
7
8
9 @pytest.mark.asyncio
10 async def test_store(client: openai.AsyncOpenAI):
11 # By default, store is True.
12 response = await client.responses.create(input="Hello!")
13 assert response.status == "completed"
14
15 # Retrieve the response.
16 response = await client.responses.retrieve(response.id)
17 assert response.status == "completed"
18
19 # Test store=False.
20 response = await client.responses.create(
21 input="Hello!",
22 store=False,
23 )
24 assert response.status == "completed"
25
26 # The response should not be found.
27 with pytest.raises(openai.NotFoundError, match="Response with id .* not found."):
28 await client.responses.retrieve(response.id)
29
30
31 @pytest.mark.asyncio
32 async def test_background(client: openai.AsyncOpenAI):
33 # NOTE: This query should be easy enough for the model to answer
34 # within the 10 seconds.
35 response = await client.responses.create(
36 input="Hello!",
37 background=True,
38 )
39 assert response.status == "queued"
40
41 max_retries = 10
42 for _ in range(max_retries):
43 await asyncio.sleep(1)
44 response = await client.responses.retrieve(response.id)
45 if response.status != "queued":
46 break
47 print(response)
48
49 assert response.status == "completed"
50
51
52 @pytest.mark.asyncio
53 async def test_background_error(client: openai.AsyncOpenAI):
54 with pytest.raises(
55 openai.BadRequestError, match="background can only be used when `store` is true"
56 ):
57 _ = await client.responses.create(
58 input="What is 13 * 24?",
59 background=True,
60 store=False,
61 )
62
63
64 @pytest.mark.asyncio
65 async def test_background_cancel(client: openai.AsyncOpenAI):
66 response = await client.responses.create(
67 input="Write a long story about a cat.",
68 background=True,
69 )
70 assert response.status == "queued"
71
72 # Cancel the response before it is completed.
73 # Poll until the response is no longer queued (started processing) or timeout
74 loop = asyncio.get_running_loop()
75 start_time = loop.time()
76 max_wait_seconds = 5.0
77 poll_interval = 0.1
78 while loop.time() - start_time < max_wait_seconds:
79 response = await client.responses.retrieve(response.id)
80 if response.status != "queued":
81 # Started processing or completed - try to cancel
82 break
83 await asyncio.sleep(poll_interval)
84
85 response = await client.responses.cancel(response.id)
86 assert response.status == "cancelled"
87
88 # Make sure the response status remains unchanged after some time.
89 max_retries = 10
90 for _ in range(max_retries):
91 await asyncio.sleep(0.5)
92 response = await client.responses.retrieve(response.id)
93 # Verify status is still cancelled
94 assert response.status == "cancelled"
95
96
97 @pytest.mark.asyncio
98 async def test_cancel_completed(client: openai.AsyncOpenAI):
99 response = await client.responses.create(input="Hello")
100 assert response.status == "completed"
101
102 with pytest.raises(
103 openai.BadRequestError, match="Cannot cancel a synchronous response."
104 ):
105 await client.responses.cancel(response.id)
106
107
108 @pytest.mark.asyncio
109 async def test_previous_response_id(client: openai.AsyncOpenAI):
110 response1 = await client.responses.create(
111 instructions="You are tested on your ability to retrieve the correct "
112 "information from the previous response.",
113 input="Hello, my name is John.",
114 )
115
116 response2 = await client.responses.create(
117 input="Actually, my name is not John. My real name is Mark.",
118 previous_response_id=response1.id,
119 )
120
121 response3 = await client.responses.create(
122 input="What is my real name again? Answer in one word.",
123 previous_response_id=response2.id,
124 )
125 print(response3)
126 assert "Mark" in response3.output[-1].content[0].text
127 assert "John" not in response3.output[-1].content[0].text
128
129
130 @pytest.mark.asyncio
131 async def test_two_responses_with_same_prev_id(client: openai.AsyncOpenAI):
132 response1 = await client.responses.create(
133 instructions="You are tested on your ability to retrieve the correct "
134 "information from the previous response.",
135 input="Hello, my name is John.",
136 )
137
138 # Both response 2 and 3 use response 1 as the previous response.
139 response2 = client.responses.create(
140 input="Actually, my name is not John. My name is Mark.",
141 previous_response_id=response1.id,
142 )
143 response3 = client.responses.create(
144 input="What is my name again? Answer in one word.",
145 previous_response_id=response1.id,
146 )
147
148 _ = await response2
149 response3_result = await response3
150 print(response3_result)
151 assert "John" in response3_result.output[-1].content[0].text
152 assert "Mark" not in response3_result.output[-1].content[0].text