このチュートリアルでは、AI Gateway 経由で Replicate 上の Pruna の P-video ↗ モデルを呼び出す方法を説明します。
- Cloudflare アカウント ↗
- API トークン付きの Replicate アカウント ↗
- replicate.com ↗ を開き、アカウントを作成します。
- ログイン後、replicate.com/settings/api-tokens ↗ を開きます。
- Create token を選択し、名前を付けます。
- トークンをコピーし、安全な場所に保管します。
- 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リクエスト を送ります。
後の手順で使う Account ID と Gateway name を控えます。
ゲートウェイに認証を追加する場合は、Authenticated Gateway を参照してください。
標準の Replicate API ベース URL を、AI Gateway の URL に置き換えます。
# Instead of:
https://api.replicate.com/v1
# Use:
https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/replicateたとえば、アカウント ID が abc123、ゲートウェイが my-gateway の場合は次のとおりです。
https://gateway.ai.cloudflare.com/v1/abc123/my-gateway/replicateP-video の予測は、通常 30 秒以内に完了します。Replicate の同期上限 60 秒未満なので、Prefer: wait ヘッダーを使い、1 回の呼び出しでリクエスト送信と結果取得ができます。
curl https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/replicate/predictions \
--header "Authorization: Bearer {replicate_api_token}" \
--header "cf-aig-authorization: Bearer {cloudflare_api_token}" \
--header "Content-Type: application/json" \
--header "Prefer: wait" \
--data '{
"version": "prunaai/p-video",
"input": {
"prompt": "A cat walking through a field of flowers in slow motion",
"duration": 5,
"aspect_ratio": "16:9",
"resolution": "720p",
"fps": 24
}
}'Authorization— Replicate の API トークンです(Replicate への認証に使います)。cf-aig-authorization— Cloudflare の API トークンです(認証済みゲートウェイ向けです)。Prefer: wait— すぐに返すのではなく、予測が完了するまで待機します。
利用できる入力パラメーターの一覧は、Replicate の prunaai/p-video モデルページ ↗ を確認してください。
予測が完了すると、レスポンスの output フィールドに、生成された動画ファイルの URL が含まれます。
リクエストが 60 秒を超える可能性がある場合(長い duration や高い解像度など)は、非同期モードを使います。Prefer: wait ヘッダーなしでリクエストを送ります。
curl https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/replicate/predictions \
--header "Authorization: Bearer {replicate_api_token}" \
--header "cf-aig-authorization: Bearer {cloudflare_api_token}" \
--header "Content-Type: application/json" \
--data '{
"version": "prunaai/p-video",
"input": {
"prompt": "A cat walking through a field of flowers in slow motion",
"duration": 5,
"aspect_ratio": "16:9",
"resolution": "720p",
"fps": 24
}
}'レスポンスには予測の id が含まれます。
{
"id": "xyz789...",
"status": "starting",
"urls": {
"get": "https://api.replicate.com/v1/predictions/xyz789...",
"cancel": "https://api.replicate.com/v1/predictions/xyz789.../cancel"
}
}予測のステータスを、完了するまでポーリングします。
curl https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/replicate/predictions/{prediction_id} \
--header "Authorization: Bearer {replicate_api_token}" \
--header "cf-aig-authorization: Bearer {cloudflare_api_token}"status が succeeded(または failed)になるまでポーリングを続けます。完了すると、output フィールドに生成された動画ファイルの URL が含まれます。
ここから次のことができます。