实时会话

GET /v1/realtime — WebSocket 实时语音对话端点,兼容 OpenAI Realtime API

GET/v1/realtime

建立 WebSocket 连接进行低延迟实时语音/文本对话,兼容 OpenAI Realtime API。握手成功返回 101 Switching Protocols,之后双方通过 JSON 事件通信。

连接地址:wss://api.jimu.chat/v1/realtime?model=<YOUR_REALTIME_MODEL_ID>

实时模型与普通对话模型是两套配置。<YOUR_REALTIME_MODEL_ID> 须替换为平台已为你账号配置的实时模型 ID——可用模型以 GET /v2/models 返回为准;填入未开通的模型 ID 会在握手阶段返回 model_not_found 错误。

鉴权

服务端客户端(可自定义请求头)直接使用:

Authorization: Bearer <令牌>

浏览器 WebSocket API 无法自定义请求头,改用 Sec-WebSocket-Protocol 子协议携带令牌:

// <YOUR_REALTIME_MODEL_ID>:平台为账号配置实时模型后获得的 ID
const ws = new WebSocket("wss://api.jimu.chat/v1/realtime?model=<YOUR_REALTIME_MODEL_ID>", [
  "realtime",
  "openai-insecure-api-key.sk-...",   // 平台从该子协议提取令牌
  "openai-beta.realtime-v1",
]);
子协议中的令牌对浏览器用户完全可见(任何打开开发者工具的人都能读到)。不要在浏览器端内嵌共享或长期令牌。 生产环境应由可信后端代理 WebSocket 连接;确需直连时,至少使用独立创建、限制额度与模型范围、可随时吊销的专用令牌。

查询参数

modelstring必填

实时模型 ID(须为账号已配置的实时模型)。缺省时平台返回 400「未指定模型名称」

客户端事件(发送)

type说明
session.update更新会话配置(见下方 session 字段)
conversation.item.create向会话追加一条消息项
response.create触发模型生成响应
input_audio_buffer.append追加音频输入(base64 编码音频块)

session.updatesession 对象常用字段:modalities["text","audio"])、instructionsvoiceinput_audio_format / output_audio_format(如 pcm16)、input_audio_transcription.modelturn_detectiontoolstool_choicetemperature

服务端事件(接收)

type说明
session.created会话建立,携带初始 session 配置
session.updated会话配置已更新
conversation.item.created消息项已创建
response.audio.delta增量音频输出(base64)
response.audio_transcript.delta增量音频转写文本
response.function_call_arguments.delta / .done工具调用参数增量 / 完成
response.done响应完成,携带 usagetotal_tokens / input_tokens / output_tokens 及 details)
error错误事件,error 字段为标准错误对象

平台对事件流做双向透传,实际出现的事件类型不限于上表(随上游能力变化)。客户端应忽略未识别的事件类型,不要当作错误处理。

连接示例(Node.js)

import WebSocket from "ws";

const model = process.env.REALTIME_MODEL_ID; // 账号已配置的实时模型 ID

const ws = new WebSocket(
  `wss://api.jimu.chat/v1/realtime?model=${model}`,
  { headers: { Authorization: `Bearer ${process.env.JIMU_API_KEY}` } }
);

ws.on("open", () => {
  ws.send(JSON.stringify({
    type: "session.update",
    session: { modalities: ["text", "audio"], voice: "alloy", instructions: "你是语音助手" },
  }));
  ws.send(JSON.stringify({
    type: "conversation.item.create",
    item: { type: "message", role: "user", content: [{ type: "input_text", text: "你好" }] },
  }));
  ws.send(JSON.stringify({ type: "response.create" }));
});

ws.on("message", (data) => {
  const event = JSON.parse(data);
  if (event.type === "response.done") console.log(event.response.usage);
});
实时会话按 token 计量(音频 token 与文本 token 分别计入 input_token_details / output_token_details),长连接期间持续计费。可用实时模型以 GET /v2/models 返回为准。