依托 Google 搜索进行接地可将 Gemini 模型与实时 Web 内容连接起来,该功能支持所有可用语言。这使 Gemini 能够提供更准确的回答,并引用其知识截点之外的可验证来源。
接地可帮助您构建能够执行以下操作的应用:
- 提高事实准确性:通过基于真实世界的信息生成回答,减少模型幻觉。
- 获取实时信息:回答有关近期事件和主题的问题。
提供引用:通过显示模型声明的来源来建立用户信任。
Python
from google import genai
client = genai.Client()
interaction = client.interactions.create(
model="gemini-3.8-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.8-flash",
input: "Who won the euro 2024?",
tools: [{ type: "google_search" }]
});
console.log(interaction.output_text);
Java
import com.google.genai.Client;
import com.google.genai.gaos.models.interactions.CreateModelInteraction;
import com.google.genai.gaos.models.interactions.GoogleSearch;
import com.google.genai.gaos.models.interactions.Interaction;
import com.google.genai.gaos.models.interactions.InteractionsInput;
import com.google.genai.gaos.models.interactions.Model;
import com.google.genai.gaos.models.operations.CreateInteractionRequestBody;
import java.util.Arrays;
Client client = new Client();
CreateModelInteraction params =
CreateModelInteraction.builder()
.model(Model.of("gemini-3.8-flash"))
.input(InteractionsInput.of("Who won the euro 2024?"))
.tools(Arrays.asList(GoogleSearch.builder().build()))
.build();
Interaction interaction =
client.interactions.create(CreateInteractionRequestBody.of(params)).interaction().get();
System.out.println(interaction.outputText().orElse(""));
Go
package main
import (
"context"
"fmt"
"log"
"google.golang.org/genai"
"google.golang.org/genai/interactions/models/interactions"
"google.golang.org/genai/interactions/models/operations"
)
func main() {
ctx := context.Background()
client, err := genai.NewClient(ctx, nil)
if err != nil {
log.Fatal(err)
}
resp, err := client.Interactions.Create(ctx, operations.CreateInteractionRequest{
Body: operations.NewCreateInteractionRequestBody(
interactions.CreateModelInteraction{
Model: interactions.Model("gemini-3.8-flash"),
Input: interactions.NewInteractionsInput("Who won the euro 2024?"),
Tools: []interactions.Tool{
interactions.NewTool(interactions.GoogleSearch{}),
},
},
),
})
if err != nil {
log.Fatal(err)
}
fmt.Println(resp.Interaction.GetOutputText())
}
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.8-flash",
"input": "Who won the euro 2024?",
"tools": [{"type": "google_search"}]
}'
“依托 Google 搜索进行接地”功能的运作方式
启用 google_search 工具后,模型会自动处理搜索、处理和引用信息的整个工作流。

- 用户提示:您的应用将用户提示发送到 Gemini API,并启用
google_search工具。 - 提示分析:模型会分析提示,并确定 Google 搜索是否可以改进回答。
- Google 搜索:如果需要,模型会自动生成一个或多个搜索查询并执行这些查询。
- 搜索结果处理:模型处理搜索结果,整合信息并制定回答。
- 依据搜索结果的回答:API 会返回最终的、人性化的回答,该回答以搜索结果为依据。此回答包含模型的文本回答,其中包含引用内容的内嵌
annotations,以及包含搜索查询和搜索建议的google_search_call和google_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_index和end_index定义)链接到一个来源网址。这是构建内嵌引用的关键
依托 Google 搜索进行接地还可以与网址上下文工具结合使用,以便同时基于公开 Web 数据和您提供的特定网址来生成回答。
使用内嵌引用注明来源
该 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}\"")
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}"`);
}
}
}
}
}
}
}
Java
import com.google.genai.Client;
import com.google.genai.gaos.models.interactions.Annotation;
import com.google.genai.gaos.models.interactions.Content;
import com.google.genai.gaos.models.interactions.CreateModelInteraction;
import com.google.genai.gaos.models.interactions.GoogleSearch;
import com.google.genai.gaos.models.interactions.Interaction;
import com.google.genai.gaos.models.interactions.InteractionsInput;
import com.google.genai.gaos.models.interactions.Model;
import com.google.genai.gaos.models.interactions.ModelOutputStep;
import com.google.genai.gaos.models.interactions.Step;
import com.google.genai.gaos.models.interactions.TextContent;
import com.google.genai.gaos.models.interactions.URLCitation;
import com.google.genai.gaos.models.operations.CreateInteractionRequestBody;
import java.util.Arrays;
Client client = new Client();
CreateModelInteraction params =
CreateModelInteraction.builder()
.model(Model.of("gemini-3.8-flash"))
.input(InteractionsInput.of("Who won the euro 2024?"))
.tools(Arrays.asList(GoogleSearch.builder().build()))
.build();
Interaction interaction =
client.interactions.create(CreateInteractionRequestBody.of(params)).interaction().get();
if (interaction.steps().isPresent()) {
for (Step step : interaction.steps().get()) {
if (step instanceof ModelOutputStep) {
ModelOutputStep outputStep = (ModelOutputStep) step;
if (outputStep.content().isPresent()) {
for (Content contentBlock : outputStep.content().get()) {
if (contentBlock instanceof TextContent) {
TextContent textContent = (TextContent) contentBlock;
String text = textContent.text().orElse("");
System.out.println(text);
if (textContent.annotations().isPresent()
&& !textContent.annotations().get().isEmpty()) {
System.out.println("\nCitations:");
for (Annotation annotation : textContent.annotations().get()) {
if (annotation instanceof URLCitation) {
URLCitation citation = (URLCitation) annotation;
int start = citation.startIndex().orElse(0);
int end = citation.endIndex().orElse(0);
String citedText =
(start >= 0 && end <= text.length() && start <= end)
? text.substring(start, end)
: "";
System.out.printf(
" [%s](%s)%n", citation.title().orElse(""), citation.url().orElse(""));
System.out.printf(" Cited text: \"%s\"%n", citedText);
}
}
}
}
}
}
}
}
}
Go
package main
import (
"context"
"fmt"
"log"
"google.golang.org/genai"
"google.golang.org/genai/interactions/models/interactions"
"google.golang.org/genai/interactions/models/operations"
)
func main() {
ctx := context.Background()
client, err := genai.NewClient(ctx, nil)
if err != nil {
log.Fatal(err)
}
resp, err := client.Interactions.Create(ctx, operations.CreateInteractionRequest{
Body: operations.NewCreateInteractionRequestBody(
interactions.CreateModelInteraction{
Model: interactions.Model("gemini-3.8-flash"),
Input: interactions.NewInteractionsInput("Who won the euro 2024?"),
Tools: []interactions.Tool{
interactions.NewTool(interactions.GoogleSearch{}),
},
},
),
})
if err != nil {
log.Fatal(err)
}
for _, step := range resp.Interaction.Steps {
if step.ModelOutputStep != nil {
for _, content := range step.ModelOutputStep.Content {
if content.TextContent != nil {
text := content.TextContent.Text
fmt.Println(text)
if len(content.TextContent.Annotations) > 0 {
fmt.Println("\nCitations:")
for _, annotation := range content.TextContent.Annotations {
if annotation.URLCitation != nil {
c := annotation.URLCitation
start := 0
if c.StartIndex != nil {
start = *c.StartIndex
}
end := 0
if c.EndIndex != nil {
end = *c.EndIndex
}
citedText := ""
if start >= 0 && end <= len(text) && start <= end {
citedText = text[start:end]
}
title := ""
if c.Title != nil {
title = *c.Title
}
url := ""
if c.URL != nil {
url = *c.URL
}
fmt.Printf(" [%s](%s)\n", title, url)
fmt.Printf(" Cited text: %q\n", 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 及更新的模型搭配使用,则模型决定执行的每个搜索查询都会向您的项目计费。此按查询结算模式包含每月 5,000 个免费搜索请求,这些请求在所有 Gemini 3 及更新的模型之间共享,超出后按每 1,000 个搜索请求 14 美元计费。
例如,如果模型决定执行多个搜索查询来回答单个提示(例如,在同一 API 调用中搜索 "UEFA Euro 2024 winner" 和 "Spain vs
England Euro 2024 final score"),则该请求会计为两次可计费的工具使用。出于结算目的,我们在统计唯一查询时会忽略空的网页搜索查询。
如果您将搜索接地功能与 Gemini 2.5 或更低版本的模型搭配使用,则项目会按提示计费。
如需详细了解价格信息,请参阅 Gemini API 价格页面。
支持的模型
您可以在模型概览页面上找到完整功能。
| 模型 | 依托 Google 搜索进行接地 |
|---|---|
| Gemini 4 Argon | ✔️ |
| Gemini 3.8 Flash | ✔️ |
| Gemini 3.6 Flash | ✔️ |
| Gemini 3.5 Flash-Lite | ✔️ |
| 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 搜索进行接地与其他工具(例如代码执行、网址上下文和Grounding with Google Maps [Gemini 3.5 Flash-Lite 及更高版本的模型支持])结合使用,以支持更复杂的用例。Gemini 3 模型还支持将这些内置工具与自定义工具(函数调用)结合使用。如需了解详情,请参阅工具组合页面。