OpenAI 實時對話介面
📝 概述
簡介
OpenAI Realtime API 提供兩種連線方式:
-
WebRTC - 適用於瀏覽器和移動客戶端的實時音影片互動
-
WebSocket - 適用於伺服器到伺服器的應用程式整合
使用場景
- 實時語音對話
- 音視訊會議
- 實時翻譯
- 語音轉寫
- 實時程式碼生成
- 伺服器端實時整合
主要特性
- 雙向音訊流傳輸
- 文字和音訊混合對話
- 函式呼叫支援
- 自動語音檢測(VAD)
- 音訊轉寫功能
- WebSocket 伺服器端整合
🔐 認證與安全
認證方式
- 標準 API 金鑰 (僅伺服器端使用)
- 臨時令牌 (客戶端使用)
臨時令牌
- 有效期: 1分鐘
- 使用限制: 單個連線
- 獲取方式: 透過伺服器端 API 建立
POST https://88api.ai/v1/realtime/sessions
Content-Type: application/json
Authorization: Bearer $NEW_API_KEY
{
"model": "gpt-4o-realtime-preview-2024-12-17",
"voice": "verse"
}安全建議
- 永遠不要在客戶端暴露標準 API 金鑰
- 使用 HTTPS/WSS 進行通訊
- 實現適當的訪問控制
- 監控異常活動
🔌 連線建立
WebRTC 連線
- URL:
https://88api.ai/v1/realtime - 查詢引數:
model - 請求頭:
Authorization: Bearer EPHEMERAL_KEYContent-Type: application/sdp
WebSocket 連線
- URL:
wss://88api.ai/v1/realtime - 查詢引數:
model - 請求頭:
Authorization: Bearer YOUR_API_KEYOpenAI-Beta: realtime=v1
連線流程
sequenceDiagram
participant Client
participant Server
participant OpenAI
alt WebRTC 連線
Client->>Server: 請求臨時令牌
Server->>OpenAI: 建立會話
OpenAI-->>Server: 返回臨時令牌
Server-->>Client: 返回臨時令牌
Client->>OpenAI: 建立 WebRTC offer
OpenAI-->>Client: 返回 answer
Note over Client,OpenAI: 建立 WebRTC 連線
Client->>OpenAI: 建立資料通道
OpenAI-->>Client: 確認資料通道
else WebSocket 連線
Server->>OpenAI: 建立 WebSocket 連線
OpenAI-->>Server: 確認連線
Note over Server,OpenAI: 開始實時對話
end資料通道
- 名稱:
oai-events - 用途: 事件傳輸
- 格式: JSON
音訊流
- 輸入:
addTrack() - 輸出:
ontrack事件
💬 對話互動
對話模式
- 純文字對話
- 語音對話
- 混合對話
會話管理
- 建立會話
- 更新會話
- 結束會話
- 會話配置
事件型別
- 文字事件
- 音訊事件
- 函式呼叫
- 狀態更新
- 錯誤事件
⚙️ 配置選項
音訊配置
- 輸入格式
pcm16g711_ulawg711_alaw
- 輸出格式
pcm16g711_ulawg711_alaw
- 語音型別
alloyechoshimmer
模型配置
- 溫度
- 最大輸出長度
- 系統提示詞
- 工具配置
VAD 配置
- 閾值
- 靜音時長
- 字首填充
💡 請求示例
WebRTC 連線 ❌
客戶端實現 (瀏覽器)
async function init() {
// 從伺服器獲取臨時金鑰 - 參見下方伺服器程式碼
const tokenResponse = await fetch('/session');
const data = await tokenResponse.json();
const EPHEMERAL_KEY = data.client_secret.value;
// 建立對等連線
const pc = new RTCPeerConnection();
// 設定播放模型返回的遠端音訊
const audioEl = document.createElement('audio');
audioEl.autoplay = true;
pc.ontrack = (e) => (audioEl.srcObject = e.streams[0]);
// 新增瀏覽器麥克風輸入的本地音訊軌道
const ms = await navigator.mediaDevices.getUserMedia({
audio: true,
});
pc.addTrack(ms.getTracks()[0]);
// 設定用於傳送和接收事件的資料通道
const dc = pc.createDataChannel('oai-events');
dc.addEventListener('message', (e) => {
// 這裡接收實時伺服器事件!
console.log(e);
});
// 使用會話描述協議(SDP)啟動會話
const offer = await pc.createOffer();
await pc.setLocalDescription(offer);
const baseUrl = 'https://88api.ai/v1/realtime';
const model = 'gpt-4o-realtime-preview-2024-12-17';
const sdpResponse = await fetch(`${baseUrl}?model=${model}`, {
method: 'POST',
body: offer.sdp,
headers: {
Authorization: `Bearer ${EPHEMERAL_KEY}`,
'Content-Type': 'application/sdp',
},
});
const answer = {
type: 'answer',
sdp: await sdpResponse.text(),
};
await pc.setRemoteDescription(answer);
}
init();伺服器端實現 (Node.js)
import express from 'express';
const app = express();
// 建立一個端點用於生成臨時令牌
// 該端點與上面的客戶端程式碼配合使用
app.get('/session', async (req, res) => {
const r = await fetch('https://88api.ai/v1/realtime/sessions', {
method: 'POST',
headers: {
Authorization: `Bearer ${process.env.NEW_API_KEY}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({
model: 'gpt-4o-realtime-preview-2024-12-17',
voice: 'verse',
}),
});
const data = await r.json();
// 將從OpenAI REST API收到的JSON傳送回客戶端
res.send(data);
});
app.listen(3000);WebRTC 事件收發示例
// 從對等連線建立資料通道
const dc = pc.createDataChannel('oai-events');
// 監聽資料通道上的伺服器事件
// 事件資料需要從JSON字串解析
dc.addEventListener('message', (e) => {
const realtimeEvent = JSON.parse(e.data);
console.log(realtimeEvent);
});
// 傳送客戶端事件:將有效的客戶端事件序列化為
// JSON,並透過資料通道傳送
const responseCreate = {
type: 'response.create',
response: {
modalities: ['text'],
instructions: 'Write a haiku about code',
},
};
dc.send(JSON.stringify(responseCreate));WebSocket 連線 ✅
Node.js (ws模組)
import WebSocket from 'ws';
const url =
'wss://88api.ai/v1/realtime?model=gpt-4o-realtime-preview-2024-12-17';
const ws = new WebSocket(url, {
headers: {
Authorization: 'Bearer ' + process.env.NEW_API_KEY,
'OpenAI-Beta': 'realtime=v1',
},
});
ws.on('open', function open() {
console.log('Connected to server.');
});
ws.on('message', function incoming(message) {
console.log(JSON.parse(message.toString()));
});Python (websocket-client)
# 需要安裝 websocket-client 庫:
# pip install websocket-client
import os
import json
import websocket
NEW_API_KEY = os.environ.get("NEW_API_KEY")
url = "wss://88api.ai/v1/realtime?model=gpt-4o-realtime-preview-2024-12-17"
headers = [
"Authorization: Bearer " + NEW_API_KEY,
"OpenAI-Beta: realtime=v1"
]
def on_open(ws):
print("Connected to server.")
def on_message(ws, message):
data = json.loads(message)
print("Received event:", json.dumps(data, indent=2))
ws = websocket.WebSocketApp(
url,
header=headers,
on_open=on_open,
on_message=on_message,
)
ws.run_forever()瀏覽器 (標準WebSocket)
/*
注意:在瀏覽器等客戶端環境中,我們建議使用WebRTC。
但在Deno和Cloudflare Workers等類瀏覽器環境中,
也可以使用標準WebSocket介面。
*/
const ws = new WebSocket(
'wss://88api.ai/v1/realtime?model=gpt-4o-realtime-preview-2024-12-17',
[
'realtime',
// 認證
'openai-insecure-api-key.' + NEW_API_KEY,
// 可選
'openai-organization.' + OPENAI_ORG_ID,
'openai-project.' + OPENAI_PROJECT_ID,
// Beta協議,必需
'openai-beta.realtime-v1',
]
);
ws.on('open', function open() {
console.log('Connected to server.');
});
ws.on('message', function incoming(message) {
console.log(message.data);
});訊息收發示例
Node.js/瀏覽器
// 接收伺服器事件
ws.on('message', function incoming(message) {
// 需要從JSON解析訊息資料
const serverEvent = JSON.parse(message.data);
console.log(serverEvent);
});
// 傳送事件,建立符合客戶端事件格式的JSON資料結構
const event = {
type: 'response.create',
response: {
modalities: ['audio', 'text'],
instructions: 'Give me a haiku about code.',
},
};
ws.send(JSON.stringify(event));Python
# 傳送客戶端事件,將字典序列化為JSON
def on_open(ws):
print("Connected to server.")
event = {
"type": "response.create",
"response": {
"modalities": ["text"],
"instructions": "Please assist the user."
}
}
ws.send(json.dumps(event))
# 接收訊息需要從JSON解析訊息負載
def on_message(ws, message):
data = json.loads(message)
print("Received event:", json.dumps(data, indent=2))⚠️ 錯誤處理
常見錯誤
- 連線錯誤
- 網路問題
- 認證失敗
- 配置錯誤
- 音訊錯誤
- 裝置許可權
- 格式不支援
- 編解碼問題
- 會話錯誤
- 令牌過期
- 會話超時
- 併發限制
錯誤恢復
- 自動重連
- 會話恢復
- 錯誤重試
- 降級處理
📝 事件參考
通用請求頭
所有事件都需要包含以下請求頭:
| 請求頭 | 型別 | 說明 | 示例值 |
|---|---|---|---|
| Authorization | 字串 | 認證令牌 | Bearer $NEW_API_KEY |
| OpenAI-Beta | 字串 | API 版本 | realtime=v1 |
客戶端事件
session.update
更新會話的預設配置。
| 引數 | 型別 | 必需 | 說明 | 示例值/可選值 |
|---|---|---|---|---|
| event_id | 字串 | 否 | 客戶端生成的事件識別符號 | event_123 |
| type | 字串 | 否 | 事件型別 | session.update |
| modalities | 字串陣列 | 否 | 模型可以響應的模態型別 | ["text", "audio"] |
| instructions | 字串 | 否 | 預置到模型呼叫前的系統指令 | "Your knowledge cutoff is 2023-10..." |
| voice | 字串 | 否 | 模型使用的語音型別 | alloy、echo、shimmer |
| input_audio_format | 字串 | 否 | 輸入音訊格式 | pcm16、g711_ulaw、g711_alaw |
| output_audio_format | 字串 | 否 | 輸出音訊格式 | pcm16、g711_ulaw、g711_alaw |
| input_audio_transcription.model | 字串 | 否 | 用於轉寫的模型 | whisper-1 |
| turn_detection.type | 字串 | 否 | 語音檢測型別 | server_vad |
| turn_detection.threshold | 數字 | 否 | VAD 啟用閾值(0.0-1.0) | 0.8 |
| turn_detection.prefix_padding_ms | 整數 | 否 | 語音開始前包含的音訊時長 | 500 |
| turn_detection.silence_duration_ms | 整數 | 否 | 檢測語音停止的靜音持續時間 | 1000 |
| tools | 陣列 | 否 | 模型可用的工具列表 | [] |
| tool_choice | 字串 | 否 | 模型選擇工具的方式 | auto/none/required |
| temperature | 數字 | 否 | 模型取樣溫度 | 0.8 |
| max_output_tokens | 字串/整數 | 否 | 單次響應最大token數 | "inf"/4096 |
input_audio_buffer.append
向輸入音訊緩衝區追加音訊資料。
| 引數 | 型別 | 必需 | 說明 | 示例值 |
|---|---|---|---|---|
| event_id | 字串 | 否 | 客戶端生成的事件識別符號 | event_456 |
| type | 字串 | 否 | 事件型別 | input_audio_buffer.append |
| audio | 字串 | 否 | Base64編碼的音訊資料 | Base64EncodedAudioData |
input_audio_buffer.commit
將緩衝區中的音訊資料提交為使用者訊息。
| 引數 | 型別 | 必需 | 說明 | 示例值 |
|---|---|---|---|---|
| event_id | 字串 | 否 | 客戶端生成的事件識別符號 | event_789 |
| type | 字串 | 否 | 事件型別 | input_audio_buffer.commit |
input_audio_buffer.clear
清空輸入音訊緩衝區中的所有音訊資料。
| 引數 | 型別 | 必需 | 說明 | 示例值 |
|---|---|---|---|---|
| event_id | 字串 | 否 | 客戶端生成的事件識別符號 | event_012 |
| type | 字串 | 否 | 事件型別 | input_audio_buffer.clear |
conversation.item.create
向對話中新增新的對話項。
| 引數 | 型別 | 必需 | 說明 | 示例值 |
|---|---|---|---|---|
| event_id | 字串 | 否 | 客戶端生成的事件識別符號 | event_345 |
| type | 字串 | 否 | 事件型別 | conversation.item.create |
| previous_item_id | 字串 | 否 | 新對話項將插入在此ID之後 | null |
| item.id | 字串 | 否 | 對話項的唯一識別符號 | msg_001 |
| item.type | 字串 | 否 | 對話項型別 | message/function_call/function_call_output |
| item.status | 字串 | 否 | 對話項狀態 | completed/in_progress/incomplete |
| item.role | 字串 | 否 | 訊息傳送者的角色 | user/assistant/system |
| item.content | 陣列 | 否 | 訊息內容 | [text/audio/transcript] |
| item.call_id | 字串 | 否 | 函式呼叫的ID | call_001 |
| item.name | 字串 | 否 | 被呼叫的函式名稱 | function_name |
| item.arguments | 字串 | 否 | 函式呼叫的引數 | {"param": "value"} |
| item.output | 字串 | 否 | 函式呼叫的輸出結果 | {"result": "value"} |
conversation.item.truncate
截斷助手訊息中的音訊內容。
| 引數 | 型別 | 必需 | 說明 | 示例值 |
|---|---|---|---|---|
| event_id | 字串 | 否 | 客戶端生成的事件識別符號 | event_678 |
| type | 字串 | 否 | 事件型別 | conversation.item.truncate |
| item_id | 字串 | 否 | 要截斷的助手訊息項的ID | msg_002 |
| content_index | 整數 | 否 | 要截斷的內容部分的索引 | 0 |
| audio_end_ms | 整數 | 否 | 音訊截斷的結束時間點 | 1500 |
conversation.item.delete
從對話歷史中刪除指定的對話項。
| 引數 | 型別 | 必需 | 說明 | 示例值 |
|---|---|---|---|---|
| event_id | 字串 | 否 | 客戶端生成的事件識別符號 | event_901 |
| type | 字串 | 否 | 事件型別 | conversation.item.delete |
| item_id | 字串 | 否 | 要刪除的對話項的ID | msg_003 |
response.create
觸發響應生成。
| 引數 | 型別 | 必需 | 說明 | 示例值 |
|---|---|---|---|---|
| event_id | 字串 | 否 | 客戶端生成的事件識別符號 | event_234 |
| type | 字串 | 否 | 事件型別 | response.create |
| response.modalities | 字串陣列 | 否 | 響應的模態型別 | ["text", "audio"] |
| response.instructions | 字串 | 否 | 給模型的指令 | "Please assist the user." |
| response.voice | 字串 | 否 | 模型使用的語音型別 | alloy/echo/shimmer |
| response.output_audio_format | 字串 | 否 | 輸出音訊格式 | pcm16 |
| response.tools | 陣列 | 否 | 模型可用的工具列表 | ["type", "name", "description"] |
| response.tool_choice | 字串 | 否 | 模型選擇工具的方式 | auto |
| response.temperature | 數字 | 否 | 取樣溫度 | 0.7 |
| response.max_output_tokens | 整數/字串 | 否 | 最大輸出token數 | 150/"inf" |
response.cancel
取消正在進行中的響應生成。
| 引數 | 型別 | 必需 | 說明 | 示例值 |
|---|---|---|---|---|
| event_id | 字串 | 否 | 客戶端生成的事件識別符號 | event_567 |
| type | 字串 | 否 | 事件型別 | response.cancel |
服務端事件
error
當發生錯誤時返回的事件。
| 引數 | 型別 | 必需 | 說明 | 示例值 |
|---|---|---|---|---|
| event_id | 字串陣列 | 否 | 服務端事件的唯一識別符號 | ["event_890"] |
| type | 字串 | 否 | 事件型別 | error |
| error.type | 字串 | 否 | 錯誤型別 | invalid_request_error/server_error |
| error.code | 字串 | 否 | 錯誤程式碼 | invalid_event |
| error.message | 字串 | 否 | 人類可讀的錯誤訊息 | "The 'type' field is missing." |
| error.param | 字串 | 否 | 與錯誤相關的引數 | null |
| error.event_id | 字串 | 否 | 相關事件的ID | event_567 |
conversation.item.input_audio_transcription.completed
當啟用輸入音訊轉寫功能並且轉寫成功時返回此事件。
| 引數 | 型別 | 必需 | 說明 | 示例值 |
|---|---|---|---|---|
| event_id | 字串 | 否 | 服務端事件的唯一識別符號 | event_2122 |
| type | 字串 | 否 | 事件型別 | conversation.item.input_audio_transcription.completed |
| item_id | 字串 | 否 | 使用者訊息項的ID | msg_003 |
| content_index | 整數 | 否 | 包含音訊的內容部分的索引 | 0 |
| transcript | 字串 | 否 | 轉寫的文字內容 | "Hello, how are you?" |
conversation.item.input_audio_transcription.failed
當配置了輸入音訊轉寫功能,但使用者訊息的轉寫請求失敗時返回此事件。
| 引數 | 型別 | 必需 | 說明 | 示例值 |
|---|---|---|---|---|
| event_id | 字串 | 否 | 服務端事件的唯一識別符號 | event_2324 |
| type | 字串陣列 | 否 | 事件型別 | ["conversation.item.input_audio_transcription.failed"] |
| item_id | 字串 | 否 | 使用者訊息項的ID | msg_003 |
| content_index | 整數 | 否 | 包含音訊的內容部分的索引 | 0 |
| error.type | 字串 | 否 | 錯誤型別 | transcription_error |
| error.code | 字串 | 否 | 錯誤程式碼 | audio_unintelligible |
| error.message | 字串 | 否 | 人類可讀的錯誤訊息 | "The audio could not be transcribed." |
| error.param | 字串 | 否 | 與錯誤相關的引數 | null |
conversation.item.truncated
當客戶端截斷了之前的助手音訊訊息項時返回此事件。
| 引數 | 型別 | 必需 | 說明 | 示例值 |
|---|---|---|---|---|
| event_id | 字串 | 否 | 服務端事件的唯一識別符號 | event_2526 |
| type | 字串 | 否 | 事件型別 | conversation.item.truncated |
| item_id | 字串 | 否 | 被截斷的助手訊息項的ID | msg_004 |
| content_index | 整數 | 否 | 被截斷的內容部分的索引 | 0 |
| audio_end_ms | 整數 | 否 | 音訊被截斷的時間點(毫秒) | 1500 |
conversation.item.deleted
當對話中的某個專案被刪除時返回此事件。
| 引數 | 型別 | 必需 | 說明 | 示例值 |
|---|---|---|---|---|
| event_id | 字串 | 否 | 服務端事件的唯一識別符號 | event_2728 |
| type | 字串 | 否 | 事件型別 | conversation.item.deleted |
| item_id | 字串 | 否 | 被刪除的對話項的ID | msg_005 |
input_audio_buffer.committed
當音訊緩衝區中的資料被提交時返回此事件。
| 引數 | 型別 | 必需 | 說明 | 示例值 |
|---|---|---|---|---|
| event_id | 字串 | 否 | 服務端事件的唯一識別符號 | event_1121 |
| type | 字串 | 否 | 事件型別 | input_audio_buffer.committed |
| previous_item_id | 字串 | 否 | 新對話項將插入在此ID對應的對話項之後 | msg_001 |
| item_id | 字串 | 否 | 將要建立的使用者訊息項的ID | msg_002 |
input_audio_buffer.cleared
當客戶端清空輸入音訊緩衝區時返回此事件。
| 引數 | 型別 | 必需 | 說明 | 示例值 |
|---|---|---|---|---|
| event_id | 字串 | 否 | 服務端事件的唯一識別符號 | event_1314 |
| type | 字串 | 否 | 事件型別 | input_audio_buffer.cleared |
input_audio_buffer.speech_started
在伺服器語音檢測模式下,當檢測到語音輸入時返回此事件。
| 引數 | 型別 | 必需 | 說明 | 示例值 |
|---|---|---|---|---|
| event_id | 字串 | 否 | 服務端事件的唯一識別符號 | event_1516 |
| type | 字串 | 否 | 事件型別 | input_audio_buffer.speech_started |
| audio_start_ms | 整數 | 否 | 從會話開始到檢測到語音的毫秒數 | 1000 |
| item_id | 字串 | 否 | 語音停止時將建立的使用者訊息項的ID | msg_003 |
input_audio_buffer.speech_stopped
在伺服器語音檢測模式下,當檢測到語音輸入停止時返回此事件。
| 引數 | 型別 | 必需 | 說明 | 示例值 |
|---|---|---|---|---|
| event_id | 字串 | 否 | 服務端事件的唯一識別符號 | event_1718 |
| type | 字串 | 否 | 事件型別 | input_audio_buffer.speech_stopped |
| audio_start_ms | 整數 | 否 | 從會話開始到檢測到語音停止的毫秒數 | 2000 |
| item_id | 字串 | 否 | 將要建立的使用者訊息項的ID | msg_003 |
response.created
當建立新的響應時返回此事件。
| 引數 | 型別 | 必需 | 說明 | 示例值 |
|---|---|---|---|---|
| event_id | 字串 | 否 | 服務端事件的唯一識別符號 | event_2930 |
| type | 字串 | 否 | 事件型別 | response.created |
| response.id | 字串 | 否 | 響應的唯一識別符號 | resp_001 |
| response.object | 字串 | 否 | 物件型別 | realtime.response |
| response.status | 字串 | 否 | 響應的狀態 | in_progress |
| response.status_details | 物件 | 否 | 狀態的附加詳細資訊 | null |
| response.output | 字串陣列 | 否 | 響應生成的輸出項列表 | ["[]"] |
| response.usage | 物件 | 否 | 響應的使用統計資訊 | null |
response.done
當響應完成流式傳輸時返回此事件。
| 引數 | 型別 | 必需 | 說明 | 示例值 |
|---|---|---|---|---|
| event_id | 字串 | 否 | 服務端事件的唯一識別符號 | event_3132 |
| type | 字串 | 否 | 事件型別 | response.done |
| response.id | 字串 | 否 | 響應的唯一識別符號 | resp_001 |
| response.object | 字串 | 否 | 物件型別 | realtime.response |
| response.status | 字串 | 否 | 響應的最終狀態 | completed/cancelled/failed/incomplete |
| response.status_details | 物件 | 否 | 狀態的附加詳細資訊 | null |
| response.output | 字串陣列 | 否 | 響應生成的輸出項列表 | ["[...]"] |
| response.usage.total_tokens | 整數 | 否 | 總token數 | 50 |
| response.usage.input_tokens | 整數 | 否 | 輸入token數 | 20 |
| response.usage.output_tokens | 整數 | 否 | 輸出token數 | 30 |
response.output_item.added
當響應生成過程中建立新的輸出項時返回此事件。
| 引數 | 型別 | 必需 | 說明 | 示例值 |
|---|---|---|---|---|
| event_id | 字串 | 否 | 服務端事件的唯一識別符號 | event_3334 |
| type | 字串 | 否 | 事件型別 | response.output_item.added |
| response_id | 字串 | 否 | 輸出項所屬的響應ID | resp_001 |
| output_index | 字串 | 否 | 輸出項在響應中的索引 | 0 |
| item.id | 字串 | 否 | 輸出項的唯一識別符號 | msg_007 |
| item.object | 字串 | 否 | 物件型別 | realtime.item |
| item.type | 字串 | 否 | 輸出項型別 | message/function_call/function_call_output |
| item.status | 字串 | 否 | 輸出項狀態 | in_progress/completed |
| item.role | 字串 | 否 | 與輸出項關聯的角色 | assistant |
| item.content | 陣列 | 否 | 輸出項的內容 | ["type", "text", "audio", "transcript"] |
response.output_item.done
當輸出項完成流式傳輸時返回此事件。
| 引數 | 型別 | 必需 | 說明 | 示例值 |
|---|---|---|---|---|
| event_id | 字串 | 否 | 服務端事件的唯一識別符號 | event_3536 |
| type | 字串 | 否 | 事件型別 | response.output_item.done |
| response_id | 字串 | 否 | 輸出項所屬的響應ID | resp_001 |
| output_index | 字串 | 否 | 輸出項在響應中的索引 | 0 |
| item.id | 字串 | 否 | 輸出項的唯一識別符號 | msg_007 |
| item.object | 字串 | 否 | 物件型別 | realtime.item |
| item.type | 字串 | 否 | 輸出項型別 | message/function_call/function_call_output |
| item.status | 字串 | 否 | 輸出項的最終狀態 | completed/incomplete |
| item.role | 字串 | 否 | 與輸出項關聯的角色 | assistant |
| item.content | 陣列 | 否 | 輸出項的內容 | ["type", "text", "audio", "transcript"] |
response.content_part.added
當響應生成過程中向助手訊息項新增新的內容部分時返回此事件。
| 引數 | 型別 | 必需 | 說明 | 示例值 |
|---|---|---|---|---|
| event_id | 字串 | 否 | 服務端事件的唯一識別符號 | event_3738 |
| type | 字串 | 否 | 事件型別 | response.content_part.added |
| response_id | 字串 | 否 | 響應的ID | resp_001 |
| item_id | 字串 | 否 | 新增內容部分的訊息項ID | msg_007 |
| output_index | 整數 | 否 | 輸出項在響應中的索引 | 0 |
| content_index | 整數 | 否 | 內容部分在訊息項內容陣列中的索引 | 0 |
| part.type | 字串 | 否 | 內容型別 | text/audio |
| part.text | 字串 | 否 | 文字內容 | "Hello" |
| part.audio | 字串 | 否 | Base64編碼的音訊資料 | "base64_encoded_audio_data" |
| part.transcript | 字串 | 否 | 音訊的轉寫文字 | "Hello" |
response.content_part.done
當助手訊息項中的內容部分完成流式傳輸時返回此事件。
| 引數 | 型別 | 必需 | 說明 | 示例值 |
|---|---|---|---|---|
| event_id | 字串 | 否 | 服務端事件的唯一識別符號 | event_3940 |
| type | 字串 | 否 | 事件型別 | response.content_part.done |
| response_id | 字串 | 否 | 響應的ID | resp_001 |
| item_id | 字串 | 否 | 新增內容部分的訊息項ID | msg_007 |
| output_index | 整數 | 否 | 輸出項在響應中的索引 | 0 |
| content_index | 整數 | 否 | 內容部分在訊息項內容陣列中的索引 | 0 |
| part.type | 字串 | 否 | 內容型別 | text/audio |
| part.text | 字串 | 否 | 文字內容 | "Hello" |
| part.audio | 字串 | 否 | Base64編碼的音訊資料 | "base64_encoded_audio_data" |
| part.transcript | 字串 | 否 | 音訊的轉寫文字 | "Hello" |
response.text.delta
當"text"型別內容部分的文字值更新時返回此事件。
| 引數 | 型別 | 必需 | 說明 | 示例值 |
|---|---|---|---|---|
| event_id | 字串 | 否 | 服務端事件的唯一識別符號 | event_4142 |
| type | 字串 | 否 | 事件型別 | response.text.delta |
| response_id | 字串 | 否 | 響應的ID | resp_001 |
| item_id | 字串 | 否 | 訊息項的ID | msg_007 |
| output_index | 整數 | 否 | 輸出項在響應中的索引 | 0 |
| content_index | 整數 | 否 | 內容部分在訊息項內容陣列中的索引 | 0 |
| delta | 字串 | 否 | 文字增量更新內容 | "Sure, I can h" |
response.text.done
當"text"型別內容部分的文字流式傳輸完成時返回此事件。
| 引數 | 型別 | 必需 | 說明 | 示例值 |
|---|---|---|---|---|
| event_id | 字串 | 否 | 服務端事件的唯一識別符號 | event_4344 |
| type | 字串 | 否 | 事件型別 | response.text.done |
| response_id | 字串 | 否 | 響應的ID | resp_001 |
| item_id | 字串 | 否 | 訊息項的ID | msg_007 |
| output_index | 整數 | 否 | 輸出項在響應中的索引 | 0 |
| content_index | 整數 | 否 | 內容部分在訊息項內容陣列中的索引 | 0 |
| delta | 字串 | 否 | 最終的完整文字內容 | "Sure, I can help with that." |
response.audio_transcript.delta
當模型生成的音訊輸出轉寫內容更新時返回此事件。
| 引數 | 型別 | 必需 | 說明 | 示例值 |
|---|---|---|---|---|
| event_id | 字串 | 否 | 服務端事件的唯一識別符號 | event_4546 |
| type | 字串 | 否 | 事件型別 | response.audio_transcript.delta |
| response_id | 字串 | 否 | 響應的ID | resp_001 |
| item_id | 字串 | 否 | 訊息項的ID | msg_008 |
| output_index | 整數 | 否 | 輸出項在響應中的索引 | 0 |
| content_index | 整數 | 否 | 內容部分在訊息項內容陣列中的索引 | 0 |
| delta | 字串 | 否 | 轉寫文字的增量更新內容 | "Hello, how can I a" |
response.audio_transcript.done
當模型生成的音訊輸出轉寫完成流式傳輸時返回此事件。
| 引數 | 型別 | 必需 | 說明 | 示例值 |
|---|---|---|---|---|
| event_id | 字串 | 否 | 服務端事件的唯一識別符號 | event_4748 |
| type | 字串 | 否 | 事件型別 | response.audio_transcript.done |
| response_id | 字串 | 否 | 響應的ID | resp_001 |
| item_id | 字串 | 否 | 訊息項的ID | msg_008 |
| output_index | 整數 | 否 | 輸出項在響應中的索引 | 0 |
| content_index | 整數 | 否 | 內容部分在訊息項內容陣列中的索引 | 0 |
| transcript | 字串 | 否 | 音訊的最終完整轉寫文字 | "Hello, how can I assist you today?" |
response.audio.delta
當模型生成的音訊內容更新時返回此事件。
| 引數 | 型別 | 必需 | 說明 | 示例值 |
|---|---|---|---|---|
| event_id | 字串 | 否 | 服務端事件的唯一識別符號 | event_4950 |
| type | 字串 | 否 | 事件型別 | response.audio.delta |
| response_id | 字串 | 否 | 響應的ID | resp_001 |
| item_id | 字串 | 否 | 訊息項的ID | msg_008 |
| output_index | 整數 | 否 | 輸出項在響應中的索引 | 0 |
| content_index | 整數 | 否 | 內容部分在訊息項內容陣列中的索引 | 0 |
| delta | 字串 | 否 | Base64編碼的音訊資料增量 | "Base64EncodedAudioDelta" |
response.audio.done
當模型生成的音訊完成時返回此事件。
| 引數 | 型別 | 必需 | 說明 | 示例值 |
|---|---|---|---|---|
| event_id | 字串 | 否 | 服務端事件的唯一識別符號 | event_5152 |
| type | 字串 | 否 | 事件型別 | response.audio.done |
| response_id | 字串 | 否 | 響應的ID | resp_001 |
| item_id | 字串 | 否 | 訊息項的ID | msg_008 |
| output_index | 整數 | 否 | 輸出項在響應中的索引 | 0 |
| content_index | 整數 | 否 | 內容部分在訊息項內容陣列中的索引 | 0 |
函式呼叫
response.function_call_arguments.delta
當模型生成的函式呼叫引數更新時返回此事件。
| 引數 | 型別 | 必需 | 說明 | 示例值 |
|---|---|---|---|---|
| event_id | 字串 | 否 | 服務端事件的唯一識別符號 | event_5354 |
| type | 字串 | 否 | 事件型別 | response.function_call_arguments.delta |
| response_id | 字串 | 否 | 響應的ID | resp_002 |
| item_id | 字串 | 否 | 訊息項的ID | fc_001 |
| output_index | 整數 | 否 | 輸出項在響應中的索引 | 0 |
| call_id | 字串 | 否 | 函式呼叫的ID | call_001 |
| delta | 字串 | 否 | JSON格式的函式呼叫引數增量 | {"location": "San"} |
response.function_call_arguments.done
當模型生成的函式呼叫引數完成流式傳輸時返回此事件。
| 引數 | 型別 | 必需 | 說明 | 示例值 |
|---|---|---|---|---|
| event_id | 字串 | 否 | 服務端事件的唯一識別符號 | event_5556 |
| type | 字串 | 否 | 事件型別 | response.function_call_arguments.done |
| response_id | 字串 | 否 | 響應的ID | resp_002 |
| item_id | 字串 | 否 | 訊息項的ID | fc_001 |
| output_index | 整數 | 否 | 輸出項在響應中的索引 | 0 |
| call_id | 字串 | 否 | 函式呼叫的ID | call_001 |
| arguments | 字串 | 否 | 最終的完整函式呼叫引數(JSON格式) | {"location": "San Francisco"} |
其他狀態更新
rate_limits.updated
在每個 "response.done" 事件之後觸發,用於指示更新後的速率限制。
| 引數 | 型別 | 必需 | 說明 | 示例值 |
|---|---|---|---|---|
| event_id | 字串 | 否 | 服務端事件的唯一識別符號 | event_5758 |
| type | 字串 | 否 | 事件型別 | rate_limits.updated |
| rate_limits | 物件陣列 | 否 | 速率限制資訊列表 | [{"name": "requests_per_min", "limit": 60, "remaining": 45, "reset_seconds": 35}] |
conversation.created
當對話建立時返回此事件。
| 引數 | 型別 | 必需 | 說明 | 示例值 |
|---|---|---|---|---|
| event_id | 字串 | 否 | 服務端事件的唯一識別符號 | event_9101 |
| type | 字串 | 否 | 事件型別 | conversation.created |
| conversation | 物件 | 否 | 對話資源物件 | {"id": "conv_001", "object": "realtime.conversation"} |
conversation.item.created
當對話項建立時返回此事件。
| 引數 | 型別 | 必需 | 說明 | 示例值 |
|---|---|---|---|---|
| event_id | 字串 | 否 | 服務端事件的唯一識別符號 | event_1920 |
| type | 字串 | 否 | 事件型別 | conversation.item.created |
| previous_item_id | 字串 | 否 | 前一個對話項的ID | msg_002 |
| item | 物件 | 否 | 對話項物件 | {"id": "msg_003", "object": "realtime.item", "type": "message", "status": "completed", "role": "user", "content": [{"type": "text", "text": "Hello"}]} |
session.created
當會話建立時返回此事件。
| 引數 | 型別 | 必需 | 說明 | 示例值 |
|---|---|---|---|---|
| event_id | 字串 | 否 | 服務端事件的唯一識別符號 | event_1234 |
| type | 字串 | 否 | 事件型別 | session.created |
| session | 物件 | 否 | 會話物件 | {"id": "sess_001", "object": "realtime.session", "model": "gpt-4", "modalities": ["text", "audio"]} |
session.updated
當會話更新時返回此事件。
| 引數 | 型別 | 必需 | 說明 | 示例值 |
|---|---|---|---|---|
| event_id | 字串 | 否 | 服務端事件的唯一識別符號 | event_5678 |
| type | 字串 | 否 | 事件型別 | session.updated |
| session | 物件 | 否 | 更新後的會話物件 | {"id": "sess_001", "object": "realtime.session", "model": "gpt-4", "modalities": ["text", "audio"]} |
速率限制事件參數列
| 引數 | 型別 | 必需 | 說明 | 示例值 |
|---|---|---|---|---|
| name | 字串 | 是 | 限制名稱 | requests_per_min |
| limit | 整數 | 是 | 限制值 | 60 |
| remaining | 整數 | 是 | 剩餘可用量 | 45 |
| reset_seconds | 整數 | 是 | 重置時間(秒) | 35 |
函式呼叫參數列
| 引數 | 型別 | 必需 | 說明 | 示例值 |
|---|---|---|---|---|
| type | 字串 | 是 | 函式型別 | function |
| name | 字串 | 是 | 函式名稱 | get_weather |
| description | 字串 | 否 | 函式描述 | Get the current weather |
| parameters | 物件 | 是 | 函式引數定義 | {"type": "object", "properties": {...}} |
音訊格式參數列
| 引數 | 型別 | 說明 | 可選值 |
|---|---|---|---|
| sample_rate | 整數 | 取樣率 | 8000, 16000, 24000, 44100, 48000 |
| channels | 整數 | 聲道數 | 1 (單聲道), 2 (立體聲) |
| bits_per_sample | 整數 | 取樣位數 | 16 (pcm16), 8 (g711) |
| encoding | 字串 | 編碼方式 | pcm16, g711_ulaw, g711_alaw |
語音檢測參數列
| 引數 | 型別 | 說明 | 預設值 | 範圍 |
|---|---|---|---|---|
| threshold | 浮點數 | VAD 啟用閾值 | 0.5 | 0.0-1.0 |
| prefix_padding_ms | 整數 | 語音字首填充(毫秒) | 500 | 0-5000 |
| silence_duration_ms | 整數 | 靜音檢測時長(毫秒) | 1000 | 100-10000 |
工具選擇參數列
| 引數 | 型別 | 說明 | 可選值 |
|---|---|---|---|
| tool_choice | 字串 | 工具選擇方式 | auto, none, required |
| tools | 陣列 | 可用工具列表 | [{type, name, description, parameters}] |
模型配置參數列
| 引數 | 型別 | 說明 | 範圍/可選值 | 預設值 |
|---|---|---|---|---|
| temperature | 浮點數 | 取樣溫度 | 0.0-2.0 | 1.0 |
| max_output_tokens | 整數/字串 | 最大輸出長度 | 1-4096/"inf" | "inf" |
| modalities | 字串陣列 | 響應模態 | ["text", "audio"] | ["text"] |
| voice | 字串 | 語音型別 | alloy, echo, shimmer | alloy |
事件通用參數列
| 引數 | 型別 | 必需 | 說明 | 示例值 |
|---|---|---|---|---|
| event_id | 字串 | 是 | 事件的唯一識別符號 | event_123 |
| type | 字串 | 是 | 事件型別 | session.update |
| timestamp | 整數 | 否 | 事件發生的時間戳(毫秒) | 1677649363000 |
會話狀態參數列
| 引數 | 型別 | 說明 | 可選值 |
|---|---|---|---|
| status | 字串 | 會話狀態 | active, ended, error |
| error | 物件 | 錯誤資訊 | {"type": "error_type", "message": "error message"} |
| metadata | 物件 | 會話後設資料 | {"client_id": "web", "session_type": "chat"} |
對話項狀態參數列
| 引數 | 型別 | 說明 | 可選值 |
|---|---|---|---|
| status | 字串 | 對話項狀態 | completed, in_progress, incomplete |
| role | 字串 | 傳送者角色 | user, assistant, system |
| type | 字串 | 對話項型別 | message, function_call, function_call_output |
內容型別參數列
| 引數 | 型別 | 說明 | 可選值 |
|---|---|---|---|
| type | 字串 | 內容型別 | text, audio, transcript |
| format | 字串 | 內容格式 | plain, markdown, html |
| encoding | 字串 | 編碼方式 | utf-8, base64 |
響應狀態參數列
| 引數 | 型別 | 說明 | 可選值 |
|---|---|---|---|
| status | 字串 | 響應狀態 | completed, cancelled, failed, incomplete |
| status_details | 物件 | 狀態詳情 | {"reason": "user_cancelled"} |
| usage | 物件 | 使用統計 | {"total_tokens": 50, "input_tokens": 20, "output_tokens": 30} |
音訊轉寫參數列
| 引數 | 型別 | 說明 | 示例值 |
|---|---|---|---|
| enabled | 布林值 | 是否啟用轉寫 | true |
| model | 字串 | 轉寫模型 | whisper-1 |
| language | 字串 | 轉寫語言 | en, zh, auto |
| prompt | 字串 | 轉寫提示詞 | "Transcript of a conversation" |
音訊流參數列
| 引數 | 型別 | 說明 | 可選值 |
|---|---|---|---|
| chunk_size | 整數 | 音訊塊大小(位元組) | 1024, 2048, 4096 |
| latency | 字串 | 延遲模式 | low, balanced, high |
| compression | 字串 | 壓縮方式 | none, opus, mp3 |
WebRTC 配置參數列
| 引數 | 型別 | 說明 | 預設值 |
|---|---|---|---|
| ice_servers | 陣列 | ICE 伺服器列表 | [{"urls": "stun:stun.l.google.com:19302"}] |
| audio_constraints | 物件 | 音訊約束 | {"echoCancellation": true} |
| connection_timeout | 整數 | 連線超時(毫秒) | 30000 |