Gemini 產生內容
透過 CometAPI 使用 Gemini 原生 API 格式,以進行文字生成、多模態輸入、思考/推理、函式呼叫(Function Calling)、Google Search grounding、JSON 模式與串流(Streaming)。
CometAPI 支援 Gemini 原生 API 格式,讓你能完整使用 Gemini 專屬功能,例如思考控制、Google Search grounding、原生圖片生成模態等。當你需要 OpenAI 相容聊天端點 無法提供的能力時,請使用此端點。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.
x-goog-api-key 與 Authorization: Bearer 標頭進行驗證。快速開始
若要搭配 CometAPI 使用任何 Gemini SDK 或 HTTP 用戶端,請替換基底 URL 與 API 金鑰:| Setting | Google Default | CometAPI |
|---|---|---|
| Base URL | generativelanguage.googleapis.com | api.cometapi.com |
| API Key | $GEMINI_API_KEY | $COMETAPI_KEY |
設定思考(推理)
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 作為 operator。每個 SSE 事件都包含一行帶有 JSON GenerateContentResponse 物件的 data:。
設定系統指示
若要在整段對話中引導模型的行為,請使用systemInstruction:
請求 JSON 輸出
若要強制輸出結構化 JSON,請設定responseMimeType。你也可以選擇提供 responseSchema 以進行嚴格的結構描述驗證:
使用 Google Search 作為 grounding
若要啟用即時網頁搜尋,請加入googleSearch 工具:
groundingMetadata。
回應範例
以下是來自 CometAPI 的 Gemini 端點的典型回應:usageMetadata 中的 thoughtsTokenCount 欄位會顯示模型在內部推理上花費了多少 Token,即使回應中不包含思考輸出也是如此。與 OpenAI 相容端點比較
| 功能 | Gemini 原生 (/v1beta/models/...) | OpenAI 相容 (/v1/chat/completions) |
|---|---|---|
| 思考控制 | 使用 thinkingConfig 搭配 thinkingLevel / thinkingBudget | 不提供 |
| Google Search grounding | tools: [\{"google_search": \{\}\}] | 不提供 |
| Google Maps grounding | tools: [\{"googleMaps": \{\}\}] | 不提供 |
| 圖像生成 modality | responseModalities: ["IMAGE"] | 不提供 |
| 驗證標頭 | x-goog-api-key 或 Bearer | 僅 Bearer |
| 回應格式 | Gemini 原生(candidates、parts) | OpenAI 格式(choices、message) |
授權
Your CometAPI key passed via the x-goog-api-key header. Bearer token authentication (Authorization: Bearer <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 主體
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.