Google AI Studio Image Generation
Google AI Studio Image Generation
Google AI Studio는 Google의 Imagen 모델로 텍스트 설명에서 고품질 이미지를 생성하는 강력한 이미지 생성 기능을 제공해요.
출처: 문서
본문
개요 (Overview)
| 속성 | 설명 |
|---|---|
| 설명 | Google AI Studio Image Generation은 Google Imagen 모델로 텍스트 설명에서 고품질 이미지 생성 |
| LiteLLM 라우트 | gemini/ |
| 공급자 문서 | Google AI Studio Image Generation |
| 지원 작업 | /images/generations |
설정 (Setup)
API 키
Google AI Studio에서 API 키를 가져와요.
# Set your Google AI Studio API key
import os
os.environ["GEMINI_API_KEY"] = "your-api-key-here"
이미지 생성 (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)
비동기 이미지 생성
import litellm
import asyncio
import os
async def generate_image():
# Set your API key
os.environ["GEMINI_API_KEY"] = "your-api-key-here"
# Generate image asynchronously
response = await litellm.aimage_generation(
model="gemini/imagen-4.0-generate-001",
prompt="A beautiful sunset over mountains with vibrant colors",
n=1,
)
print(response.data[0].url)
return response
# Run the async function
asyncio.run(generate_image())
추가 파라미터 이미지 생성
import litellm
import os
# Set your API key
os.environ["GEMINI_API_KEY"] = "your-api-key-here"
# Generate image with additional parameters
response = litellm.image_generation(
model="gemini/imagen-4.0-generate-001",
prompt="A futuristic cityscape at night with neon lights",
n=1,
size="1024x1024",
)
for image in response.data:
print(f"Generated image URL: {image.url}")
LiteLLM Proxy Server 사용법
1. config.yaml 설정
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: os.environ/LITELLM_MASTER_KEY
2. Proxy 서버 시작
litellm --config /path/to/config.yaml
# RUNNING on http://0.0.0.0:4000
3. OpenAI Python SDK로 요청
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-<your-litellm-api-key>" # 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)
LiteLLM SDK:
import litellm
# Configure LiteLLM to use your proxy
response = litellm.image_generation(
model="litellm_proxy/google-imagen",
prompt="A serene Japanese garden with cherry blossoms",
api_base="http://localhost:4000",
api_key="sk-<your-litellm-api-key>",
)
print(response.data[0].url)
cURL:
curl --location 'http://localhost:4000/v1/images/generations' \
--header 'Content-Type: application/json' \
--header "Authorization: Bearer ***" \
--data '{
"model": "google-imagen",
"prompt": "A cozy coffee shop interior with warm lighting",
"n": 1,
"size": "1024x1024"
}'
Gemini 이미지 모델
Gemini 이미지 모델(예: gemini-3.1-flash-image-preview, gemini-3-pro-image-preview)은 generateContent API를 사용하고 base64 이미지를 반환해요. 또한 /v1/images/generations에서 Google Search grounding을 지원해요.
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 요청 (web_search_options 포함):
curl --location 'http://localhost:4000/v1/images/generations' \
--header 'Content-Type: application/json' \
--header "Authorization: Bearer ***" \
--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에 직접 매핑돼요.
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)
Proxy:
curl --location 'http://localhost:4000/v1/images/generations' \
--header 'Content-Type: application/json' \
--header "Authorization: Bearer ***" \
--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
}
}
}'
지원 파라미터 (Supported Parameters)
Google AI Studio Image Generation은 다음 OpenAI 호환 파라미터를 지원해요:
| 파라미터 | 타입 | 설명 | 기본값 | 예시 |
|---|---|---|---|---|
| prompt | string | 생성할 이미지의 텍스트 설명 | 필수 | "A sunset over the ocean" |
| model | string | 생성에 사용할 모델 | 필수 | "gemini/imagen-4.0-generate-001" |
| n | integer | 생성할 이미지 수 (1-4) | 1 | 2 |
| size | string | 이미지 크기 | "1024x1024" | "512x512", "1024x1024" |
| web_search_options | object | Google Search grounding 활성화 (Gemini 이미지 모델만) | - | {} |
| tools | array | {"type": "web_search"} 또는 {"googleSearch": {}} 전달 (Gemini 이미지 모델만) |
- | [{"type": "web_search"}] |
| imageConfig | object | 전체 ImageConfig 객체 (Gemini 이미지 모델만). 필드: aspectRatio, imageSize, personGeneration, imageOutputOptions | - | {"aspectRatio": "16:9", "imageSize": "2K"} |
시작하기:
- Google AI Studio에서 계정 만들기
- API Keys 섹션에서 API 키 생성
- GEMINI_API_KEY 환경 변수 설정
- LiteLLM으로 이미지 생성 시작