本教程介绍如何通过 AI Gateway 在 Replicate 上调用 Pruna 的 P-video ↗ 模型。
- 一个 Cloudflare 账户 ↗
- 一个带有 API 令牌的 Replicate 账户 ↗
- 前往 replicate.com ↗ 并注册账户。
- 登录后,前往 replicate.com/settings/api-tokens ↗。
- 选择 Create token(创建令牌) 并为其命名。
- 复制令牌并妥善保存。
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请求。
记下你的 Account ID 和 Gateway name,供后续步骤使用。
要为 gateway 添加身份验证,请参阅已认证的 Gateway。
将标准 Replicate API base 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,gateway 是 my-gateway:
https://gateway.ai.cloudflare.com/v1/abc123/my-gateway/replicateP-video 预测通常在 30 秒内完成。由于这低于 Replicate 的 60 秒同步限制,你可以使用 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" \
--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 令牌(用于已认证的 gateway)。Prefer: wait— 阻塞直到预测完成,而不是立即返回。
有关可用输入参数的完整列表,请查看 Replicate 上的 prunaai/p-video 模型页面 ↗。
预测完成后,响应会包含 output 字段,其中带有生成视频文件的 URL。
如果请求可能超过 60 秒(例如,时长更长或分辨率更高),请改用异步模式。发送请求时不要带 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。
接下来你可以: