跳至主要內容

Vertex AI 影片生成(Veo)

LiteLLM 支援 Vertex AI 的 Veo 影片生成模型,使用統一的 OpenAI 影片 API 介面。

屬性詳細資訊
說明Google Cloud Vertex AI Veo 影片生成模型
LiteLLM 上的提供者路由vertex_ai/
支援的模型veo-2.0-generate-001, veo-3.0-generate-preview, veo-3.0-fast-generate-preview, veo-3.1-generate-preview, veo-3.1-fast-generate-preview
成本追蹤✅ 依期間計費
記錄支援✅ 完整的請求/回應記錄
代理伺服器支援✅ 完整的代理整合,支援虛擬金鑰
支出管理✅ 預算追蹤與速率限制
提供者文件連結Vertex AI Veo 文件 ↗

快速開始

必要的環境設定

import json
import os

os.environ["VERTEXAI_PROJECT"] = "your-gcp-project-id"
os.environ["VERTEXAI_LOCATION"] = "us-central1"

# Option 1: Point to a service account file
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "/path/to/service_account.json"

# Option 2: Store the service account JSON directly
with open("/path/to/service_account.json", "r", encoding="utf-8") as f:
os.environ["VERTEXAI_CREDENTIALS"] = f.read()

基本使用方式

from litellm import video_generation, video_status, video_content
import json
import os
import time

with open("/path/to/service_account.json", "r", encoding="utf-8") as f:
vertex_credentials = f.read()

response = video_generation(
model="vertex_ai/veo-3.0-generate-preview",
prompt="A cat playing with a ball of yarn in a sunny garden",
vertex_project="your-gcp-project-id",
vertex_location="us-central1",
vertex_credentials=vertex_credentials,
seconds="8",
size="1280x720",
)

print(f"Video ID: {response.id}")
print(f"Initial Status: {response.status}")

# Poll for completion
while True:
status = video_status(
video_id=response.id,
vertex_project="your-gcp-project-id",
vertex_location="us-central1",
vertex_credentials=vertex_credentials,
)

print(f"Current Status: {status.status}")

if status.status == "completed":
break
if status.status == "failed":
raise RuntimeError("Video generation failed")

time.sleep(10)

# Download the rendered video
video_bytes = video_content(
video_id=response.id,
vertex_project="your-gcp-project-id",
vertex_location="us-central1",
vertex_credentials=vertex_credentials,
)

with open("generated_video.mp4", "wb") as f:
f.write(video_bytes)

支援的模型

模型名稱說明最長時間狀態
veo-2.0-generate-001Veo 2.0 影片生成5 秒GA
veo-3.0-generate-previewVeo 3.0 高品質8 秒預覽
veo-3.0-fast-generate-previewVeo 3.0 快速生成8 秒預覽
veo-3.1-generate-previewVeo 3.1 高品質10 秒預覽
veo-3.1-fast-generate-previewVeo 3.1 快速10 秒預覽

影片生成參數

LiteLLM 會自動將 OpenAI 風格的參數轉換為 Veo 的 API 格式:

OpenAI 參數Vertex AI 參數說明範例
promptinstances[].prompt影片的文字描述"A cat playing"
sizeparameters.aspectRatio轉換為 16:99:16"1280x720" → 16:9
secondsparameters.durationSeconds片長(秒)"8" → 8
input_referenceinstances[].image用於動畫的參考圖片open("image.jpg", "rb")
特定於提供者的參數extra_body轉送至 Vertex API{"negativePrompt": "blurry"}

尺寸到長寬比對應

  • 1280x720, 1920x108016:9
  • 720x1280, 1080x19209:16
  • 未知尺寸預設為 16:9

非同步使用方式

from litellm import avideo_generation, avideo_status, avideo_content
import asyncio
import json

with open("/path/to/service_account.json", "r", encoding="utf-8") as f:
vertex_credentials = f.read()


async def workflow():
response = await avideo_generation(
model="vertex_ai/veo-3.1-generate-preview",
prompt="Slow motion water droplets splashing into a pool",
seconds="10",
vertex_project="your-gcp-project-id",
vertex_location="us-central1",
vertex_credentials=vertex_credentials,
)

while True:
status = await avideo_status(
video_id=response.id,
vertex_project="your-gcp-project-id",
vertex_location="us-central1",
vertex_credentials=vertex_credentials,
)

if status.status == "completed":
break
if status.status == "failed":
raise RuntimeError("Video generation failed")

await asyncio.sleep(10)

video_bytes = await avideo_content(
video_id=response.id,
vertex_project="your-gcp-project-id",
vertex_location="us-central1",
vertex_credentials=vertex_credentials,
)

with open("veo_water.mp4", "wb") as f:
f.write(video_bytes)

asyncio.run(workflow())

LiteLLM 代理使用方式

將 Veo 模型加入您的 config.yaml

model_list:
- model_name: veo-3
litellm_params:
model: vertex_ai/veo-3.0-generate-preview
vertex_project: os.environ/VERTEXAI_PROJECT
vertex_location: os.environ/VERTEXAI_LOCATION
vertex_credentials: os.environ/VERTEXAI_CREDENTIALS

啟動代理並發出請求:

# Step 1: Generate video
curl --location 'http://0.0.0.0:4000/videos' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer sk-1234' \
--data '{
"model": "veo-3",
"prompt": "Aerial shot over a futuristic city at sunrise",
"seconds": "8"
}'

# Step 2: Poll status
curl --location 'http://localhost:4000/v1/videos/{video_id}' \
--header 'x-litellm-api-key: sk-1234'

# Step 3: Download video
curl --location 'http://localhost:4000/v1/videos/{video_id}/content' \
--header 'x-litellm-api-key: sk-1234' \
--output video.mp4

成本追蹤

LiteLLM 會記錄 Veo 回傳的期間,讓您可以套用依期間計費。

with open("/path/to/service_account.json", "r", encoding="utf-8") as f:
vertex_credentials = f.read()

response = video_generation(
model="vertex_ai/veo-2.0-generate-001",
prompt="Flowers blooming in fast forward",
seconds="5",
vertex_project="your-gcp-project-id",
vertex_location="us-central1",
vertex_credentials=vertex_credentials,
)

print(response.usage) # {"duration_seconds": 5.0}

疑難排解

  • vertex_project is required:設定 VERTEXAI_PROJECT 環境變數,或在請求中傳入 vertex_project
  • Permission denied:請確認服務帳戶具有 Vertex AI User 角色,且已啟用正確的區域。
  • 影片卡在 processing:Veo 作業屬於長時間執行。請持續每 10–15 秒輪詢一次,最長約 10 分鐘。

另請參見