使用 Google 搜索建立依据

依托 Google 搜索进行接地,可将 Gemini 模型连接到实时网络内容,并支持所有可用语言。这样一来,Gemini 就可以提供更准确的回答,并引用知识截点之后的可验证来源。

接地有助于您构建能够执行以下操作的应用:

  • 提高事实准确性:通过以真实世界的信息为依据来减少模型幻觉。
  • 获取实时信息:回答有关近期事件和主题的问题。
  • 提供引用:通过显示模型声明的来源来建立用户信任。

Python

# This will only work for SDK newer than 2.0.0
from google import genai

client = genai.Client()

interaction = client.interactions.create(
    model="gemini-3-flash-preview",
    input="Who won the euro 2024?",
    tools=[{"type": "google_search"}]
)

# Print the model's text response
for step in interaction.steps:
    if step.type == "model_output":
        for content_block in step.content:
            if content_block.type == "text":
                print(content_block.text)

JavaScript

// This will only work for SDK newer than 2.0.0
import { GoogleGenAI } from "@google/genai";

const client = new GoogleGenAI({});

const interaction = await client.interactions.create({
    model: "gemini-3-flash-preview",
    input: "Who won the euro 2024?",
    tools: [{ type: "google_search" }]
});

const modelStep = interaction.steps.find(s => s.type === 'model_output');
if (modelStep) {
  for (const contentBlock of modelStep.content) {
    if (contentBlock.type === 'text') console.log(contentBlock.text);
  }
}

REST

# Specifies the API revision to avoid breaking changes when they become default
curl -X POST "https://generativelanguage.googleapis.com/v1beta/interactions" \
  -H "x-goog-api-key: $GEMINI_API_KEY" \
  -H "Content-Type: application/json" \
  -H "Api-Revision: 2026-05-20" \
  -d '{
    "model": "gemini-3-flash-preview",
    "input": "Who won the euro 2024?",
    "tools": [{"type": "google_search"}]
  }'

“依托 Google 搜索进行接地”功能的运作方式

启用 google_search 工具后,模型会自动处理搜索、处理和引用信息的整个工作流程。

grounding-overview

  1. 用户提示:您的应用在启用 google_search 工具的情况下,将用户提示发送到 Gemini API。
  2. 提示分析:模型分析提示,确定 Google 搜索是否可以改进回答。
  3. Google 搜索:如果需要,模型会自动生成一个或多个搜索查询并执行这些查询。
  4. 搜索结果处理:模型处理搜索结果,综合信息并生成回答。
  5. 以搜索结果为依据的回答:API 会返回以搜索结果为依据的最终且用户友好的回答。此回答包含模型的文本答案(内嵌 annotations,其中包含引用),以及包含搜索查询和搜索建议的 google_search_callgoogle_search_result 步骤。

了解接地响应

如果成功进行了事实依据核查,模型生成的文本输出会在文本内容块中直接包含内嵌的 annotations。这些注释提供了引用信息,可将回答的各个部分与其来源相关联。

{
  "steps": [
    {
      "type": "thought",
      "summary": [
        {
          "type": "text",
          "text": "The user is asking for the winner of Euro 2024. I need to search for the result of the Euro 2024 final."
        }
      ],
      "signature": "CoMDAXLI2nynRYojJIy6B1Jh9os2crpWLfB0..."
    },
    {
      "type": "google_search_call",
      "arguments": {
        "queries": ["UEFA Euro 2024 winner"]
      }
    },
    {
      "type": "google_search_result",
      "call_id": "search_001",
      "result": [
        {
          "search_suggestions": "<!-- HTML and CSS for the search widget -->"
        }
      ]
    },
    {
      "type": "model_output",
      "content": [
        {
          "type": "text",
          "text": "Spain won Euro 2024, defeating England 2-1 in the final. This victory marks Spain's record fourth European Championship title.",
          "annotations": [
            {
              "type": "url_citation",
              "url": "https://www.aljazeera.com/sports/euro-2024-final",
              "title": "aljazeera.com",
              "start_index": 0,
              "end_index": 56
            },
            {
              "type": "url_citation",
              "url": "https://www.uefa.com/euro2024/news/spain-wins-euro-2024",
              "title": "uefa.com",
              "start_index": 57,
              "end_index": 124
            }
          ]
        }
      ]
    }
  ]
}

响应中的关键字段:

  • google_search_call:包含模型执行的搜索 queries
  • google_search_result:包含 search_suggestions,这是一个用于在界面中呈现搜索建议的 HTML 代码段。如需了解完整的使用要求,请参阅服务条款
  • text(含 annotations):模型合成的回答,其中包含内嵌引用。每个 url_citation 注释都将一段文字(由 start_indexend_index 定义)链接到源网址。这是创建内嵌引文的关键。

依托 Google 搜索进行接地还可以与网址上下文工具搭配使用,以便基于公开 Web 数据和您提供的特定网址来生成回答。

使用内嵌引用注明来源

该 API 会在文本内容块上返回内嵌 url_citation 注释,让您可以完全控制如何在用户界面中显示来源。 每条注释都包含 start_indexend_index,用于标识注释引用的文本部分。下面介绍了如何提取和显示这些数据。

Python

for step in interaction.steps:
    if step.type == "model_output":
        for content_block in step.content:
            if content_block.type == "text":
                print(content_block.text)
                if content_block.annotations:
                    print("\nCitations:")
                    for annotation in content_block.annotations:
                        if annotation.type == "url_citation":
                            cited_text = content_block.text[annotation.start_index:annotation.end_index]
                            print(f"  [{annotation.title}]({annotation.url})")
                            print(f"    Cited text: \"{cited_text}\"")

JavaScript

for (const step of interaction.steps) {
  if (step.type === 'model_output') {
    for (const contentBlock of step.content) {
      if (contentBlock.type === 'text') {
        console.log(contentBlock.text);
        if (contentBlock.annotations) {
          console.log("\nCitations:");
          for (const annotation of contentBlock.annotations) {
            if (annotation.type === 'url_citation') {
              const citedText = contentBlock.text.slice(annotation.startIndex, annotation.endIndex);
              console.log(`  [${annotation.title}](${annotation.url})`);
              console.log(`    Cited text: "${citedText}"`);
            }
          }
        }
      }
    }
  }
}

输出将显示文本及其引用:

Spain won Euro 2024, defeating England 2-1 in the final. This victory marks Spain's record fourth European Championship title.

Citations:
  [aljazeera.com](https://www.aljazeera.com/sports/euro-2024-final)
    Cited text: "Spain won Euro 2024, defeating England 2-1 in the final."
  [uefa.com](https://www.uefa.com/euro2024/news/spain-wins-euro-2024)
    Cited text: "This victory marks Spain's record fourth European Championship title."

价格

如果您将“依托 Google 搜索进行接地”与 Gemini 3 搭配使用,则系统会针对模型决定执行的每项搜索查询向您的项目收取费用。如果模型决定执行多个搜索查询来回答单个提示(例如,在同一次 API 调用中搜索 "UEFA Euro 2024 winner""Spain vs England Euro 2024 final score"),则该请求会被计为两次工具使用次数。出于结算目的,我们在统计唯一查询时会忽略空白的网页搜索查询。此结算模式仅适用于 Gemini 3 模型;如果您将搜索关联标准答案功能与 Gemini 2.5 或更旧的模型搭配使用,系统会按提示向您的项目收取费用。

如需详细了解价格信息,请参阅 Gemini API 价格页面

支持的模型

您可以在模型概览页面上找到完整的功能。

模型 使用 Google 搜索建立依据
Gemini 3.1 Flash Image 预览版 ✔️
Gemini 3 Pro 预览版 ✔️
Gemini 3 Pro Image 预览版 ✔️
Gemini 3 Flash 预览版 ✔️
Gemini 2.5 Pro ✔️
Gemini 2.5 Flash ✔️
Gemini 2.5 Flash-Lite ✔️
Gemini 2.0 Flash ✔️

支持的工具组合

您可以将“依托 Google 搜索进行接地”与其他工具(例如代码执行网址 上下文)搭配使用,以实现更复杂的用例。

Gemini 3 模型支持将内置工具(例如“使用 Google 搜索进行接地”)与自定义工具(函数调用)相结合。如需了解详情,请参阅工具组合页面。

后续步骤