このチュートリアルでは、Cloudflare ダッシュボードで Workers AI を使い、最初の AI Gateway を作成します。対象は、AI Gateway と Workers AI を初めて使う方です。AI Gateway を作成すると、AI リクエストを効率よく管理・保護できます。コンテンツ生成、データ処理、予測分析などのタスクで、制御と性能を高めて AI モデルを使えます。
- サインアップ: Cloudflare アカウントがない場合は、サインアップ ↗ します。
- ログイン: Cloudflare ダッシュボード ↗ にログインして、ダッシュボードを開きます。
次に、新しい AI Gateway を作成します。
- Cloudflare ダッシュボード ↗ にログインし、アカウントを選択します。
- AI > AI Gateway を開きます。
- Create Gateway を選択します。
- Gateway name を入力します。ゲートウェイ名は 64 文字までです。
- Workers AI Billing で、このゲートウェイ経由の Workers AI リクエストの課金方法を選びます。
- Standard billing は、各請求サイクルの終わりに Cloudflare アカウントへ課金します。
- Unified billing は、前払いの AI Gateway クレジット残高からリアルタイムで差し引きます。
- Create を選択します。
API で AI Gateway を設定するには、次の手順を行います。
-
次の権限を持つ API トークンを作成 します。
AI Gateway - ReadAI Gateway - Edit
-
Account ID を取得します。
-
その API トークンと Account ID を使い、Cloudflare API へ
POSTリクエスト を送ります。
- AI Gateway のセクションで、作成したゲートウェイを選びます。
- プロバイダーとして Workers AI を選び、Workers AI 専用のエンドポイントを設定します。 リクエスト送信用のエンドポイント URL が発行されます。
-
Cloudflare ダッシュボードで AI > Workers AI を開きます。
-
Use 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 を開きます。
- ゲートウェイを選び、リクエスト数、トークン使用量、キャッシュ効率、エラー、推定コストなどのメトリクスを確認します。ログやレート制限などの追加設定も有効にできます。
Workers でさらに作るには、チュートリアル を参照してください。
質問がある場合、サポートが必要な場合、プロジェクトを共有したい場合は、Discord ↗ の Cloudflare Developer コミュニティに参加してください。ほかの開発者や Cloudflare チームとつながれます。