生成内容
通过 CometAPI 使用 Gemini 原生 API 格式进行文本生成、视频输入、思考摘要、Google 搜索依据、JSON 输出和流式输出。
gemini-3.8-flash。
x-goog-api-key 和 Authorization: Bearer 请求头进行身份验证。快速入门
要将 Google Gen AI SDK 或 HTTP 客户端与 CometAPI 配合使用,请配置基础 URL 和 API 密钥:generateContent 请求结构不变。
发送视频输入
将视频作为内容部分发送。根据视频的存储位置选择输入形式:inlineData.mimeType 和 fileData.fileUri。<base64-encoded-mp4> 替换为视频的 base64 内容:
配置思考 (推理)
使用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 搜索进行事实依据验证
添加一个googleSearch 工具以请求搜索事实依据验证。此示例询问 UEFA EURO 2024 决赛的结果:
groundingMetadata,获取搜索查询、来源 URL,以及来源与响应文本之间的链接。
保留对话内容
对于多轮对话,请在user 中发送此前的 model 内容。SDK 聊天示例会为你维护此历史记录。请在 contents 中包含这些内容。
对于函数调用(Function Calling),请为每个 functionResponse 返回一个 functionCall,并包含匹配的 name 以及所有返回的 id。将此前的模型内容原样传回,包括 thoughtSignature 字段。该签名是不透明的;请勿根据显示的文本重建它。
响应示例
文本响应包含生成的内容和 Token 使用情况。此简化示例省略了可选字段:thoughtsTokenCount 会报告内部思考 Token,即使响应不包含思考摘要也是如此。请检查每个内容部分;响应可以包含文本、摘要或函数调用(Function Calling)。比较请求格式
对于 Gemini 请求和响应字段,请选择原生端点。请参阅 聊天补全 了解与 OpenAI 兼容的格式。授权
Your CometAPI key passed via the x-goog-api-key header. Bearer token authentication (Authorization: Bearer $COMETAPI_KEY) is also supported.
路径参数
Gemini model ID. These examples use gemini-3.8-flash. See the Models page for available model IDs.
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 查询参数
Set to sse when the operator is streamGenerateContent. Omit this parameter for generateContent.
sse 请求体
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.
System instructions that guide the model's behavior across the entire conversation. Text only.
Tools available to the model during generation. Use googleSearch for search grounding.
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.