OpenAI TTS
The OpenAITTSService provides high-quality, natural-sounding voice synthesis using OpenAI's state-of-the-art TTS models. It is seamlessly integrated with the OpenAI ecosystem, making it a perfect choice for agents already leveraging GPT models.
Installation
To use OpenAI TTS, install the required dependencies:
pip install "piopiy-ai[openai]"
Prerequisites
- An OpenAI account and API key (Get yours here).
- Set your API key in your environment:
export OPENAI_API_KEY="your_api_key_here"
Configuration
OpenAITTSService Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
api_key | str | None | Your OpenAI API key (defaults to env var). |
base_url | str | None | Custom base URL for OpenAI API. |
voice | str | "alloy" | Voice ID (alloy, echo, fable, onyx, nova, shimmer). |
model | str | "gpt-4o-mini-tts" | TTS model ID. |
sample_rate | int | None | Output audio sample rate (OpenAI defaults to 24kHz). |
params | InputParams | None | Advanced synthesis controls. |
InputParams
| Parameter | Type | Default | Description |
|---|---|---|---|
instructions | str | None | Instructions to guide voice synthesis behavior. |
speed | float | None | Voice speed control (0.25 to 4.0). |
Usage
Basic Setup
import os
from piopiy.services.openai.tts import OpenAITTSService
tts = OpenAITTSService(
api_key=os.getenv("OPENAI_API_KEY"),
voice="nova",
model="gpt-4o-mini-tts"
)
With Speed Control
from piopiy.services.openai.tts import OpenAITTSService
tts = OpenAITTSService(
voice="shimmer",
params=OpenAITTSService.InputParams(
speed=1.1
)
)
Notes
- Sample Rate: OpenAI TTS always outputs at 24kHz. Piopiy handles any necessary resampling for your specific transport.
- Latency: The
gpt-4o-mini-ttsmodel is highly optimized for performance, providing sub-second TTFB for real-time conversations. - Streaming: This service uses OpenAI's streaming response to provide audio units as they are generated.