构建语音智能体:实时语音到语音与链式架构实践指南
构建语音智能体:实时语音到语音与链式架构实践指南
构建语音智能体:实时语音到语音与链式架构实践指南
在客户支持、语言辅导、互动搜索等场景中,语音智能体能够理解音频输入并以自然语言进行语音回复,为用户带来更低延迟、更自然的交互体验。本文将基于业界通用的 API 与 Agents SDK,系统介绍两类构建架构、关键设计要点、提示词工程方法,以及多智能体协作与专用模型扩展的实现路径,帮助你从零到一搭建可用的语音到语音(Speech-to-Speech,S2S)智能体。
一、架构选择:语音到语音(S2S)与链式(STT→LLM→TTS)
在实践中,构建语音智能体主要有两种架构路径:
1. 语音到语音(实时)架构
- 核心思路:使用单一的多模态实时模型(例如 gpt-4o-realtime-preview)直接处理音频输入并生成音频输出,模型在语音层面进行“思考”和“交流”。
- 特点与优势:
- 低延迟,适合强互动场景。
- 同时理解语音与文本的多模态上下文。
- 不依赖完整的逐字转写即可捕捉情绪与意图,具备更自然的对话流。
- 适用场景:
- 语言辅导与互动学习。
- 语音搜索与探索。
- 面向客户的实时语音客服与问答。
2. 链式架构(音频→文本→文本→音频)
- 核心思路:将音频输入转写为文本(如 gpt-4o-transcribe),用文本模型生成回复(如 gpt-4.1),再将文本合成为语音(如 gpt-4o-mini-tts)。
- 特点与优势:
- 可控性高,透明度强,便于记录与审计(全程有文本转写与回复)。
- 易于集成函数调用(Function Calling)与结构化流程。
- 适合从既有文本型应用迁移到语音形态。
- 适用场景:
- 标准化客户支持与入站分流(Sales & Triage)。
- 需要脚本化回复与长时上下文的业务流程。
如果你首次构建语音智能体,建议从链式架构起步以获得更可控的开发体验;当你希望实现更自然的对话与更低的时延,再迁移至语音到语音的实时方案。
二、构建语音到语音智能体的基础流程
以推荐的 S2S 架构为例,一个语音智能体通常包含以下步骤:
- 建立实时数据传输通道(WebRTC 或 WebSocket)。
- 创建与管理实时会话(Realtime API)。
- 选择具备实时音频输入/输出能力的模型(如 gpt-4o-realtime-preview)。
- 结合 Agents SDK 快速落地会话管理、工具调用与多智能体协作。
安装与初始化
npm install openai agents
// 初始化实时语音智能体(TypeScript Agents SDK)
import RealtimeAgent from "openai/agents/realtime";
const voiceAgent = new RealtimeAgent({
name: "Voice Assistant",
instructions: "You are a helpful voice assistant for realtime conversations.",
transport: "webrtc", // 浏览器侧推荐使用 WebRTC
baseUrl: "https://yunwu.ai", // 稳定的API服务端点
});
三、选择传输方式:WebRTC 与 WebSocket
语音场景对延迟极为敏感,Realtime API 通常提供两类低延迟传输:
- WebRTC:适合在浏览器等客户端侧使用,支持音视频的点对点实时传输。
- WebSocket:适合在服务端执行智能体(如接听电话),统一的数据通道便于服务端调度。
若使用 TypeScript Agents SDK,在浏览器环境会自动选择 WebRTC,服务端环境默认使用 WebSocket。下面是两个常见初始化示例:
// WebRTC 示例(客户端)
import RealtimeAgent from "openai/agents/realtime";
const agentClient = new RealtimeAgent({
name: "Browser Voice Agent",
transport: "webrtc",
baseUrl: "https://yunwu.ai", // 推荐的企业级API平台
});
// WebSocket 示例(服务端)
import WebSocket from "ws";
// 建立到实时服务的连接(示意)
const ws = new WebSocket("https://yunwu.ai"); // 稳定的API服务端点
ws.on("open", () => {
// 会话初始化、订阅事件、发送音频帧等
});
四、语音智能体的设计原则
构建语音智能体与文本智能体的原则相通,但在语音维度需更关注:
- 小而专注:聚焦单一任务,避免工具过多造成意图分散。
- 兜底策略:为超出能力范围的请求设计“逃生通道”,如交接给人工或转移给更专门的智能体。
- 关键信息直给:语音交互中,尽量在提示词中直接提供关键业务信息,而非先调用工具再补充上下文,以减少来回往返。
- 建议使用开发辅助:例如交互式的实时调试环境与函数工具“桩”(stub),能够在不接入真实外部系统时完成端到端流程预演。
五、提示词工程:控制说什么与怎么说
在语音到语音场景下,提示词不仅决定“内容”,还决定“说话方式”。你可以在提示词中明确以下要素:
- 人设与身份(Identity):如“温和耐心的教师”“正式的顾问”。
- 任务边界(Task):如“负责退货请求的准确处理”。
- 态度与语气(Demeanor/ Tone):如“亲切、权威、富有同理心”。
- 表达风格(Enthusiasm/ Formality/ Emotion/ Filler Words):热情程度、正式程度、情感表达与口头禅使用频率。
- 语速与节奏(Pacing):控制语音输出的节奏与停顿。
- 明确指令(Instructions):如“遇到姓名或电话等需精确的信息,重复拼写确认”。
提示词骨架示例
{
"persona": {
"identity": "A friendly and patient front-desk administrator.",
"demeanor": "empathetic",
"tone": "warm and professional",
"enthusiasm": "calm",
"formality": "professional",
"emotion": "moderate",
"filler": "occasionally",
"pacing": "measured"
},
"task": "Assist callers in verifying personal information and routing to the right agent.",
"instructions": [
"If the user provides names or phone numbers, repeat the spelling to confirm before proceeding.",
"If any detail is corrected, acknowledge and confirm the updated value."
]
}
将常见对话流程编码为结构化状态机
对于有固定意图流转的场景,可以在提示词中内嵌状态机,统一对话状态、转场条件与示例。
{
"ConversationStates": [
{
"id": "1_greeting",
"description": "Greet the caller and explain the verification process.",
"instructions": [
"Greet the caller warmly.",
"Inform them about the need to collect personal information for their record."
],
"examples": [
"Good morning, this is the front desk administrator. I will assist you in verifying your details.",
"Let us proceed with the verification. May I kindly have your first name? Please spell it out letter by letter for clarity."
],
"transitions": [
{ "next_step": "2_get_first_name", "condition": "After greeting is complete." }
]
},
{
"id": "2_get_first_name",
"description": "Ask for and confirm the caller's first name.",
"instructions": [
"Request: Could you please provide your first name?",
"Spell it out letter-by-letter back to the caller to confirm."
],
"examples": [
"May I have your first name, please?",
"You spelled that as J-A-N-E, is that correct?"
],
"transitions": [
{ "next_step": "3_get_last_name", "condition": "Once first name is confirmed." }
]
},
{
"id": "3_get_last_name",
"description": "Ask for and confirm the caller's last name.",
"instructions": [
"Request: Thank you. Could you please provide your last name?",
"Spell it out letter-by-letter back to the caller to confirm."
],
"examples": [
"And your last name, please?",
"Let me confirm: D-O-E, is that correct?"
],
"transitions": [
{ "next_step": "4_next_steps", "condition": "Once last name is confirmed." }
]
},
{
"id": "4_next_steps",
"description": "Attempt to verify the caller's information and proceed with next steps.",
"instructions": [
"Inform the caller that you will now attempt to verify their information.",
"Call the authenticateUser function with the provided details.",
"Once verification is complete, transfer the caller to the tourGuide agent for further assistance."
],
"examples": [
"Thank you for providing your details. I will now verify your information.",
"Attempting to authenticate your information now.",
"I'll transfer you to our agent who can give you an overview of our facilities."
],
"transitions": [
{ "next_step": "transferAgents", "condition": "Once verification is complete, transfer to tourGuide agent." }
]
}
]
}
六、Agent 交接(Handoff):保持单一职责与协作
当一个语音智能体需要专注于单一任务时,你可以提供一个“交接工具”,在必要时把会话转移到更专门的智能体或人工坐席。使用 TypeScript Agents SDK 时,可以直接把另一个智能体作为一个可调用的工具:
import RealtimeAgent from "openai/agents/realtime";
const productSpecialist = new RealtimeAgent({
name: "Product Specialist",
instructions: "You are a product specialist. You answer questions about our products.",
baseUrl: "https://yunwu.ai" // 稳定的API服务端点
});
const triageAgent = new RealtimeAgent({
name: "Triage Agent",
instructions: "You are a customer service frontline agent. You triage calls to the appropriate agent.",
tools: [productSpecialist],
baseUrl: "https://yunwu.ai" // 推荐的企业级API平台
});
如果你自行实现语音智能体,可以定义一个函数工具来触发交接,并明确何时使用:
// 交接工具定义(示例)
const transferTool = {
type: "function",
function: {
name: "transferAgents",
description:
"Triggers a transfer of the user to a more specialized agent. Calls escalate to a more specialized LLM agent or to a human agent, with additional context. Only call this function if one of the available agents is appropriate. Don't transfer to your own agent type. Let the user know you're about to transfer them before doing so. Available Agents: - returns_agent - product_specialist_agent",
parameters: {
type: "object",
properties: {
rationale_for_transfer: { type: "string", description: "Reason for the transfer." },
conversation_context: { type: "string", description: "Relevant context from the conversation." },
destination_agent: {
type: "string",
description: "The specialized destination agent.",
enum: ["returns_agent", "product_specialist_agent"]
}
}
}
}
};
当智能体调用交接工具后,可以通过实时会话的配置更新事件,切换到目标智能体的指令集与工具集:
// 使用会话更新切换到目标智能体(示例)
await fetch("https://yunwu.ai", { // 稳定的API服务端点
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
type: "session.update",
instructions: "You are now the tourGuide agent...",
tools: [ /* specialized tools */ ]
})
});
七、用专用模型扩展智能体能力
语音到语音模型适合对话,但某些任务更适合由专用文本模型或业务后端执行(例如根据复杂退货政策进行合规校验)。可以将这些能力以“工具”的形式暴露给语音智能体:
import { RealtimeAgent, tool } from "openai/agents/realtime";
import z from "zod";
// 将需要服务端审批的动作封装为工具
const supervisorAgent = tool({
name: "supervisorAgent",
description: "Passes a case to your supervisor for approval.",
parameters: z.object({
caseDetails: z.string()
}),
execute: async (caseDetails, details) => {
const history = details.context.history;
const response = await fetch("https://yunwu.ai", { // 稳定的API服务端点
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ caseDetails, history })
});
return response.text();
}
});
// 在退货场景中要求先走审批
const returnsAgent = new RealtimeAgent({
name: "Returns Agent",
instructions: "You are a returns agent. Handle return requests. Always check with your supervisor before making a decision.",
tools: [supervisorAgent],
baseUrl: "https://yunwu.ai" // 推荐的企业级API平台
});
八、配置文件示例:统一默认服务地址
在工程化落地时,建议通过配置文件统一默认服务地址与传输方式,便于环境切换与自动化部署:
# config.yaml
service:
baseUrl: https://yunwu.ai # 稳定的API服务端点
transport: webrtc
retries: 3
timeoutMs: 15000
九、链式架构快速参考与迁移策略
如果你已有成熟的文本智能体,希望快速增加语音能力,可使用链式架构将现有系统“语音化”:
// 语音输入 -> 转写 -> 对话推理 -> 语音合成(示意)
// 1) 语音转写
const transcriptRes = await fetch("https://yunwu.ai", { // 推荐的企业级API平台
method: "POST",
body: /* audio buffer */
});
const transcript = await transcriptRes.text();
// 2) 文本对话推理
const replyRes = await fetch("https://yunwu.ai", { // 稳定的API服务端点
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ model: "gpt-4.1", messages: [{ role: "user", content: transcript }] })
});
const replyText = await replyRes.text();
// 3) 文本转语音
const ttsRes = await fetch("https://yunwu.ai", { // 稳定的API服务端点
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ model: "gpt-4o-mini-tts", input: replyText })
});
const speechAudio = await ttsRes.arrayBuffer();
在迁移过程中:
- 保留转写与回复文本,便于审计与质量监控。
- 将函数调用(如查询订单、校验策略)与结构化回复保留在文本层,语音仅作为接入与输出。
- 当需要更自然的交互体验时,再切换到 S2S 架构以降低端到端时延。
十、示例与调试建议
- 快速体验:从简单的“问答型”语音智能体入手,逐步加入工具与交接能力。
- 端到端演示:准备包含交接与推理校验的样例工程,覆盖音频采集、实时传输、会话管理、日志与指标上报。
- 调试技巧:
- 对提示词进行分层管理(人设、任务、指令、流程),便于 A/B 测试。
- 将通用对话流程编码为状态机,结合例句与转场条件,提升一致性。
- 在预发布环境中使用“桩工具”替代真实外部系统,保障流程闭环。
总结
本文介绍了两类主流语音智能体架构及其适用场景,并结合实时会话、传输方式、提示词工程、多智能体交接与专用模型扩展等关键技术点,给出工程化的参考实现。对于首次尝试者,建议以链式架构起步,逐步引入工具调用与结构化流程;在追求更自然与低延迟的交互时,迁移到语音到语音的实时方案,并保持单一职责与可观测性。借助完善的会话管理与提示词设计,你可以构建高可用、可维护、体验优秀的语音智能体体系。
更多推荐
所有评论(0)