リアルタイム
Realtime API を使って、低遅延・マルチモーダルな体験を構築しましょう。
OPE.AI Realtime API は、音声対話やリアルタイム文字起こしを含む低遅延のマルチモーダル対話を可能にします。
WebSocket で接続する
WebSocket はリアルタイムデータ転送のための広くサポートされた API であり、OPE.AI Realtime API にサーバー間で接続する際に最適な手段です。
概要
サーバー間での統合では、あなたのバックエンドシステムが WebSocket 経由で Realtime API に直接接続します。この接続には、標準の API キー を使用して認証できます。トークンは安全なバックエンド環境内でのみ利用されることが前提です。
標準の API トークンは、必ず安全なサーバー側環境でのみ使用してください。
接続情報
WebSocket 接続に必要な情報は以下の通りです:
URL | wss://api-platform.ope.ai/v1/realtime |
---|---|
クエリ パラメータ |
model 接続対象の Realtime モデル ID(例: opeai/AudioLLM/Voice2 ) |
ヘッダー | Authorization: Bearer $API_KEY OpenAI-Beta: realtime=v1 このヘッダーはベータ期間中は必須です。 |
以下は、この接続情報を使って WebSocket 接続を初期化するいくつかの例です:
import WebSocket from "ws";
const url = "wss://api-platform.ope.ai/v1/realtime?model=opeai/AudioLLM/Voice2";
const ws = new WebSocket(url, {
headers: {
"Authorization": "Bearer " + process.env.API_KEY,
"OpenAI-Beta": "realtime=v1",
},
});
ws.on("open", () => console.log("接続完了"));
ws.on("message", (msg) => console.log(JSON.parse(msg.toString())));
import os
import json
import websocket
API_KEY = os.environ.get("API_KEY")
url = "wss://api-platform.ope.ai/v1/realtime?model=opeai/AudioLLM/Voice2"
headers = [
"Authorization: Bearer " + API_KEY,
"OpenAI-Beta: realtime=v1"
]
def on_open(ws):
print("サーバーに接続しました。")
def on_message(ws, message):
data = json.loads(message)
print("イベント受信:", json.dumps(data, indent=2))
ws = websocket.WebSocketApp(
url,
header=headers,
on_open=on_open,
on_message=on_message,
)
ws.run_forever()
const ws = new WebSocket(
"wss://api-platform.ope.ai/v1/realtime?model=opeai/AudioLLM/Voice2",
[
"realtime",
// 認証
"openai-insecure-api-key." + API_KEY,
// ベータ用プロトコル(必須)
"openai-beta.realtime-v1"
]
);
ws.on("open", function open() {
console.log("サーバーに接続されました。");
});
ws.on("message", function incoming(message) {
console.log(message.data);
});
WebSocket 接続に必要な情報は以下の通りです:
URL | wss://api-platform.ope.ai/v1/realtime |
---|---|
クエリ パラメータ |
intent 接続の目的: transcription |
ヘッダー | Authorization: Bearer $API_KEY OpenAI-Beta: realtime=v1 このヘッダーはベータ期間中は必須です。 |
以下は、この接続情報を使って WebSocket 接続を初期化するいくつかの例です:
import WebSocket from "ws";
const url = "wss://api-platform.ope.ai/v1/realtime?intent=transcription";
const ws = new WebSocket(url, {
headers: {
"Authorization": "Bearer " + process.env.API_KEY,
"OpenAI-Beta": "realtime=v1",
},
});
ws.on("open", function open() {
console.log("サーバーに接続されました。");
});
ws.on("message", function incoming(message) {
console.log(JSON.parse(message.toString()));
});
import os
import json
import websocket
API_KEY = os.environ.get("API_KEY")
url = "wss://api-platform.ope.ai/v1/realtime?intent=transcription"
headers = [
"Authorization: Bearer " + API_KEY,
"OpenAI-Beta: realtime=v1"
]
def on_open(ws):
print("サーバーに接続しました。")
def on_message(ws, message):
data = json.loads(message)
print("イベント受信:", json.dumps(data, indent=2))
ws = websocket.WebSocketApp(
url,
header=headers,
on_open=on_open,
on_message=on_message,
)
ws.run_forever()
const ws = new WebSocket(
"wss://api-platform.ope.ai/v1/realtime?intent=transcription",
[
"realtime",
// 認証
"openai-insecure-api-key." + API_KEY,
// ベータ用プロトコル(必須)
"openai-beta.realtime-v1"
]
);
ws.on("open", function open() {
console.log("サーバーに接続されました。");
});
ws.on("message", function incoming(message) {
console.log(message.data);
});