跳至主要內容

Vertex AI SDK

Vertex AI 的 pass-through 端點 - 以原生格式呼叫特定提供者端點(不進行轉換)。

功能支援備註
成本追蹤支援 /generateContent 端點上的所有模型
記錄可跨所有整合使用
端使用者追蹤如果您需要這項功能,請告訴我們
串流

支援的端點

LiteLLM 支援 3 個 vertex ai passthrough 路由:

  1. /vertex_ai → 路由至 https://{vertex_location}-aiplatform.googleapis.com/
  2. /vertex_ai/discovery → 路由至 https://discoveryengine.googleapis.com - 請參閱 Search Datastores 指南
  3. /vertex_ai/live → 升級至 Vertex AI Live API WebSocket(google.cloud.aiplatform.v1.LlmBidiService/BidiGenerateContent)- 請參閱 Live WebSocket 指南

如何使用

只要將 https://REGION-aiplatform.googleapis.com 替換為 LITELLM_PROXY_BASE_URL/vertex_ai

LiteLLM 支援透過 pass-through 呼叫 Vertex AI 端點的 3 種流程:

  1. 特定憑證:管理員為特定專案/區域設定 passthrough 憑證。

  2. 預設憑證:管理員設定預設憑證。

  3. 用戶端憑證:使用者可將用戶端憑證傳送至 Vertex AI(預設行為 - 如果找不到預設或對應的憑證,請求會直接 pass-through)。

範例用法

model_list:
- model_name: gemini-1.0-pro
litellm_params:
model: vertex_ai/gemini-1.0-pro
vertex_project: adroit-crow-413218
vertex_location: us-central1
vertex_credentials: /path/to/credentials.json
use_in_pass_through: true # 👈 KEY CHANGE

範例用法

curl http://localhost:4000/vertex_ai/v1/projects/${PROJECT_ID}/locations/us-central1/publishers/google/models/${MODEL_ID}:generateContent \
-H "Content-Type: application/json" \
-H "x-litellm-api-key: Bearer sk-1234" \
-d '{
"contents":[{
"role": "user",
"parts":[{"text": "How are you doing today?"}]
}]
}'

Vertex AI Live API WebSocket

LiteLLM 現在可以代理 Vertex AI Live API,協助您在不向用戶端暴露 Google 憑證的情況下,試驗來自 Gemini Live models 的串流音訊/文字。

  • 透過 default_vertex_config 或環境變數設定預設的 Vertex 憑證(請參閱上方範例)。
  • 連線至 wss://<PROXY_URL>/vertex_ai/live。LiteLLM 會將您儲存的憑證交換為短效存取權杖,並雙向轉送訊息。
  • 可選的查詢參數 vertex_projectvertex_locationmodel 可讓您在多專案設定或僅全域模型中覆寫預設值。
client.py
import asyncio
import json

from websockets.asyncio.client import connect


async def main() -> None:
headers = {
"x-litellm-api-key": "Bearer sk-your-litellm-key",
"Content-Type": "application/json",
}
async with connect(
"ws://localhost:4000/vertex_ai/live",
additional_headers=headers,
) as ws:
await ws.send(
json.dumps(
{
"setup": {
"model": "projects/your-project/locations/us-central1/publishers/google/models/gemini-2.0-flash-live-preview-04-09",
"generation_config": {"response_modalities": ["TEXT"]},
}
}
)
)

async for message in ws:
print("server:", message)


if __name__ == "__main__":
asyncio.run(main())

快速開始

我們來呼叫 Vertex AI /generateContent 端點

  1. 將 Vertex AI 憑證加入您的環境
export DEFAULT_VERTEXAI_PROJECT="" # "adroit-crow-413218"
export DEFAULT_VERTEXAI_LOCATION="" # "us-central1"
export DEFAULT_GOOGLE_APPLICATION_CREDENTIALS="" # "/Users/Downloads/adroit-crow-413218-a956eef1a2a8.json"
  1. 啟動 LiteLLM Proxy
litellm

# RUNNING on http://0.0.0.0:4000
  1. 測試它!

我們來呼叫 Google AI Studio token counting 端點

curl http://localhost:4000/vertex-ai/v1/projects/${PROJECT_ID}/locations/us-central1/publishers/google/models/gemini-1.0-pro:generateContent \
-H "Content-Type: application/json" \
-H "Authorization: Bearer sk-1234" \
-d '{
"contents":[{
"role": "user",
"parts":[{"text": "How are you doing today?"}]
}]
}'

支援的 API 端點

  • Gemini API
  • Embeddings API
  • Imagen API
  • Code Completion API
  • Batch prediction API
  • Tuning API
  • CountTokens API

Vertex AI 的驗證

LiteLLM Proxy Server 支援兩種對 Vertex AI 的驗證方法:

  1. 將 Vertex 憑證從用戶端傳遞到 proxy server

  2. 在 proxy server 上設定 Vertex AI 憑證

使用範例

Gemini API(產生內容)

curl http://localhost:4000/vertex_ai/v1/projects/${PROJECT_ID}/locations/us-central1/publishers/google/models/gemini-1.5-flash-001:generateContent \
-H "Content-Type: application/json" \
-H "x-litellm-api-key: Bearer sk-1234" \
-d '{"contents":[{"role": "user", "parts":[{"text": "hi"}]}]}'

Embeddings API

curl http://localhost:4000/vertex_ai/v1/projects/${PROJECT_ID}/locations/us-central1/publishers/google/models/textembedding-gecko@001:predict \
-H "Content-Type: application/json" \
-H "x-litellm-api-key: Bearer sk-1234" \
-d '{"instances":[{"content": "gm"}]}'

Imagen API

curl http://localhost:4000/vertex_ai/v1/projects/${PROJECT_ID}/locations/us-central1/publishers/google/models/imagen-3.0-generate-001:predict \
-H "Content-Type: application/json" \
-H "x-litellm-api-key: Bearer sk-1234" \
-d '{"instances":[{"prompt": "make an otter"}], "parameters": {"sampleCount": 1}}'

Count Tokens API

curl http://localhost:4000/vertex_ai/v1/projects/${PROJECT_ID}/locations/us-central1/publishers/google/models/gemini-1.5-flash-001:countTokens \
-H "Content-Type: application/json" \
-H "x-litellm-api-key: Bearer sk-1234" \
-d '{"contents":[{"role": "user", "parts":[{"text": "hi"}]}]}'

Tuning API

建立 Fine Tuning Job

curl http://localhost:4000/vertex_ai/v1/projects/${PROJECT_ID}/locations/us-central1/publishers/google/models/gemini-1.5-flash-001:tuningJobs \
-H "Content-Type: application/json" \
-H "x-litellm-api-key: Bearer sk-1234" \
-d '{
"baseModel": "gemini-1.0-pro-002",
"supervisedTuningSpec" : {
"training_dataset_uri": "gs://cloud-samples-data/ai-platform/generative_ai/sft_train_data.jsonl"
}
}'

進階

先決條件

使用這個方式,可避免將原始的 Anthropic API 金鑰提供給開發人員,但仍可讓他們使用 Anthropic 端點。

搭配虛擬金鑰使用

  1. 設定環境
export DATABASE_URL=""
export LITELLM_MASTER_KEY=""

# vertex ai credentials
export DEFAULT_VERTEXAI_PROJECT="" # "adroit-crow-413218"
export DEFAULT_VERTEXAI_LOCATION="" # "us-central1"
export DEFAULT_GOOGLE_APPLICATION_CREDENTIALS="" # "/Users/Downloads/adroit-crow-413218-a956eef1a2a8.json"
litellm

# RUNNING on http://0.0.0.0:4000
  1. 產生虛擬金鑰
curl -X POST 'http://0.0.0.0:4000/key/generate' \
-H 'x-litellm-api-key: Bearer sk-1234' \
-H 'Content-Type: application/json' \
-d '{}'

預期回應

{
...
"key": "sk-1234ewknldferwedojwojw"
}
  1. 測試它!
curl http://localhost:4000/vertex_ai/v1/projects/${PROJECT_ID}/locations/us-central1/publishers/google/models/gemini-1.0-pro:generateContent \
-H "Content-Type: application/json" \
-H "x-litellm-api-key: Bearer sk-1234" \
-d '{
"contents":[{
"role": "user",
"parts":[{"text": "How are you doing today?"}]
}]
}'

在請求標頭中傳送 tags

如果您希望將 tags 追蹤到 LiteLLM DB 和記錄回呼中,請使用這個方式

tags 以逗號分隔清單的形式傳入請求標頭。在下方範例中,將會追蹤以下標籤

tags: ["vertex-js-sdk", "pass-through-endpoint"]
curl http://localhost:4000/vertex_ai/v1/projects/${PROJECT_ID}/locations/us-central1/publishers/google/models/gemini-1.0-pro:generateContent \
-H "Content-Type: application/json" \
-H "x-litellm-api-key: Bearer sk-1234" \
-H "tags: vertex-js-sdk,pass-through-endpoint" \
-d '{
"contents":[{
"role": "user",
"parts":[{"text": "How are you doing today?"}]
}]
}'

在 Vertex AI 上使用 Anthropic Beta 功能

當透過 Vertex AI passthrough 使用 Anthropic models(例如 Vertex 上的 Claude)時,您可以啟用 Anthropic beta 功能,例如延伸的 context windows。

在呼叫 Anthropic models 時,anthropic-beta 標頭會自動轉送到 Vertex AI。

curl http://localhost:4000/vertex_ai/v1/projects/${PROJECT_ID}/locations/us-east5/publishers/anthropic/models/claude-3-5-sonnet:rawPredict \
-H "Content-Type: application/json" \
-H "Authorization: Bearer sk-1234" \
-H "anthropic-beta: context-1m-2025-08-07" \
-d '{
"anthropic_version": "vertex-2023-10-16",
"messages": [{"role": "user", "content": "Hello"}],
"max_tokens": 500
}'

透過 x-pass- 前綴轉送自訂標頭

您可以透過在任意自訂標頭前加上 x-pass-,將其轉送給提供者。該前綴會在標頭送出給提供者之前被移除。

例如:

  • x-pass-anthropic-beta: value 會變成 anthropic-beta: value
  • x-pass-custom-header: value 會變成 custom-header: value

當您需要傳送不在預設允許清單中的提供者特定標頭時,這很有用。

curl http://localhost:4000/vertex_ai/v1/projects/${PROJECT_ID}/locations/us-east5/publishers/anthropic/models/claude-3-5-sonnet:rawPredict \
-H "Content-Type: application/json" \
-H "Authorization: Bearer sk-1234" \
-H "x-pass-anthropic-beta: context-1m-2025-08-07" \
-H "x-pass-custom-feature: enabled" \
-d '{
"anthropic_version": "vertex-2023-10-16",
"messages": [{"role": "user", "content": "Hello"}],
"max_tokens": 500
}'
資訊

x-pass- 前綴適用於所有 LLM pass-through 端點,不僅限於 Vertex AI。