用 Google Search Grounding 给 Gemini 回答加来源
打开 Google Search grounding,让回答引用可核验的网络来源。
基于 Gemini API 整理 · Grounding · 官方资料
时效性问题需要外部检索。Gemini 的 Google Search grounding 把搜索接进生成过程。
这篇启用 grounding 工具,提问一个需要当前事实的问题,并检查 grounding metadata / 引用来源。
启用 Google Search 工具
Google Search grounding
python
from google import genai
from google.genai import types
client = genai.Client()
response = client.models.generate_content(
model="gemini-3.1-pro-preview",
contents="今天有哪些公开的 AI 模型发布值得关注?请给出可点击来源。",
config=types.GenerateContentConfig(
tools=[types.Tool(google_search=types.GoogleSearch())]
),
)
print(response.text)
print(response.candidates[0].grounding_metadata)最容易踩的坑
没有 grounding metadata
确认工具已启用且问题需要检索;纯定义题可能不触发搜索。