跳至主要內容

Google AI Studio 圖像生成

Google AI Studio 提供強大的圖像生成功能,使用 Google 的 Imagen 模型,根據文字描述建立高品質圖像。

概覽

屬性詳細資訊
說明Google AI Studio 圖像生成使用 Google 的 Imagen 模型,根據文字描述生成高品質圖像。
LiteLLM 上的提供者路由gemini/
提供者文件Google AI Studio 圖像生成 ↗
支援的操作/images/generations

設定

API 金鑰

# Set your Google AI Studio API key
import os
os.environ["GEMINI_API_KEY"] = "your-api-key-here"

請從 Google AI Studio 取得您的 API 金鑰。

圖像生成

用法 - LiteLLM Python SDK

Basic Image Generation
import litellm
import os

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

# Generate a single image
response = litellm.image_generation(
model="gemini/imagen-4.0-generate-001",
prompt="A cute baby sea otter swimming in crystal clear water"
)

print(response.data[0].url)

用法 - LiteLLM Proxy Server

1. 設定您的 config.yaml

Google AI Studio Image Generation Configuration
model_list:
- model_name: google-imagen
litellm_params:
model: gemini/imagen-4.0-generate-001
api_key: os.environ/GEMINI_API_KEY
model_info:
mode: image_generation

general_settings:
master_key: sk-1234

2. 啟動 LiteLLM Proxy Server

Start LiteLLM Proxy Server
litellm --config /path/to/config.yaml

# RUNNING on http://0.0.0.0:4000

3. 使用 OpenAI Python SDK 發出請求

Google AI Studio Image Generation via Proxy - OpenAI SDK
from openai import OpenAI

# Initialize client with your proxy URL
client = OpenAI(
base_url="http://localhost:4000", # Your proxy URL
api_key="sk-1234" # Your proxy API key
)

# Generate image
response = client.images.generate(
model="google-imagen",
prompt="A majestic eagle soaring over snow-capped mountains",
n=1,
size="1024x1024"
)

print(response.data[0].url)

Gemini 圖像模型

Gemini 圖像模型(例如 gemini-3.1-flash-image-previewgemini-3-pro-image-preview)使用 generateContent API,並回傳 base64 圖像。它們也支援 /v1/images/generations 上的Google Search grounding

Gemini image generation with Google Search
import litellm
import os

os.environ["GEMINI_API_KEY"] = "your-api-key-here"

response = litellm.image_generation(
model="gemini/gemini-3.1-flash-image-preview",
prompt="Generate an image of the latest iPhone design",
web_search_options={},
)

print(response.data[0].b64_json)
Proxy request with web_search_options
curl --location 'http://localhost:4000/v1/images/generations' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer sk-1234' \
--data '{
"model": "gemini-3.1-flash-image-preview",
"prompt": "Generate an image of the latest iPhone design",
"web_search_options": {}
}'

您也可以傳遞 tools=[{"type": "web_search"}] 或原生 tools=[{"googleSearch": {}}]

傳遞 imageConfig

Gemini 圖像模型接受完整的 imageConfig 物件。所有欄位都會直接對應到基礎 generateContent 請求中的 generationConfig.imageConfig

imageConfig with all fields
import litellm
import os

os.environ["GEMINI_API_KEY"] = "your-api-key-here"

response = litellm.image_generation(
model="gemini/gemini-3.1-flash-image-preview",
prompt="A nano banana on a desk",
imageConfig={
"aspectRatio": "16:9",
"imageSize": "2K",
"personGeneration": "DONT_ALLOW",
"imageOutputOptions": {
"mimeType": "image/jpeg",
"compressionQuality": 85,
},
},
)

print(response.data[0].b64_json)
imageConfig via Proxy
curl --location 'http://localhost:4000/v1/images/generations' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer sk-1234' \
--data '{
"model": "gemini-3.1-flash-image-preview",
"prompt": "A nano banana on a desk",
"imageConfig": {
"aspectRatio": "16:9",
"imageSize": "2K",
"personGeneration": "DONT_ALLOW",
"imageOutputOptions": {
"mimeType": "image/jpeg",
"compressionQuality": 85
}
}
}'

支援的參數

Google AI Studio 圖像生成支援以下與 OpenAI 相容的參數:

參數類型說明預設值範例
promptstring要生成之圖像的文字描述必填"A sunset over the ocean"
modelstring用於生成的模型必填"gemini/imagen-4.0-generate-001"
ninteger要生成的圖像數量(1-4)12
sizestring圖像尺寸"1024x1024""512x512""1024x1024"
web_search_optionsobject啟用 Google Search grounding(僅限 Gemini 圖像模型)-{}
toolsarray傳遞 {"type": "web_search"}{"googleSearch": {}}(僅限 Gemini 圖像模型)-[{"type": "web_search"}]
imageConfigobject完整的 ImageConfig 物件(僅限 Gemini 圖像模型)。欄位:aspectRatioimageSizepersonGenerationimageOutputOptions-{"aspectRatio": "16:9", "imageSize": "2K"}
  1. Google AI Studio 建立帳戶
  2. API Keys 區段 產生 API 金鑰
  3. 設定您的 GEMINI_API_KEY 環境變數
  4. 開始使用 LiteLLM 生成圖像

其他資源

🚅
LiteLLM Enterprise
為正式環境打造的 SSO/SAML、稽核記錄、支出追蹤、多團隊管理與防護欄。
深入瞭解 →