Grounding API

生成 AI において、グラウンディングとは、モデルの出力を検証可能な情報源に紐付ける仕組みを指します。特定のデータソースにアクセスできるモデルを用意することで、グラウンディングによりその出力が特定のデータに紐づけされ、コンテンツのねつ造が起こりにくくなります。

Vertex AI では、次の方法でモデル出力をグラウンディングできます。

  • Google 検索でグラウンディングする - 一般公開されているウェブデータでモデルをグラウンディングします。
  • Google マップでグラウンディングする - Google マップの地理空間データでモデルをグラウンディングします。
  • データにグラウンディングする - Vertex AI Search のデータをデータストアとしてモデルをグラウンディングします。

グラウンディングの詳細については、グラウンディングの概要をご覧ください。

サポートされているモデル

パラメータ リスト

実装の詳細については、をご覧ください。

googleSearch

Google 検索の一般公開されているウェブデータでレスポンスをグラウンディングします。

googleMaps

レスポンスを Google マップの一般公開されている地理空間データでグラウンディングします。

API 入力には次のパラメータが含まれます。

入力パラメータ

enable_widget

必須: boolean

true または false に設定できるフラグ。値 true は、Google マップ コンテキスト ウィジェットのユーザー インターフェースで使用できる API レスポンスを使用してトークンを返します。

API レスポンスの構造には次のパラメータが含まれます。

レスポンス パラメータ

grounding_metadata

必須: Object

グラウンディング情報を含むプライマリ フィールド。

  • grounding_support: グラウンディング サポートのレベルを示すサブフィールド。
  • grounding_chunks.maps: グラウンディングされたレスポンスの生成に使用された場所のソースを含むサブフィールド。
    • place_answer_sources.review_snippets: grounding_chunks.maps 内のサブフィールド。場所の回答がクエリの回答に使用されると表示されます。場所の回答では、ユーザーのクチコミなどのデータを使用して、特定の場所に関するより深いコンテキスト情報を提供します。場所の回答は、ユーザー レビューなどのソースのリストによって裏付けられています。

属性

場所またはユーザー レビューのソースには、次の属性があります。

属性

title

必須: Object

ソースのタイトル。

uri

必須: string

ソースにリンクする URI。

place_id

必須: string

場所の一意の識別子。

review_id

必須: string

レビューの一意の識別子。

retrieval

Vertex AI Search の限定公開データをデータストアとして使用してレスポンスをグラウンディングします。モデルが外部情報にアクセスするために呼び出せる検索ツールを定義します。

パラメータ

vertexAiSearch

必須: VertexAISearch

Vertex AI Search のデータソースでグラウンディングします。

VertexAISearch

パラメータ

datastore

必須: string

Vertex AI Search の完全修飾データストア リソース ID(形式: projects/{project}/locations/{location}/collections/default_collection/dataStores/{datastore}

このセクションでは、Google 検索を使用して一般公開されているウェブデータでレスポンスをグラウンディングする例と、Vertex AI Search を使用してプライベート データでレスポンスをグラウンディングする例を示します。

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
}

Java

Java をインストールまたは更新します。

詳しくは、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 com.google.genai.Client;
import com.google.genai.types.GenerateContentConfig;
import com.google.genai.types.GenerateContentResponse;
import com.google.genai.types.GoogleSearch;
import com.google.genai.types.HttpOptions;
import com.google.genai.types.Tool;

public class ToolsGoogleSearchWithText {

  public static void main(String[] args) {
    // TODO(developer): Replace these variables before running the sample.
    String modelId = "gemini-2.5-flash";
    generateContent(modelId);
  }

  // Generates text with Google Search tool
  public static String generateContent(String modelId) {
    // Initialize client that will be used to send requests. This client only needs to be created
    // once, and can be reused for multiple requests.
    try (Client client =
        Client.builder()
            .location("global")
            .vertexAI(true)
            .httpOptions(HttpOptions.builder().apiVersion("v1").build())
            .build()) {

      // Create a GenerateContentConfig and set Google Search tool
      GenerateContentConfig contentConfig =
          GenerateContentConfig.builder()
              .tools(Tool.builder().googleSearch(GoogleSearch.builder().build()).build())
              .build();

      GenerateContentResponse response =
          client.models.generateContent(
              modelId, "When is the next total solar eclipse in the United States?", contentConfig);

      System.out.print(response.text());
      // Example response:
      // The next total solar eclipse in the United States will occur on...
      return response.text();
    }
  }
}

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

次のステップ

詳細なドキュメントについては、以下をご覧ください。