Sub topic 1
How Speech Fits Around an LLM
Prerequisites
Assume you know basic Python, functions, file handling, environment variables, and how to call a chat API. You should also recognize common audio files such as MP3 and WAV.
How Speech Fits Around an LLM
A voice application commonly uses three stages:
- Speech-to-text (STT) converts an audio recording into text.
- An LLM reads that text, remembers the conversation, and produces a response.
- Text-to-speech (TTS) converts the response back into audio.
Each stage can add latency. The recording must upload before STT can begin. The LLM then generates text, and TTS must generate enough audio for playback. A cascaded pipeline is easy to inspect because each result is separate, but it usually waits between stages.
Audio Basics You Need Before Calling an API
A sample rate is the number of audio measurements captured per second. A channel is one recording stream, such as left or right stereo audio. A codec compresses or encodes audio, while a container packages the encoded data and metadata. MP3 is a compressed format; WAV often stores uncompressed PCM data.
For transcription, clear speech matters more than high fidelity. Check the provider's supported formats and file-size limit. Convert unusual input formats before uploading, and preserve the original if you may need to investigate recognition errors.
Transcribing Audio with an API Call
The SDK reads the API key from OPENAI_API_KEY. The file is sent as multipart data to the transcription endpoint.
from openai import OpenAI
client = OpenAI()
with open("meeting.mp3", "rb") as audio_file:
result = client.audio.transcriptions.create(
model="whisper-1",
file=audio_file,
)
print(result.text)Tuning Transcription
Set language when you know it. This can improve recognition and reduce language detection work. Use prompt for names, product terms, or acronyms. Request verbose_json and segment timestamps when you need to align text with playback.
with open("meeting.mp3", "rb") as audio_file:
result = client.audio.transcriptions.create(
model="whisper-1",
file=audio_file,
language="en",
prompt="Acme, Kubernetes, OAuth",
response_format="verbose_json",
timestamp_granularities=["segment"],
)
for segment in result.segments:
print(f"{segment.start:.1f}-{segment.end:.1f}: {segment.text}")Exercise
Which option should you add when you need to display each transcript segment at its position in the recording?
Show answer
Request verbose_json and include timestamp_granularities=["segment"].