Chuyển đến nội dung chính
POST
/
v1beta
/
models
/
{model}
:generateContent
from google import genai
from google.genai import types
import os

client = genai.Client(
    http_options={"api_version": "v1beta", "base_url": "https://api.cometapi.com"},
    api_key=os.environ.get("COMETAPI_KEY"),
)

response = client.models.generate_content(
    model="gemini-3.1-flash-image-preview",
    contents="A Monarch butterfly anatomical sketch on textured parchment",
    config=types.GenerateContentConfig(
        response_modalities=["TEXT", "IMAGE"],
        image_config=types.ImageConfig(aspect_ratio="1:1", image_size="4K"),
    ),
)

for part in response.parts:
    if part.text:
        print(part.text)
    elif image := part.as_image():
        image.save("output.png")
{
  "candidates": [
    {
      "content": {
        "role": "<string>",
        "parts": [
          {
            "text": "<string>",
            "inlineData": {
              "mimeType": "<string>",
              "data": "<string>"
            }
          }
        ]
      },
      "index": 123,
      "safetyRatings": [
        {
          "category": "<string>",
          "probability": "<string>"
        }
      ]
    }
  ],
  "usageMetadata": {
    "promptTokenCount": 123,
    "candidatesTokenCount": 123,
    "totalTokenCount": 123,
    "thoughtsTokenCount": 123
  }
}

Documentation Index

Fetch the complete documentation index at: https://apidoc.cometapi.com/llms.txt

Use this file to discover all available pages before exploring further.

Để xem các ví dụ mã từng bước (text-to-image, image-to-image, phối hợp nhiều ảnh, chỉnh sửa trong chat), hãy xem Hướng dẫn tạo ảnh bằng Gemini.
gemini-2.5-flash-image dự kiến sẽ bị nhà cung cấp ngừng hoạt động vào 2026-10-02. Với các dự án mới, hãy dùng gemini-3.1-flash-image-preview hoặc gemini-3-pro-image-preview. Xem lịch ngừng hỗ trợ của Google để biết chi tiết.

Ủy quyền

Authorization
string
header
bắt buộc

Use your CometAPI API key.

Tham số đường dẫn

model
string
mặc định:gemini-3.1-flash-image-preview
bắt buộc

The Gemini image model to use. See the Models page for current options.

Nội dung

application/json
contents
object[]
bắt buộc

Conversation turns. Each item has a role ("user" or "model") and parts array containing text and/or image data.

generationConfig
object

Controls generation behavior — output modalities, image resolution, thinking, etc.

tools
object[]

Optional tools. Pass [{"google_search": {}}] to enable Google Search grounding for real-time information in generated images.

Phản hồi

200 - application/json

Success

candidates
object[]
usageMetadata
object