跳轉到主要內容
POST
/
v1beta
/
models
/
{model}
:
{operator}
from google import genai client = genai.Client( api_key="<COMETAPI_KEY>", http_options={"api_version": "v1beta", "base_url": "https://api.cometapi.com"}, ) response = client.models.generate_content( model="gemini-3-flash-preview", contents="Explain how AI works in a few words", ) print(response.text)
{
  "candidates": [
    {
      "content": {
        "role": "<string>",
        "parts": [
          {
            "text": "<string>",
            "functionCall": {
              "name": "<string>",
              "args": {}
            },
            "inlineData": {
              "mimeType": "<string>",
              "data": "<string>"
            },
            "thought": true
          }
        ]
      },
      "safetyRatings": [
        {
          "category": "<string>",
          "probability": "<string>",
          "blocked": true
        }
      ],
      "citationMetadata": {
        "citationSources": [
          {
            "startIndex": 123,
            "endIndex": 123,
            "uri": "<string>",
            "license": "<string>"
          }
        ]
      },
      "tokenCount": 123,
      "avgLogprobs": 123,
      "groundingMetadata": {
        "groundingChunks": [
          {
            "web": {
              "uri": "<string>",
              "title": "<string>"
            }
          }
        ],
        "groundingSupports": [
          {
            "groundingChunkIndices": [
              123
            ],
            "confidenceScores": [
              123
            ],
            "segment": {
              "startIndex": 123,
              "endIndex": 123,
              "text": "<string>"
            }
          }
        ],
        "webSearchQueries": [
          "<string>"
        ]
      },
      "index": 123
    }
  ],
  "promptFeedback": {
    "safetyRatings": [
      {
        "category": "<string>",
        "probability": "<string>",
        "blocked": true
      }
    ]
  },
  "usageMetadata": {
    "promptTokenCount": 123,
    "candidatesTokenCount": 123,
    "totalTokenCount": 123,
    "trafficType": "<string>",
    "thoughtsTokenCount": 123,
    "promptTokensDetails": [
      {
        "modality": "<string>",
        "tokenCount": 123
      }
    ],
    "candidatesTokensDetails": [
      {
        "modality": "<string>",
        "tokenCount": 123
      }
    ]
  },
  "modelVersion": "<string>",
  "createTime": "<string>",
  "responseId": "<string>"
}

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.

CometAPI 支援 Gemini 原生 API 格式,讓你能完整使用 Gemini 專屬功能,例如思考控制、Google Search grounding、原生圖片生成模態等。當你需要 OpenAI 相容聊天端點 無法提供的能力時,請使用此端點。
支援使用 x-goog-api-keyAuthorization: Bearer 標頭進行驗證。

快速開始

若要搭配 CometAPI 使用任何 Gemini SDK 或 HTTP 用戶端,請替換基底 URL 與 API 金鑰:
SettingGoogle DefaultCometAPI
Base URLgenerativelanguage.googleapis.comapi.cometapi.com
API Key$GEMINI_API_KEY$COMETAPI_KEY

設定思考(推理)

Gemini 模型可以在產生回應前先進行內部推理。控制方式取決於模型世代。
Gemini 3 模型使用 thinkingLevel 控制推理深度。可用層級:MINIMALLOWMEDIUMHIGH除非你明確需要不同的 Gemini 3 變體,否則請使用 gemini-3-flash-preview 作為預設範例模型。
curl "https://api.cometapi.com/v1beta/models/gemini-3-flash-preview:generateContent" \
  -H "Content-Type: application/json" \
  -H "x-goog-api-key: $COMETAPI_KEY" \
  -d '{
    "contents": [{"parts": [{"text": "Explain quantum physics simply."}]}],
    "generationConfig": {
      "thinkingConfig": {"thinkingLevel": "LOW"}
    }
  }'
在 Gemini 2.5 模型中使用 thinkingLevel(或在 Gemini 3 模型中使用 thinkingBudget)可能會導致錯誤。請為你的模型版本使用正確的參數。

串流回應

若要在模型產生內容時接收 Server-Sent Events,請使用 streamGenerateContent?alt=sse 作為 operator。每個 SSE 事件都包含一行帶有 JSON GenerateContentResponse 物件的 data:
curl "https://api.cometapi.com/v1beta/models/gemini-3-flash-preview:streamGenerateContent?alt=sse" \
  -H "Content-Type: application/json" \
  -H "x-goog-api-key: $COMETAPI_KEY" \
  --no-buffer \
  -d '{
    "contents": [{"parts": [{"text": "Write a short poem about the stars"}]}]
  }'

設定系統指示

若要在整段對話中引導模型的行為,請使用 systemInstruction
curl "https://api.cometapi.com/v1beta/models/gemini-3-flash-preview:generateContent" \
  -H "Content-Type: application/json" \
  -H "x-goog-api-key: $COMETAPI_KEY" \
  -d '{
    "contents": [{"parts": [{"text": "What is 2+2?"}]}],
    "systemInstruction": {
      "parts": [{"text": "You are a math tutor. Always show your work."}]
    }
  }'

請求 JSON 輸出

若要強制輸出結構化 JSON,請設定 responseMimeType。你也可以選擇提供 responseSchema 以進行嚴格的結構描述驗證:
curl "https://api.cometapi.com/v1beta/models/gemini-3-flash-preview:generateContent" \
  -H "Content-Type: application/json" \
  -H "x-goog-api-key: $COMETAPI_KEY" \
  -d '{
    "contents": [{"parts": [{"text": "List 3 planets with their distances from the sun"}]}],
    "generationConfig": {
      "responseMimeType": "application/json"
    }
  }'

使用 Google Search 作為 grounding

若要啟用即時網頁搜尋,請加入 googleSearch 工具:
curl "https://api.cometapi.com/v1beta/models/gemini-3-flash-preview:generateContent" \
  -H "Content-Type: application/json" \
  -H "x-goog-api-key: $COMETAPI_KEY" \
  -d '{
    "contents": [{"parts": [{"text": "Who won the euro 2024?"}]}],
    "tools": [{"google_search": {}}]
  }'
回應中會包含帶有來源 URL 與信心分數的 groundingMetadata

回應範例

以下是來自 CometAPI 的 Gemini 端點的典型回應:
{
  "candidates": [
    {
      "content": {
        "role": "model",
        "parts": [{"text": "Hello"}]
      },
      "finishReason": "STOP",
      "avgLogprobs": -0.0023
    }
  ],
  "usageMetadata": {
    "promptTokenCount": 5,
    "candidatesTokenCount": 1,
    "totalTokenCount": 30,
    "trafficType": "ON_DEMAND",
    "thoughtsTokenCount": 24,
    "promptTokensDetails": [{"modality": "TEXT", "tokenCount": 5}],
    "candidatesTokensDetails": [{"modality": "TEXT", "tokenCount": 1}]
  },
  "modelVersion": "gemini-3-flash-preview",
  "createTime": "2026-03-25T04:21:43.756483Z",
  "responseId": "CeynaY3LDtvG4_UP0qaCuQY"
}
usageMetadata 中的 thoughtsTokenCount 欄位會顯示模型在內部推理上花費了多少 Token,即使回應中不包含思考輸出也是如此。

與 OpenAI 相容端點比較

功能Gemini 原生 (/v1beta/models/...)OpenAI 相容 (/v1/chat/completions)
思考控制使用 thinkingConfig 搭配 thinkingLevel / thinkingBudget不提供
Google Search groundingtools: [\{"google_search": \{\}\}]不提供
Google Maps groundingtools: [\{"googleMaps": \{\}\}]不提供
圖像生成 modalityresponseModalities: ["IMAGE"]不提供
驗證標頭x-goog-api-keyBearerBearer
回應格式Gemini 原生(candidatespartsOpenAI 格式(choicesmessage

授權

x-goog-api-key
string
header
必填

Your CometAPI key passed via the x-goog-api-key header. Bearer token authentication (Authorization: Bearer <key>) is also supported.

路徑參數

model
string
必填

Gemini model ID. Example: gemini-3-flash-preview, gemini-2.5-pro. See the Models page for current options.

operator
enum<string>
必填

The operation to perform. Use generateContent for synchronous responses, or streamGenerateContent?alt=sse for Server-Sent Events streaming.

可用選項:
generateContent,
streamGenerateContent?alt=sse

主體

application/json
contents
object[]
systemInstruction
object

System instructions that guide the model's behavior across the entire conversation. Text only.

tools
object[]

Tools the model may use to generate responses. Supports function declarations, Google Search, Google Maps, and code execution.

toolConfig
object

Configuration for tool usage, such as function calling mode.

safetySettings
object[]

Safety filter settings. Override default thresholds for specific harm categories.

generationConfig
object

Configuration for model generation behavior including temperature, output length, and response format.

cachedContent
string

The name of cached content to use as context. Format: cachedContents/{id}. See the Gemini context caching documentation for details.

回應

200 - application/json

Successful response. For streaming requests, the response is a stream of SSE events, each containing a GenerateContentResponse JSON object prefixed with data: .

candidates
object[]

The generated response candidates.

promptFeedback
object

Feedback on the prompt, including safety blocking information.

usageMetadata
object

Token usage statistics for the request.

modelVersion
string

The model version that generated this response.

createTime
string

The timestamp when this response was created (ISO 8601 format).

responseId
string

Unique identifier for this response.