Skip to content
Cloudflare Docs
非官方翻译 - 此文档为非官方中文翻译版本,仅供参考。如有疑问请以 英文官方文档 为准。

非实时 WebSockets API

非实时 WebSockets API 允许您为 AI 请求建立持久连接,而无需重复握手。这种方法非常适合不需要实时交互但仍能从减少的延迟和持续通信中受益的应用程序。

设置 WebSockets API

  1. 生成具有相应 AI 网关运行权限的 AI 网关令牌,并选择使用经过身份验证的网关。
  2. 通过将 https:// 替换为 wss:// 来修改您的通用端点 URL,以启动 WebSocket 连接:
    wss://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}
  3. 打开一个使用具有 AI 网关运行权限的 Cloudflare 令牌进行身份验证的 WebSocket 连接。

示例请求

import WebSocket from "ws";
const ws = new WebSocket(
"wss://gateway.ai.cloudflare.com/v1/my-account-id/my-gateway/",
{
headers: {
"cf-aig-authorization": "Bearer AI_GATEWAY_TOKEN",
},
},
);
ws.send(
JSON.stringify({
type: "universal.create",
request: {
eventId: "my-request",
provider: "workers-ai",
endpoint: "@cf/meta/llama-3.1-8b-instruct",
headers: {
Authorization: "Bearer WORKERS_AI_TOKEN",
"Content-Type": "application/json",
},
query: {
prompt: "tell me a joke",
},
},
}),
);
ws.on("message", function incoming(message) {
console.log(message.toString());
});

示例响应

{
"type": "universal.created",
"metadata": {
"cacheStatus": "MISS",
"eventId": "my-request",
"logId": "01JC3R94FRD97JBCBX3S0ZAXKW",
"step": "0",
"contentType": "application/json"
},
"response": {
"result": {
"response": "Why was the math book sad? Because it had too many problems. Would you like to hear another one?"
},
"success": true,
"errors": [],
"messages": []
}
}

示例流式请求

对于流式请求,AI 网关发送一个初始消息,其中包含请求元数据,指示流已开始:

{
"type": "universal.created",
"metadata": {
"cacheStatus": "MISS",
"eventId": "my-request",
"logId": "01JC40RB3NGBE5XFRZGBN07572",
"step": "0",
"contentType": "text/event-stream"
}
}

在此初始消息之后,所有流式块在从推理提供商到达时都会实时中继到 WebSocket 连接。在这些流式块的元数据中仅包含 eventId 字段。eventId 允许 AI 网关在每个消息中包含一个客户端定义的 ID,即使在流式 WebSocket 环境中也是如此。

{
"type": "universal.stream",
"metadata": {
"eventId": "my-request"
},
"response": {
"response": "would"
}
}

一旦请求的所有块都已流式传输完毕,AI 网关会发送最后一条消息以表示请求完成。为了增加灵活性,此消息再次包含所有元数据,即使它最初是在流式处理开始时提供的。

{
"type": "universal.done",
"metadata": {
"cacheStatus": "MISS",
"eventId": "my-request",
"logId": "01JC40RB3NGBE5XFRZGBN07572",
"step": "0",
"contentType": "text/event-stream"
}
}