跳轉到主要內容
POST
透過 CometAPI 使用 Gemini 原生請求格式來產生文字、傳送影片輸入,以及設定思考或工具。範例使用 gemini-3.8-flash。
使用 Google 的 GenerateContent API 參考資料 瞭解欄位定義和模型專屬選項。本頁說明 CometAPI 基礎 URL、驗證方式及請求範例。
驗證支援 x-goog-api-key 和 Authorization: Bearer 標頭。

快速入門

若要搭配 CometAPI 使用 Google Gen AI SDK 或 HTTP 用戶端,請設定基礎 URL 和 API 金鑰: 使用 GenerateContent 文字產生指南 取得原生 API 範例。變更基礎 URL 時,請維持 generateContent 請求結構。

傳送影片輸入

將影片作為內容部分傳送。請根據影片儲存位置選擇輸入格式:
對於 REST 和 curl 請求,請使用 camelCase 欄位名稱,例如 inlineData.mimeType 和 fileData.fileUri。
此範例會傳送內嵌 MP4 資料。請以影片的 base64 內容取代 <base64-encoded-mp4>:
此範例會分析一段花朵綻放的公開 MP4 影片:

設定思考 (推理)

使用 thinkingConfig.thinkingLevel 引導推理深度。以下範例使用 LOW 和 MEDIUM。
此範例將思考層級設為 LOW:
thinkingBudget 是適用模型的數值控制項,包括 Gemini 2.5。在 Gemini 3 範例中使用 thinkingLevel,且不要同時傳送這兩個控制項。請參閱 Google 的 思考指南 以瞭解特定模型的值。

串流回應

使用 streamGenerateContent?alt=sse 來接收 Server-Sent Events。每一行 data: 都包含一個 JSON GenerateContentResponse 物件:

設定系統指示

使用 systemInstruction 引導回應。此範例要求只輸出一個方程式,不含其他文字:

要求 JSON 輸出

將 responseMimeType 設為 application/json,並提供 responseSchema。此範例要求傳回一個包含行星名稱和數值距離的陣列:

使用 Google Search 進行搜尋依據

新增 googleSearch 工具以請求搜尋依據。此範例會查詢 UEFA EURO 2024 決賽的結果:
使用搜尋時,請檢查候選項目的 groundingMetadata,以取得搜尋查詢、來源 URL,以及來源與回應文字之間的連結。

保留對話內容

對於多輪對話,請在 user 中傳送先前的 model 內容。SDK 聊天範例會為您維護此歷程記錄。 contents 對於函式呼叫(Function Calling),請為每個 functionResponse 傳回一個 functionCall,並使用相符的 name 和任何傳回的 id。將先前的模型內容原封不動地傳回,包括 thoughtSignature 欄位。簽章是不透明的;請勿根據顯示的文字重新建構它。

回應範例

文字回應包含產生的內容和 Token 使用量。此簡化範例省略了選用欄位:
thoughtsTokenCount 即使回應未包含思考摘要,也會回報內部思考 Token。請檢查每個內容部分;回應可包含文字、摘要或函式呼叫。

比較請求格式

請針對 Gemini 請求與回應欄位選擇原生端點。請參閱 聊天補全 以瞭解與 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-3.8-flash
必填

Gemini model ID. These examples use gemini-3.8-flash. See the Models page for available model IDs.

operator
enum<string>
預設值:generateContent
必填

Operation to perform. Use generateContent for a JSON response. For Server-Sent Events, select streamGenerateContent and set the separate alt query parameter to sse.

可用選項:
generateContent,
streamGenerateContent

查詢參數

alt
enum<string>

Set to sse when the operator is streamGenerateContent. Omit this parameter for generateContent.

可用選項:
sse

主體

application/json
contents
object[]

Conversation content. Each entry has an optional role (user or model) and a parts array. For tool results, preserve the complete preceding model content, including any thoughtSignature fields.

systemInstruction
object

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

tools
object[]

Tools available to the model during generation. Use googleSearch for search grounding.

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.

最後修改於 2026年9月8日