落地

在生成式 AI 中,建立依据是指将模型输出连接到可验证的信息源的能力。如果您为模型提供访问特定数据源的权限,则建立依据可以将其输出仅限于这些数据范围,从而降低内容创造的可能性。

使用 Vertex AI,您可以通过以下方式为模型输出建立依据:

  • 使用 Google 搜索建立依据 - 使用公开提供的 Web 数据为模型建立依据。
  • 使用您自己的数据建立依据 - 使用作为数据存储区的 Vertex AI Search 中您自己的数据为模型建立依据

如需详细了解依据,请参阅依据概览

支持的模型

参数列表

如需了解实现详情,请参阅示例

GoogleSearchRetrieval

以公开数据作为回答依据。

参数

google_search_retrieval

必需:Object

以公开提供的 Web 数据作为依据。

Retrieval

以作为数据存储区的 Vertex AI Search 中的私有数据作为回答依据。定义模型可以调用以访问外部知识的检索工具。

参数

source

必需:VertexAISearch

以 Vertex AI Search 数据源作为依据。

VertexAISearch

参数

datastore

必需:string

来自 Vertex AI Search 的完全限定数据存储区资源 ID,格式如下:projects/{project}/locations/{location}/collections/default_collection/dataStores/{datastore}

示例

使用 Google 搜索基于公开 Web 数据作为回答依据

以 Google 搜索公开数据作为回答依据。在请求中添加 google_search_retrieval 工具。无需其他参数。

Python

安装

pip install --upgrade google-genai

如需了解详情,请参阅 SDK 参考文档

设置环境变量以将 Gen AI SDK 与 Vertex AI 搭配使用:

# Replace the `GOOGLE_CLOUD_PROJECT` and `GOOGLE_CLOUD_LOCATION` values
# with appropriate values for your project.
export GOOGLE_CLOUD_PROJECT=GOOGLE_CLOUD_PROJECT
export GOOGLE_CLOUD_LOCATION=global
export GOOGLE_GENAI_USE_VERTEXAI=True

from google import genai
from google.genai.types import (
    GenerateContentConfig,
    GoogleSearch,
    HttpOptions,
    Tool,
)

client = genai.Client(http_options=HttpOptions(api_version="v1"))

response = client.models.generate_content(
    model="gemini-2.5-flash",
    contents="When is the next total solar eclipse in the United States?",
    config=GenerateContentConfig(
        tools=[
            # Use Google Search Tool
            Tool(google_search=GoogleSearch())
        ],
    ),
)

print(response.text)
# Example response:
# 'The next total solar eclipse in the United States will occur on ...'

Go

了解如何安装或更新 Go

如需了解详情,请参阅 SDK 参考文档

设置环境变量以将 Gen AI SDK 与 Vertex AI 搭配使用:

# Replace the `GOOGLE_CLOUD_PROJECT` and `GOOGLE_CLOUD_LOCATION` values
# with appropriate values for your project.
export GOOGLE_CLOUD_PROJECT=GOOGLE_CLOUD_PROJECT
export GOOGLE_CLOUD_LOCATION=global
export GOOGLE_GENAI_USE_VERTEXAI=True

import (
	"context"
	"fmt"
	"io"

	genai "google.golang.org/genai"
)

// generateWithGoogleSearch shows how to generate text using Google Search.
func generateWithGoogleSearch(w io.Writer) error {
	ctx := context.Background()

	client, err := genai.NewClient(ctx, &genai.ClientConfig{
		HTTPOptions: genai.HTTPOptions{APIVersion: "v1"},
	})
	if err != nil {
		return fmt.Errorf("failed to create genai client: %w", err)
	}

	modelName := "gemini-2.5-flash"
	contents := []*genai.Content{
		{Parts: []*genai.Part{
			{Text: "When is the next total solar eclipse in the United States?"},
		},
			Role: "user"},
	}
	config := &genai.GenerateContentConfig{
		Tools: []*genai.Tool{
			{GoogleSearch: &genai.GoogleSearch{}},
		},
	}

	resp, err := client.Models.GenerateContent(ctx, modelName, contents, config)
	if err != nil {
		return fmt.Errorf("failed to generate content: %w", err)
	}

	respText := resp.Text()

	fmt.Fprintln(w, respText)

	// Example response:
	// The next total solar eclipse in the United States will occur on March 30, 2033, but it will only ...

	return nil
}

使用 Vertex AI Search 基于私有数据作出回答

以 Vertex AI Search 数据存储区中的数据作为回答依据。如需了解详情,请参阅 AI 应用

在使用私有数据使回答接地之前,请创建数据存储区搜索应用

警告:目前,此“接地”接口不支持 Vertex AI Search“分块模式”。

Gen AI SDK for Python

from google import genai
from google.genai.types import (
    GenerateContentConfig,
    HttpOptions,
    Retrieval,
    Tool,
    VertexAISearch,
)

client = genai.Client(http_options=HttpOptions(api_version="v1"))

# Load Data Store ID from Vertex AI Search
# datastore = "projects/111111111111/locations/global/collections/default_collection/dataStores/data-store-id"

response = client.models.generate_content(
    model="gemini-2.5-flash",
    contents="How do I make an appointment to renew my driver's license?",
    config=GenerateContentConfig(
        tools=[
            # Use Vertex AI Search Tool
            Tool(
                retrieval=Retrieval(
                    vertex_ai_search=VertexAISearch(
                        datastore=datastore,
                    )
                )
            )
        ],
    ),
)

print(response.text)
# Example response:
# 'The process for making an appointment to renew your driver's license varies depending on your location. To provide you with the most accurate instructions...'

后续步骤

如需查看详细文档,请参阅以下内容: