NanoBanana Pro

NanoBanana Pro là dịch vụ tạo ảnh của Gemini. Nó hỗ trợ nhiều tỷ lệ khung hình (1:1, 16:9, 9:16, v.v.) và độ phân giải (1K, 2K, 4K). Mọi độ phân giải có cùng mức giá; 4K sinh chậm hơn và không được khuyến nghị. Xem Pricing.

Endpoint: POST https://aiberm.com/v1beta/models/gemini-3-pro-image-preview:generateContent

Điểm chính: Gửi mô tả ảnh trong phần thân yêu cầu contents; đặt responseModalities, imageConfig (tỷ lệ khung hình, độ phân giải) trong generationConfig. Ảnh được sinh nằm trong candidates[0].content.parts dưới dạng inline_data.

Ví dụ mã

Python dùng snake_case (ví dụ response_modalities); REST/cURL dùng camelCase (ví dụ responseModalities). Chúng cùng ý nghĩa.

1from google import genai
2from google.genai import types
3 
4base_url = "https://aiberm.com"
5api_key = "YOUR_API_KEY"
6prompt = "Generate a cute sea otter image"
7model = "gemini-3-pro-image-preview"
8output_file = "gemini_sdk_generated.png"
9 
10# Image generation config
11response_modalities = ["IMAGE"] # or ["TEXT", "IMAGE"]
12aspect_ratio = "16:9" # "1:1","2:3","3:2","3:4","4:3","4:5","5:4","9:16","16:9","21:9"
13image_size = "2K" # "1K", "2K", "4K" (gemini-3-pro-image-preview only)
14 
15# Generation params
16temperature = 1.0
17top_p = 0.95
18max_output_tokens = 8192
19enable_google_search = False # Enable Google Search(adds latency & cost; disable for image-only)
20 
21client = genai.Client(
22 api_key=api_key,
23 http_options=types.HttpOptions(api_version="v1beta", base_url=base_url)
24)
25 
26config = types.GenerateContentConfig(
27 response_modalities=response_modalities,
28 temperature=temperature,
29 top_p=top_p,
30 max_output_tokens=max_output_tokens,
31 system_instruction="You are a helpful assistant.",
32 image_config=types.ImageConfig(
33 aspect_ratio=aspect_ratio,
34 image_size=image_size if "pro-image" in model else None
35 )
36)
37if enable_google_search:
38 config.tools = [{"google_search": {}}]
39 
40response = client.models.generate_content(
41 model=model,
42 contents=[prompt],
43 config=config
44)
45 
46for part in response.parts:
47 if part.text is not None:
48 print(part.text)
49 elif part.inline_data is not None:
50 part.as_image().save(output_file)
51 print(f"Image saved: {output_file}")

Tham số ảnh cURL

Dưới đây là tên tham số (camelCase) cho cURL; dùng snake_case trong Python (ví dụ aspectRatioaspect_ratio).

imageConfig (generationConfig.imageConfig hoặc SDK ImageConfig):

  • aspectRatio string1:1, 2:3, 3:2, 3:4, 4:3, 4:5, 5:4, 9:16, 16:9, 21:9
  • imageSize string1K, 2K, 4K (chỉ gemini-3-pro-image-preview)

Các trường generationConfig phổ biến khác:

  • responseModalities string[]["IMAGE"] hoặc ["TEXT", "IMAGE"]
  • temperature number — 0–2, cao hơn = ngẫu nhiên hơn
  • topP number — 0–1, độ đa dạng
  • maxOutputTokens number — ví dụ 8192
  • systemInstruction object — ví dụ {"parts": [{"text": "..."}]}
  • tools array — ví dụ [{"google_search": {}}]

Chỉ API kiểu Gemini hỗ trợ Google Search. Truyền tools trong cấu hình; mô hình sẽ dùng thông tin web thời gian thực khi cần.

Không khuyến nghị theo mặc định: Tăng độ trễ và chi phí; không cần thiết nếu chỉ tạo ảnh. Tắt nếu bạn không cần tìm kiếm.

Python (SDK):

config = types.GenerateContentConfig(
    # ... other params like response_modalities, image_config
    tools=[{"google_search": {}}]
)
response = client.models.generate_content(model=model, contents=[prompt], config=config)

cURL (REST):

Thêm tools ở cấp cao nhất của phần thân yêu cầu:

curl -X POST "https://aiberm.com/v1beta/models/gemini-3-pro-image-preview:generateContent" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "contents": [{"parts": [{"text": "Your question"}]}],
    "generationConfig": { ... },
    "tools": [{"google_search": {}}]
  }'

FAQ

  • Không có ảnh trong phản hồi: Đảm bảo mô hình là gemini-3-pro-image-preview và prompt là mô tả ảnh rõ ràng.
  • Lỗi giải mã: Chỉ dùng chuỗi base64 sau data:image/...;base64, và xóa xuống dòng trước khi giải mã.