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

與基礎搜尋模型相比,經過調整的搜尋模型可提供更高品質的結果。

如有產業專屬或公司專屬查詢,但一般 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 網際網路資產和權益的控股公司。2015 年 10 月 24 日,Sundar Pichai 獲任命為 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

關於測試

訓練完成後,我們會測試調整後的搜尋功能,判斷調整是否提升了搜尋結果品質。您可以明確提供要測試的查詢。如未提供測試查詢,Vertex AI Search 會將 20% 的訓練查詢做為測試查詢。

訓練用檔案

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

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

語料庫檔案

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

如果資料儲存庫中的文件少於 500 字,您可以將整份文件做為區隔。否則,請以程式輔助方式從資料儲存庫中的文件建立 250 至 500 字的隨機片段,然後將這些片段加入語料庫檔案。

語料庫檔案為 JSONL (JSON 行) 檔案,每行都有 _idtext 欄位,且值為字串。例如:

  {"_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-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

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

事前準備

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

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

控制台

如要使用 Google Cloud 控制台微調模型,請按照下列步驟操作:

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

  2. 將檔案上傳至 Cloud Storage

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

    AI 應用程式

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

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

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

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

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

  9. 按一下「開始調整」

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

REST

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

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

  2. 將檔案放入 Cloud Storage bucket。

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

詳情請參閱 AI Applications Python API 參考說明文件

如要向 AI Applications 進行驗證,請設定應用程式預設憑證。 詳情請參閱「為本機開發環境設定驗證」。


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 應用程式

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

    3. 按一下「Configurations」

    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

詳情請參閱 AI Applications Python API 參考說明文件

如要向 AI Applications 進行驗證,請設定應用程式預設憑證。 詳情請參閱「為本機開發環境設定驗證」。

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. 前往「微調」分頁:

    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

詳情請參閱 AI Applications Python API 參考說明文件

如要向 AI Applications 進行驗證,請設定應用程式預設憑證。 詳情請參閱「為本機開發環境設定驗證」。


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. 前往「微調」分頁:

    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

詳情請參閱 AI Applications Python API 參考說明文件

如要向 AI Applications 進行驗證,請設定應用程式預設憑證。 詳情請參閱「為本機開發環境設定驗證」。


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

後續步驟

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