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

使用 Workers AI 创建您的第一个 AI 网关

Last reviewed: 11 months ago

本教程将指导您使用 Cloudflare 仪表板上的 Workers AI 创建您的第一个 AI 网关。目标受众是刚接触 AI 网关和 Workers AI 的初学者。创建一个 AI 网关可以使用户高效地管理和保护 AI 请求,从而使他们能够利用 AI 模型执行内容生成、数据处理或预测分析等任务,并具有增强的控制和性能。

注册和登录

  1. 注册:如果您没有 Cloudflare 帐户,请注册
  2. 登录:通过登录Cloudflare 仪表板访问 Cloudflare 仪表板。

创建网关

然后,创建一个新的 AI 网关。

To set up an AI Gateway in the dashboard:

  1. Log into the Cloudflare dashboard and select your account.
  2. Go to AI > AI Gateway.
  3. Select Create Gateway.
  4. Enter your Gateway name. Note: Gateway name has a 64 character limit.
  5. Select Create.

连接您的 AI 提供商

  1. 在 AI 网关部分,选择您创建的网关。
  2. 选择 Workers AI 作为您的提供商,以设置一个特定于 Workers AI 的端点。 您将收到一个用于发送请求的端点 URL。

配置您的 Workers AI

  1. 在 Cloudflare 仪表板中转到 AI > Workers AI

  2. 选择 使用 REST API 并按照步骤创建并复制 API 令牌和账户 ID。

  3. 向 Workers AI 发送请求:使用提供的 API 端点。例如,您可以使用 curl 命令通过 API 运行模型。将 {account_id}{gateway_id}{cf_api_token} 替换为您的实际账户 ID 和 API 令牌:

    Terminal window
    curl https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/workers-ai/@cf/meta/llama-3.1-8b-instruct \
    --header 'Authorization: Bearer {cf_api_token}' \
    --header 'Content-Type: application/json' \
    --data '{"prompt": "What is Cloudflare?"}'

预期的输出将类似于:

Terminal window
{"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 网关以查看使用指标。

  1. 在仪表板中转到 AI > AI 网关
  2. 选择您的网关以查看请求计数、令牌使用、缓存效率、错误和预估成本等指标。您还可以开启额外的配置,如日志记录和速率限制。

可选 - 后续步骤

要使用 Workers 构建更多内容,请参阅教程

如果您有任何问题、需要帮助或想分享您的项目,请加入 Discord 上的 Cloudflare 开发者社区,与其他开发者和 Cloudflare 团队联系。