
语音驱动的应用程序已从科幻概念转变为 OpenAI 可部署的解决方案's 最新工具。本指南将逐步讲解 使用开放的多语言语音代理AI 代理 SDK,展示如何创建跨语言处理语音的系统,同时保持类似人类的交互节奏。
什么是开放的AI 代理 SDK?
可选AI Agent SDK 为开发人员提供了构建 AI 能够处理和响应各种输入(包括语音)的代理。该 SDK 支持 GPT-4o-realtime-preview 模型,该模型能够 实时对话功能 通过其先进的 自然语言处理 (自然语言处理)功能。
SDK 特别包含 VoicePipeline,这是一个旨在无缝处理语音交互的组件。该管道管理将语音转换为文本、处理信息和生成自然响应的复杂过程。
现代语音代理的核心架构

1.语音处理管道
OpenAI's VoicePipeline 通过三个同步阶段运行:音频采集、语言处理和响应生成。系统首先使用 语音到文本 像 GPT-4o Transcribe 这样的模型。然后,这些文本输入会被输入到语言模型中,用于分析上下文、意图和情绪基调。最后,文本转语音组件会生成听起来自然的语音响应,同时保持对话流畅。
2. 多模式与链式架构
两种不同的方法主导着语音代理的开发:
多语言语音代理指南:从代码到对话
使用 Open 创建语音代理AI Agent SDK 需要特定的环境配置。请按照以下步骤建立具有语音功能的功能开发环境。
步骤 1. Python 和虚拟环境设置
确保已安装 Python 3.8+。使用以下命令进行验证:
python --version
对于新安装,请从以下位置下载 Python 蟒蛇网.
a. 创建虚拟环境
隔离依赖关系以避免冲突:
p-ython -m venv voice_agent_env
湾。 激活:
- Linux/macOS:
source voice_agent_env/bin/activate
- Windows:
voice_agent_env\Scripts\activate
安装语音特定的依赖项
安装开放AI 带有语音扩展和音频库的 Agents SDK:
pip install 'openai-agents[voice]' numpy sounddevice scipy python-dotenv
d. 配置打开AI API密钥:使用环境变量安全地存储您的 API 密钥:
- 创建一个
.env文件:
echo "OPENAI_API_KEY=your-api-key-here" > .env
- 克隆示例存储库(可选):
为了加快速度,你可以从 可选AI Agents SDK GitHub 存储库.
git clone https://github.com/openai/openai-agents-python.git
cd openai-agents-python/examples/voice/static
第 2 步:构建多语言代理
主要组成部分包括:
- 特定语言的代理 针对不同语言(西班牙语、印地语)
- 处理初始交互的主要代理
- 提供附加功能的功能工具(例如天气信息)
服务's 简化版的代码结构:
a. 定义你的代理
为您想要支持的每种语言创建不同的代理实例。例如,可以创建西班牙语代理和印地语代理,并使用各自语言的说明:
from agents import Agent
from agents.extensions.handoff_prompt import prompt_with_handoff_instructions
spanish_agent = Agent(
name="Spanish",
handoff_description="A Spanish speaking agent.",
instructions=prompt_with_handoff_instructions(
"You're speaking to a human, so be polite and concise. Speak in Spanish."
),
model="gpt-4o-mini",
)
hindi_agent = Agent(
name="Hindi",
handoff_description="A Hindi speaking agent.",
instructions=prompt_with_handoff_instructions(
"You're speaking to a human, so be polite and concise. Speak in Hindi."
),
model="gpt-4o-mini",
)
创建您的主要助手,它将从用户的语音中检测语言,并在需要时委托给适当的代理:
agent = Agent(
name="Assistant",
instructions=prompt_with_handoff_instructions(
"You're speaking to a human, so be polite and concise. If the user speaks in Spanish, hand off to the Spanish agent. If the user speaks in Hindi, hand off to the Hindi agent."
),
model="gpt-4o-mini",
handoffs=[spanish_agent, hindi_agent],
)
b. 添加工具(可选)
例如,您可以添加代理可能会调用的简单天气工具:
import random
from agents import function_tool
@function_tool
def get_weather(city: str) -> str:
choices = ["sunny", "cloudy", "rainy", "snowy"]
return f"The weather in {city} is {random.choice(choices)}."
agent.tools.append(get_weather)
步骤 3. 设置语音管道

SDK 的语音管道结合了三个组件:
- 语音转文本 (STT): 将您的音频输入转换为文本。
- 代理工作流程: 处理文本(包括语言检测和工具调用)。
- 文本转语音 (TTS): 转换代理's 文本回复转为音频。
服务's 一个简化的例子:
import asyncio
import numpy as np
import sounddevice as sd
from agents.voice import AudioInput, SingleAgentVoiceWorkflow, VoicePipeline
async def main():
# Create the voice pipeline with your primary agent
pipeline = VoicePipeline(workflow=SingleAgentVoiceWorkflow(agent))
# For demonstration, we'll simulate 3 seconds of audio input with silence.
buffer = np.zeros(24000 * 3, dtype=np.int16)
audio_input = AudioInput(buffer=buffer)
# Run the pipeline
result = await pipeline.run(audio_input)
# Set up the audio player (using sounddevice)
player = sd.OutputStream(samplerate=24000, channels=1, dtype=np.int16)
player.start()
# Stream and play audio events from the agent's output
async for event in result.stream():
if event.type == "voice_stream_event_audio":
player.write(event.data)
if __name__ == "__main__":
asyncio.run(main())
在实际应用中,您不会听到静音,而是会捕获实时麦克风输入,并且代理会实时检测语言。
步骤 4:运行语音代理
python -m examples.voice.static.main
语音代理开发的最佳实践
使用 OpenAI Agent SDK,请考虑以下最佳实践:
立即利用您的多语言语音代理占据主导地位
使用 Open 构建语音代理AI Agent SDK 的可访问性显著提升。开发者现在可以根据自身需求选择多模态或链式架构,并设置 VoicePipeline,然后让 SDK 处理 复杂的加工.
对于对话流质量,多模式方法效果最好。对于结构和控制,链式方法更合适。这项技术不断进步,为语音驱动应用开辟了新的可能性。

