使用 Google 搜索建立依据

依托 Google 搜索进行接地可将 Gemini 模型与实时 Web 内容连接起来,该功能支持所有可用语言。这让 Gemini 能够提供更准确的回答,并引用其知识截点之外的可验证来源。

接地功能可帮助您构建能够执行以下操作的应用:

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

Python

from google import genai

client = genai.Client()

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

print(interaction.output_text)

JavaScript

import { GoogleGenAI } from "@google/genai";

const client = new GoogleGenAI({});

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

console.log(interaction.output_text);

REST

curl -X POST "https://generativelanguage.googleapis.com/v1beta/interactions" \
  -H "x-goog-api-key: $GEMINI_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gemini-3.6-flash",
    "input": "Who won the euro 2024?",
    "tools": [{"type": "google_search"}]
  }'

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

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

grounding-overview

  1. 用户提示 :您的应用会向 Gemini API 发送用户提示,并启用 google_search 工具。
  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",