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>"
            }
          }
        ]
      },
      "finishReason": "STOP",
      "index": 123,
      "safetyRatings": [
        {
          "category": "<string>",
          "probability": "<string>"
        }
      ]
    }
  ],
  "usageMetadata": {
    "promptTokenCount": 123,
    "candidatesTokenCount": 123,
    "totalTokenCount": 123,
    "thoughtsTokenCount": 123
  }
}
Để xem các ví dụ mã từng bước (text-to-image, image-to-image, ghép nhiều ảnh, chỉnh sửa qua chat), hãy xem Hướng dẫn gọi các model ảnh Gemini.

Ủ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