️ 教程 11:语音智能体 借助 OpenAI 智能体 SDK,掌握支持语音的 AI 智能体!本教程将演示如何利用语音转文本、文本转语音以及智能代理工作流,构建自然语音交互的对话式语音智能体。 您将学到的内容 语音管道架构:完整的语音 ↔ 文本 ↔ 语音工作流 静态语音处理:基于录制音频的回合制语音交互 流式语音处理:实时语音对话与直播音频 多语言支持:自动检测语言并实现智能体交接 语音优化工具:专为语音交互设计的工具 音频管理:录音、播放和流媒体音频实用工具 核心概念:语音智能体 语音智能体结合了 AI 语言模型与语音处理技术,打造自然的对话式界面。
借助 OpenAI 智能体 SDK,掌握支持语音的 AI 智能体!本教程将演示如何利用语音转文本、文本转语音以及智能代理工作流,构建自然语音交互的对话式语音智能体。
语音智能体结合了 AI 语言模型与语音处理技术,打造自然的对话式界面。不妨把语音智能体想象成一种你可以自然交谈的 AI 助手,它们具备以下功能:
┌─────────────────────────────────────────────────────────────┐ │ VOICE AGENT SYSTEM │ ├─────────────────────────────────────────────────────────────┤ │ │ │ USER SPEECH │ │ │ │ │ ▼ │ │ ┌─────────────┐ 1. SPEECH-TO-TEXT │ │ │ AUDIO │ ◦ Convert speech to text │ │ │ PIPELINE │ ◦ Handle multiple languages │ │ └─────────────┘ │ │ │ │ │ ▼ │ │ ┌─────────────┐ 2. AGENT PROCESSING │ │ │ AGENT │ ◦ Multi-agent workflows │ │ │ ECOSYSTEM │ ◦ Tool calling & handoffs │ │ └─────────────┘ ◦ Context management │ │ │ │ │ ▼ │ │ ┌─────────────┐ 3. TEXT-TO-SPEECH │ │ │ SPEECH │ ◦ Convert response to speech │ │ │ SYNTHESIS │ ◦ Natural voice output │ │ └─────────────┘ │ │ │ │ │ ▼ │ │ AI RESPONSE │ └─────────────────────────────────────────────────────────────┘
本教程将演示三种核心语音交互模式:
static/)streamed/)realtime/)11_voice/ ├── README.md # This file - voice agents overview ├── static/ # Static voice processing example │ ├── agent.py # Complete static voice agent │ ├── util.py # Audio recording and playback utilities │ ├── requirements.txt # Dependencies for static example │ ├── env.example # Environment variables │ └── README.md # Static voice documentation ├── streamed/ # Streaming voice processing example │ ├── agent.py # Real-time streaming voice agent │ ├── util.py # Streaming audio utilities │ ├── requirements.txt # Dependencies for streaming example │ ├── env.example # Environment variables │ └── README.md # Streaming voice documentation ├── realtime/ # Realtime voice processing example │ ├── agent.py # Basic realtime voice agent │ ├── requirements.txt # Dependencies for realtime example │ ├── env.example # Environment variables │ └── README.md # Realtime voice documentation └── __init__.py # Module initialization
完成本教程后,您将理解:
安装支持语音的 OpenAI 智能体 SDK:
pip install 'openai-agents[voice]'
安装音频依赖库:
pip install sounddevice numpy soundfile librosa
设置环境变量:
cp static/env.example static/.env cp streamed/env.example streamed/.env # Edit .env files and add your OpenAI API key
选项 1:静态语音(推荐给初学者)
cd static/ python agent.py
选项 2:流式语音(进阶版)
cd streamed/ python agent.py
选项 3:实时语音(超低延迟)
cd realtime/ python agent.py
两个示例均包含:
get_weather(city): Weather information with voice-friendly responsesget_time(): Current time with natural speech outputcalculate_tip(bill, percentage): Tip calculations for voice queriesset_reminder(message, minutes): Voice-activated reminders (streaming only)get_news_summary():适合语音的新闻更新(仅限流式)from agents.voice import VoicePipeline, SingleAgentVoiceWorkflow pipeline = VoicePipeline( workflow=SingleAgentVoiceWorkflow(agent) )
from agents.voice import AudioInput audio_buffer = record_audio(duration=5.0) audio_input = AudioInput(buffer=audio_buffer) result = await pipeline.run(audio_input)
from agents.voice import StreamedAudioInput streamed_input = StreamedAudioInput() result = await pipeline.run(streamed_input) # Push audio chunks in real-time streamed_input.push_audio(audio_chunk)
spanish_agent = Agent( name="Spanish", handoff_description="A spanish speaking agent.", instructions="Speak in Spanish only..." ) main_agent = Agent( name="Assistant", handoffs=[spanish_agent, french_agent], instructions="If user speaks Spanish, handoff to Spanish agent..." )
| 特性 | 静态语音 | 流式语音 |
|---|---|---|
| 处理方式 | 回合制 | 实时 |
| 复杂度 | 更简单 | 更复杂 |
| 延迟 | 更高 | 更低 |
| 适用场景 | 命令、查询 | 对话 |
| 活动检测 | 手动 | 自动 |
| 资源占用 | 更低 | 更高 |
| 用户体验 | 结构化 | 自然 |
openai-agents[voice]: Voice-enabled Agents SDKsounddevice: Real-time audio I/Onumpy: Audio data processingsoundfile: Audio file operations (optional)librosa:音频重采样(可选)掌握语音智能体后:
免责声明:
本文档采用基于机器的 AI 翻译服务进行翻译。尽管我们力求准确,但请注意,自动翻译可能存在错误或不准确之处。应以原文(母语)文档作为权威依据。如需获取关键信息,建议使用专业的人工翻译。对于因使用本翻译而产生的任何误解或误读,我们概不负责。