-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathtest_openai_chatcompletions.py
292 lines (264 loc) · 10.3 KB
/
test_openai_chatcompletions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
from __future__ import annotations
from collections.abc import AsyncIterator
from typing import Any
import httpx
import pytest
from openai import NOT_GIVEN
from openai.types.chat.chat_completion import ChatCompletion, Choice
from openai.types.chat.chat_completion_chunk import ChatCompletionChunk
from openai.types.chat.chat_completion_message import ChatCompletionMessage
from openai.types.chat.chat_completion_message_tool_call import (
ChatCompletionMessageToolCall,
Function,
)
from openai.types.completion_usage import CompletionUsage
from openai.types.responses import (
Response,
ResponseFunctionToolCall,
ResponseOutputMessage,
ResponseOutputRefusal,
ResponseOutputText,
)
from agents import (
ModelResponse,
ModelSettings,
ModelTracing,
OpenAIChatCompletionsModel,
OpenAIProvider,
generation_span,
)
from agents.models.fake_id import FAKE_RESPONSES_ID
@pytest.mark.allow_call_model_methods
@pytest.mark.asyncio
async def test_get_response_with_text_message(monkeypatch) -> None:
"""
When the model returns a ChatCompletionMessage with plain text content,
`get_response` should produce a single `ResponseOutputMessage` containing
a `ResponseOutputText` with that content, and a `Usage` populated from
the completion's usage.
"""
msg = ChatCompletionMessage(role="assistant", content="Hello")
choice = Choice(index=0, finish_reason="stop", message=msg)
chat = ChatCompletion(
id="resp-id",
created=0,
model="fake",
object="chat.completion",
choices=[choice],
usage=CompletionUsage(completion_tokens=5, prompt_tokens=7, total_tokens=12),
)
async def patched_fetch_response(self, *args, **kwargs):
return chat
monkeypatch.setattr(OpenAIChatCompletionsModel, "_fetch_response", patched_fetch_response)
model = OpenAIProvider(use_responses=False).get_model("gpt-4")
resp: ModelResponse = await model.get_response(
system_instructions=None,
input="",
model_settings=ModelSettings(),
tools=[],
output_schema=None,
handoffs=[],
tracing=ModelTracing.DISABLED,
)
# Should have produced exactly one output message with one text part
assert isinstance(resp, ModelResponse)
assert len(resp.output) == 1
assert isinstance(resp.output[0], ResponseOutputMessage)
msg_item = resp.output[0]
assert len(msg_item.content) == 1
assert isinstance(msg_item.content[0], ResponseOutputText)
assert msg_item.content[0].text == "Hello"
# Usage should be preserved from underlying ChatCompletion.usage
assert resp.usage.input_tokens == 7
assert resp.usage.output_tokens == 5
assert resp.usage.total_tokens == 12
assert resp.referenceable_id is None
@pytest.mark.allow_call_model_methods
@pytest.mark.asyncio
async def test_get_response_with_refusal(monkeypatch) -> None:
"""
When the model returns a ChatCompletionMessage with a `refusal` instead
of normal `content`, `get_response` should produce a single
`ResponseOutputMessage` containing a `ResponseOutputRefusal` part.
"""
msg = ChatCompletionMessage(role="assistant", refusal="No thanks")
choice = Choice(index=0, finish_reason="stop", message=msg)
chat = ChatCompletion(
id="resp-id",
created=0,
model="fake",
object="chat.completion",
choices=[choice],
usage=None,
)
async def patched_fetch_response(self, *args, **kwargs):
return chat
monkeypatch.setattr(OpenAIChatCompletionsModel, "_fetch_response", patched_fetch_response)
model = OpenAIProvider(use_responses=False).get_model("gpt-4")
resp: ModelResponse = await model.get_response(
system_instructions=None,
input="",
model_settings=ModelSettings(),
tools=[],
output_schema=None,
handoffs=[],
tracing=ModelTracing.DISABLED,
)
assert len(resp.output) == 1
assert isinstance(resp.output[0], ResponseOutputMessage)
refusal_part = resp.output[0].content[0]
assert isinstance(refusal_part, ResponseOutputRefusal)
assert refusal_part.refusal == "No thanks"
# With no usage from the completion, usage defaults to zeros.
assert resp.usage.requests == 0
assert resp.usage.input_tokens == 0
assert resp.usage.output_tokens == 0
@pytest.mark.allow_call_model_methods
@pytest.mark.asyncio
async def test_get_response_with_tool_call(monkeypatch) -> None:
"""
If the ChatCompletionMessage includes one or more tool_calls, `get_response`
should append corresponding `ResponseFunctionToolCall` items after the
assistant message item with matching name/arguments.
"""
tool_call = ChatCompletionMessageToolCall(
id="call-id",
type="function",
function=Function(name="do_thing", arguments="{'x':1}"),
)
msg = ChatCompletionMessage(role="assistant", content="Hi", tool_calls=[tool_call])
choice = Choice(index=0, finish_reason="stop", message=msg)
chat = ChatCompletion(
id="resp-id",
created=0,
model="fake",
object="chat.completion",
choices=[choice],
usage=None,
)
async def patched_fetch_response(self, *args, **kwargs):
return chat
monkeypatch.setattr(OpenAIChatCompletionsModel, "_fetch_response", patched_fetch_response)
model = OpenAIProvider(use_responses=False).get_model("gpt-4")
resp: ModelResponse = await model.get_response(
system_instructions=None,
input="",
model_settings=ModelSettings(),
tools=[],
output_schema=None,
handoffs=[],
tracing=ModelTracing.DISABLED,
)
# Expect a message item followed by a function tool call item.
assert len(resp.output) == 2
assert isinstance(resp.output[0], ResponseOutputMessage)
fn_call_item = resp.output[1]
assert isinstance(fn_call_item, ResponseFunctionToolCall)
assert fn_call_item.call_id == "call-id"
assert fn_call_item.name == "do_thing"
assert fn_call_item.arguments == "{'x':1}"
@pytest.mark.asyncio
async def test_fetch_response_non_stream(monkeypatch) -> None:
"""
Verify that `_fetch_response` builds the correct OpenAI API call when not
streaming and returns the ChatCompletion object directly. We supply a
dummy ChatCompletion through a stubbed OpenAI client and inspect the
captured kwargs.
"""
# Dummy completions to record kwargs
class DummyCompletions:
def __init__(self) -> None:
self.kwargs: dict[str, Any] = {}
async def create(self, **kwargs: Any) -> Any:
self.kwargs = kwargs
return chat
class DummyClient:
def __init__(self, completions: DummyCompletions) -> None:
self.chat = type("_Chat", (), {"completions": completions})()
self.base_url = httpx.URL("http://fake")
msg = ChatCompletionMessage(role="assistant", content="ignored")
choice = Choice(index=0, finish_reason="stop", message=msg)
chat = ChatCompletion(
id="resp-id",
created=0,
model="fake",
object="chat.completion",
choices=[choice],
)
completions = DummyCompletions()
dummy_client = DummyClient(completions)
model = OpenAIChatCompletionsModel(model="gpt-4", openai_client=dummy_client) # type: ignore
# Execute the private fetch with a system instruction and simple string input.
with generation_span(disabled=True) as span:
result = await model._fetch_response(
system_instructions="sys",
input="hi",
model_settings=ModelSettings(),
tools=[],
output_schema=None,
handoffs=[],
span=span,
tracing=ModelTracing.DISABLED,
stream=False,
)
assert result is chat
# Ensure expected args were passed through to OpenAI client.
kwargs = completions.kwargs
assert kwargs["stream"] is False
assert kwargs["store"] is True
assert kwargs["model"] == "gpt-4"
assert kwargs["messages"][0]["role"] == "system"
assert kwargs["messages"][0]["content"] == "sys"
assert kwargs["messages"][1]["role"] == "user"
# Defaults for optional fields become the NOT_GIVEN sentinel
assert kwargs["tools"] is NOT_GIVEN
assert kwargs["tool_choice"] is NOT_GIVEN
assert kwargs["response_format"] is NOT_GIVEN
assert kwargs["stream_options"] is NOT_GIVEN
@pytest.mark.asyncio
async def test_fetch_response_stream(monkeypatch) -> None:
"""
When `stream=True`, `_fetch_response` should return a bare `Response`
object along with the underlying async stream. The OpenAI client call
should include `stream_options` to request usage-delimited chunks.
"""
async def event_stream() -> AsyncIterator[ChatCompletionChunk]:
if False: # pragma: no cover
yield # pragma: no cover
class DummyCompletions:
def __init__(self) -> None:
self.kwargs: dict[str, Any] = {}
async def create(self, **kwargs: Any) -> Any:
self.kwargs = kwargs
return event_stream()
class DummyClient:
def __init__(self, completions: DummyCompletions) -> None:
self.chat = type("_Chat", (), {"completions": completions})()
self.base_url = httpx.URL("http://fake")
completions = DummyCompletions()
dummy_client = DummyClient(completions)
model = OpenAIChatCompletionsModel(model="gpt-4", openai_client=dummy_client) # type: ignore
with generation_span(disabled=True) as span:
response, stream = await model._fetch_response(
system_instructions=None,
input="hi",
model_settings=ModelSettings(),
tools=[],
output_schema=None,
handoffs=[],
span=span,
tracing=ModelTracing.DISABLED,
stream=True,
)
# Check OpenAI client was called for streaming
assert completions.kwargs["stream"] is True
assert completions.kwargs["store"] is True
assert completions.kwargs["stream_options"] == {"include_usage": True}
# Response is a proper openai Response
assert isinstance(response, Response)
assert response.id == FAKE_RESPONSES_ID
assert response.model == "gpt-4"
assert response.object == "response"
assert response.output == []
# We returned the async iterator produced by our dummy.
assert hasattr(stream, "__aiter__")