生成内容
通过 CometAPI 使用 Gemini 原生 API 格式进行文本生成、多模态输入、推理、函数调用、Google Search grounding、JSON 模式和流式输出。
x-goog-api-key 和 Authorization: Bearer 请求头进行身份验证。快速开始
要通过 CometAPI 使用任意 Gemini SDK 或 HTTP 客户端,请替换基础 URL 和 API key:发送视频输入
GeminigenerateContent 接受将视频作为内容 part 传入。根据视频的存储位置选择输入形式:
inlineData.mimeType 和 fileData.fileUri。不要将 URL 媒体作为 file_data.file_uri 发送。fileData.fileUri 发送公开的 MP4 URL:
generateContent 请求本身中通过 inlineData 或 fileData.fileUri 发送媒体。
配置思考(推理)
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 对象。
设置系统指令
要在整个对话过程中引导模型行为,请使用systemInstruction:
请求 JSON 输出
要强制输出结构化 JSON,请设置responseMimeType。你也可以选择提供 responseSchema 以进行严格的 schema 校验:
使用 Google Search 进行 grounding
要启用实时网页搜索,请添加一个googleSearch 工具:
groundingMetadata,其中带有来源 URL 和置信度分数。
响应示例
CometAPI 的 Gemini 端点返回的典型响应:usageMetadata 中的 thoughtsTokenCount 字段显示了模型在内部推理上消耗了多少 Token,即使响应中未包含思考输出也是如此。与 OpenAI-compatible 端点对比
授权
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.