الانتقال إلى المحتوى الرئيسي
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
  }
}
للاطلاع على أمثلة برمجية خطوة بخطوة (من النص إلى الصورة، ومن صورة إلى صورة، وتركيب عدة صور، والتحرير عبر الدردشة)، راجع دليل استدعاء نماذج صور Gemini.

التفويضات

Authorization
string
header
مطلوب

Use your CometAPI API key.

معلمات المسار

model
string
افتراضي:gemini-3.1-flash-image-preview
مطلوب

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

الجسم

application/json
contents
object[]
مطلوب

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.

الاستجابة

200 - application/json

Success

candidates
object[]
usageMetadata
object