Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for OpenAI optional parameters #802

Merged
merged 3 commits into from
Dec 7, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 51 additions & 2 deletions tests/recognizers/test_openai.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
from unittest.mock import MagicMock

import httpx
import pytest
import respx

from speech_recognition import AudioData, Recognizer
from speech_recognition.recognizers import openai


@respx.mock(assert_all_called=True, assert_all_mocked=True)
def test_transcribe_with_openai_whisper(respx_mock, monkeypatch):
@pytest.fixture
def setenv_openai_api_key(monkeypatch):
monkeypatch.setenv("OPENAI_API_KEY", "sk_openai_api_key")


@respx.mock(assert_all_called=True, assert_all_mocked=True)
def test_transcribe_with_openai_whisper(respx_mock, setenv_openai_api_key):
respx_mock.post(
"https://api.openai.com/v1/audio/transcriptions",
headers__contains={"Authorization": "Bearer sk_openai_api_key"},
Expand All @@ -29,3 +33,48 @@ def test_transcribe_with_openai_whisper(respx_mock, monkeypatch):

assert actual == "Transcription by OpenAI Whisper"
audio_data.get_wav_data.assert_called_once()


@respx.mock(assert_all_called=True, assert_all_mocked=True)
def test_transcribe_with_specified_language(respx_mock, setenv_openai_api_key):
# https://github.com/Uberi/speech_recognition/issues/681
respx_mock.post(
"https://api.openai.com/v1/audio/transcriptions",
data__contains={"language": "en"},
).respond(
200,
json={"text": "English transcription"},
)

audio_data = MagicMock(spec=AudioData)
audio_data.get_wav_data.return_value = b"english_audio"

actual = openai.recognize(
MagicMock(spec=Recognizer), audio_data, language="en"
)

assert actual == "English transcription"


@respx.mock(assert_all_called=True, assert_all_mocked=True)
def test_transcribe_with_specified_prompt(respx_mock, setenv_openai_api_key):
# https://github.com/Uberi/speech_recognition/pull/676
respx_mock.post(
"https://api.openai.com/v1/audio/transcriptions",
# ref: https://cookbook.openai.com/examples/whisper_prompting_guide
data__contains={"prompt": "Glossary: Aimee, Shawn, BBQ"},
).respond(
200,
json={"text": "Prompted transcription"},
)

audio_data = MagicMock(spec=AudioData)
audio_data.get_wav_data.return_value = b"audio_data"

actual = openai.recognize(
MagicMock(spec=Recognizer),
audio_data,
prompt="Glossary: Aimee, Shawn, BBQ",
)

assert actual == "Prompted transcription"
Loading