跳至主要內容

/generateContent

使用 LiteLLM 來呼叫 Google AI 的 generateContent 端點,以進行文字生成、多模態互動與串流回應。

概覽

功能支援備註
成本追蹤
記錄適用於所有整合
終端使用者追蹤
串流
備援於受支援的模型之間
負載平衡於受支援的模型之間
中繼資料追蹤將 trace ID、metadata 傳遞給可觀測性回呼(例如 S3、Langfuse)

使用方式


LiteLLM Python SDK

非串流範例

Basic Text Generation
from litellm.google_genai import agenerate_content
from google.genai.types import ContentDict, PartDict
import os

# Set API key
os.environ["GEMINI_API_KEY"] = "your-gemini-api-key"

contents = ContentDict(
parts=[
PartDict(text="Hello, can you tell me a short joke?")
],
role="user",
)

response = await agenerate_content(
contents=contents,
model="gemini/gemini-2.0-flash",
max_tokens=100,
)
print(response)

串流範例

Streaming Text Generation
from litellm.google_genai import agenerate_content_stream
from google.genai.types import ContentDict, PartDict
import os

# Set API key
os.environ["GEMINI_API_KEY"] = "your-gemini-api-key"

contents = ContentDict(
parts=[
PartDict(text="Write a long story about space exploration")
],
role="user",
)

response = await agenerate_content_stream(
contents=contents,
model="gemini/gemini-2.0-flash",
max_tokens=500,
)

async for chunk in response:
print(chunk)

LiteLLM Proxy Server

  1. 設定 config.yaml
model_list:
- model_name: gemini-flash
litellm_params:
model: gemini/gemini-2.0-flash
api_key: os.environ/GEMINI_API_KEY
  1. 啟動 proxy
litellm --config /path/to/config.yaml
  1. 測試它!
Google GenAI SDK with LiteLLM Proxy
from google.genai import Client
import os

# Configure Google GenAI SDK to use LiteLLM proxy
os.environ["GOOGLE_GEMINI_BASE_URL"] = "http://localhost:4000"
os.environ["GEMINI_API_KEY"] = "sk-1234"

client = Client()

response = client.models.generate_content(
model="gemini-flash",
contents=[
{
"parts": [{"text": "Write a short story about AI"}],
"role": "user"
}
],
config={"max_output_tokens": 100}
)

原生請求欄位

generateContent 端點可直接替代 Google 的 Generative Language REST API,因此 Google 的 GenerateContentRequest 作為 generationConfig 同層級欄位所帶的頂層欄位,會原封不動轉送給 Google。這涵蓋 safetySettingstoolConfigcachedContentlabels。請將它們直接以請求本文的頂層欄位送出,就像直接呼叫 Google 時一樣;不需要將它們包在 extra_body 中。若您同時傳入 extra_body,其中明確指定的值在衝突時會優先。

Native top-level fields via LiteLLM Proxy
curl -L -X POST 'http://localhost:4000/v1beta/models/gemini-flash:generateContent' \
-H 'content-type: application/json' \
-H 'authorization: Bearer sk-1234' \
-d '{
"contents": [
{
"parts": [{"text": "Say hi"}],
"role": "user"
}
],
"generationConfig": {
"maxOutputTokens": 100
},
"safetySettings": [
{
"category": "HARM_CATEGORY_HATE_SPEECH",
"threshold": "BLOCK_NONE"
}
],
"toolConfig": {
"functionCallingConfig": {"mode": "AUTO"}
}
}'
🚅
LiteLLM Enterprise
為正式環境打造的 SSO/SAML、稽核記錄、支出追蹤、多團隊管理與防護欄。
深入瞭解 →