產生內容
透過 CometAPI 使用 Gemini 原生 API 格式,以進行文字生成、多模態輸入、推理、函式呼叫(Function Calling)、Google Search grounding、JSON 模式與串流。
x-goog-api-key 與 Authorization: Bearer 標頭。快速開始
若要搭配 CometAPI 使用任何 Gemini SDK 或 HTTP 用戶端,請替換 base URL 與 API key:傳送影片輸入
GeminigenerateContent 可接受影片作為內容部分。請根據影片的儲存位置選擇輸入形式:
inlineData.mimeType 與 fileData.fileUri。不要將 URL 媒體以 file_data.file_uri 傳送。fileData.fileUri 傳送公開的 MP4 URL:
generateContent 請求中使用 inlineData 或 fileData.fileUri 傳送媒體。
設定 thinking(推理)
Gemini 模型可以在產生回應之前先進行內部推理。控制方式取決於模型世代。- Gemini 3 (thinkingLevel)
- Gemini 2.5 (thinkingBudget)
thinkingLevel 來控制推理深度。可用層級:MINIMAL、LOW、MEDIUM、HIGH。除非你明確需要不同的 Gemini 3 變體,否則請使用 gemini-3-flash-preview 作為預設範例模型。串流回應
若要在模型產生內容時接收 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 相容端點比較
授權
Your CometAPI key passed via the x-goog-api-key header. Bearer token authentication (Authorization: Bearer $COMETAPI_KEY) is also supported.
路徑參數
Gemini model ID. Example: gemini-3-flash-preview, gemini-2.5-pro. See the Models page for current options.
The operation to perform. Use generateContent for synchronous responses, or streamGenerateContent?alt=sse for Server-Sent Events streaming.
generateContent, streamGenerateContent?alt=sse 主體
Conversation content. Each entry has an optional role (user or model) and a parts array.
System instructions that guide the model's behavior across the entire conversation. Text only.
Tools the model may use to generate responses. Supports function declarations, Google Search, Google Maps, and code execution.
Configuration for tool usage, such as function calling mode.
Safety filter settings. Override default thresholds for specific harm categories.
Configuration for model generation behavior including temperature, output length, and response format.
The name of cached content to use as context. Format: cachedContents/{id}. See the Gemini context caching documentation for details.
回應
Successful response. For streaming requests, the response is a stream of SSE events, each containing a GenerateContentResponse JSON object prefixed with data: .
The generated response candidates.
Feedback on the prompt, including safety blocking information.
Token usage statistics for the request.
The model version that generated this response.
The timestamp when this response was created (ISO 8601 format).
Unique identifier for this response.