Skip to content

Commit

Permalink
[test] **kwargs supports #676
Browse files Browse the repository at this point in the history
  • Loading branch information
ftnext committed Dec 7, 2024
1 parent 161385b commit cc00004
Showing 1 changed file with 26 additions and 2 deletions.
28 changes: 26 additions & 2 deletions tests/recognizers/test_openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
from speech_recognition import AudioData, Recognizer
from speech_recognition.recognizers import openai


@pytest.fixture
def setenv_openai_api_key(monkeypatch):
def test_transcribe_with_openai_whisper(respx_mock, monkeypatch):
monkeypatch.setenv("OPENAI_API_KEY", "sk_openai_api_key")


Expand All @@ -36,7 +36,7 @@ def test_transcribe_with_openai_whisper(respx_mock, setenv_openai_api_key):


@respx.mock(assert_all_called=True, assert_all_mocked=True)
def test_transcribe_with_specified_language(respx_mock, monkeypatch):
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",
Expand All @@ -54,3 +54,27 @@ def test_transcribe_with_specified_language(respx_mock, monkeypatch):
)

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"

0 comments on commit cc00004

Please sign in to comment.