流式语音代理 使用 OpenAI Agents SDK 实现实时语音交互的示例,支持连续音频流。此示例展示了先进的语音处理管道功能,包括实时语音检测、实时处理以及基于轮次的对话管理。 本示例演示的内容 实时音频处理:连续音频输入与输出流 活动检测:自动检测语音开始与结束 轮次管理:智能对话轮次处理 实时代理处理:对话过程中实时生成代理响应 打断处理:对话流程管理的生命周期事件 流式回调:实时监控与调试 核心概念:流式语音管道 流式语音管道以实时方式连续处理音频。
使用 OpenAI Agents SDK 实现实时语音交互的示例,支持连续音频流。此示例展示了先进的语音处理管道功能,包括实时语音检测、实时处理以及基于轮次的对话管理。
流式语音管道以实时方式连续处理音频。可以将其想象成一个实时对话助手,它:
┌─────────────────────────────────────────────────────────────┐ │ STREAMING VOICE WORKFLOW │ ├─────────────────────────────────────────────────────────────┤ │ │ │ CONTINUOUS AUDIO INPUT │ │ │ │ │ ▼ │ │ ┌─────────────┐ 1. REAL-TIME CAPTURE │ │ │ STREAMING │ ◦ Continuous microphone input │ │ │ AUDIO │ ◦ Chunk-based processing │ │ │ RECORDER │ ◦ Activity detection │ │ └─────────────┘ │ │ │ │ │ ▼ │ │ ┌─────────────┐ 2. LIVE TRANSCRIPTION │ │ │ STREAMING │ ◦ Real-time speech-to-text │ │ │TRANSCRIPTION│ ◦ Turn boundary detection │ │ └─────────────┘ │ │ │ │ │ ▼ │ │ ┌─────────────┐ 3. CONCURRENT PROCESSING │ │ │ PARALLEL │ ◦ Agent workflow execution │ │ │ AGENT EXEC │ ◦ Tool calls & handoffs │ │ └─────────────┘ ◦ Multiple turns in session │ │ │ │ │ ▼ │ │ ┌─────────────┐ 4. STREAMING RESPONSE │ │ │ LIVE TTS │ ◦ Real-time text-to-speech │ │ │ PLAYBACK │ ◦ Chunked audio output │ │ └─────────────┘ ◦ Immediate response playback │ │ │ │ │ ▼ │ │ CONTINUOUS AUDIO OUTPUT │ │ │ │ ↺ LOOP FOR MULTIPLE TURNS │ └─────────────────────────────────────────────────────────────┘
安装语音依赖项:
pip install 'openai-agents[voice]' pip install sounddevice numpy soundfile librosa
设置环境:
cp env.example .env # Edit .env and add your OpenAI API key
运行流式语音代理:
python agent.py
开始讲话:代理会自动检测到你的讲话,并实时作出回应!
get_weather(city): Real-time weather informationget_time(): Current time with live updatesset_reminder(message, minutes): Demo reminder functionalityget_news_summary():模拟新闻更新pipeline = VoicePipeline( workflow=SingleAgentVoiceWorkflow(agent, callbacks=StreamingWorkflowCallbacks()) )
with StreamedAudioRecorder() as recorder: streamed_input = StreamedAudioInput() while session_active: if recorder.has_audio(): audio_chunk = recorder.get_audio_chunk() streamed_input.push_audio(audio_chunk)
with AudioPlayer() as player: async for event in result.stream(): if event.type == "voice_stream_event_audio": player.add_audio(event.data) elif event.type == "voice_stream_event_lifecycle": handle_turn_events(event)
class VoiceSessionManager: async def start_session(self): # Concurrent input/output processing input_task = asyncio.create_task(self._process_audio_input()) output_task = asyncio.create_task(self._process_audio_output()) await asyncio.gather(input_task, output_task)
turn_started and turn_ended notificationsopenai-agents[voice]: OpenAI Agents SDK with voice supportsounddevice: Real-time audio I/Onumpy: Audio data processingthreading: Concurrent audio processingasyncio: Asynchronous pipeline managementThe SDK currently doesn't support built-in interruptions. Use lifecycle events to:
turn_started)turn_ended掌握流式语音代理后:
免责声明:
本文档采用基于机器的 AI 翻译服务进行翻译。尽管我们力求准确,但请注意,自动翻译可能存在错误或不准确之处。应以原文语言版本的文档作为权威依据。如需获取关键信息,建议使用专业的人工翻译。对于因使用本翻译而产生的任何误解或误读,我们概不负责。