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

OpenRouter

OpenRouter 是一个提供统一接口来访问和使用大型语言模型 (LLMs) 的平台。

端点

https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/openrouter

URL 结构

在向 OpenRouter 发出请求时,将您当前使用的 URL 中的 https://openrouter.ai/api/v1/chat/completions 替换为 https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/openrouter

前提条件

在向 OpenRouter 发出请求时,确保您具有以下内容:

  • 您的 AI 网关账户 ID。
  • 您的 AI 网关网关名称。
  • 一个有效的 OpenRouter API 令牌或来自原始模型提供商的令牌。
  • 您要使用的 OpenRouter 模型的名称。

示例

cURL

请求
curl -X POST https://gateway.ai.cloudflare.com/v1/ACCOUNT_TAG/GATEWAY/openrouter/v1/chat/completions \
--header 'content-type: application/json' \
--header 'Authorization: Bearer OPENROUTER_TOKEN' \
--data '{
"model": "openai/gpt-3.5-turbo",
"messages": [
{
"role": "user",
"content": "What is Cloudflare?"
}
]
}'

在 JavaScript 中使用 OpenAI SDK

如果您使用 JavaScript 中的 OpenAI SDK,您可以这样设置您的端点:

JavaScript
import OpenAI from "openai";
const openai = new OpenAI({
apiKey: env.OPENROUTER_TOKEN,
baseURL:
"https://gateway.ai.cloudflare.com/v1/ACCOUNT_TAG/GATEWAY/openrouter",
});
try {
const chatCompletion = await openai.chat.completions.create({
model: "openai/gpt-3.5-turbo",
messages: [{ role: "user", content: "What is Cloudflare?" }],
});
const response = chatCompletion.choices[0].message;
return new Response(JSON.stringify(response));
} catch (e) {
return new Response(e);
}