跳转到内容
搜索文档

通过 AI Gateway 使用 Pruna P-video

最后更新 查看 MarkdownAgent 设置

本教程介绍如何通过 AI Gateway 在 Replicate 上调用 Pruna 的 P-video 模型。

前提条件

1. 获取 Replicate API 令牌

  1. 前往 replicate.com 并注册账户。
  2. 登录后,前往 replicate.com/settings/api-tokens
  3. 选择 Create token(创建令牌) 并为其命名。
  4. 复制令牌并妥善保存。

2. 创建 AI Gateway

Go to AI Gateway ↗
  1. 登录 Cloudflare 仪表板 并选择你的账户。
  2. 前往 AI > AI Gateway
  3. 选择 Create Gateway(创建网关)
  4. 输入 Gateway name(网关名称)。注意:网关名称最多 64 个字符。
  5. 选择 Create(创建)

要使用 API 设置 AI Gateway:

  1. 创建 API token,并授予以下权限:

    • AI Gateway - Read
    • AI Gateway - Edit
  2. 获取你的 Account ID

  3. 使用该 API token 和 Account ID,向 Cloudflare API 发送 POST 请求

记下你的 Account IDGateway name,供后续步骤使用。

要为 gateway 添加身份验证,请参阅已认证的 Gateway

3. 构造 gateway URL

将标准 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/replicate

4. 生成视频

P-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。

5. (可选)对较长请求使用异步轮询

如果请求可能超过 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}"

持续轮询,直到 statussucceeded(或 failed)。完成后,output 字段包含生成视频文件的 URL。

后续步骤

接下来你可以:

这篇文档对您有帮助吗?