Skip to content

实时语音(Realtime)

实时接口基于 WebSocket,支持低延迟的语音 / 文本双向交互,适用于实时语音对话、同声传译等场景。

建立连接

GET wss://ai.youqi.tech/v1/realtime?model=<model>

通过 WebSocket 连接,并在握手头中携带认证:

http
Authorization: Bearer sk-YOUR_API_KEY

连接示例(Node.js)

javascript
import WebSocket from "ws";

const ws = new WebSocket(
  "wss://ai.youqi.tech/v1/realtime?model=gpt-4o-realtime-preview",
  {
    headers: { Authorization: "Bearer sk-YOUR_API_KEY" },
  },
);

ws.on("open", () => {
  ws.send(
    JSON.stringify({
      type: "response.create",
      response: { modalities: ["text", "audio"], instructions: "你好" },
    }),
  );
});

ws.on("message", (data) => {
  console.log(JSON.parse(data.toString()));
});

事件模型

实时接口采用事件驱动:客户端发送事件(如 session.updateinput_audio_buffer.appendresponse.create),服务端推送事件(如 response.audio.deltaresponse.text.deltaresponse.done)。

常见客户端事件说明
session.update更新会话配置(音色、模态、系统提示等)
input_audio_buffer.append追加输入音频分片(base64)
response.create请求模型生成回复
常见服务端事件说明
response.text.delta文本增量
response.audio.delta音频增量(base64)
response.done一次回复结束

注意

实时接口为长连接、事件驱动模式,与普通 REST 接口差异较大。可用模型与具体事件集以所选模型为准。