Skip to content

实时语音(Realtime)

Realtime API 通过 WebSocket 建立低延迟双向连接,可用于实时语音对话、音频输入输出和实时转写。

WebSocket 地址

text
wss://api.nexusmodels.cn/v1/realtime?model=YOUR_REALTIME_MODEL

其中 model 必须替换为实际可用的实时模型名称。

身份认证

WebSocket 握手时需要携带虚拟 Key:

http
Authorization: Bearer YOUR_NEXUSMODELS_API_KEY

使用 wscat 连接

安装 wscat

bash
npm install -g wscat

建立连接:

bash
wscat \
  -c 'wss://api.nexusmodels.cn/v1/realtime?model=YOUR_REALTIME_MODEL' \
  -H 'Authorization: Bearer YOUR_NEXUSMODELS_API_KEY'

连接成功后,可以发送会话配置事件:

json
{
  "type": "session.update",
  "session": {
    "modalities": [
      "text",
      "audio"
    ],
    "instructions": "请使用中文回答。",
    "voice": "alloy"
  }
}

发送一条文本消息:

json
{
  "type": "conversation.item.create",
  "item": {
    "type": "message",
    "role": "user",
    "content": [
      {
        "type": "input_text",
        "text": "请介绍一下 NexusModels。"
      }
    ]
  }
}

请求模型生成响应:

json
{
  "type": "response.create"
}

常见事件

事件方向说明
session.update客户端 → 服务端更新会话配置
conversation.item.create客户端 → 服务端添加用户消息
input_audio_buffer.append客户端 → 服务端添加 Base64 音频数据
response.create客户端 → 服务端请求模型生成响应
response.text.delta服务端 → 客户端返回增量文本
response.audio.delta服务端 → 客户端返回增量音频数据
response.done服务端 → 客户端本轮响应完成
error服务端 → 客户端返回错误信息