- Notifications
You must be signed in to change notification settings - Fork 2.8k
/
Copy pathtest_openai.py
473 lines (395 loc) · 15 KB
/
test_openai.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
importjson
importos
importsys
fromdatetimeimportdatetime
fromunittest.mockimportAsyncMock, patch
fromtypingimportOptional
sys.path.insert(
0, os.path.abspath("../..")
) # Adds the parent directory to the system path
importhttpx
importpytest
fromrespximportMockRouter
importlitellm
fromlitellmimportChoices, Message, ModelResponse
frombase_llm_unit_testsimportBaseLLMChatTest
importasyncio
fromlitellm.types.llms.openaiimport (
ChatCompletionAnnotation,
ChatCompletionAnnotationURLCitation,
)
frombase_audio_transcription_unit_testsimportBaseLLMAudioTranscriptionTest
deftest_openai_prediction_param():
litellm.set_verbose=True
code="""
/// <summary>
/// Represents a user with a first name, last name, and username.
/// </summary>
public class User
{
/// <summary>
/// Gets or sets the user's first name.
/// </summary>
public string FirstName { get; set; }
/// <summary>
/// Gets or sets the user's last name.
/// </summary>
public string LastName { get; set; }
/// <summary>
/// Gets or sets the user's username.
/// </summary>
public string Username { get; set; }
}
"""
completion=litellm.completion(
model="gpt-4o-mini",
messages=[
{
"role": "user",
"content": "Replace the Username property with an Email property. Respond only with code, and with no markdown formatting.",
},
{"role": "user", "content": code},
],
prediction={"type": "content", "content": code},
)
print(completion)
assert (
completion.usage.completion_tokens_details.accepted_prediction_tokens>0
orcompletion.usage.completion_tokens_details.rejected_prediction_tokens>0
)
@pytest.mark.asyncio
asyncdeftest_openai_prediction_param_mock():
"""
Tests that prediction parameter is correctly passed to the API
"""
litellm.set_verbose=True
code="""
/// <summary>
/// Represents a user with a first name, last name, and username.
/// </summary>
public class User
{
/// <summary>
/// Gets or sets the user's first name.
/// </summary>
public string FirstName { get; set; }
/// <summary>
/// Gets or sets the user's last name.
/// </summary>
public string LastName { get; set; }
/// <summary>
/// Gets or sets the user's username.
/// </summary>
public string Username { get; set; }
}
"""
fromopenaiimportAsyncOpenAI
client=AsyncOpenAI(api_key="fake-api-key")
withpatch.object(
client.chat.completions.with_raw_response, "create"
) asmock_client:
try:
awaitlitellm.acompletion(
model="gpt-4o-mini",
messages=[
{
"role": "user",
"content": "Replace the Username property with an Email property. Respond only with code, and with no markdown formatting.",
},
{"role": "user", "content": code},
],
prediction={"type": "content", "content": code},
client=client,
)
exceptExceptionase:
print(f"Error: {e}")
mock_client.assert_called_once()
request_body=mock_client.call_args.kwargs
# Verify the request contains the prediction parameter
assert"prediction"inrequest_body
# verify prediction is correctly sent to the API
assertrequest_body["prediction"] == {"type": "content", "content": code}
@pytest.mark.asyncio
asyncdeftest_openai_prediction_param_with_caching():
"""
Tests using `prediction` parameter with caching
"""
fromlitellm.caching.cachingimportLiteLLMCacheType
importlogging
fromlitellm._loggingimportverbose_logger
verbose_logger.setLevel(logging.DEBUG)
importtime
litellm.set_verbose=True
litellm.cache=litellm.Cache(type=LiteLLMCacheType.LOCAL)
code="""
/// <summary>
/// Represents a user with a first name, last name, and username.
/// </summary>
public class User
{
/// <summary>
/// Gets or sets the user's first name.
/// </summary>
public string FirstName { get; set; }
/// <summary>
/// Gets or sets the user's last name.
/// </summary>
public string LastName { get; set; }
/// <summary>
/// Gets or sets the user's username.
/// </summary>
public string Username { get; set; }
}
"""
completion_response_1=litellm.completion(
model="gpt-4o-mini",
messages=[
{
"role": "user",
"content": "Replace the Username property with an Email property. Respond only with code, and with no markdown formatting.",
},
{"role": "user", "content": code},
],
prediction={"type": "content", "content": code},
)
time.sleep(0.5)
# cache hit
completion_response_2=litellm.completion(
model="gpt-4o-mini",
messages=[
{
"role": "user",
"content": "Replace the Username property with an Email property. Respond only with code, and with no markdown formatting.",
},
{"role": "user", "content": code},
],
prediction={"type": "content", "content": code},
)
assertcompletion_response_1.id==completion_response_2.id
completion_response_3=litellm.completion(
model="gpt-4o-mini",
messages=[
{"role": "user", "content": "What is the first name of the user?"},
],
prediction={"type": "content", "content": code+"FirstName"},
)
assertcompletion_response_3.id!=completion_response_1.id
@pytest.mark.asyncio()
asyncdeftest_vision_with_custom_model():
"""
Tests that an OpenAI compatible endpoint when sent an image will receive the image in the request
"""
importbase64
importrequests
fromopenaiimportAsyncOpenAI
client=AsyncOpenAI(api_key="fake-api-key")
litellm.set_verbose=True
api_base="https://my-custom.api.openai.com"
# Fetch and encode a test image
url="https://dummyimage.com/100/100/fff&text=Test+image"
response=requests.get(url)
file_data=response.content
encoded_file=base64.b64encode(file_data).decode("utf-8")
base64_image=f"data:image/png;base64,{encoded_file}"
withpatch.object(
client.chat.completions.with_raw_response, "create"
) asmock_client:
try:
response=awaitlitellm.acompletion(
model="openai/my-custom-model",
max_tokens=10,
api_base=api_base, # use the mock api
messages=[
{
"role": "user",
"content": [
{"type": "text", "text": "What's in this image?"},
{
"type": "image_url",
"image_url": {"url": base64_image},
},
],
}
],
client=client,
)
exceptExceptionase:
print(f"Error: {e}")
mock_client.assert_called_once()
request_body=mock_client.call_args.kwargs
print("request_body: ", request_body)
assertrequest_body["messages"] == [
{
"role": "user",
"content": [
{"type": "text", "text": "What's in this image?"},
{
"type": "image_url",
"image_url": {
"url": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAGQAAABkBAMAAACCzIhnAAAAG1BMVEURAAD///+ln5/h39/Dv79qX18uHx+If39MPz9oMSdmAAAACXBIWXMAAA7EAAAOxAGVKw4bAAABDElEQVRYhe2SzWqEMBRGPyQTfQxJsc5jBKGzFmlslyFIZxsCQ7sUaWd87EanpdpIrbtC71mE/NyTm9wEIAiCIAiC+N/otQBxU2Sf/aeh4enqptHXri+/yxIq63jlKCw6cXssnr3ObdzdGYFYCJ2IzHKXLygHXCB98Gm4DE+ZZemu5EisQSyZTmyg+AuzQbkezCuIy7EI0k9Ig3FtruwydY+qniqtV5yQyo8qpUIl2fc90KVzJWohWf2qu75vlw52rdfjVDHg8vLWwixW7PChqLkSyUadwfSS0uQZhEvRuIkS53uJvrK8cGWYaPwpGt8efvw+vlo8TPMzcmP8w7lrNypc1RsNgiAIgiD+Iu/RyDYhCaWrgQAAAABJRU5ErkJggg=="
},
},
],
},
]
assertrequest_body["model"] =="my-custom-model"
assertrequest_body["max_tokens"] ==10
classTestOpenAIChatCompletion(BaseLLMChatTest):
defget_base_completion_call_args(self) ->dict:
return {"model": "gpt-4o-mini"}
deftest_tool_call_no_arguments(self, tool_call_no_arguments):
"""Test that tool calls with no arguments is translated correctly. Relevant issue: https://github.com/BerriAI/litellm/issues/6833"""
pass
deftest_prompt_caching(self):
"""
Test that prompt caching works correctly.
Skip for now, as it's working locally but not in CI
"""
pass
deftest_multilingual_requests(self):
"""
Tests that the provider can handle multilingual requests and invalid utf-8 sequences
Context: https://github.com/openai/openai-python/issues/1921
"""
base_completion_call_args=self.get_base_completion_call_args()
try:
response=self.completion_function(
**base_completion_call_args,
messages=[{"role": "user", "content": "你好世界!\ud83e, ö"}],
)
assertresponseisnotNone
exceptlitellm.InternalServerError:
pytest.skip("Skipping test due to InternalServerError")
deftest_prompt_caching(self):
"""
Works locally but CI/CD is failing this test. Temporary skip to push out a new release.
"""
pass
deftest_completion_bad_org():
importlitellm
litellm.set_verbose=True
_old_org=os.environ.get("OPENAI_ORGANIZATION", None)
os.environ["OPENAI_ORGANIZATION"] ="bad-org"
messages= [{"role": "user", "content": "hi"}]
withpytest.raises(Exception) asexc_info:
comp=litellm.completion(
model="gpt-4o-mini", messages=messages, organization="bad-org"
)
print(exc_info.value)
assert"header should match organization for API key"instr(exc_info.value)
if_old_orgisnotNone:
os.environ["OPENAI_ORGANIZATION"] =_old_org
else:
delos.environ["OPENAI_ORGANIZATION"]
@patch("litellm.main.openai_chat_completions._get_openai_client")
deftest_openai_max_retries_0(mock_get_openai_client):
importlitellm
litellm.set_verbose=True
response=litellm.completion(
model="gpt-4o-mini",
messages=[{"role": "user", "content": "hi"}],
max_retries=0,
)
mock_get_openai_client.assert_called_once()
assertmock_get_openai_client.call_args.kwargs["max_retries"] ==0
@pytest.mark.parametrize("model", ["o1", "o1-preview", "o1-mini", "o3-mini"])
deftest_o1_parallel_tool_calls(model):
litellm.completion(
model=model,
messages=[
{
"role": "user",
"content": "foo",
}
],
parallel_tool_calls=True,
drop_params=True,
)
deftest_openai_chat_completion_streaming_handler_reasoning_content():
fromlitellm.llms.openai.chat.gpt_transformationimport (
OpenAIChatCompletionStreamingHandler,
)
fromunittest.mockimportMagicMock
streaming_handler=OpenAIChatCompletionStreamingHandler(
streaming_response=MagicMock(),
sync_stream=True,
)
response=streaming_handler.chunk_parser(
chunk={
"id": "e89b6501-8ac2-464c-9550-7cd3daf94350",
"object": "chat.completion.chunk",
"created": 1741037890,
"model": "deepseek-reasoner",
"system_fingerprint": "fp_5417b77867_prod0225",
"choices": [
{
"index": 0,
"delta": {"content": None, "reasoning_content": "."},
"logprobs": None,
"finish_reason": None,
}
],
}
)
assertresponse.choices[0].delta.reasoning_content=="."
defvalidate_response_url_citation(url_citation: ChatCompletionAnnotationURLCitation):
assert"end_index"inurl_citation
assert"start_index"inurl_citation
assert"url"inurl_citation
defvalidate_web_search_annotations(annotations: ChatCompletionAnnotation):
"""validates litellm response contains web search annotations"""
print("annotations: ", annotations)
assertannotationsisnotNone
assertisinstance(annotations, list)
forannotationinannotations:
assertannotation["type"] =="url_citation"
url_citation: ChatCompletionAnnotationURLCitation=annotation["url_citation"]
validate_response_url_citation(url_citation)
@pytest.mark.flaky(reruns=3)
deftest_openai_web_search():
"""Makes a simple web search request and validates the response contains web search annotations and all expected fields are present"""
litellm._turn_on_debug()
response=litellm.completion(
model="openai/gpt-4o-search-preview",
messages=[
{
"role": "user",
"content": "What was a positive news story from today?",
}
],
)
print("litellm response: ", response.model_dump_json(indent=4))
message=response.choices[0].message
annotations: ChatCompletionAnnotation=message.annotations
validate_web_search_annotations(annotations)
deftest_openai_web_search_streaming():
"""Makes a simple web search request and validates the response contains web search annotations and all expected fields are present"""
# litellm._turn_on_debug()
test_openai_web_search: Optional[ChatCompletionAnnotation] =None
response=litellm.completion(
model="openai/gpt-4o-search-preview",
messages=[
{
"role": "user",
"content": "What was a positive news story from today?",
}
],
stream=True,
)
forchunkinresponse:
print("litellm response chunk: ", chunk)
if (
hasattr(chunk.choices[0].delta, "annotations")
andchunk.choices[0].delta.annotationsisnotNone
):
test_openai_web_search=chunk.choices[0].delta.annotations
# Assert this request has at-least one web search annotation
asserttest_openai_web_searchisnotNone
validate_web_search_annotations(test_openai_web_search)
classTestOpenAIGPT4OAudioTranscription(BaseLLMAudioTranscriptionTest):
defget_base_audio_transcription_call_args(self) ->dict:
return {
"model": "openai/gpt-4o-transcribe",
}
defget_custom_llm_provider(self) ->litellm.LlmProviders:
returnlitellm.LlmProviders.OPENAI