返回模型调用 API 参考
POST/v1beta/models/gemini-2.5-pro:generateContent

Gemini:函数调用与思考签名多轮

声明函数并回传 functionResponse,完整保留 model content 与 thoughtSignature。

API Key application/json

功能与适用场景

由模型生成函数调用建议,应用校验后执行。示例只返回固定的北京天气,明确标注为示例数据。

支持模型与组合

gemini-3.8-flash。原生格式适合需要保留思考签名的多轮工具应用。

第一步:设置鉴权

使用青云TOP API 的 API Key:Authorization: Bearer YOUR_API_KEY。替换占位符,保留 Bearer 后的空格。无需厂商 API Key。

第二步:调用模型

POST https://top.qingyuntop.ai/v1beta/models/gemini-3.8-flash:generateContent

请求为 application/json。

Request

请求

/v1beta/models/gemini-2.5-pro:generateContent

Body

必填
Bodyapplication/json
Bodyobject
contentsarray必填

必填,顶层 content 数组。role 使用 user / model,parts 保持原始顺序;下一轮重新发送历史。

元素object

数组元素。

partsarray<object>

每条 content 必填。文本 {text:...};媒体 {inlineData:{mimeType,data}};一张图片一个 part,顺序影响引用。

元素object

数组元素。

textstring
inline_dataobject
mime_typestring必填
datastring必填
generationConfigobject

选填,顶层对象。输出长度、采样、结构化格式、思考和语音设置分别放在对象对应子字段。

responseModalitiesarray<string>必填
元素string
temperaturenumber
topPnumber
topKinteger
maxOutputTokensinteger

选填,正整数,无统一默认。限制输出 token,思考也占用预算;示例 4096。

stopSequencesarray<string>
元素string

数组元素。

model(路径)string必填

必填,在 /v1beta/models/{model}:动作 中填写;generateContent 请求体无需重复 model。

toolsarray<object>

选填,顶层工具数组。按本页选择 functionDeclarations、googleSearch、urlContext 或 codeExecution;名称区分大小写。

元素object

数组元素。

safetySettingsarray<object>

选填字段;是否可用取决于当前模型和协议。

元素object

数组元素。

categorystring
thresholdstring
systemInstructionobject

选填,顶层。parts 中填写文本指令,不放入 generationConfig。

partsarray<object>
元素object

数组元素。

路径、查询与 Header 参数

参数位置类型必填说明示例
keyQuerystring是-YOUR_API_KEY
Content-TypeHeaderstring是-application/json
Response

返回响应 200

HTTP 响应
响应 200json
响应 200object
candidatesarray

候选数组,可缺省或为空。按 index 区分,遍历 content.parts;text 是文字,inlineData 是媒体,functionCall 是函数调用。不能只取第一个 part。

元素object

数组元素。

contentobject
rolestring
partsarray<object>
元素object

数组元素。

finishReasonstring

流式中间片可缺省。STOP 为正常结束,MAX_TOKENS 为截断,SAFETY / RECITATION 等需显示相应限制;缺少最终原因时不能认定内容完整。

safetyRatingsarray<object>
元素object

数组元素。

usageMetadataobject

可缺省的用量对象,含 promptTokenCount、candidatesTokenCount、totalTokenCount,思考详情按实际返回;流式汇总不逐片相加。

promptTokenCountinteger
candidatesTokenCountinteger
totalTokenCountinteger
promptFeedbackobject
blockReasonstring

提示词被拦截时可能出现;这时可能没有 candidates。先检查拦截原因,不把空候选当作空白的成功答案。

校验参数并回传结果

保留上一段生成的 request.json 与 response.json,运行下方代码。它回传整个 model content 和工具声明,保留 thoughtSignature 与函数调用位置;结果进入下一条 user 消息。后续仍可能返回函数调用,应再次校验后处理。

set -euo pipefail
python3 - <<'PY'
import base64
import json
from pathlib import Path

payload = json.loads(r'''{
  "contents": []
}''')
previous = json.loads(Path("request.json").read_text(encoding="utf-8"))
result = json.loads(Path("response.json").read_text(encoding="utf-8"))
content = result["candidates"][0]["content"]
replies = []
for part in content["parts"]:
    call = part.get("functionCall")
    if not call:
        continue
    if call["name"] != "get_weather" or call["args"].get("city") != "北京":
        raise ValueError("工具名称或参数不在示例白名单中")
    reply = {"name": call["name"], "response": {"temperature_c": 22, "source": "示例数据"}}
    if "id" in call:
        reply["id"] = call["id"]
    replies.append({"functionResponse": reply})
if not replies:
    raise RuntimeError("本轮没有工具调用")
payload["contents"] = previous["contents"] + [content, {"role": "user", "parts": replies}]
payload["tools"] = previous["tools"]
if "systemInstruction" in previous:
    payload["systemInstruction"] = previous["systemInstruction"]
encoded = json.dumps(payload, ensure_ascii=False).encode("utf-8")
Path("next-request.json").write_bytes(encoded)
PY
curl --fail-with-body --connect-timeout 30 --max-time 600 "https://top.qingyuntop.ai/v1beta/models/gemini-3.8-flash:generateContent" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  --data-binary @next-request.json \
  --output next-response.json

常见错误与参考

删除签名、把 camelCase 改为 snake_case、重排 parts、仅回传一段 text,都可能导致下一轮校验失败。生产应用限制工具轮数、耗时和允许操作。

401:检查本站 API Key 和 Bearer 格式;403:确认此 Key 可用该模型;429:遵循 Retry-After 退避。超时或 5xx 时保留请求标识和脱敏错误,避免盲目重发;其他通用排查见聊天接入总览。

Google 官方 generateContent 示例。