Test to exercise gpt-4o-audio-preview, closes #608

This commit is contained in:
Simon Willison 2024-11-05 21:50:00 -08:00
parent 0cc4072bcd
commit 1a60fa1667

View file

@ -56,3 +56,91 @@ def test_openai_options_min_max():
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", ("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/wave"},
)
# Another mock for the correct model
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/wave"
},
)
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/wave"
assert (
f"This model does not support attachments of type '{long}'" in result.output
)