運用搜尋調整功能,提升搜尋結果品質

調整後的搜尋模型可提供比基礎搜尋模型更優質的結果。

如有產業或公司專屬查詢,一般 LLM 無法妥善處理,就特別適合使用搜尋調整功能。可用於進一步訓練搜尋模型。

限制

搜尋調整功能只能用於非結構化資料儲存庫。

關於訓練資料

如要調整搜尋模型,請先彙整訓練資料。

訓練資料應包含您預期使用者會提出的查詢,以及 250 到 500 字的文字片段,其中包含回答查詢所需的相關資訊。只要每個摘要都包含回答查詢的資訊,查詢就能與多個摘要建立關聯。

訓練資料也應包含未與查詢配對的文字片段,但其他方面與答案的樣式和長度相似。這些沒有相關聯查詢的程式碼片段會提供隨機負面樣本,用於調整模型。Google 建議您至少提供 10,000 個這類語音片段。

以下是您需要提供的訓練資料相關術語:

  • 訓練查詢:您預期使用者會提出的查詢。請務必著重於包含特定領域或公司術語的查詢。

    請至少提供 100 個。

  • 擷取片段:摘要 (通常為多個段落) 必須逐字取自資料儲存庫中的文件。資料儲存庫中的所有文件統稱為「語料庫」。

    您必須提供兩種擷取片段:

    • 包含回答訓練查詢所需相關資訊的片段。這些區隔與查詢有正向比對。

    • 未與任何訓練查詢建立關聯的區隔。這些片段會在模型調整中做為隨機負面樣本。

    一兩個句子不足以構成擷取片段,片段必須包含足夠的訓練背景資訊。舉例來說,如果使用者查詢「誰創立了 Google」,「Larry Page」這類簡短摘要就不夠完整。如需長度足夠的片段範例,請參閱下表。

    每個查詢至少提供一個擷取片段,並額外提供至少 10,000 個擷取片段。

  • 關聯分數:關聯分數是非負整數,用於預估擷取片段與查詢的關聯程度。您必須為每個查詢和擷取語句組合提供分數值。分數為 0 表示擷取片段與查詢完全無關。分數大於零表示有一定程度的關聯性。為快速評分,Google 建議將所有相關區隔設為 1,不相關區隔設為 0。或者,如要為關聯性排名,可以指派 0 到 10 的關聯性分數 (舉例來說),10 代表最相關的片段,0 代表完全不相關的片段。

    請提供至少 100 個相關分數,並視需要提供其他不相關分數。

查詢和擷取片段配對範例

下表提供一些查詢和擷取片段組合的範例。這些一般範例取自維基百科。不過,如要有效調整,建議提供專屬資料集的文件,其中包含貴公司特有的資訊,且難以在網路上找到。

這個表格中的最後一組是零分組的範例,其中答案與查詢無關。

訓練查詢 擷取片段 分數
Google 的創辦人是誰? Google 於 1998 年 9 月 4 日由美國電腦科學家 Larry Page 和 Sergey Brin 創立,當時他們是加州史丹佛大學的博士生。他們共同擁有約 14% 的公開上市股份,並透過超級投票股票控制 56% 的股東投票權。該公司於 2004 年首次公開募股 (IPO) 上市。2015 年,Google 改組為 Alphabet Inc. 的全資子公司。Google 是 Alphabet 最大的子公司,也是 Alphabet 網際網路資產和權益的控股公司。Sundar Pichai 於 2015 年 10 月 24 日獲任 Google 執行長,接替成為 Alphabet 執行長的 Larry Page。2019 年 12 月 3 日,Pichai 兼任 Alphabet 執行長。[...] 在最具價值品牌排行榜中,Google 獲《富比士》評為第二名,Interbrand 則評為第四名。 1
血液離開右心室後會流向何處? 含氧血透過肺靜脈離開肺部,回到心臟左側,完成肺循環。接著,血液會進入左心房,並經由二尖瓣泵入左心室。血液會從左心室穿過主動脈瓣,進入主動脈。接著,血液會透過體循環分布到全身,然後再次回到肺循環。動脈 主要文章:肺動脈 血液從右心室經半月肺動脈瓣泵入左、右主肺動脈(每個肺一個),分支成較小的肺動脈,遍佈整個肺部。[...] 心臟分流是指心臟各部分之間出現非自然連結,導致血流繞過肺部。 1
where is the bowling hall of fame located? 世界保齡球作家協會 ( WBW) 國際保齡球名人堂成立於 1993 年,位於德州阿靈頓國際保齡球園區的國際保齡球博物館和名人堂內。歷史 國際保齡球博物館和名人堂位於美國密蘇里州聖路易斯體育場廣場 11 號,與聖路易斯紅雀隊名人堂博物館共用同一棟建築,直到 2008 年 11 月 8 日。後來搬到阿靈頓,並於 2010 年初重新開幕。2012 年,WBW 與國際保齡球媒體協會 (International Bowling Media Association) 合併。合併後,WBW 名人堂入選者成為 IBMA Luby 名人堂的一部分。 世界保齡球作家協會的幹部,組成了名人堂的董事會。][...] 獲得最多票數的男女將當選。 1
為什麼天空是藍色的? 「Hello, World!」程式通常是簡短的電腦程式,會將類似「Hello, World!」的訊息輸出 (或顯示) 到螢幕 (通常是控制台),同時忽略任何使用者輸入內容。這是一段簡短的程式碼,在大多數一般用途程式設計語言中,這個程式用於說明語言的基本語法。「Hello, World!」程式通常是學生學習新程式語言時編寫的第一個程式,但這類程式也可用來檢查電腦軟體是否正確安裝,以便編譯或執行原始碼,並確保操作人員瞭解如何使用。[...] C 語言版本之前,Kernighan 曾在 1972 年發表《A Tutorial Introduction to the Language B》,其中第一個已知版本的程式,是用來示範外部變數的範例 0

關於測試

訓練完成後,系統會測試調整後的搜尋,判斷調整是否能改善結果。您可以明確提供要測試的查詢。如未提供測試查詢,代理搜尋會使用 20% 的訓練查詢做為測試查詢。

訓練用檔案

訓練資料必須上傳至三個 (或四個) 特定檔案:

三個訓練檔案 (語料庫檔案、查詢檔案和訓練標籤檔案) 和 (選用) 測試標籤檔案必須位於 Cloud Storage 中。檔案路徑由 trainCustomMethod 呼叫中的欄位定義。

語料庫檔案

語料庫檔案包含擷取片段:這些片段包含回答查詢檔案中查詢的資訊,以及許多額外片段,可在調整模型時做為隨機負面樣本。您至少應有 100 個包含查詢答案的區隔;查詢可由多個區隔回答。此外,您也應至少有 10,000 個隨機區隔。

如果資料儲存庫中的文件少於 500 個字詞,可以將整份文件視為一個區隔。否則,請透過程式輔助方式,從資料儲存庫中的文件建立 250 到 500 字的隨機片段,並將這些片段新增至語料庫檔案。

corpus 檔案是 JSONL (JSON 行) 檔案,每行都有 _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 行。

查詢檔案

查詢檔案包含用於調整模型的範例查詢。每個查詢在語料庫檔案中,都應有一或多個對應的擷取片段。請提供至少 100 個正面比對查詢。您也可以提供不相關的查詢,這些查詢對應的擷取片段關聯性分數為零。

查詢檔案為 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,以及片段與查詢的關聯性分數。每項查詢至少須有一行;如果查詢由兩個區隔回答,則該查詢會有兩行。Score 是非負整數值。只要分數大於零,就表示文件與查詢相關。數字越大,表示關聯性越高。如果省略分數,預設值為 1。

訓練標籤檔案是含有標頭的 TSV (以定位點分隔值) 檔案。檔案必須包含 query-id、corpus-id 和 score 資料欄。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

測試標籤檔案為選用,但如果提供,則必須包含至少三個不重複的查詢 ID。

事前準備

為應用程式啟用「Enterprise 版功能」。

如要使用自己的訓練資料調整搜尋模型,請按照下列步驟操作。

控制台

如要透過 Google Cloud 控制台調整模型,請按照下列步驟操作:

  1. 準備訓練資料,以及測試資料檔案 (選用)。請使用「訓練檔案」一節所述的格式。

  2. 將檔案上傳至 Cloud Storage。

  3. 前往 Google Cloud 控制台的「AI Applications」頁面。

    AI 應用程式

  4. 在「Apps」(應用程式) 頁面中,按一下要訓練模型的應用程式名稱。

  5. 在導覽選單中,按一下「Configurations」(設定)。

  6. 按一下「調整」分頁標籤。

  7. 按一下「調整基礎模型」。

  8. 指定您在上述步驟 1 和 2 中準備的語料庫、查詢、訓練和測試檔案 (選用)。

  9. 按一下「Start Tuning」(開始調整)。

  10. 重新整理頁面,即可在「調整」分頁的「近期調整活動」表格中查看狀態。

REST

如要使用 trainCustomModel 方法調整資料儲存庫,請按照下列步驟操作:

  1. 準備訓練資料 (以及測試資料,如有需要) 檔案。請使用「訓練檔案」一節所述的格式。

  2. 將檔案放入 Cloud Storage bucket。

  3. 執行下列 curl 指令,將 Cloud Storage bucket 中的檔案上傳至 Agent 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

詳情請參閱 Agent Search Python API 參考文件。

如要向 Agent 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. 前往「Tuning」(調整) 分頁:

    1. 前往 Google Cloud 控制台的「AI Applications」頁面。

      AI 應用程式

    2. 按一下要預覽的應用程式名稱。

    3. 按一下「Configurations」。

    4. 按一下「調整」分頁標籤。

  2. 按一下「Tuned model」(調整後的模型),然後使用右側的預覽面板,透過調整後的模型執行查詢。

  3. 按一下「Base model」(基礎模型),然後使用右側的預覽面板,透過原始模型執行查詢。

  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

詳情請參閱 Agent Search Python API 參考文件。

如要向 Agent 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

開啟調整後的搜尋功能

測試調整後的搜尋模型後,如果您決定將其用於所有搜尋查詢,可以將其設為預設搜尋模型。

控制台

如要將調整後的模型設為預設模型,並套用至「預覽」主頁面、小工具和 API 呼叫,請按照下列步驟操作:

  1. 前往「Tuning」(調整) 分頁:

    1. 前往 Google Cloud 控制台的「AI Applications」頁面。

      AI 應用程式

    2. 按一下應用程式名稱。

    3. 按一下「Configurations」。

    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

詳情請參閱 Agent Search Python API 參考文件。

如要向 Agent 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

關閉調整後的搜尋功能

如果不想再使用調整後的搜尋版本 (例如發現結果並未比調整前更好,甚至更差),可以停用調整後的搜尋功能。

控制台

如要改回使用基礎模型做為預設模型,請按照下列步驟操作:

  1. 前往「Tuning」(調整) 分頁:

    1. 前往 Google Cloud 控制台的「AI Applications」頁面。

      AI 應用程式

    2. 按一下應用程式名稱。

    3. 按一下「Configurations」。

    4. 按一下「調整」分頁標籤。

  2. 按一下「基礎模型」。

  3. 按一下「發布」。

REST

如要停止使用調整後的模型,請執行與上述類似的 curl 呼叫,但將 enableSearchAdaptor 設為 false:

  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

詳情請參閱 Agent Search Python API 參考文件。

如要向 Agent 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

後續步驟

  • 如要瞭解搜尋調整對搜尋品質的影響,請評估搜尋品質。詳情請參閱「評估搜尋品質」。