Realtime 接口
使用 OpenAI Realtime 兼容 WebSocket 路由进行实时交互。
https://duoyuanx.com/v1/realtimeRealtime 路由使用 WebSocket 连接,适合低延迟语音对话、实时事件流和双向交互场景。该路由会经过 TokenAuth、模型限流和渠道分发。
连接地址
wss://https://duoyuanx.com/v1/realtime
请求头
Authorizationstring必填请求身份认证。使用 Bearer Token,示例值:Bearer YOUR_API_KEY。若客户端不方便设置 WebSocket 请求头,可按自身网关配置使用支持的临时 token 或由代理层注入认证头。
连接示例
const ws = new WebSocket("wss://https://duoyuanx.com/v1/realtime", [], {
headers: {
Authorization: "Bearer YOUR_API_KEY",
},
});
ws.onopen = () => {
ws.send(JSON.stringify({
type: "session.update",
session: {
modalities: ["text"],
instructions: "你是一个实时语音助手。"
}
}));
};
ws.onmessage = (event) => {
console.log(JSON.parse(event.data));
};
会话配置(session.update)
连接建立后,客户端可随时发送 session.update 事件更新会话配置,服务端会以 session.updated 事件返回生效后的完整配置。只更新消息中出现的字段;要清空 instructions 传空字符串,清空 tools 传空数组,关闭 turn_detection 传 null。
modalitiesarray<string>模型响应的模态集合,默认 ["text", "audio"]。设为 ["text"] 可禁用音频输出。
modelstring会话使用的 Realtime 模型,例如 gpt-realtime、gpt-4o-realtime-preview。也可通过连接 URL 的 model 查询参数指定。
instructionsstring会话的默认系统指令,用于引导模型的回复内容与格式(如 "be extremely succinct")以及音频行为(如 "talk quickly")。
voicestring默认值: alloy模型回复使用的声音。内置声音:alloy、ash、ballad、coral、echo、sage、shimmer、verse、marin、cedar。模型输出过音频后不能再更换声音。
input_audio_formatstring默认值: pcm16输入音频格式:pcm16(24kHz PCM)、g711_ulaw、g711_alaw。
output_audio_formatstring默认值: pcm16输出音频格式:pcm16、g711_ulaw、g711_alaw。
input_audio_transcriptionobject输入音频转写配置,默认关闭。
modelstring转写模型,如 whisper-1、gpt-4o-transcribe、gpt-4o-mini-transcribe。
languagestring输入音频的语言(ISO-639-1 格式,如 zh、en),指定后可提升准确率和延迟表现。
promptstring可选的提示词,用于引导转写风格或补充专有名词。
turn_detectionobject服务端轮流发言检测配置。传 null 关闭,关闭后客户端需要手动触发模型响应。
typestring默认值: server_vad检测类型:server_vad(基于音量的语音活动检测)或 semantic_vad(用语义模型判断用户是否说完,更自然但延迟可能更高)。
thresholdnumber默认值: 0.5仅 server_vad。VAD 激活阈值(0.0 到 1.0),阈值越高需要越大的音量才能激活,噪声环境下可适当调高。
prefix_padding_msinteger默认值: 300仅 server_vad。在检测到的语音起点之前保留的音频时长(毫秒)。
silence_duration_msinteger默认值: 500仅 server_vad。判定说话结束所需的静音时长(毫秒)。取值越小响应越快,但可能在用户短暂停顿时抢话。
eagernessstring默认值: auto仅 semantic_vad。模型抢答的积极程度:low(等待更久)、medium、high(更快响应)、auto(默认,等价于 medium)。low / medium / high 的最长等待时间分别约为 8 秒、4 秒、2 秒。
create_responseboolean默认值: true检测到发言结束时是否自动生成响应。
interrupt_responseboolean默认值: true检测到新的发言开始时是否自动打断正在进行的响应。create_response 和 interrupt_response 都为 false 时模型不会自动响应,但仍会发出 VAD 事件。
input_audio_noise_reductionobject输入音频降噪配置,在音频进入 VAD 和模型之前过滤,可提升检测准确率。传 null 关闭。
typestring降噪类型:near_field(近讲麦克风,如头戴耳机)或 far_field(远场麦克风,如笔记本、会议室麦克风)。
temperaturenumber默认值: 1采样温度,范围 0.6 到 1.2,默认 1。
max_response_output_tokensinteger | string默认值: inf单次助手响应的最大输出 token 数(含工具调用)。传 1 到 4096 的整数限制输出,或传 "inf" 使用模型的最大可用 token 数(默认)。
toolsarray<object>模型可调用的工具列表,目前支持函数工具:{ "type": "function", "name": ..., "description": ..., "parameters": ...(JSON Schema) }。
tool_choicestring | object默认值: auto工具选择策略:auto(默认)、none、required,或传 { "type": "function", "name": "..." } 强制调用指定函数。
常见事件
客户端事件:
| event | 说明 |
|---|---|
session.update | 更新会话配置,字段见上表 |
input_audio_buffer.append | 追加 Base64 编码的输入音频 |
input_audio_buffer.commit | 提交输入音频缓冲(关闭 VAD 时手动提交) |
input_audio_buffer.clear | 清空输入音频缓冲 |
conversation.item.create | 创建对话项 |
conversation.item.delete | 删除对话项 |
response.create | 触发模型响应 |
response.cancel | 取消进行中的响应 |
服务端事件:
| event | 说明 |
|---|---|
session.created / session.updated | 会话创建 / 配置已更新 |
input_audio_buffer.speech_started / speech_stopped | VAD 检测到发言开始 / 结束 |
conversation.item.input_audio_transcription.completed | 输入音频转写完成 |
response.created / response.done | 响应开始 / 完成 |
response.text.delta | 文本增量 |
response.audio.delta / response.audio.done | 音频增量 / 音频输出完成 |
response.audio_transcript.delta / done | 输出音频转写增量 / 完成 |
response.function_call_arguments.done | 函数调用参数生成完成 |
error | 错误事件 |
rate_limits.updated | 速率限制更新 |