Vertex AI Search または Google 検索を使用してテキスト生成モデルからのレスポンスをグラウンディングする

このサンプルは、Vertex AI の Grounding API の使用方法を示しています。グラウンディングを行うと、ドキュメントや URL などの追加情報をモデルに提供できるため、生成される回答の精度と関連性を高めることができます。この例では、Vertex AI Search(独自のデータ)または Google 検索(インターネット上のウェブ検索)をグラウンディング ソースとして使用しています。グラウンディングされたテキスト レスポンスと引用が返されます。

もっと見る

このコードサンプルを含む詳細なドキュメントについては、以下をご覧ください。

コードサンプル

Python

このサンプルを試す前に、Vertex AI クイックスタート: クライアント ライブラリの使用にある Python の設定手順を完了してください。 詳細については、Vertex AI Python API のリファレンス ドキュメントをご覧ください。

Vertex AI に対する認証を行うには、アプリケーションのデフォルト認証情報を設定します。詳細については、ローカル開発環境の認証を設定するをご覧ください。

import vertexai

from vertexai.language_models import GroundingSource, TextGenerationModel

# TODO(developer): Update project_id and location
vertexai.init(project=PROJECT_ID, location="us-central1")

# TODO developer - override these parameters as needed:
parameters = {
    "temperature": 0.7,  # Temperature controls the degree of randomness in token selection.
    "max_output_tokens": 256,  # Token limit determines the maximum amount of text output.
    "top_p": 0.8,  # Tokens are selected from most probable to least until the sum of their probabilities equals the top_p value.
    "top_k": 40,  # A top_k of 1 means the selected token is the most probable among all tokens.
}

model = TextGenerationModel.from_pretrained("text-bison@002")

# TODO(developer): Update values for data_store_location, data_store_id
# data_store_id = ""
# data_store_location = ""
if data_store_id and data_store_location:
    # Use Vertex AI Search data store
    grounding_source = GroundingSource.VertexAISearch(
        data_store_id=data_store_id, location=data_store_location
    )
else:
    # Use Google Search for grounding (Private Preview)
    grounding_source = GroundingSource.WebSearch()

response = model.predict(
    "What are the price, available colors, and storage size options of a Pixel Tablet?",
    grounding_source=grounding_source,
    **parameters,
)
print(f"Response from Model: {response.text}")
print(f"Grounding Metadata: {response.grounding_metadata}")

次のステップ

他の Google Cloud プロダクトに関連するコードサンプルを検索およびフィルタするには、Google Cloud のサンプル ブラウザをご覧ください。