本教程引导你在 Cloudflare 仪表板上使用 Workers AI 创建第一个 AI Gateway。目标受众是 AI Gateway 和 Workers AI 的新手。创建 AI Gateway 使用户能够高效管理和保护 AI 请求,使其能够利用 AI 模型进行内容生成、数据处理或预测分析等任务,并增强控制和性能。
- 注册:如果你没有 Cloudflare 账户,请注册 ↗。
- 登录:通过登录 Cloudflare 仪表板 ↗ 访问 Cloudflare 仪表板。
然后,创建新的 AI Gateway。
Go to AI Gateway ↗
- 登录 Cloudflare 仪表板 ↗ 并选择你的账户。
- 前往 AI > AI Gateway。
- 选择 Create Gateway(创建网关)。
- 输入 Gateway name(网关名称)。注意:网关名称最多 64 个字符。
- 选择 Create(创建)。
要使用 API 设置 AI Gateway:
-
创建 API token,并授予以下权限:
AI Gateway - ReadAI Gateway - Edit
-
获取你的 Account ID。
-
使用该 API token 和 Account ID,向 Cloudflare API 发送
POST请求。
- 在 AI Gateway 部分,选择你创建的 gateway。
- 选择 Workers AI 作为提供商,以设置 Workers AI 专用端点。 你将收到用于发送请求的端点 URL。
-
在 Cloudflare 仪表板中前往 AI > Workers AI。
-
选择 Use REST API(使用 REST API) 并按照步骤创建和复制 API 令牌和 Account ID。
-
使用 REST API 发送请求。将
$CLOUDFLARE_ACCOUNT_ID和$CLOUDFLARE_API_TOKEN替换为你的实际账户 ID 和 API 令牌:# Run `wrangler whoami` to get your account ID to replace $CLOUDFLARE_ACCOUNT_ID, # and `wrangler auth token` to get an auth token to replace $CLOUDFLARE_API_TOKEN. curl -X POST "https://api.cloudflare.com/client/v4/accounts/$CLOUDFLARE_ACCOUNT_ID/ai/v1/chat/completions" \ --header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \ --header "cf-aig-gateway-id: default" \ --header "Content-Type: application/json" \ --data '{ "model": "@cf/moonshotai/kimi-k2.6", "messages": [{"role": "user", "content": "What is Cloudflare?"}] }'
预期输出类似于:
{"result":{"response":"I'd be happy to explain what Cloudflare is.\n\nCloudflare is a cloud-based service that provides a range of features to help protect and improve the performance, security, and reliability of websites, applications, and other online services. Think of it as a shield for your online presence!\n\nHere are some of the key things Cloudflare does:\n\n1. **Content Delivery Network (CDN)**: Cloudflare has a network of servers all over the world. When you visit a website that uses Cloudflare, your request is sent to the nearest server, which caches a copy of the website's content. This reduces the time it takes for the content to load, making your browsing experience faster.\n2. **DDoS Protection**: Cloudflare protects against Distributed Denial-of-Service (DDoS) attacks. This happens when a website is overwhelmed with traffic from multiple sources to make it unavailable. Cloudflare filters out this traffic, ensuring your site remains accessible.\n3. **Firewall**: Cloudflare acts as an additional layer of security, filtering out malicious traffic and hacking attempts, such as SQL injection or cross-site scripting (XSS) attacks.\n4. **SSL Encryption**: Cloudflare offers free SSL encryption, which secure sensitive information (like passwords, credit card numbers, and browsing data) with an HTTPS connection (the \"S\" stands for Secure).\n5. **Bot Protection**: Cloudflare has an AI-driven system that identifies and blocks bots trying to exploit vulnerabilities or scrape your content.\n6. **Analytics**: Cloudflare provides insights into website traffic, helping you understand your audience and make informed decisions.\n7. **Cybersecurity**: Cloudflare offers advanced security features, such as intrusion protection, DNS filtering, and Web Application Firewall (WAF) protection.\n\nOverall, Cloudflare helps protect against cyber threats, improves website performance, and enhances security for online businesses, bloggers, and individuals who need to establish a strong online presence.\n\nWould you like to know more about a specific aspect of Cloudflare?"},"success":true,"errors":[],"messages":[]}%监控 AI Gateway 以查看使用指标。
- 在仪表板中前往 AI > AI Gateway。
- 选择你的 gateway 以查看请求数、token 使用量、缓存效率、错误和估算成本等指标。你还可以开启日志记录和速率限制等其他配置。
要了解更多 Workers 构建内容,请参阅教程。
如果你有任何问题、需要帮助或想分享你的项目,请加入 Discord ↗ 上的 Cloudflare 开发者社区,与其他开发者和 Cloudflare 团队交流。