Skip to main content
POST
CometAPI 支持 Gemini 原生 API 格式,让你可以完整使用 Gemini 特有功能,例如思考控制、Google Search grounding、原生图像生成模态等。当你需要 OpenAI-compatible chat endpoint 无法提供的能力时,请使用此端点。
请将 Google 官方的 GenerateContent API reference 作为完整请求字段、响应 schema 和 Gemini 模型特定行为的权威来源。本 CometAPI 页面说明了如何通过 CometAPI 发送该原生请求格式。
随着 Google 更新原生 API,Gemini 请求参数和响应字段可能会发生变化。请查看 Gemini 文本生成文档 以获取最新的完整参数列表和提供商特定行为。
支持使用 x-goog-api-keyAuthorization: Bearer 请求头进行身份验证。

快速开始

要通过 CometAPI 使用任意 Gemini SDK 或 HTTP 客户端,请替换基础 URL 和 API key:

发送视频输入

Gemini generateContent 接受将视频作为内容 part 传入。根据视频的存储位置选择输入形式:
对于 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 发送媒体。

配置思考(推理)

Gemini 模型可以在生成响应之前进行内部推理。控制方式取决于模型代际。
Gemini 3 模型使用 thinkingLevel 来控制推理深度。可用级别:MINIMALLOWMEDIUMHIGH除非你明确需要其他 Gemini 3 变体,否则默认示例模型请使用 gemini-3-flash-preview
thinkingLevel 用于 Gemini 2.5 模型(或将 thinkingBudget 用于 Gemini 3 模型)可能会导致错误。请为你的模型版本使用正确的参数。

流式输出响应

要在模型生成内容时接收 Server-Sent Events,请使用 streamGenerateContent?alt=sse 作为操作符。每个 SSE 事件都包含一行 data:,其中带有一个 JSON GenerateContentResponse 对象。

设置系统指令

要在整个对话过程中引导模型行为,请使用 systemInstruction

请求 JSON 输出

要强制输出结构化 JSON,请设置 responseMimeType。你也可以选择提供 responseSchema 以进行严格的 schema 校验:

使用 Google Search 进行 grounding

要启用实时网页搜索,请添加一个 googleSearch 工具:
响应中会包含 groundingMetadata,其中带有来源 URL 和置信度分数。

响应示例

CometAPI 的 Gemini 端点返回的典型响应:
usageMetadata 中的 thoughtsTokenCount 字段显示了模型在内部推理上消耗了多少 Token,即使响应中未包含思考输出也是如此。

与 OpenAI-compatible 端点对比

授权

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.