返回模型调用 API 参考POST/v1beta/models/gemini-2.5-pro:generateContent
Gemini:函数调用与思考签名多轮
声明函数并回传 functionResponse,完整保留 model content 与 thoughtSignature。
功能与适用场景
由模型生成函数调用建议,应用校验后执行。示例只返回固定的北京天气,明确标注为示例数据。
支持模型与组合
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:generateContentBody
必填路径、查询与 Header 参数
| 参数 | 位置 | 类型 | 必填 | 说明 | 示例 |
|---|
key | Query | string | 是 | - | YOUR_API_KEY |
Content-Type | Header | string | 是 | - | application/json |
校验参数并回传结果
保留上一段生成的 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 示例。