Skip to main content
POST
CometAPI 支援 Gemini 原生 API 格式,讓你能完整使用 Gemini 專屬功能,例如思考控制、Google Search grounding、原生圖片生成模態等。當你需要 OpenAI 相容聊天端點 無法提供的能力時,請使用此端點。
請使用 Google 官方的 GenerateContent API reference 作為完整請求欄位、回應結構描述與 Gemini 模型特定行為的權威來源。此 CometAPI 頁面說明如何透過 CometAPI 傳送該原生請求格式。
隨著 Google 更新原生 API,Gemini 的請求參數與回應欄位可能會變更。請查閱 Gemini 文字生成文件 以取得最新且完整的參數清單與供應商特定行為。
驗證同時支援 x-goog-api-keyAuthorization: Bearer 標頭。

快速開始

若要搭配 CometAPI 使用任何 Gemini SDK 或 HTTP 用戶端,請替換 base URL 與 API key:

傳送影片輸入

Gemini generateContent 可接受影片作為內容部分。請根據影片的儲存位置選擇輸入形式:
對於 REST 與 curl 請求,請使用 Gemini 的 camelCase 欄位名稱,例如 inlineData.mimeTypefileData.fileUri。不要將 URL 媒體以 file_data.file_uri 傳送。
此範例會讀取本機 MP4 檔案,將其編碼為 base64,並在請求主體中傳送:
此範例會使用 fileData.fileUri 傳送公開的 MP4 URL:
CometAPI 不建議為此端點使用獨立的 Gemini Files API 上傳流程。請直接在 generateContent 請求中使用 inlineDatafileData.fileUri 傳送媒體。

設定 thinking(推理)

Gemini 模型可以在產生回應之前先進行內部推理。控制方式取決於模型世代。
Gemini 3 模型使用 thinkingLevel 來控制推理深度。可用層級:MINIMALLOWMEDIUMHIGH除非你明確需要不同的 Gemini 3 變體,否則請使用 gemini-3-flash-preview 作為預設範例模型。
在 Gemini 2.5 模型中使用 thinkingLevel(或在 Gemini 3 模型中使用 thinkingBudget)可能會導致錯誤。請針對你的模型版本使用正確的參數。

串流回應

若要在模型產生內容時接收 Server-Sent Events,請使用 streamGenerateContent?alt=sse 作為運算子。每個 SSE 事件都包含一行 data:,其中帶有一個 JSON GenerateContentResponse 物件。

設定 system instructions

若要在整段對話中引導模型的行為,請使用 systemInstruction

請求 JSON 輸出

若要強制輸出結構化 JSON,請設定 responseMimeType。你也可以選擇提供 responseSchema,以進行嚴格的結構描述驗證:

使用 Google Search 進行 grounding

若要啟用即時網頁搜尋,請加入 googleSearch 工具:
回應會包含 groundingMetadata,其中包含來源 URL 和信心分數。

回應範例

來自 CometAPI 的 Gemini 端點的典型回應:
usageMetadata 中的 thoughtsTokenCount 欄位會顯示模型在內部推理上花費了多少 Token,即使回應中未包含 thinking 輸出也是如此。

與 OpenAI 相容端點比較

授權

x-goog-api-key
string
header
必填

Your CometAPI key passed via the x-goog-api-key header. Bearer token authentication (Authorization: Bearer $COMETAPI_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[]

Conversation content. Each entry has an optional role (user or model) and a parts array.

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.