-
-
Notifications
You must be signed in to change notification settings - Fork 957
Expand file tree
/
Copy pathtest_cli_openai_models.py
More file actions
554 lines (519 loc) · 17.2 KB
/
Copy pathtest_cli_openai_models.py
File metadata and controls
554 lines (519 loc) · 17.2 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
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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
from click.testing import CliRunner
import json
import llm
from llm.cli import cli
from llm.default_plugins.openai_models import combine_chunks
from openai.types.chat.chat_completion_chunk import ChatCompletionChunk
import pytest
import sqlite_utils
@pytest.fixture
def mocked_models(httpx_mock):
httpx_mock.add_response(
method="GET",
url="https://api.openai.com/v1/models",
json={
"data": [
{
"id": "ada:2020-05-03",
"object": "model",
"created": 1588537600,
"owned_by": "openai",
},
{
"id": "babbage:2020-05-03",
"object": "model",
"created": 1588537600,
"owned_by": "openai",
},
]
},
headers={"Content-Type": "application/json"},
)
return httpx_mock
def test_openai_models(mocked_models):
runner = CliRunner()
result = runner.invoke(cli, ["openai", "models", "--key", "x"])
assert result.exit_code == 0
assert result.output == (
"id owned_by created \n"
"ada:2020-05-03 openai 2020-05-03T20:26:40+00:00\n"
"babbage:2020-05-03 openai 2020-05-03T20:26:40+00:00\n"
)
def test_combine_chunks_preserves_streaming_timings():
chunks = [
ChatCompletionChunk.model_validate(
{
"id": "chatcmpl-llamacpp",
"object": "chat.completion.chunk",
"created": 1779911036,
"model": "qwen36",
"choices": [
{
"index": 0,
"delta": {"role": "assistant", "content": "Hi"},
"finish_reason": None,
}
],
}
),
ChatCompletionChunk.model_validate(
{
"id": "chatcmpl-llamacpp",
"object": "chat.completion.chunk",
"created": 1779911036,
"model": "qwen36",
"choices": [],
"usage": {
"prompt_tokens": 22,
"completion_tokens": 7,
"total_tokens": 29,
},
"timings": {
"prompt_per_second": 9.953251387913042,
"predicted_per_second": 6.667936749857116,
},
}
),
]
combined = combine_chunks(chunks)
assert combined["content"] == "Hi"
assert combined["usage"]["prompt_tokens"] == 22
assert combined["usage"]["completion_tokens"] == 7
assert combined["timings"]["prompt_per_second"] == 9.953251387913042
assert combined["timings"]["predicted_per_second"] == 6.667936749857116
def test_openai_options_min_max():
options = {
"temperature": [0, 2],
"top_p": [0, 1],
"frequency_penalty": [-2, 2],
"presence_penalty": [-2, 2],
}
runner = CliRunner()
for option, [min_val, max_val] in options.items():
result = runner.invoke(cli, ["-m", "chatgpt", "-o", option, "-10"])
assert result.exit_code == 1
assert f"greater than or equal to {min_val}" in result.output
result2 = runner.invoke(cli, ["-m", "chatgpt", "-o", option, "10"])
assert result2.exit_code == 1
assert f"less than or equal to {max_val}" in result2.output
@pytest.mark.parametrize(
"model_id",
(
"gpt-5",
"gpt-5-mini",
"gpt-5.1",
"gpt-5.2",
"gpt-5.4",
"gpt-5.5",
),
)
def test_gpt5_models_support_verbosity_option(model_id):
assert "verbosity" in llm.get_model(model_id).Options.model_fields
assert "verbosity" in llm.get_async_model(model_id).Options.model_fields
@pytest.mark.parametrize("model_id", ("gpt-4o", "gpt-4.5-preview", "o3", "o4-mini"))
def test_non_gpt5_openai_chat_models_do_not_support_verbosity_option(model_id):
assert "verbosity" not in llm.get_model(model_id).Options.model_fields
assert "verbosity" not in llm.get_async_model(model_id).Options.model_fields
def test_gpt5_verbosity_option_is_sent_to_openai_chat_completions(httpx_mock):
httpx_mock.add_response(
method="POST",
url="https://api.openai.com/v1/chat/completions",
json={
"model": "gpt-5",
"usage": {},
"choices": [{"message": {"content": "Verbose enough"}}],
},
headers={"Content-Type": "application/json"},
)
runner = CliRunner()
result = runner.invoke(
cli,
[
"-m",
"gpt-5",
"-o",
"chat_completions",
"1",
"-o",
"verbosity",
"high",
"--no-stream",
"--key",
"x",
"Say hi",
],
catch_exceptions=False,
)
assert result.exit_code == 0
request_body = json.loads(httpx_mock.get_requests()[-1].content)
assert request_body["verbosity"] == "high"
assert "text" not in request_body
def test_gpt5_verbosity_option_is_sent_to_openai_responses_by_default(httpx_mock):
httpx_mock.add_response(
method="POST",
url="https://api.openai.com/v1/responses",
json={
"id": "resp_test_1",
"object": "response",
"created_at": 1,
"model": "gpt-5",
"output": [
{
"type": "message",
"id": "msg_1",
"role": "assistant",
"status": "completed",
"content": [
{
"type": "output_text",
"text": "Verbose enough",
"annotations": [],
}
],
}
],
"usage": {
"input_tokens": 5,
"output_tokens": 3,
"total_tokens": 8,
},
"status": "completed",
},
headers={"Content-Type": "application/json"},
)
runner = CliRunner()
result = runner.invoke(
cli,
[
"-m",
"gpt-5",
"-o",
"verbosity",
"high",
"--no-stream",
"--key",
"x",
"Say hi",
],
catch_exceptions=False,
)
assert result.exit_code == 0
request_body = json.loads(httpx_mock.get_requests()[-1].content)
assert request_body["text"]["verbosity"] == "high"
assert request_body["include"] == ["reasoning.encrypted_content"]
assert "verbosity" not in request_body
def test_gpt5_verbosity_option_validates_allowed_values():
runner = CliRunner()
result = runner.invoke(
cli,
["-m", "gpt-5", "-o", "verbosity", "extreme", "Say hi"],
)
assert result.exit_code == 1
assert "Input should be 'low', 'medium' or 'high'" in result.output
@pytest.mark.parametrize(
"model_id,expected_description",
(
(
"gpt-4o",
"Controls the detail level for image attachments. Supported values are low, high, and auto.",
),
(
"gpt-5.4",
"Controls the detail level for image attachments. Supported values are low, high, original, and auto.",
),
(
"gpt-5.5",
"Controls the detail level for image attachments. Supported values are low, high, original, and auto.",
),
),
)
def test_openai_image_detail_option_description(model_id, expected_description):
field = llm.get_model(model_id).Options.model_fields["image_detail"]
assert field.description == expected_description
def test_openai_image_detail_option_is_sent_on_image_attachments(httpx_mock):
httpx_mock.add_response(
method="POST",
url="https://api.openai.com/v1/chat/completions",
json={
"model": "gpt-4o",
"usage": {},
"choices": [{"message": {"content": "Looks detailed"}}],
},
headers={"Content-Type": "application/json"},
)
runner = CliRunner()
result = runner.invoke(
cli,
[
"-m",
"gpt-4o",
"-o",
"image_detail",
"high",
"--at",
"https://example.com/image.jpg",
"image/jpeg",
"--no-stream",
"--key",
"x",
"Describe this",
],
catch_exceptions=False,
)
assert result.exit_code == 0
request_body = json.loads(httpx_mock.get_requests()[-1].content)
image_part = request_body["messages"][0]["content"][1]
assert image_part == {
"type": "image_url",
"image_url": {
"url": "https://example.com/image.jpg",
"detail": "high",
},
}
assert "image_detail" not in request_body
def test_openai_image_detail_original_is_sent_for_gpt54(httpx_mock):
httpx_mock.add_response(
method="POST",
url="https://api.openai.com/v1/chat/completions",
json={
"model": "gpt-5.4",
"usage": {},
"choices": [{"message": {"content": "Original detail"}}],
},
headers={"Content-Type": "application/json"},
)
runner = CliRunner()
result = runner.invoke(
cli,
[
"-m",
"gpt-5.4",
"-o",
"chat_completions",
"1",
"-o",
"image_detail",
"original",
"--at",
"https://example.com/image.jpg",
"image/jpeg",
"--no-stream",
"--key",
"x",
"Describe this",
],
catch_exceptions=False,
)
assert result.exit_code == 0
request_body = json.loads(httpx_mock.get_requests()[-1].content)
image_part = request_body["messages"][0]["content"][1]
assert image_part["image_url"]["detail"] == "original"
def test_openai_image_detail_original_is_sent_for_gpt54_responses_by_default(
httpx_mock,
):
httpx_mock.add_response(
method="POST",
url="https://api.openai.com/v1/responses",
json={
"id": "resp_test_1",
"object": "response",
"created_at": 1,
"model": "gpt-5.4",
"output": [
{
"type": "message",
"id": "msg_1",
"role": "assistant",
"status": "completed",
"content": [
{
"type": "output_text",
"text": "Original detail",
"annotations": [],
}
],
}
],
"usage": {
"input_tokens": 5,
"output_tokens": 3,
"total_tokens": 8,
},
"status": "completed",
},
headers={"Content-Type": "application/json"},
)
runner = CliRunner()
result = runner.invoke(
cli,
[
"-m",
"gpt-5.4",
"-o",
"image_detail",
"original",
"--at",
"https://example.com/image.jpg",
"image/jpeg",
"--no-stream",
"--key",
"x",
"Describe this",
],
catch_exceptions=False,
)
assert result.exit_code == 0
request_body = json.loads(httpx_mock.get_requests()[-1].content)
image_part = request_body["input"][0]["content"][1]
assert image_part == {
"type": "input_image",
"image_url": "https://example.com/image.jpg",
"detail": "original",
}
assert "image_detail" not in request_body
def test_openai_image_detail_original_is_rejected_for_other_models():
runner = CliRunner()
result = runner.invoke(
cli,
["-m", "gpt-5", "-o", "image_detail", "original", "Say hi"],
)
assert result.exit_code == 1
assert "Input should be 'low', 'high' or 'auto'" in result.output
@pytest.mark.parametrize("model", ("gpt-4o-mini", "gpt-4o-audio-preview"))
@pytest.mark.parametrize("filetype", ("mp3", "wav"))
def test_only_gpt4_audio_preview_allows_mp3_or_wav(httpx_mock, model, filetype):
httpx_mock.add_response(
method="HEAD",
url=f"https://www.example.com/example.{filetype}",
content=b"binary-data",
headers={"Content-Type": "audio/mpeg" if filetype == "mp3" else "audio/wav"},
)
if model == "gpt-4o-audio-preview":
httpx_mock.add_response(
method="POST",
# chat completion request
url="https://api.openai.com/v1/chat/completions",
json={
"id": "chatcmpl-AQT9a30kxEaM1bqxRPepQsPlCyGJh",
"object": "chat.completion",
"created": 1730871958,
"model": "gpt-4o-audio-preview-2024-10-01",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "Why did the pelican get kicked out of the restaurant?\n\nBecause he had a big bill and no way to pay it!",
"refusal": None,
},
"finish_reason": "stop",
}
],
"usage": {
"prompt_tokens": 55,
"completion_tokens": 25,
"total_tokens": 80,
"prompt_tokens_details": {
"cached_tokens": 0,
"audio_tokens": 44,
"text_tokens": 11,
"image_tokens": 0,
},
"completion_tokens_details": {
"reasoning_tokens": 0,
"audio_tokens": 0,
"text_tokens": 25,
"accepted_prediction_tokens": 0,
"rejected_prediction_tokens": 0,
},
},
"system_fingerprint": "fp_49254d0e9b",
},
headers={"Content-Type": "application/json"},
)
httpx_mock.add_response(
method="GET",
url=f"https://www.example.com/example.{filetype}",
content=b"binary-data",
headers={
"Content-Type": "audio/mpeg" if filetype == "mp3" else "audio/wav"
},
)
runner = CliRunner()
result = runner.invoke(
cli,
[
"-m",
model,
"-a",
f"https://www.example.com/example.{filetype}",
"--no-stream",
"--key",
"x",
],
)
if model == "gpt-4o-audio-preview":
assert result.exit_code == 0
assert result.output == (
"Why did the pelican get kicked out of the restaurant?\n\n"
"Because he had a big bill and no way to pay it!\n"
)
else:
assert result.exit_code == 1
long = "audio/mpeg" if filetype == "mp3" else "audio/wav"
assert (
f"This model does not support attachments of type '{long}'" in result.output
)
@pytest.mark.parametrize("async_", (False, True))
@pytest.mark.parametrize("usage", (None, "-u", "--usage"))
def test_gpt4o_mini_sync_and_async(monkeypatch, tmpdir, httpx_mock, async_, usage):
user_path = tmpdir / "user_dir"
log_db = user_path / "logs.db"
monkeypatch.setenv("LLM_USER_PATH", str(user_path))
assert not log_db.exists()
httpx_mock.add_response(
method="POST",
# chat completion request
url="https://api.openai.com/v1/chat/completions",
json={
"id": "chatcmpl-AQT9a30kxEaM1bqxRPepQsPlCyGJh",
"object": "chat.completion",
"created": 1730871958,
"model": "gpt-4o-mini",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "Ho ho ho",
"refusal": None,
},
"finish_reason": "stop",
}
],
"usage": {
"prompt_tokens": 1000,
"completion_tokens": 2000,
"total_tokens": 12,
},
"system_fingerprint": "fp_49254d0e9b",
},
headers={"Content-Type": "application/json"},
)
runner = CliRunner()
args = ["-m", "gpt-4o-mini", "--key", "x", "--no-stream"]
if usage:
args.append(usage)
if async_:
args.append("--async")
result = runner.invoke(cli, args, catch_exceptions=False)
assert result.exit_code == 0
assert result.stdout == "Ho ho ho\n"
if usage:
assert result.stderr == "Token usage: 1,000 input, 2,000 output\n"
# Confirm it was correctly logged
assert log_db.exists()
db = sqlite_utils.Database(str(log_db))
assert db["responses"].count == 1
row = next(db["responses"].rows)
assert row["response"] == "Ho ho ho"