以 Agent Search 建立基準

本頁說明如何使用 Agent Search 中的資料建立基準回覆。

以您的資料建立 Gemini 基準

如要執行檢索增強生成 (RAG),請將模型連結至網站資料或文件集,然後使用 Agent Search 建立基準。

以自有資料建立基準時,最多可使用 10 個 Agent Search 資料來源,並可與以 Google 搜尋建立基準功能搭配使用。

支援的模型

本節列出支援使用資料建立基準的模型。

按一下即可展開支援的模型

必要條件

您必須先完成下列步驟,才能使用自有資料,做為模型輸出內容的基準:

  1. 前往 Google Cloud 控制台的「IAM」頁面,搜尋 discoveryengine.servingConfigs.search 權限,這是建立基準服務所需的權限。

    前往「IAM」頁面

    如要取得使用 Agent Search 建立基準所需的權限,請要求管理員授予您下列 IAM 角色:

    • 如要讀取所有 Discovery Engine 資源:Discovery Engine 檢視者 (roles/discoveryengine.viewer)。

    • 如要讀取及寫入所有 Discovery Engine 資源,並建立 Agent Search 執行個體,請使用 Discovery Engine 編輯者 (roles/discoveryengine.editor)。

    如要進一步瞭解 IAM,請參閱「IAM 角色和權限」。

  2. 啟用 AI Applications 和 API。

  3. 建立 AI Applications 資料來源和應用程式。

    詳情請參閱「Agent Search 簡介」。

啟用 AI Applications

如要使用 Agent Search 建立回覆基準,請按照下列步驟啟用 Agent Search 搜尋服務:

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

    前往「AI Applications」(AI 應用程式)

  2. 選用:查看資料使用條款。

AI Applications 適用於 global 位置,或 eu 和 us 多區域。詳情請參閱「AI Applications 位置」。

在 AI Applications 建立資料儲存庫

如要在 AI Applications 建立資料儲存庫,可以選擇以網站資料或文件做為基準。

網站

  1. 從 Google Cloud 控制台開啟「Create Data Store」(建立資料儲存庫) 頁面。

  2. 在「網站內容」方塊中,按一下「選取」。
    系統會顯示「指定資料儲存庫的網站」窗格。

  3. 如果未勾選「Advanced website indexing」(進階網站索引建立功能),請勾選「Advanced website indexing」(進階網站索引建立功能) 核取方塊,啟用這項功能。
    系統會顯示「Configure your data store」(設定資料儲存庫) 窗格。

  4. 在「指定要建立索引的網址模式」部分,執行下列操作:

    • 在「要納入的網站」中新增網址。
    • 選用:在「要排除的網站」中新增網址。
  5. 按一下「繼續」。

  6. 在「Configure your data store」(設定資料儲存庫) 窗格中,

    1. 從「Location of your data store」(資料儲存庫位置) 清單中選取值。
    2. 在「Your data store name」(資料儲存庫名稱) 欄位中輸入名稱。系統會產生 ID。使用資料儲存庫生成基準回覆時,請使用這個 ID。詳情請參閱「使用資料儲存庫生成基準回覆」。
    3. 點選「建立」。

文件

  1. 從 Google Cloud 控制台開啟「Create Data Store」(建立資料儲存庫) 頁面。

  2. 在「Cloud Storage」方塊中,按一下「Select」(選取)。
    系統會顯示「Import data from Cloud Storage」(從 Cloud Storage 匯入資料) 窗格。

  3. 在「Unstructured documents (PDF, HTML, TXT and more)」(非結構化文件 (PDF、HTML、TXT 等)) 區段中,選取「Unstructured documents (PDF, HTML, TXT and more)」(非結構化文件 (PDF、HTML、TXT 等))。

  4. 選取「同步處理頻率」選項。

  5. 選取「Select a folder or a file you want to import」(選取要匯入的資料夾或檔案) 選項,然後在欄位中輸入路徑。

  6. 按一下「Continue」(繼續)。
    「Configure your data store」(設定資料儲存庫) 窗格隨即顯示。

  7. 在「設定資料儲存庫」窗格中,

    1. 從「Location of your data store」(資料儲存庫位置) 清單中選取值。
    2. 在「Your data store name」(資料儲存庫名稱) 欄位中輸入名稱。系統會產生 ID。
    3. 如要選取文件的剖析和分塊選項,請展開「Document Processing Options」(文件處理選項) 專區。如要進一步瞭解不同的剖析器,請參閱「剖析文件」。
    4. 點選「建立」。
  8. 點選「建立」。

使用資料儲存庫生成有憑有據的回覆

請按照下列操作說明,使用資料建立模型基準。最多支援 10 個資料儲存庫。

如果不知道資料儲存庫 ID,請按照下列步驟操作:

  1. 前往 Google Cloud 控制台的「AI Applications」頁面,然後點按導覽選單中的「Data stores」(資料儲存庫)。

    前往「Data Stores」(資料儲存庫) 頁面

  2. 點按資料儲存庫的名稱。

  3. 在資料儲存庫的「Data」(資料) 頁面,取得資料儲存庫 ID。

控制台

如要在Google Cloud 控制台使用 Agent Studio,將模型輸出內容建立在 AI 應用程式中,請按照下列步驟操作:

  1. 前往 Google Cloud 控制台的「Agent Studio」頁面。

    前往 Agent Studio

  2. 如要啟用基準建立功能,請按照下列步驟操作:
    1. 在側邊面板的「模型設定」下方,選取模型。
    2. 前往「建立基準」部分,然後開啟「您的資料」切換鈕。設定窗格隨即顯示。
      1. 從下表選取基準來源選項:
      2. 建立基準選項 說明 輸入內容
        RAG Engine 使用自有資料和 DIY 元件建立基準。 如果沒有語料庫,請先建立一個。如果不是,請輸入語料庫。
        Agent Search 使用 Google代管的搜尋引擎,以您的資料建立基準。 在「Gemini Enterprise Agent Platform datastore path」(Gemini Enterprise Agent Platform 資料儲存庫路徑) 欄位中輸入路徑。
        Elasticsearch 使用 Elasticsearch 建立基準。 輸入下列資訊:
        1. 在「Elasticsearch endpoint」(Elasticsearch 端點) 欄位中輸入值。
        2. 在「Elasticsearch API Key」欄位中輸入值。
        3. 在「Elasticsearch index」欄位中輸入值。
        4. 在「Elasticsearch search template」(Elasticsearch 搜尋範本) 欄位中輸入值。
      3. 按一下 [儲存]。
  3. 在文字方塊中輸入提示詞,然後點選「提交」。提示回覆是以 AI 應用程式為依據。

Python

安裝

pip install --upgrade google-genai

詳情請參閱 SDK 參考文件。

設定環境變數,以便透過 Vertex AI 使用 Google 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_ENTERPRISE=True

from google import genai
from google.genai.types import (
    GenerateContentConfig,
    VertexAISearch,
    Retrieval,
    Tool,
    HttpOptions,
)

client = genai.Client(http_options=HttpOptions(api_version="v1"))

# Replace with your Agent Search data store details
DATASTORE_PATH = "projects/PROJECT_ID/locations/global/collections/default_collection/dataStores/DATASTORE_ID"

tool = Tool(
    retrieval=Retrieval(
        vertex_ai_search=VertexAISearch(
            datastore=DATASTORE_PATH
        )
    )
)

response = client.models.generate_content(
    model="gemini-2.5-flash",  # Or another supported model
    contents="What information can you find about topic X in the provided documents?", # Your query
    config=GenerateContentConfig(
        tools=[tool],
    ),
)

print(response.text)

REST

如要使用 Agent Platform API 測試文字提示詞,請傳送 POST 要求至發布者模型端點。

使用任何要求資料之前,請先修改下列項目的值:

  • LOCATION:處理要求的區域。如要使用 global 端點,請從端點名稱中排除位置,並將資源位置設為 global。
  • PROJECT_ID:您的專案 ID。。
  • MODEL_ID:多模態模型的模型 ID。
  • PROMPT:要傳送至模型的提示。

HTTP 方法和網址:

POST https://LOCATION-aiplatform.googleapis.com/v1beta1/projects/PROJECT_ID/locations/LOCATION/publishers/google/models/MODEL_ID:generateContent

JSON 要求主體:

{
  "contents": [{
    "role": "user",
    "parts": [{
      "text": "PROMPT"
    }]
  }],
  "tools": [{
    "retrieval": {
      "vertexAiSearch": {
        "datastore": projects/PROJECT_ID/locations/global/collections/default_collection/dataStores/DATASTORE_ID
      }
    }
  }],
  "model": "projects/PROJECT_ID/locations/LOCATION/publishers/google/models/MODEL_ID"
}

請展開以下其中一個選項,以傳送要求:

您應該會收到如下的 JSON 回覆:

{
  "candidates": [
    {
      "content": {
        "role": "model",
        "parts": [
          {
            "text": "You can make an appointment on the website https://dmv.gov/"
          }
        ]
      },
      "finishReason": "STOP",
      "safetyRatings": [
        "..."
      ],
      "groundingMetadata": {
        "retrievalQueries": [
          "How to make appointment to renew driving license?"
        ],
        "groundingChunks": [
          {
            "retrievedContext": {
              "uri": "https://vertexaisearch.cloud.google.com/grounding-api-redirect/AXiHM.....QTN92V5ePQ==",
              "title": "dmv"
            }
          }
        ],
        "groundingSupport": [
          {
            "segment": {
              "startIndex": 25,
              "endIndex": 147
            },
            "segment_text": "ipsum lorem ...",
            "supportChunkIndices": [1, 2],
            "confidenceScore": [0.9541752, 0.97726375]
          },
          {
            "segment": {
              "startIndex": 294,
              "endIndex": 439
            },
            "segment_text": "ipsum lorem ...",
            "supportChunkIndices": [1],
            "confidenceScore": [0.9541752, 0.9325467]
          }
        ]
      }
    }
  ],
  "usageMetadata": {
    "..."
  }
}

瞭解回覆

這兩個 API 的回應都包含 LLM 生成的文字,稱為候選文字。如果模型提示成功建立基準,並連結至資料來源,回覆就會包含基準中繼資料,指出回覆中哪些部分來自您的資料。不過,有幾個原因可能導致系統無法提供這類中繼資料,提示回覆也無法建立基準。可能原因包括來源關聯性低,或模型回覆中的資訊不完整。

以下是輸出資料的細目:

  • 角色:指出基準答案的傳送者。由於回覆一律包含有根據的文字,因此角色一律為 model。
  • 文字:LLM 生成的有依據答案。
  • 建立基準中繼資料:建立基準來源的相關資訊,包含下列元素:
    • 建立基準區塊:索引中支援答案的結果清單。
    • 基準支援:答案中特定聲明的相關資訊,可用於顯示引用內容:
    • Segment:模型答案中,由基準資訊區塊證實的部分。
    • 基準資訊塊索引:基準資訊塊清單中,與這項聲明對應的基準資訊塊索引。
    • 可信度分數:介於 0 到 1 之間的數字,表示聲明在提供的基準資料區塊中的基準程度。不適用於 Gemini 2.5 以上版本。

後續步驟

  • 如要瞭解如何傳送對話提示詞要求,請參閱「多輪對話」。
  • 如要瞭解負責任 AI 最佳做法和 Agent Platform 的安全篩選器,請參閱「安全最佳做法」。