検索チューニングで検索結果を改善する

チューニングされた検索モデルは、ベース検索モデルよりも高品質の結果を返すことができます。

検索のチューニングは、一般的な LLM では十分に処理できない業界固有または企業固有のクエリがある場合に特に有用です。検索モデルの追加トレーニングに使用できます。

制限事項

検索チューニングは、非構造化データストアにのみ適用できます。

トレーニング データについて

検索モデルをチューニングするには、まずトレーニング データをまとめます。

トレーニング データには、エンドユーザーが尋ねると予想されるクエリと、クエリに回答するために必要な関連情報を含む 250~500 語のテキスト スニペットを含める必要があります。 各スニペットにクエリの答えとなる情報が含まれている限り、1 つのクエリを複数のスニペットに関連付けることができます。

トレーニング データには、クエリとはペアになっていないものの、スタイルと長さが回答に似ているテキスト スニペットも含める必要があります。 関連するクエリのないこれらのスニペットは、モデルを調整するためのランダムなネガティブを提供します。少なくとも 10,000 個のスニペットを提供することをおすすめします。

以下に、用意する必要があるトレーニング データの用語を示します。

  • トレーニング クエリ: エンドユーザーが質問すると予想されるクエリ。特定のドメインや会社の用語を含むクエリに焦点を当ててください。

    少なくとも 100 個用意してください。

  • 抽出セグメント: スニペット(通常は複数の段落)は、データストア内のドキュメントからそのまま取得する必要があります。データストア内のすべてのドキュメントをまとめて「コーパス」と呼びます。

    次の 2 種類の抽出セグメントを指定する必要があります。

    • トレーニング クエリへの回答に必要な関連情報を含むセグメント。これらは、クエリと正確に一致するセグメントです。

    • トレーニング クエリに関連付けられていないセグメント。これらのセグメントは、モデル チューニングでランダムなネガティブとして使用されます。

    1 つまたは 2 つの文では、抽出セグメントとして十分な長さではありません。セグメントには、トレーニングに十分なコンテキストが含まれている必要があります。たとえば、「Google の創設者は誰ですか?」というクエリに対して、「ラリー・ペイジ」のような短い抜粋では不十分です。十分に長いセグメントの例については、次の例の表をご覧ください。

    クエリごとに少なくとも 1 つの抽出セグメントと、少なくとも 10,000 個の追加の抽出セグメントを指定します。

  • 関連性スコア: 関連性スコアは、抽出セグメントがクエリにどの程度関連しているかを推定する負ではない整数です。クエリと抽出セグメントのペアごとにスコア値を指定します。スコアが 0 の場合は、抽出されたセグメントがクエリとまったく関連していないことを意味します。スコアが 0 より大きい場合は、関連性があることを示します。簡単なスコアリングのために、関連性のあるセグメントにはすべて 1、関連性のないセグメントには 0 を割り当てることをおすすめします。代わりに、関連性をランク付けする場合は、関連性スコア(0~10 など)を割り当てることができます。最も関連性の高いセグメントには 10、関連性がまったくないセグメントには 0 を割り当てます。

    関連するスコアを 100 個以上指定します。関連しないスコアも指定できます。

クエリと抽出セグメントのペアの例

次の表に、クエリと抽出セグメントのペアの例を示します。これらの一般的な例は Wikipedia から引用したものです。ただし、効果的なチューニングを行うには、ビジネスに固有の情報が含まれており、ウェブ上で簡単には見つからない独自のデータセットのドキュメントを指定する必要があります。

この表の最後のペアは、回答がクエリに関連していないゼロスコアのペアの例です。

トレーニング クエリ 抽出セグメント スコア
Google の創設者は誰ですか? Google は、1998 年 9 月 4 日に、米国のコンピュータ サイエンティストであるラリー ペイジとセルゲイ ブリンによって、彼らがカリフォルニア州のスタンフォード大学の博士課程の学生だったときに設立されました。2 人は、公開株式の約 14% を保有し、優先株を通じて株主の投票権の 56% を支配しています。2004 年に新規株式公開(IPO)により株式を公開しました。2015 年に、Google は Alphabet Inc. の完全子会社として再編されました。Google は Alphabet の最大の子会社であり、Alphabet のインターネット関連の資産と権益を保有する持株会社です。2015 年 10 月 24 日、スンドラ ピチャイが Google の CEO に任命されました。ラリー ペイジは Alphabet の CEO に就任しました。2019 年 12 月 3 日、ピチャイは Alphabet の CEO にも就任しました。[...] 最も価値のあるブランドのリストでは、Google は Forbes で 2 位、Interbrand で 4 位にランクされています。 1
右心室を出た血液はどこに送られますか? 酸素化された血液は肺から肺静脈を通って心臓の左側に戻り、肺循環が完了します。この血液は左心房に入り、僧帽弁を通って左心室に送り込まれます。左心室から大動脈弁を通って大動脈に血液が送られます。血液は体循環を介して全身に運ばれ、再び肺循環に戻ります。動脈の主な記事: 右心室から肺動脈、血液は半月形の肺動脈弁を介して左肺動脈と右肺動脈(肺ごとに 1 つずつ)に送られます。これらの動脈は、肺全体に広がる小さな肺動脈に分岐します。[...] 心臓シャントは、心臓の一部間の不自然な接続であり、肺をバイパスする血流につながります。 1
ボウリングの殿堂はどこにありますか? 世界ボウリング ライター協会(WBW)国際ボウリング殿堂は 1993 年に設立され、テキサス州アーリントンの国際ボウリング キャンパスにある国際ボウリング博物館と殿堂にあります。歴史 国際ボウリング博物館と殿堂は、2008 年 11 月 8 日まで、米国ミズーリ州セントルイスの 11 Stadium Plaza にあり、セントルイス カージナルス殿堂博物館と同じ建物にありました。アーリントンに移転し、2010 年初頭に再オープンしました。2012 年に、WBW は国際ボウリング メディア協会と統合されました。統合後、WBW 殿堂入り選手は IBMA ルビー殿堂入り選手になりました。 殿堂の理事会を構成する世界ボウリング ライター協会の役員。][...] 最も多くの票を獲得した男女が選出されます。 1
空はなぜ青いのですか? 「Hello, World!」プログラムは、通常、ユーザー入力を無視しながら、「Hello, World!」のようなメッセージを画面(多くの場合、コンソール)に出力(または表示)するシンプルなコンピュータ プログラムです。ほとんどの汎用プログラミング言語の小さなコードで、言語の基本的な構文を示すために使用されます。「Hello, World!」プログラムは、新しいプログラミング言語を学ぶ学生が最初に作成することが多いプログラムですが、ソースコードをコンパイルまたは実行するコンピュータ ソフトウェアが正しくインストールされていること、およびそのオペレーターがソフトウェアの使用方法を理解していることを確認するチェックとしても使用できます。[...] C 言語版の前に、Kernighan 自身の 1972 年の「A Tutorial Introduction to the Language B」があり、そこで外部変数を説明する例として、プログラムの最初の既知のバージョンが示されています。 0

テストについて

トレーニング後、チューニングされた検索がテストされ、チューニングによって結果が改善されたかどうかが判断されます。テストするクエリを明示的に指定できます。テストクエリを指定しない場合、Vertex AI Search はトレーニング クエリの 20% をテストクエリとして使用します。

トレーニング ファイル

トレーニング データは、3 つ(必要に応じて 4 つ)の特定のファイルにアップロードする必要があります。

3 つのトレーニング ファイル(コーパス ファイル、クエリファイル、トレーニング ラベル ファイル)と(省略可)テストラベル ファイルは Cloud Storage に保存する必要があります。ファイルのパスは、trainCustomMethod 呼び出しのフィールドで定義されます。

コーパス ファイル

コーパス ファイルには、抽出セグメントが含まれています。抽出セグメントとは、クエリ ファイル内のクエリに回答するための情報を含むセグメントと、モデルをチューニングする際にランダムなネガティブとして使用される多くの追加セグメントのことです。クエリの回答を含むセグメントが 100 個以上必要です。クエリは複数のセグメントで回答できます。また、ランダム セグメントが 10,000 個以上必要です。

データストア内のドキュメントに含まれる単語数が 500 未満の場合は、ドキュメント全体をセグメントとして使用できます。それ以外の場合は、データストア内のドキュメントから 250~500 語のランダムなセグメントをプログラムで作成し、コーパス ファイルに追加します。

コーパス ファイルは JSONL(JSON Lines)ファイルです。各行には、文字列値を持つ _id フィールドと text フィールドがあります。例:

  {"_id": "doc1", "text": "Google was founded on September 4, 1998, by American computer scientists Larry Page and Sergey Brin while they were PhD students at Stanford University in California. Together they own about 14% of its publicly listed shares and control 56% of its stockholder voting power through super-voting stock. The company went public via an initial public offering (IPO) in 2004. In 2015, Google was reorganized as a wholly owned subsidiary of Alphabet Inc. Google is Alphabet's largest subsidiary and is a holding company for Alphabet's internet properties and interests. Sundar Pichai was appointed CEO of Google on October 24, 2015, replacing Larry Page, who became the CEO of Alphabet. On December 3, 2019, Pichai also became the CEO of Alphabet. [...] On the list of most valuable brands, Google is 105 ranked second by Forbes and fourth by Interbrand."}
  {"_id": "doc2", "text": "Oxygenated blood leaves the lungs through pulmonary veins, which return it to the left part of the heart, completing the pulmonary cycle. This blood then enters the left atrium, which pumps it through the mitral valve into the left ventricle. From the left ventricle, the blood passes through the aortic valve to the aorta. The blood is then distributed to the body through the systemic circulation before returning again to the pulmonary circulation. Arteries Main article: Pulmonary artery From the right ventricle, blood is pumped through the semilunar pulmonary valve into the left and right main pulmonary artery (one for each lung), which branch into smaller pulmonary arteries that spread throughout the lungs. [...] Cardiac shunt is an unnatural connection between parts of the heart that leads to blood flow that bypasses the lungs."}
  {"_id": "doc3", "text": "The World Bowling Writers ( WBW ) International Bowling Hall of Fame was established in 1993 and is located in the International Bowling Museum and Hall of Fame , on the International Bowling Campus in Arlington , Texas. History The International Bowling Museum and Hall of Fame was located at 11 Stadium Plaza, St. Louis, Missouri, USA, and shared the same building with the St. Louis Cardinals Hall of Fame Museum, until November 8, 2008. It moved to Arlington and reopened in early 2010. In 2012, the WBW was merged with the International Bowling Media Association. After the merger, the WBW Hall of Fame inductees became part of the IBMA Luby Hall of Fame.  officers of the World Bowling Writers, which formed the Hall's Board.][...] The man and woman who receive the most votes are elected."}
  {"_id": "doc4", "text": "A \"Hello, World!\" program is generally a simple computer program which outputs (or displays) to the screen (often the console) a message similar to "Hello, World!" while ignoring any user input. A small piece of code in most general-purpose programming languages, this program is used to illustrate a language's basic syntax. A "Hello, World!" program is often the first written by a student of a new programming language, but such a program can also be used as a check to ensure that the computer software intended to compile or run source code is correctly installed, and that its operator understands how to use it.  [...] The C-language version was preceded by Kernighan's own 1972 A Tutorial Introduction to the Language B, where the first known version of the program is found in an example used to illustrate external variables."}

ファイルの最大サイズは 500,000 行です。

クエリファイル

クエリファイルには、モデルのチューニングに使用されるサンプルクエリが含まれています。各クエリには、コーパス ファイルに 1 つ以上の対応する抽出セグメントが必要です。少なくとも 100 件のポジティブ一致クエリを指定する必要があります。関連性のないクエリを指定することもできます。これは、関連性スコアが 0 の抽出セグメントに対応するクエリです。

クエリファイルは JSONL 形式であり、コーパス ファイルと同じフィールドを持ちます。

例:

  {"_id": "query1", "text": "who founded Google?"}
  {"_id": "query2", "text": "where is blood pumped after it leaves the right ventricle?"}
  {"_id": "query3", "text": "where is the bowling hall of fame located?"}
  {"_id": "query4", "text": "why is the sky blue?"}

ファイルで許可されるクエリの最大数は 40,000 です。

トレーニング ラベル

トレーニング ラベル ファイルは、クエリと抽出セグメントを接続し、各クエリとセグメントのペアをスコア付けします。

テストラベル ファイルが存在しない場合、トレーニング ラベル ファイル内のクエリの 20% が、トレーニング後のチューニング済みモデルの評価用に予約されます。

このファイルには、クエリの ID、一致する(または一致しない)抽出セグメントの ID、クエリに対するセグメントの関連性のスコアが含まれています。クエリごとに少なくとも 1 つの行が必要です。クエリが 2 つのセグメントで回答される場合は、そのクエリに 2 つの行があります。Score は正の整数値です。スコアが 0 より大きい場合は、ドキュメントがクエリに関連していることを示します。数値が大きいほど、関連性が高いことを表します。スコアが省略されている場合、デフォルト値は 1 です。

トレーニング ラベル ファイルは、ヘッダー付きの TSV(タブ区切り値)ファイルです。 ファイルには query-idcorpus-idscore の各列が必要です。query-id はクエリファイルの _id キーと一致する文字列であり、corpus-id はコーパス ファイル内の _id と一致する文字列です。

例:

query-id    corpus-id   score
query1  doc1    1
query2  doc2    1
query3  doc3    1
query3  doc9    1
query4  doc4    0

トレーニング ラベル ファイルには、少なくとも 100 個の一意のクエリ ID を含める必要があります。トレーニング ラベルファイル内のクエリ ID の数とテスト ラベルファイル内のクエリの数を合計した数が 500,000 未満である必要があります。

テストラベル

トレーニング ラベル ファイルと同様に、この省略可能なファイルには、クエリと抽出セグメントの ID と関連性スコアが含まれています。トレーニング ラベル ファイルよりもクエリの数が少なく、内容も異なります。ファイルにクエリと抽出セグメントのペアが存在する場合、それらのペアを使用してチューニングを評価します。テストラベル ファイルが存在しない場合は、トレーニング ラベル ファイルのクエリと抽出セグメントのペアが評価に使用されます。

このファイルは、トレーニング ラベル ファイルと同じ形式です。

例:

query-id    corpus-id   score
query200    doc200  1
query201    doc201  1
query202    doc202  1

テストラベル ファイルは省略可能ですが、指定する場合は、少なくとも 3 つの一意のクエリ ID を含める必要があります。

始める前に

アプリの Enterprise エディションの機能をオンにします。

独自のトレーニング データを使用して検索モデルをチューニングする手順は次のとおりです。

コンソール

Google Cloud コンソールを使用してモデルをチューニングする手順は次のとおりです。

  1. トレーニング データと、必要に応じてテストデータ ファイルを準備します。トレーニング ファイルで説明されている形式を使用します。

  2. Cloud Storage にファイルをアップロードします。

  3. Google Cloud コンソールで、[AI Applications] ページに移動します。

    AI Applications

  4. [アプリ] ページで、トレーニング済みモデルを作成するアプリの名前をクリックします。

  5. ナビゲーション メニューで [構成] をクリックします。

  6. [チューニング] タブをクリックします。

  7. [ベースモデルをチューニングする] をクリックします。

  8. 前の手順 1 と 2 で準備したコーパス、クエリ、トレーニング、テスト(省略可)の各ファイルを指定します。

  9. [チューニングを開始] をクリックします。

  10. ページを更新して、[チューニング] タブの [最近のチューニング アクティビティ] テーブルでステータスを確認します。

REST

trainCustomModel メソッドを使用してデータストアを調整する手順は次のとおりです。

  1. トレーニング データ(必要に応じてテストデータ)ファイルを用意します。トレーニング ファイルで説明されている形式を使用します。

  2. ファイルを Cloud Storage バケットに配置します。

  3. 次の curl コマンドを実行して、Cloud Storage バケットから Vertex AI Search にファイルをアップロードします。

    curl -X POST \
    -H "Authorization: Bearer $(gcloud auth print-access-token)" \
    -H "Content-Type: application/json" \
    "https://discoveryengine.googleapis.com/v1/projects/PROJECT_ID/locations/global/collections/default_collection/dataStores/DATA_STORE_ID:trainCustomModel" \
    -d '{
    "gcsTrainingInput": {
     "corpusDataPath": "CORPUS_JSONL_GCS_PATH",
     "queryDataPath": "QUERY_JSONL_GCS_PATH",
     "trainDataPath": "TRAIN_TSV_GCS_PATH",
     "testDataPath": "TEST_TSV_GCS_PATH"
    },
    "modelType": "search-tuning"
    }'
    

    次のように置き換えます。

    • PROJECT_ID: 実際の Google Cloud プロジェクト ID。

    • DATA_STORE_ID: チューニングするデータストアの ID。

    • CORPUS_JSONL_GCS_PATH: Cloud Storage 内のコーパス JSONL ファイルのパス(例: gs://my-bucket/corpus.jsonl)。

    • QUERY_JSONL_GCS_PATH: Cloud Storage 内のクエリ JSONL ファイルのパス(例: gs://my-bucket/query.jsonl)。

    • TRAIN_TSV_GCS_PATH: Cloud Storage 内のトレーニング ラベル TSV ファイルのパス(例: gs://my-bucket/train.tsv)。

    • TEST_TSV_GCS_PATH: テストラベルの TSV ファイルの Cloud Storage パスを指定する省略可能なフィールド(例: gs://my-bucket/test.tsv)。テストラベル ファイルがない場合は、testDataPath フィールドを削除するか、空のままにします。

    このメソッドの一般的な情報については、trainCustomModel をご覧ください。

    データファイルがアップロードされると、チューニングが自動的に開始されます。

    クリックして curl コマンドとレスポンスの例を表示します。

    curl -X POST
    -H "Authorization: Bearer $(gcloud auth print-access-token)"
    -H "Content-Type: application/json"
    "https://discoveryengine.googleapis.com/v1/projects/12345/locations/global/collections/default_collection/dataStores/my-data-store_4321:trainCustomModel"
    -d '{
    "dataStore":
    "projects/12345/locations/global/collections/default_collection/dataStores/my-data-store_4321",
    "gcsTrainingInput": {
    "corpusDataPath": "gs://my-bucket/corpus.jsonl",
    "queryDataPath": "gs://my-bucket/query.jsonl",
    "trainDataPath": "gs://my-bucket/train.tsv"
    },
    "modelType": "search-tuning"
    }
    {
      "name": "projects/12345/locations/global/collections/default_collection/dataStores/my-data-store_4321/operations/train-custom-model-6071430366161939774",
      "metadata": {
        "@type": "type.googleapis.com/google.cloud.discoveryengine.v1.TrainCustomModelMetadata"
      },
      "response": {
        "@type": "type.googleapis.com/google.cloud.discoveryengine.v1.TrainCustomModelResponse",
        "modelStatus": "in-progress"
      }
    }
  4. trainCustomModel メソッドから返された name 値をメモし、長時間実行オペレーションの詳細を取得するの手順に沿って、検索チューニング オペレーションが完了するタイミングを確認します。

Python

詳細については、Vertex AI Search Python API のリファレンス ドキュメントをご覧ください。

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


from google.api_core.client_options import ClientOptions
from google.api_core.operation import Operation
from google.cloud import discoveryengine

# TODO(developer): Uncomment these variables before running the sample.
# project_id = "YOUR_PROJECT_ID"
# location = "YOUR_LOCATION" # Values: "global"
# data_store_id = "YOUR_DATA_STORE_ID"
# corpus_data_path = "gs://my-bucket/corpus.jsonl"
# query_data_path = "gs://my-bucket/query.jsonl"
# train_data_path = "gs://my-bucket/train.tsv"
# test_data_path = "gs://my-bucket/test.tsv"


def train_custom_model_sample(
    project_id: str,
    location: str,
    data_store_id: str,
    corpus_data_path: str,
    query_data_path: str,
    train_data_path: str,
    test_data_path: str,
) -> Operation:
    #  For more information, refer to:
    # https://cloud.google.com/generative-ai-app-builder/docs/locations#specify_a_multi-region_for_your_data_store
    client_options = (
        ClientOptions(api_endpoint=f"{location}-discoveryengine.googleapis.com")
        if location != "global"
        else None
    )
    # Create a client
    client = discoveryengine.SearchTuningServiceClient(client_options=client_options)

    # The full resource name of the data store
    data_store = f"projects/{project_id}/locations/{location}/collections/default_collection/dataStores/{data_store_id}"

    # Make the request
    operation = client.train_custom_model(
        request=discoveryengine.TrainCustomModelRequest(
            gcs_training_input=discoveryengine.TrainCustomModelRequest.GcsTrainingInput(
                corpus_data_path=corpus_data_path,
                query_data_path=query_data_path,
                train_data_path=train_data_path,
                test_data_path=test_data_path,
            ),
            data_store=data_store,
            model_type="search-tuning",
        )
    )

    # Optional: Wait for training to complete
    # print(f"Waiting for operation to complete: {operation.operation.name}")
    # response = operation.result()

    # After the operation is complete,
    # get information from operation metadata
    # metadata = discoveryengine.TrainCustomModelMetadata(operation.metadata)

    # Handle the response
    # print(response)
    # print(metadata)
    print(operation)

    return operation

調整された検索をテストし、個々の検索クエリに使用する

チューニングが完了したら、チューニング済みモデルに対するクエリの結果と、ベースモデルに対する同じクエリの結果を比較して、テストできます。

コンソール

Google Cloud コンソールを使用してチューニング済みモデルの動作をプレビューする手順は次のとおりです。

  1. [チューニング] タブに移動します。

    1. Google Cloud コンソールで、[AI Applications] ページに移動します。

      AI Applications

    2. プレビューするアプリの名前をクリックします。

    3. [設定] をクリックします。

    4. [チューニング] タブをクリックします。

  2. [チューニング済みモデル] をクリックし、右側のプレビュー パネルを使用して、チューニング済みモデルを使用するクエリを作成します。

  3. [ベースモデル] をクリックし、右側のプレビュー パネルを使用して、元のモデルでクエリを作成します。

  4. 結果の品質を比較します。

REST

チューニングの効果を評価するには、enableSearchAdaptor フィールドを true に設定してから false に設定してクエリを実行し、結果を比較します。enableSearchAdaptor フィールドを true に設定すると、そのクエリにチューニングされた検索バージョンが使用されることを示します。

チューニング済みモデルを使用する検索クエリを作成するには:

  1. クエリ メソッド呼び出しで、customFineTuningSpec フィールドの enableSearchAdaptor フィールドを true に設定します。

    例:

    curl -X POST -H "Authorization: Bearer $(gcloud auth print-access-token)" \
    -H "Content-Type: application/json" \
    "https://discoveryengine.googleapis.com/v1/projects/PROJECT_ID/locations/global/collections/default_collection/engines/APP_ID/servingConfigs/default_search:search" \
    -d '{
    "query": "QUERY",
    "customFineTuningSpec": { "enableSearchAdaptor": true }
    }'
    

    次のように置き換えます。

    • PROJECT_ID: 実際の Google Cloud プロジェクト ID。
    • APP_ID: クエリを実行するアプリの ID。

検索クエリの詳細については、検索結果を取得するservingConfigs.search メソッドをご覧ください。

Python

詳細については、Vertex AI Search Python API のリファレンス ドキュメントをご覧ください。

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

from google.api_core.client_options import ClientOptions
from google.cloud import discoveryengine_v1 as discoveryengine

# TODO(developer): Uncomment these variables before running the sample.
# project_id = "YOUR_PROJECT_ID"
# location = "YOUR_LOCATION"          # Values: "global", "us", "eu"
# engine_id = "YOUR_APP_ID"
# search_query = "YOUR_SEARCH_QUERY"


def search_sample(
    project_id: str,
    location: str,
    engine_id: str,
    search_query: str,
) -> discoveryengine.services.search_service.pagers.SearchPager:
    #  For more information, refer to:
    # https://cloud.google.com/generative-ai-app-builder/docs/locations#specify_a_multi-region_for_your_data_store
    client_options = (
        ClientOptions(api_endpoint=f"{location}-discoveryengine.googleapis.com")
        if location != "global"
        else None
    )

    # Create a client
    client = discoveryengine.SearchServiceClient(client_options=client_options)

    # The full resource name of the search app serving config
    serving_config = f"projects/{project_id}/locations/{location}/collections/default_collection/engines/{engine_id}/servingConfigs/default_config"

    # Optional - only supported for unstructured data: Configuration options for search.
    # Refer to the `ContentSearchSpec` reference for all supported fields:
    # https://cloud.google.com/python/docs/reference/discoveryengine/latest/google.cloud.discoveryengine_v1.types.SearchRequest.ContentSearchSpec
    content_search_spec = discoveryengine.SearchRequest.ContentSearchSpec(
        # For information about snippets, refer to:
        # https://cloud.google.com/generative-ai-app-builder/docs/snippets
        snippet_spec=discoveryengine.SearchRequest.ContentSearchSpec.SnippetSpec(
            return_snippet=True
        ),
        # For information about search summaries, refer to:
        # https://cloud.google.com/generative-ai-app-builder/docs/get-search-summaries
        summary_spec=discoveryengine.SearchRequest.ContentSearchSpec.SummarySpec(
            summary_result_count=5,
            include_citations=True,
            ignore_adversarial_query=True,
            ignore_non_summary_seeking_query=True,
            model_prompt_spec=discoveryengine.SearchRequest.ContentSearchSpec.SummarySpec.ModelPromptSpec(
                preamble="YOUR_CUSTOM_PROMPT"
            ),
            model_spec=discoveryengine.SearchRequest.ContentSearchSpec.SummarySpec.ModelSpec(
                version="stable",
            ),
        ),
    )

    # Refer to the `SearchRequest` reference for all supported fields:
    # https://cloud.google.com/python/docs/reference/discoveryengine/latest/google.cloud.discoveryengine_v1.types.SearchRequest
    request = discoveryengine.SearchRequest(
        serving_config=serving_config,
        query=search_query,
        page_size=10,
        content_search_spec=content_search_spec,
        query_expansion_spec=discoveryengine.SearchRequest.QueryExpansionSpec(
            condition=discoveryengine.SearchRequest.QueryExpansionSpec.Condition.AUTO,
        ),
        spell_correction_spec=discoveryengine.SearchRequest.SpellCorrectionSpec(
            mode=discoveryengine.SearchRequest.SpellCorrectionSpec.Mode.AUTO
        ),
        # Optional: Use fine-tuned model for this request
        # custom_fine_tuning_spec=discoveryengine.CustomFineTuningSpec(
        #     enable_search_adaptor=True
        # ),
    )

    page_result = client.search(request)

    # Handle the response
    for response in page_result:
        print(response)

    return page_result

調整済み検索をオンにする

チューニングされた検索をテストし、すべての検索クエリで使用することに決めたら、デフォルトの検索モデルにすることができます。

Console

チューニングされたモデルをデフォルトのモデルにして、メインのプレビュー ページ、ウィジェット、API 呼び出しに適用する手順は次のとおりです。

  1. [チューニング] タブに移動します。

    1. Google Cloud コンソールで、[AI Applications] ページに移動します。

      AI Applications

    2. アプリの名前をクリックします。

    3. [設定] をクリックします。

    4. [チューニング] タブをクリックします。

  2. [チューニング済みモデル] をクリックします。

  3. [公開] をクリックします。

REST

チューニング済みモデルをデフォルト モデルとして設定すると、前の手順のように検索クエリで customFineTuningSpec フィールドを指定する必要はありません。

すべての検索クエリでデフォルトでチューニングされた検索バージョンを使用する手順は次のとおりです。

  1. チューニングされた検索をデフォルト モデルとして設定するには、次の curl コマンドを実行します。

    curl -X PATCH \
    -H "Authorization: Bearer $(gcloud auth print-access-token)" \
    -H "Content-Type: application/json" \
    -H "X-Goog-User-Project: PROJECT_ID" \
    "https://discoveryengine.googleapis.com/v1/projects/PROJECT_ID/locations/global/collections/default_collection/engines/APP_ID/servingConfigs/default_search?updateMask=customFineTuningSpec.enableSearchAdaptor" \
    -d '{
    "customFineTuningSpec": {
     "enableSearchAdaptor": true
    }
    }'
    

    このメソッドの一般的な情報については、servingConfigs.patch をご覧ください。

Python

詳細については、Vertex AI Search Python API のリファレンス ドキュメントをご覧ください。

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


from google.api_core.client_options import ClientOptions
from google.cloud import discoveryengine_v1alpha as discoveryengine

# TODO(developer): Uncomment these variables before running the sample.
# project_id = "YOUR_PROJECT_ID"
# location = "YOUR_LOCATION" # Values: "global"
# engine_id = "YOUR_DATA_STORE_ID"


def update_serving_config_sample(
    project_id: str,
    location: str,
    engine_id: str,
) -> discoveryengine.ServingConfig:
    #  For more information, refer to:
    # https://cloud.google.com/generative-ai-app-builder/docs/locations#specify_a_multi-region_for_your_data_store
    client_options = (
        ClientOptions(api_endpoint=f"{location}-discoveryengine.googleapis.com")
        if location != "global"
        else None
    )
    # Create a client
    client = discoveryengine.ServingConfigServiceClient(client_options=client_options)

    # The full resource name of the serving config
    serving_config_name = f"projects/{project_id}/locations/{location}/collections/default_collection/engines/{engine_id}/servingConfigs/default_search"

    update_mask = "customFineTuningSpec.enableSearchAdaptor"

    serving_config = client.update_serving_config(
        request=discoveryengine.UpdateServingConfigRequest(
            serving_config=discoveryengine.ServingConfig(
                name=serving_config_name,
                custom_fine_tuning_spec=discoveryengine.CustomFineTuningSpec(
                    enable_search_adaptor=True  # Switch to `False` to disable tuned model
                ),
            ),
            update_mask=update_mask,
        )
    )

    # Handle the response
    print(serving_config)

    return serving_config

調整済み検索をオフにする

チューニングされた検索の使用を停止する場合は(たとえば、チューニング後の検索結果がチューニング前と比べて改善されていない、または悪化している場合)、チューニングされた検索を無効にできます。

Console

ベースモデルをデフォルト モデルとして使用するように戻す手順は次のとおりです。

  1. [チューニング] タブに移動します。

    1. Google Cloud コンソールで、[AI Applications] ページに移動します。

      AI Applications

    2. アプリの名前をクリックします。

    3. [設定] をクリックします。

    4. [チューニング] タブをクリックします。

  2. [ベースモデル] をクリックします。

  3. [公開] をクリックします。

REST

チューニング済みモデルの使用を停止するには、前述の curl 呼び出しと同様の呼び出しを実行しますが、enableSearchAdaptorfalse に設定します。

  1. 次の curl コマンドを実行します。

    curl -X PATCH \
    -H "Authorization: Bearer $(gcloud auth print-access-token)" \
    -H "Content-Type: application/json" \
    -H "X-Goog-User-Project: PROJECT_ID" \
    "https://discoveryengine.googleapis.com/v1/projects/PROJECT_ID/locations/global/collections/default_collection/engines/APP_ID/servingConfigs/default_search?updateMask=customFineTuningSpec.enableSearchAdaptor" \
    -d '{
    "customFineTuningSpec": {
     "enableSearchAdaptor": false
    }
    }'
    

    このメソッドの一般的な情報については、servingConfigs.patch をご覧ください。

Python

詳細については、Vertex AI Search Python API のリファレンス ドキュメントをご覧ください。

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


from google.api_core.client_options import ClientOptions
from google.cloud import discoveryengine_v1alpha as discoveryengine

# TODO(developer): Uncomment these variables before running the sample.
# project_id = "YOUR_PROJECT_ID"
# location = "YOUR_LOCATION" # Values: "global"
# engine_id = "YOUR_DATA_STORE_ID"


def update_serving_config_sample(
    project_id: str,
    location: str,
    engine_id: str,
) -> discoveryengine.ServingConfig:
    #  For more information, refer to:
    # https://cloud.google.com/generative-ai-app-builder/docs/locations#specify_a_multi-region_for_your_data_store
    client_options = (
        ClientOptions(api_endpoint=f"{location}-discoveryengine.googleapis.com")
        if location != "global"
        else None
    )
    # Create a client
    client = discoveryengine.ServingConfigServiceClient(client_options=client_options)

    # The full resource name of the serving config
    serving_config_name = f"projects/{project_id}/locations/{location}/collections/default_collection/engines/{engine_id}/servingConfigs/default_search"

    update_mask = "customFineTuningSpec.enableSearchAdaptor"

    serving_config = client.update_serving_config(
        request=discoveryengine.UpdateServingConfigRequest(
            serving_config=discoveryengine.ServingConfig(
                name=serving_config_name,
                custom_fine_tuning_spec=discoveryengine.CustomFineTuningSpec(
                    enable_search_adaptor=True  # Switch to `False` to disable tuned model
                ),
            ),
            update_mask=update_mask,
        )
    )

    # Handle the response
    print(serving_config)

    return serving_config

次のステップ

  • 検索のチューニングが検索品質に与える影響を把握するために、検索品質を評価します。詳細については、検索品質を評価するをご覧ください。