-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Expand file tree
/
Copy pathtest_kwargs_functionality.py
More file actions
216 lines (180 loc) · 6.84 KB
/
test_kwargs_functionality.py
File metadata and controls
216 lines (180 loc) · 6.84 KB
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
import litellm
import pytest
from litellm.types.utils import Choices, Message, ModelResponse, Usage
from openai.types.chat.chat_completion import ChatCompletion, Choice
from openai.types.chat.chat_completion_message import ChatCompletionMessage
from openai.types.completion_usage import CompletionUsage
from agents.extensions.models.litellm_model import LitellmModel
from agents.model_settings import ModelSettings
from agents.models.interface import ModelTracing
from agents.models.openai_chatcompletions import OpenAIChatCompletionsModel
@pytest.mark.allow_call_model_methods
@pytest.mark.asyncio
async def test_litellm_kwargs_forwarded(monkeypatch):
"""
Test that kwargs from ModelSettings are forwarded to litellm.acompletion.
"""
captured: dict[str, object] = {}
async def fake_acompletion(model, messages=None, **kwargs):
captured.update(kwargs)
msg = Message(role="assistant", content="test response")
choice = Choices(index=0, message=msg)
return ModelResponse(choices=[choice], usage=Usage(0, 0, 0))
monkeypatch.setattr(litellm, "acompletion", fake_acompletion)
settings = ModelSettings(
temperature=0.5,
extra_args={
"custom_param": "custom_value",
"seed": 42,
"stop": ["END"],
"logit_bias": {123: -100},
},
)
model = LitellmModel(model="test-model")
await model.get_response(
system_instructions=None,
input="test input",
model_settings=settings,
tools=[],
output_schema=None,
handoffs=[],
tracing=ModelTracing.DISABLED,
previous_response_id=None,
conversation_id=None,
)
# Verify that all kwargs were passed through
assert captured["custom_param"] == "custom_value"
assert captured["seed"] == 42
assert captured["stop"] == ["END"]
assert captured["logit_bias"] == {123: -100}
# Verify regular parameters are still passed
assert captured["temperature"] == 0.5
@pytest.mark.allow_call_model_methods
@pytest.mark.asyncio
async def test_openai_chatcompletions_kwargs_forwarded(monkeypatch):
"""
Test that kwargs from ModelSettings are forwarded to OpenAI chat completions API.
"""
captured: dict[str, object] = {}
class MockChatCompletions:
async def create(self, **kwargs):
captured.update(kwargs)
msg = ChatCompletionMessage(role="assistant", content="test response")
choice = Choice(index=0, message=msg, finish_reason="stop")
return ChatCompletion(
id="test-id",
created=0,
model="gpt-4",
object="chat.completion",
choices=[choice],
usage=CompletionUsage(completion_tokens=5, prompt_tokens=10, total_tokens=15),
)
class MockChat:
def __init__(self):
self.completions = MockChatCompletions()
class MockClient:
def __init__(self):
self.chat = MockChat()
self.base_url = "https://api.openai.com/v1"
settings = ModelSettings(
temperature=0.7,
extra_args={
"seed": 123,
"logit_bias": {456: 10},
"stop": ["STOP", "END"],
"user": "test-user",
},
)
mock_client = MockClient()
model = OpenAIChatCompletionsModel(model="gpt-4", openai_client=mock_client) # type: ignore
await model.get_response(
system_instructions="Test system",
input="test input",
model_settings=settings,
tools=[],
output_schema=None,
handoffs=[],
tracing=ModelTracing.DISABLED,
previous_response_id=None,
)
# Verify that all kwargs were passed through
assert captured["seed"] == 123
assert captured["logit_bias"] == {456: 10}
assert captured["stop"] == ["STOP", "END"]
assert captured["user"] == "test-user"
# Verify regular parameters are still passed
assert captured["temperature"] == 0.7
@pytest.mark.allow_call_model_methods
@pytest.mark.asyncio
async def test_empty_kwargs_handling(monkeypatch):
"""
Test that empty or None kwargs are handled gracefully.
"""
captured: dict[str, object] = {}
async def fake_acompletion(model, messages=None, **kwargs):
captured.update(kwargs)
msg = Message(role="assistant", content="test response")
choice = Choices(index=0, message=msg)
return ModelResponse(choices=[choice], usage=Usage(0, 0, 0))
monkeypatch.setattr(litellm, "acompletion", fake_acompletion)
# Test with None kwargs
settings_none = ModelSettings(temperature=0.5, extra_args=None)
model = LitellmModel(model="test-model")
await model.get_response(
system_instructions=None,
input="test input",
model_settings=settings_none,
tools=[],
output_schema=None,
handoffs=[],
tracing=ModelTracing.DISABLED,
previous_response_id=None,
)
# Should work without error and include regular parameters
assert captured["temperature"] == 0.5
# Test with empty dict
captured.clear()
settings_empty = ModelSettings(temperature=0.3, extra_args={})
await model.get_response(
system_instructions=None,
input="test input",
model_settings=settings_empty,
tools=[],
output_schema=None,
handoffs=[],
tracing=ModelTracing.DISABLED,
previous_response_id=None,
)
# Should work without error and include regular parameters
assert captured["temperature"] == 0.3
@pytest.mark.allow_call_model_methods
@pytest.mark.asyncio
async def test_reasoning_effort_falls_back_to_extra_args(monkeypatch):
"""
Ensure reasoning_effort from extra_args is promoted when reasoning settings are missing.
"""
captured: dict[str, object] = {}
async def fake_acompletion(model, messages=None, **kwargs):
captured.update(kwargs)
msg = Message(role="assistant", content="test response")
choice = Choices(index=0, message=msg)
return ModelResponse(choices=[choice], usage=Usage(0, 0, 0))
monkeypatch.setattr(litellm, "acompletion", fake_acompletion)
# GitHub issue context: https://github.com/openai/openai-agents-python/issues/1764.
settings = ModelSettings(
extra_args={"reasoning_effort": "none", "custom_param": "custom_value"}
)
model = LitellmModel(model="test-model")
await model.get_response(
system_instructions=None,
input="test input",
model_settings=settings,
tools=[],
output_schema=None,
handoffs=[],
tracing=ModelTracing.DISABLED,
previous_response_id=None,
)
assert captured["reasoning_effort"] == "none"
assert captured["custom_param"] == "custom_value"
assert settings.extra_args == {"reasoning_effort": "none", "custom_param": "custom_value"}