Gemini 生成内容
通过 CometAPI 使用 Gemini 原生 API 格式进行文本生成、多模态(Multimodal)输入、思考/推理、函数调用(Function Calling)、Google Search grounding、JSON 模式和流式输出(Streaming)。
CometAPI 支持 Gemini 原生 API 格式,让你能够完整使用 Gemini 特有功能,例如思考控制、Google Search grounding、原生图像生成模态等。当你需要 OpenAI-compatible chat endpoint 无法提供的能力时,请使用此端点。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 客户端,请替换 base URL 和 API key:| 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 以进行严格的 schema 验证:
使用 Google Search 进行 grounding
如需启用实时网页搜索,请添加一个googleSearch 工具:
groundingMetadata,其中带有来源 URL 和置信度分数。
响应示例
以下是来自 CometAPI 的 Gemini 端点的一个典型响应:usageMetadata 中的 thoughtsTokenCount 字段显示了模型在内部推理上消耗了多少 tokens,即使响应中不包含思考输出也是如此。与 OpenAI 兼容端点对比
| 功能 | Gemini 原生 (/v1beta/models/...) | OpenAI 兼容 (/v1/chat/completions) |
|---|---|---|
| 思考控制 | 带有 thinkingLevel / thinkingBudget 的 thinkingConfig | 不可用 |
| Google Search grounding | tools: [\{"google_search": \{\}\}] | 不可用 |
| Google Maps grounding | tools: [\{"googleMaps": \{\}\}] | 不可用 |
| 图像生成模态 | 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.