Assistant answers general questions and stays silent for overheard talk ("Ignored." is not said); stt prompt setting sent as the transcription prompt

This commit is contained in:
Melvin Strobl
2026-11-06 08:04:19 +01:00
committed by stroblme
parent 7ddf8aa8f9
commit c80aad7dfe
6 changed files with 93 additions and 24 deletions
+16 -8
View File
@@ -194,12 +194,19 @@ async def transcribe(
*,
model: str = "whisper-1",
timeout: float = 30.0,
prompt: str = "",
) -> str:
"""``POST {api_base}/audio/transcriptions``: a whole clip, and what it says."""
"""``POST {api_base}/audio/transcriptions``: a whole clip, and what it says.
``prompt`` is words the clip is likely to hold, such as room names.
"""
data = {"model": model, "response_format": "json"}
if prompt:
data["prompt"] = prompt
response = await _client(api_base).post(
api_base.rstrip("/") + "/audio/transcriptions",
files={"file": (filename, audio, media_type)},
data={"model": model, "response_format": "json"},
data=data,
timeout=timeout,
)
if response.is_error:
@@ -236,25 +243,26 @@ async def transcribe_stream(
*,
rate: int = 16000,
idle_s: float = 10.0,
prompt: str = "",
) -> AsyncIterator[tuple[bool, str]]:
"""``WS {api_base}/realtime``: one utterance, transcribed while it is spoken.
Sends each chunk of PCM16 taken off ``audio`` and commits at ``None``.
Yields ``(False, delta)`` as words are agreed on, then ``(True,
transcript)`` once. A chunk that takes longer than ``idle_s`` to come
raises ``TimeoutError``, so an utterance nobody commits ends.
raises ``TimeoutError``, so an utterance nobody commits ends. ``prompt``
is words the speech is likely to hold, as for ``transcribe``.
"""
url = "ws" + api_base.rstrip("/").removeprefix("http") + "/realtime"
async with connect(url, open_timeout=idle_s) as ws:
update = {"type": "audio/pcm", "rate": rate}
given: dict[str, Any] = {"format": {"type": "audio/pcm", "rate": rate}}
if prompt:
given["transcription"] = {"prompt": prompt}
await ws.send(
json.dumps(
{
"type": "session.update",
"session": {
"type": "transcription",
"audio": {"input": {"format": update}},
},
"session": {"type": "transcription", "audio": {"input": given}},
}
)
)