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

Anthropic

Anthropic 帮助构建可靠、可解释和可操控的 AI 系统。

端点

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

前提条件

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

  • 您的 AI 网关账户 ID。
  • 您的 AI 网关网关名称。
  • 一个有效的 Anthropic API 令牌。
  • 您要使用的 Anthropic 模型的名称。

示例

cURL

示例获取请求
curl https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/anthropic/v1/messages \
--header 'x-api-key: {anthropic_api_key}' \
--header 'anthropic-version: 2023-06-01' \
--header 'Content-Type: application/json' \
--data '{
"model": "claude-3-opus-20240229",
"max_tokens": 1024,
"messages": [
{"role": "user", "content": "What is Cloudflare?"}
]
}'

在 JavaScript 中使用 Anthropic SDK

如果您使用 @anthropic-ai/sdk,您可以这样设置您的端点:

JavaScript
import Anthropic from "@anthropic-ai/sdk";
const apiKey = env.ANTHROPIC_API_KEY;
const accountId = "{account_id}";
const gatewayId = "{gateway_id}";
const baseURL = `https://gateway.ai.cloudflare.com/v1/${accountId}/${gatewayId}/anthropic`;
const anthropic = new Anthropic({
apiKey,
baseURL,
});
const model = "claude-3-opus-20240229";
const messages = [{ role: "user", content: "What is Cloudflare?" }];
const maxTokens = 1024;
const message = await anthropic.messages.create({
model,
messages,
max_tokens: maxTokens,
});

OpenAI-Compatible Endpoint

You can also use the OpenAI-compatible endpoint (/ai-gateway/chat-completion/) to access Anthropic models using the OpenAI API schema. To do so, send your requests to:

https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/compat/chat/completions

Specify:

{
"model": "anthropic/{model}"
}