跳转到内容
本站为社区维护的非官方中文翻译,与 Cloudflare, Inc. 无关联。本站不提供账户登录,请勿在任何非官方站点输入您的 Cloudflare 账户信息。官方文档:developers.cloudflare.com

使用动态路由

最后更新 查看 MarkdownAgent 设置

以 dynamic/<your-dynamic-route-name> 格式,用路由名称代替模型名称。动态路由仅接受 OpenAI 聊天补全请求格式;Anthropic Messages 等其他格式会返回 400 错误。

路由仅在创建它的网关内有效。使用 REST API 或 AI 绑定时,请明确指定该网关;否则,请求会发送到默认网关并返回 404 错误。

示例

REST API

将路由名称作为 model 传给 /ai/v1/chat/completions,并将 cf-aig-gateway-id 设置为该路由所属的网关。

curl -X POST "https://api.cloudflare.com/client/v4/accounts/{account_id}/ai/v1/chat/completions" \
  --header 'Authorization: Bearer {CLOUDFLARE_API_TOKEN}' \
  --header 'cf-aig-gateway-id: {gateway_id}' \
  --header 'Content-Type: application/json' \
  --data '{
    "model": "dynamic/<your-dynamic-route-name>",
    "messages": [
      {
        "role": "user",
        "content": "What is Cloudflare?"
      }
    ]
  }'

OpenAI SDK

将 SDK 指向您网关上兼容 OpenAI 的端点。

import OpenAI from "openai";

const cloudflareToken = "CF_AIG_TOKEN";
const accountId = "{account_id}";
const gatewayId = "{gateway_id}";
const baseURL = `https://gateway.ai.cloudflare.com/v1/${accountId}/${gatewayId}/compat`;

const openai = new OpenAI({
	apiKey: cloudflareToken,
	baseURL,
});

try {
	const model = "dynamic/<your-dynamic-route-name>";
	const messages = [{ role: "user", content: "What is a neuron?" }];
	const chatCompletion = await openai.chat.completions.create({
		model,
		messages,
	});
	const response = chatCompletion.choices[0].message;
	console.log(response);
} catch (e) {
	console.error(e);
}

Fetch

curl -X POST https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/compat/chat/completions \
  --header 'cf-aig-authorization: Bearer {CF_AIG_TOKEN}' \
  --header 'Content-Type: application/json' \
  --data '{
    "model": "dynamic/<your-dynamic-route-name>",
    "messages": [
      {
        "role": "user",
        "content": "What is Cloudflare?"
      }
    ]
  }'

Workers

通过 AI 绑定从 Worker 调用动态路由。将路由名称作为模型传入,并在 gateway 选项中指定该路由所属的网关。

index.tsts
export interface Env {
	AI: Ai;
}

export default {
	async fetch(request: Request, env: Env) {
		const response = await env.AI.run(
			"dynamic/<your-dynamic-route-name>",
			{
				messages: [{ role: "user", content: "What is Cloudflare?" }],
			},
			{
				gateway: {
					id: "{gateway_id}",
				},
			},
		);
		return Response.json(response);
	},
};

响应元数据

动态路由的响应与模型响应相同。还有额外元数据用于告知所用模型和提供商,你可以检查以下标头:

  • cf-aig-model - 所用模型
  • cf-aig-provider - 所用提供商的 slug

这篇文档对您有帮助吗?