AI 网关中的自定义元数据允许您使用用户 ID 或其他标识符标记请求,从而更好地跟踪和分析您的请求。元数据值可以是字符串、数字或布尔值,并将出现在您的日志中,使您可以轻松搜索和过滤数据。
- 自定义标记:向您的请求添加用户 ID、团队名称、测试指示器和其他相关信息。
- 增强日志记录:元数据出现在您的日志中,允许详细检查和故障排除。
- 搜索和过滤:使用元数据高效搜索和过滤已记录的请求。
- 字符串
- 数字
- 布尔值
要在使用 cURL 的请求中包含自定义元数据:
curl https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/openai/chat/completions \ --header 'Authorization: Bearer {api_token}' \ --header 'Content-Type: application/json' \ --header 'cf-aig-metadata: {"team": "AI", "user": 12345, "test":true}' \ --data '{"model": "gpt-4o", "messages": [{"role": "user", "content": "What should I eat for lunch?"}]}'
要在使用 OpenAI SDK 的请求中包含自定义元数据:
import OpenAI from "openai";
export default { async fetch(request, env, ctx) { const openai = new OpenAI({ apiKey: env.OPENAI_API_KEY, baseURL: "https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/openai", });
try { const chatCompletion = await openai.chat.completions.create( { model: "gpt-4o", messages: [{ role: "user", content: "What should I eat for lunch?" }], max_tokens: 50, }, { headers: { "cf-aig-metadata": JSON.stringify({ user: "JaneDoe", team: 12345, test: true, }), }, }, );
const response = chatCompletion.choices[0].message; return new Response(JSON.stringify(response)); } catch (e) { console.log(e); return new Response(e); } },};
要在使用绑定的请求中包含自定义元数据:
export default { async fetch(request, env, ctx) { const aiResp = await env.AI.run( "@cf/mistral/mistral-7b-instruct-v0.1", { prompt: "What should I eat for lunch?" }, { gateway: { id: "gateway_id", metadata: { team: "AI", user: 12345, test: true }, }, }, );
return new Response(aiResp); },};
- @2025 Cloudflare Ubitools
- Cf Repo