Vertex AI Search 擴充功能

本文說明如何透過 Google Cloud 控制台和 Vertex AI API,註冊及使用 Google 提供的 Vertex AI Search 擴充功能。這項擴充功能可存取及搜尋網站語料庫和非結構化資料,針對自然語言問題提供相關回覆,例如:

  • 「與去年第 1 季相比,今年第 1 季的公司競爭威脅有何變化?」
  • 「公司哪些部門的成長速度最快?有多快?」

Vertex AI Search 擴充功能會使用 Vertex AI Search 從資料儲存庫擷取有意義的結果。Vertex AI Search 擴充功能是在 OpenAPI 規格 vertex_ai_search.yaml 檔案中定義。

如要使用 Vertex AI Search 擴充功能,您必須在全域區域建立資料儲存庫,並指定搜尋範圍。為獲得最佳搜尋結果,請為網站資料啟用進階索引功能,並為非結構化資料啟用 Enterprise 版。詳情請參閱「關於進階功能」。

openapi: "3.0.0"
info:
  title: Vertex AI Search
  version: v1alpha
  description: >
    Performs search on user ingested data including website and unstructured data type.

    This extension is used when user wants to search or retrieve meaningful results from their ingested data in the Vertex AI Search service.

    Supported AuthTypes:
    - GOOGLE_SERVICE_ACCOUNT_AUTH: (only supports using Vertex AI Extension Service Agent).
paths:
  /search:
    get:
      operationId: search
      description: Retrieves the results from user's query by searching in the data store.
      parameters:
      - name: query
        in: query
        schema:
          type: string
        description: User natural language instructions for search.
        required: true
      responses:
        default:
          description: Search execution result.
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/SearchResult"

components:
  schemas:
    SearchResult:
      description: Top results from search response.
      type: object
      properties:
        results:
          type: array
          items:
            type: object
            properties:
              title:
                type: string
                description: Retrieved document title.
              display_link:
                type: string
                description: Retrieved document link to display.
              link:
                type: string
                description: Retrieved document link.
              extractive_segments:
                type: array
                description: Extractive segments from the retrieved file.
                items:
                  type: string
              extractive_answers:
                type: array
                description: Extractive answers from the retrieved file. These are generated from the extractive segments.
                items:
                  type: string

    

如要透過端對端教學課程瞭解 Google 擴充功能,請參閱下列 Jupyter 筆記本:

事前準備

  1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  3. Make sure that billing is enabled for your Google Cloud project.

  4. Enable the Vertex AI API.

    Enable the API

  5. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  6. Make sure that billing is enabled for your Google Cloud project.

  7. Enable the Vertex AI API.

    Enable the API

  8. 註冊及執行 Vertex AI Search 擴充功能

    以下各節將說明如何使用 Google Cloud 控制台 和 Vertex AI API 註冊 Vertex AI Search 擴充功能。註冊擴充功能後,您可以使用 Vertex AI API 執行擴充功能。

    控制台

    註冊擴充功能

    請按照下列步驟,使用 Google Cloud 控制台註冊 Vertex AI Search 擴充功能。

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

      前往 Vertex AI Extensions

    2. 按一下「建立擴充功能」

    3. 在「Create a new extension」(建立新擴充功能) 對話方塊中,填入下列欄位:

      • 擴充功能名稱:輸入擴充功能的名稱,例如「vertex_search_extension」。
      • 說明: (選填) 輸入擴充功能說明,例如「Vertex AI 搜尋擴充功能」。
      • 擴充功能類型:選取 Vertex AI search
    4. 在隨即顯示的「OpenAPI Spec file」(OpenAPI 規格檔案) 部分中,確認下列欄位已正確設定:

      • API 名稱: vertex_ai_search
      • API 說明: Performs search on user ingested data including website and unstructured data type...
      • 來源: Cloud Storage
      • OpenAPI 規格: vertex-extension-public/vertex_ai_search.yaml
      • 驗證: Google service account
    5. 在「執行階段設定」部分中,提供供應設定名稱。供應設定名稱是在 vertexAiSearchRuntimeConfig 中指定。格式和填寫方式如下: projects/PROJECT_ID/locations/global/collections/COLLECTION_NAME/engines/ENGINE/servingConfigs/SERVING_CONFIG

      • COLLECTION_NAME 設為 default_collection
      • ENGINE 設為您建立搜尋應用程式時收到的應用程式 ID。詳情請參閱「使用網站資料建立搜尋應用程式」。
      • SERVING_CONFIG 設為 default_search
    6. 按一下「建立擴充功能」

    REST

    註冊擴充功能

    提交 Vertex AI API extensions.import 要求,註冊 Vertex AI Search 擴充功能。

    使用任何要求資料之前,請先替換以下項目:

    • PROJECT_ID:您的 Google Cloud 專案 ID。
    • REGIONCompute Engine 區域
    • DISPLAY_NAME:向使用者顯示的名稱擴充功能,例如「my_search_extension」。
    • DESCRIPTION:(選用) 向使用者顯示的擴充功能說明,例如「搜尋擴充功能」。
    • SERVICE_ACCOUNT:(選用) Vertex AI Search 擴充功能會使用 GOOGLE_SERVICE_ACCOUNT_AUTH,如範例要求內文所示。如未指定服務帳戶,擴充功能會使用預設的 Vertex AI Extension 服務代理服務帳戶。如果您指定其他服務帳戶,請在指定服務帳戶中,將 iam.serviceAccounts.getAccessToken 權限授予 Vertex AI Extension 服務代理服務帳戶。
    • SERVING_CONFIG_NAME:供應設定名稱是在 vertexAiSearchRuntimeConfig 中指定。格式如下: projects/PROJECT_ID/locations/global/collections/COLLECTION_NAME/engines/ENGINE/servingConfigs/SERVING_CONFIG
      • PROJECT_ID:您的 Google Cloud 專案 ID。
      • COLLECTION_NAME 設為 default_collection
      • ENGINE:建立搜尋應用程式時收到的應用程式 ID。詳情請參閱「使用網站資料建立搜尋應用程式」。
      • SERVING_CONFIG 設為 default_search

    HTTP 方法和網址:

    POST https://REGION-aiplatform.googleapis.com/v1beta1/projects/PROJECT_ID/locations/REGION/extensions:import

    JSON 要求主體:

    {
      "displayName":"DISPLAY_NAME",
      "description":"DESCRIPTION",
      "manifest":{
        "name":"vertex_ai_search_tool",
        "description":"A Vertex AI Search tool",
        "apiSpec":{
          "openApiGcsUri":"gs://vertex-extension-public/vertex_ai_search.yaml"
        },
        "authConfig":{
          "authType":"GOOGLE_SERVICE_ACCOUNT_AUTH",
          "googleServiceAccountConfig":{
            "serviceAccount":"SERVICE_ACCOUNT"
          }
        }
      }
      "runtimeConfig": {
         "vertexAiSearchRuntimeConfig": {
            "servingConfigName": "SERVING_CONFIG_NAME",
         }
      }
    }
    
    

    如要傳送要求,請選擇以下其中一個選項:

    curl

    將要求主體儲存在名為 request.json 的檔案中,然後執行下列指令:

    curl -X POST \
    -H "Authorization: Bearer $(gcloud auth print-access-token)" \
    -H "Content-Type: application/json; charset=utf-8" \
    -d @request.json \
    "https://REGION-aiplatform.googleapis.com/v1beta1/projects/PROJECT_ID/locations/REGION/extensions:import"

    PowerShell

    將要求主體儲存在名為 request.json 的檔案中,然後執行下列指令:

    $cred = gcloud auth print-access-token
    $headers = @{ "Authorization" = "Bearer $cred" }

    Invoke-WebRequest `
    -Method POST `
    -Headers $headers `
    -ContentType: "application/json; charset=utf-8" `
    -InFile request.json `
    -Uri "https://REGION-aiplatform.googleapis.com/v1beta1/projects/PROJECT_ID/locations/REGION/extensions:import" | Select-Object -Expand Content

    執行擴充功能

    您可以向 Vertex AI API 提交 execute 作業,從資料儲存庫取得有意義的結果。

    使用任何要求資料之前,請先替換以下項目:

    • PROJECT_ID:您的 Google Cloud 專案 ID。
    • REGIONCompute Engine 區域
    • EXTENSION_ID:Vertex AI Search 擴充功能的 ID,列於 Google Cloud 控制台的「擴充功能詳細資料」中。

    HTTP 方法和網址:

    POST https://REGION-aiplatform.googleapis.com/v1beta1/projects/PROJECT_ID/locations/REGION/extensions/EXTENSION_ID:execute

    JSON 要求主體:

    {
      "operation_id":"search",
      "operation_params":{
        "query":"Housing affordability since 2010",
      }
    }
    
    

    如要傳送要求,請選擇以下其中一個選項:

    curl

    將要求主體儲存在名為 request.json 的檔案中,然後執行下列指令:

    curl -X POST \
    -H "Authorization: Bearer $(gcloud auth print-access-token)" \
    -H "Content-Type: application/json; charset=utf-8" \
    -d @request.json \
    "https://REGION-aiplatform.googleapis.com/v1beta1/projects/PROJECT_ID/locations/REGION/extensions/EXTENSION_ID:execute"

    PowerShell

    將要求主體儲存在名為 request.json 的檔案中,然後執行下列指令:

    $cred = gcloud auth print-access-token
    $headers = @{ "Authorization" = "Bearer $cred" }

    Invoke-WebRequest `
    -Method POST `
    -Headers $headers `
    -ContentType: "application/json; charset=utf-8" `
    -InFile request.json `
    -Uri "https://REGION-aiplatform.googleapis.com/v1beta1/projects/PROJECT_ID/locations/REGION/extensions/EXTENSION_ID:execute" | Select-Object -Expand Content