跳至主要內容

搭配 LiteLLM 的 Google GenAI SDK

透過 LiteLLM Proxy,使用 Google 的官方 GenAI SDK(JavaScript/TypeScript 和 Python)搭配任何 LLM 提供者。

Google GenAI SDK(JS 使用 @google/genai,Python 使用 google-genai)提供呼叫 Gemini 模型的原生介面。只要將其指向 LiteLLM,您就能用同一套 SDK 搭配 OpenAI、Anthropic、Bedrock、Azure、Vertex AI,或任何其他提供者,同時保留原生的 Gemini 請求/回應格式。

為什麼要將 LiteLLM 與 Google GenAI SDK 搭配使用?

開發者優勢:

  • 通用模型存取:透過 Google GenAI SDK 介面使用任何 LiteLLM 支援的模型(Anthropic、OpenAI、Vertex AI、Bedrock 等)
  • 更高的速率限制與可靠性:在多個模型與提供者之間進行負載平衡,以避免碰到單一提供者的限制,並以備援機制確保即使某個提供者失敗,您仍可取得回應

Proxy 管理員優勢:

  • 集中管理:透過單一 LiteLLM proxy 執行個體控制所有模型的存取,而不需要提供開發者各個提供者的 API 金鑰
  • 預算控制:設定支出上限並追蹤所有 SDK 使用量的成本
  • 記錄與可觀測性:以成本追蹤、記錄與分析功能追蹤所有請求
功能支援備註
成本追蹤/generateContent 端點上的所有模型
記錄可跨所有整合運作
串流支援 streamGenerateContent
虛擬金鑰使用 LiteLLM 金鑰而非 Google 金鑰
負載平衡透過原生路由端點
備援透過原生路由端點

快速開始

1. 安裝 SDK

npm install @google/genai

2. 啟動 LiteLLM Proxy

config.yaml
model_list:
- model_name: gemini-2.5-flash
litellm_params:
model: gemini/gemini-2.5-flash
api_key: os.environ/GEMINI_API_KEY
litellm --config config.yaml

3. 透過 LiteLLM 呼叫 SDK

index.js
const { GoogleGenAI } = require("@google/genai");

const ai = new GoogleGenAI({
apiKey: "sk-1234", // LiteLLM virtual key (not a Google key)
httpOptions: {
baseUrl: "http://localhost:4000/gemini", // LiteLLM proxy URL
},
});

async function main() {
const response = await ai.models.generateContent({
model: "gemini-2.5-flash",
contents: "Explain how AI works",
});
console.log(response.text);
}

main();

串流

streaming.js
const { GoogleGenAI } = require("@google/genai");

const ai = new GoogleGenAI({
apiKey: "sk-1234",
httpOptions: {
baseUrl: "http://localhost:4000/gemini",
},
});

async function main() {
const response = await ai.models.generateContentStream({
model: "gemini-2.5-flash",
contents: "Write a short poem about the ocean",
});

for await (const chunk of response) {
process.stdout.write(chunk.text);
}
}

main();

多輪對話

chat.js
const { GoogleGenAI } = require("@google/genai");

const ai = new GoogleGenAI({
apiKey: "sk-1234",
httpOptions: {
baseUrl: "http://localhost:4000/gemini",
},
});

async function main() {
const chat = ai.chats.create({
model: "gemini-2.5-flash",
});

const response1 = await chat.sendMessage({ message: "I have 2 dogs and 3 cats." });
console.log(response1.text);

const response2 = await chat.sendMessage({ message: "How many pets is that in total?" });
console.log(response2.text);
}

main();

進階:在 GenAI SDK 中使用任何模型

預設情況下,GenAI SDK 會與 Gemini 模型通訊。但透過 LiteLLM 的路由器,您可以將 GenAI SDK 請求路由到任何提供者——Anthropic、OpenAI、Bedrock 等。

這是透過使用 model_group_alias,將 Gemini 模型名稱對應到您想要的提供者模型來運作。LiteLLM 會在內部處理格式轉換。

資訊

若要讓這項功能運作,請將 SDK baseUrl 指向 http://localhost:4000(不含 /gemini)。這會將請求路由經由 LiteLLM 的原生 Google 端點,再通過路由器並支援模型別名。

gemini-2.5-flash 請求路由到 Claude Sonnet:

config.yaml
model_list:
- model_name: claude-sonnet
litellm_params:
model: anthropic/claude-sonnet-4-20250514
api_key: os.environ/ANTHROPIC_API_KEY

router_settings:
model_group_alias: {"gemini-2.5-flash": "claude-sonnet"}

接著使用 SDK,並將 baseUrl 指向 LiteLLM(不含 /gemini):

any_model.js
const { GoogleGenAI } = require("@google/genai");

const ai = new GoogleGenAI({
apiKey: "sk-1234",
httpOptions: {
baseUrl: "http://localhost:4000", // No /gemini — goes through the router
},
});

async function main() {
// This calls Claude/GPT-4o/Bedrock under the hood via model_group_alias
const response = await ai.models.generateContent({
model: "gemini-2.5-flash",
contents: "Hello from any model!",
});
console.log(response.text);
}

main();

轉送 vs 原生路由端點

LiteLLM 提供兩種處理 GenAI SDK 請求的方式:

轉送(/gemini原生路由(/
baseUrlhttp://localhost:4000/geminihttp://localhost:4000
模型僅 Gemini透過 model_group_alias 使用任何提供者
轉換無——直接代理到 Google在內部轉換
成本追蹤
虛擬金鑰
負載平衡
備援
最適合簡單的 Gemini 代理多提供者路由

環境變數設定

您也可以透過環境變數而非程式碼來設定 SDK:

# For JavaScript SDK (@google/genai)
export GOOGLE_GEMINI_BASE_URL="http://localhost:4000/gemini"
export GEMINI_API_KEY="sk-1234"

# For Python SDK (google-genai)
# Note: The Python SDK does not support a base URL env var.
# Configure it in code with http_options={"base_url": "..."} instead.
export GEMINI_API_KEY="sk-1234"

這對於建立在 GenAI SDK 之上的工具特別有用(例如 Gemini CLI)。