API 参考Google 兼容
搜索接地
Gemini Grounding API - 使用 Google 搜索实时数据增强 AI 响应。构建事实核查、实时更新的 AI 应用,集成网络搜索。
使用 Google Search 接地
接地功能将 Gemini 连接到 Google Search 的实时信息,确保响应基于当前、真实的数据,而不仅仅是训练知识。
支持的模型: 接地功能在 Gemini 1.5+ 模型上可用。
工作原理
- 启用 Google Search 接地
- 模型查询 Google Search 获取相关信息
- 响应引用搜索结果中的来源
- 接地元数据显示使用了哪些来源
启用接地
curl -X POST "https://api.uniterouter.ai/v1beta/models/gemini-2.0-flash:generateContent" \
-H "Content-Type: application/json" \
-H "x-goog-api-key: $UNITEROUTER_API_KEY" \
-d '{
"contents": [
{
"role": "user",
"parts": [{"text": "AI 监管的最新进展是什么?"}]
}
],
"tools": [
{
"googleSearch": {}
}
]
}'import google.generativeai as genai
genai.configure(
api_key="your_uniterouter_api_key",
transport="rest",
client_options={"api_endpoint": "api.uniterouter.ai"}
)
model = genai.GenerativeModel(
"gemini-2.0-flash",
tools=["google_search"]
)
response = model.generate_content(
"AI 监管的最新进展是什么?"
)
print(response.text)
# 访问接地元数据
if response.candidates[0].grounding_metadata:
for chunk in response.candidates[0].grounding_metadata.grounding_chunks:
if chunk.web:
print(f"来源: {chunk.web.title} - {chunk.web.uri}")import { GoogleGenerativeAI } from "@google/generative-ai";
const genAI = new GoogleGenerativeAI("your_uniterouter_api_key", {
baseUrl: "https://api.uniterouter.ai"
});
const model = genAI.getGenerativeModel({
model: "gemini-2.0-flash",
tools: [{ googleSearch: {} }]
});
const result = await model.generateContent(
"AI 监管的最新进展是什么?"
);
console.log(result.response.text());
// 访问接地元数据
const metadata = result.response.candidates?.[0]?.groundingMetadata;
if (metadata?.groundingChunks) {
for (const chunk of metadata.groundingChunks) {
if (chunk.web) {
console.log(`来源: ${chunk.web.title} - ${chunk.web.uri}`);
}
}
}带接地的响应
{
"candidates": [{
"content": {
"parts": [{
"text": "AI 监管的最新进展包括..."
}],
"role": "model"
},
"groundingMetadata": {
"searchEntryPoint": {
"renderedContent": "<搜索结果 HTML>"
},
"groundingChunks": [
{
"web": {
"uri": "https://example.com/ai-regulation",
"title": "AI 监管更新 2024"
}
}
],
"groundingSupports": [
{
"segment": {
"startIndex": 0,
"endIndex": 100
},
"groundingChunkIndices": [0],
"confidenceScores": [0.95]
}
],
"webSearchQueries": ["AI 监管 2024 最新新闻"]
}
}]
}接地元数据
| 字段 | 描述 |
|---|---|
searchEntryPoint | 渲染的搜索结果 |
groundingChunks | 使用的来源(网页) |
groundingSupports | 响应的哪些部分引用了哪些来源 |
webSearchQueries | 发送到 Google Search 的查询 |
retrievalMetadata | 额外的检索信息 |
动态检索
控制何时使用接地:
{
"tools": [{
"googleSearch": {
"dynamicRetrievalConfig": {
"mode": "MODE_DYNAMIC",
"dynamicThreshold": 0.5
}
}
}]
}| 模式 | 描述 |
|---|---|
MODE_UNSPECIFIED | 默认行为 |
MODE_DYNAMIC | 模型根据查询决定 |
使用场景
| 使用场景 | 示例查询 |
|---|---|
| 时事新闻 | "今天有什么新闻?" |
| 事实核查 | "东京的人口是多少?" |
| 研究 | "量子计算的最新研究" |
| 产品信息 | "iPhone 15 是什么时候发布的?" |
| 体育 | "谁赢得了上届世界杯?" |
Google Maps 接地
用于基于位置的查询:
model = genai.GenerativeModel(
"gemini-2.0-flash",
tools=["google_maps"]
)
response = model.generate_content(
"找找时代广场附近的意大利餐厅"
)与其他工具结合接地
model = genai.GenerativeModel(
"gemini-2.0-flash",
tools=[
"google_search",
"code_execution",
{
"function_declarations": [...]
}
]
)最佳实践
- 用于当前信息 - 接地最适合时效性查询
- 检查引用 - 验证重要声明与来源是否一致
- 组合工具 - 与代码执行结合进行数据分析
- 动态模式 - 让模型决定何时搜索以提高效率
限制
- 可能存在搜索配额限制
- 某些查询可能不会触发搜索
- 结果取决于 Google Search 可用性
相关文档
How is this guide?