Gemini 中文指南
获取 API 密钥 ↗

使用 Google 搜索提供事实依据

↓ 下载 Markdown

事实依据与 Google 搜索将 Gemini 模型连接到实时 Web 内容,并适用于所有可用语言。这使得 Gemini 能够提供更准确的答案,并引用超出其知识范围的可验证来源。

事实依据帮助您构建能够:

  • 提高事实准确性: 通过基于现实世界信息的响应来减少模型幻觉。

  • 获取实时信息: 回答有关最近事件和主题的问题。

  • 提供引用: 通过显示模型声明的来源来建立用户信任。

python
from google import genai

client = genai.Client()

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

print(interaction.output_text)

事实依据与 Google 搜索的工作原理 #

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

  1. 用户提示: 您的应用程序将用户提示发送到 Gemini API,其中包含 google_search 工具已启用。

  2. 提示分析: 该模型分析提示并确定 Google 搜索是否可以改进答案。

  3. Google 搜索: 如果需要,该模型会自动生成一个或多个搜索查询并执行它们。

  4. 搜索结果处理: 该模型处理搜索结果、综合信息并制定响应。

  5. 提供事实依据响应: API 返回基于搜索结果的最终用户友好响应。此响应包括模型的内联文本答案 annotations 包含引文,以及 google_search_call 和 google_search_result 搜索查询和搜索建议的步骤。

了解事实依据响应 #

当响应成功提供事实依据时,模型的文本输出包括内联 annotations 直接在文本内容块上。这些注释提供了将部分响应与其来源链接起来的引用信息。

json
{
  "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,用于在 UI 中呈现搜索建议的 HTML 片段。完整的使用要求详见 服务条款.

  • text 使用 annotations :模型的综合答案以及内嵌引用。每个 url_citation 注释链接文本段(定义为 start_index 和 end_index) 到源 URL。这是建立内联引用的关键。

事实依据与 Google 搜索还可以与 URL上下文工具 公共网络数据和您提供的特定 URL 中的地面响应。

通过内嵌引用来确定来源 #

API 内联返回 url_citation 文本内容块上的注释,使您可以完全控制如何在用户界面中显示源。每个注释包括 start_index 和 end_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}\"")

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

代码
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 及更新型号一起使用时,您的项目将按 每个 模型决定执行的搜索查询。这种按查询计费模型包括所有 Gemini 3 和更新型号之间共享的每月 5,000 免费搜索请求,然后是成本 $14 每个 1,000 搜索请求.

例如,如果模型决定执行多个搜索查询来回答单个提示(例如搜索 "UEFA Euro 2024 winner" 和 "Spain vs England Euro 2024 final score" 在同一个 API 调用中),这算作该请求对该工具的两次计费使用。出于计费目的,我们在计算唯一查询时忽略空的网络搜索查询。

当您将搜索事实依据与 Gemini 2.5 或旧型号一起使用时,您的项目将按提示计费。

有关详细定价信息,请参阅 Gemini API 定价页面.

支持的模型 #

您可以在以下位置找到完整功能 型号概览 页。

模型使用 Google 搜索提供事实依据
Gemini 4 Carbon✔️
Gemini 4 Argon✔️
Gemini 4 Hydrogen✔️
Gemini 3.8 Flash✔️
Gemini 3.6 Flash✔️
Gemini 3.5 Flash-Lite✔️
Gemini 3.1 Flash Image Preview✔️
Gemini 3.1 Pro Preview✔️
Gemini 3 Pro Image Preview✔️
Gemini 3 Flash Preview✔️
Gemini 2.5 Pro✔️
Gemini 2.5 Flash✔️
Gemini 2.5 Flash-Lite✔️
Gemini 2.0 Flash✔️

支持的工具组合 #

您可以将事实依据与 Google 搜索以及其他工具一起使用,例如 代码执行, URL 上下文, 和 使用 Google 地图提供事实依据 (在 Gemini 3.5 Flash 及更高版本上受支持)为更复杂的用例提供支持。 Gemini 4 和 Gemini 3 型号还支持将这些内置工具与自定义工具 (函数调用) 相结合。了解更多信息 工具组合 页。

后续步骤 #

搜索标题、API 名称或正文内容