連結

在生成式 AI 領域,建立基準是指將模型輸出內容連結至可驗證的資訊來源。如果讓模型存取特定來源的資料,基準功能就會限制模型要使用這些資料來源生成輸出內容,減少捏造內容的機率。

您可以使用 Vertex AI,透過下列方式建立模型輸出內容的基準:

  • 以 Google 搜尋建立基準:以公開網路資料建立模型基準。
  • 以自有資料建立基準 - 使用 Vertex AI Search 做為資料儲存庫,以自有資料建立模型基準。

如要進一步瞭解基準建立功能,請參閱基準建立總覽

支援的模型

參數清單

如需實作詳情,請參閱範例

GoogleSearchRetrieval

根據公開資料生成回覆。

參數

google_search_retrieval

必要條件:Object

以公開的網路資料為基礎。

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 搜尋,以公開網路資料建立回覆基準

以 Google 搜尋公開資料為回覆內容建立基準。在要求中加入 google_search_retrieval 工具。不需要其他參數。

Python

安裝

pip install --upgrade google-genai

詳情請參閱 SDK 參考說明文件

設定環境變數,透過 Vertex AI 使用 Gen AI SDK:

# 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 參考說明文件

設定環境變數,透過 Vertex AI 使用 Gen AI SDK:

# 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 Applications」。

使用私人資料做為回覆基準前,請先建立資料儲存庫搜尋應用程式

警告:目前這個「基礎」介面不支援 Vertex AI Search「區塊模式」。

Python 適用的 Gen AI SDK

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...'

後續步驟

如需詳細說明文件,請參閱下列內容: