コンテンツにスキップ

ユニバーサルエンドポイント

ユニバーサルエンドポイントを使用して、すべてのプロバイダーに連絡できます。

https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}

説明

AI Gatewayは、作成した各ゲートウェイに対して複数のエンドポイントを提供します - プロバイダーごとに1つのエンドポイントと1つのユニバーサルエンドポイントです。ユニバーサルエンドポイントは、スキーマの調整が必要ですが、追加機能をサポートしています。これらの機能のいくつかは、例えば、最初のリクエストが失敗した場合に再試行することや、フォールバックモデル/プロバイダーを設定することです。

ユニバーサルエンドポイントを使用して、すべてのプロバイダーに連絡できます。ペイロードはメッセージの配列を期待しており、各メッセージは以下のパラメータを持つオブジェクトです:

  • provider : このメッセージを送信したいプロバイダーの名前。OpenAI、workers-ai、またはサポートされている任意のプロバイダーを指定できます。
  • endpoint: 到達しようとしているプロバイダーAPIのパス名。例えば、OpenAIではchat/completions、Workers AIでは@cf/meta/llama-3.1-8b-instructかもしれません。各プロバイダーに特有のセクションで詳細を確認してください。[/ai-gateway/providers/]
  • authorization: このプロバイダーに連絡する際に使用すべきAuthorization HTTPヘッダーの内容。通常は「Token」または「Bearer」で始まります。
  • query: プロバイダーが公式APIで期待するペイロード。

Request
curl https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id} \
--header 'Content-Type: application/json' \
--data '[
{
"provider": "workers-ai",
"endpoint": "@cf/meta/llama-3.1-8b-instruct",
"headers": {
"Authorization": "Bearer {cloudflare_token}",
"Content-Type": "application/json"
},
"query": {
"messages": [
{
"role": "system",
"content": "You are a friendly assistant"
},
{
"role": "user",
"content": "What is Cloudflare?"
}
]
}
},
{
"provider": "openai",
"endpoint": "chat/completions",
"headers": {
"Authorization": "Bearer {open_ai_token}",
"Content-Type": "application/json"
},
"query": {
"model": "gpt-4o-mini",
"stream": true,
"messages": [
{
"role": "user",
"content": "What is Cloudflare?"
}
]
}
}
]'

上記は、Workers AI推論APIにリクエストを送信し、失敗した場合はOpenAIに進みます。必要に応じて、配列に別のJSONを追加することで、フォールバックをいくつでも追加できます。