在搜尋索引中設定向量化工具

重要

此功能在補充使用規定下處於公開預覽狀態。 2023-10-01-Preview REST API 支援此功能。

在 Azure AI 搜尋中, 向量化工具 是執行向量化的軟體,例如在 Azure OpenAI 上部署的內嵌模型,在查詢執行期間將文字轉換成向量。

它定義在搜尋索引,它會套用至可搜尋的向量字段,並在查詢時用來產生文字查詢輸入的內嵌。 如果您需要將文字向量化做為索引程式的一部分,請參閱 整合向量化 (預覽) 。 若要在編製索引期間進行內建向量化,您可以設定索引器和技能集,以針對原始文字內容呼叫 Azure OpenAI 內嵌模型。

若要將向量化程式新增至搜尋索引,您可以使用 Azure 入口網站 中的索引設計工具,或呼叫 Create or Update Index 2023-10-01-preview REST API,或使用任何更新的 Azure Beta SDK 套件來提供這項功能。

必要條件

建議您在搜尋服務上啟用診斷記錄,以確認向量查詢執行。

嘗試使用範例數據進行向量化

入和向量化數據精靈 會從 Azure Blob 記憶體讀取檔案、建立具有區塊化和向量化欄位的索引,以及新增向量化工具。 根據設計,精靈所建立的向量化程式會設定為用來編製 Blob 內容索引的相同內嵌模型。

  1. 將範例數據檔上傳至 Azure 儲存體 上的容器。 我們使用美國宇航局地球書籍中的一些小型文本檔,在免費的搜尋服務上測試這些指示。

  2. 執行匯 入和向量化數據精靈,選擇數據源的 Blob 容器。

    線上至數據頁面的螢幕快照。

  3. 選擇文字內嵌-ada-002 的現有部署。 此模型會在編製索引期間產生內嵌,也可用來設定查詢期間所使用的向量化程式。

    向量化和擴充數據頁面的螢幕快照。

  4. 精靈完成且所有索引器處理完成之後,您應該會有具有可搜尋向量字段的索引。 欄位的 JSON 定義如下所示:

     {
         "name": "vector",
         "type": "Collection(Edm.Single)",
         "searchable": true,
         "retrievable": true,
         "dimensions": 1536,
         "vectorSearchProfile": "vector-nasa-ebook-text-profile"
     }
    
  5. 您也應該有向量設定檔和向量化工具,類似下列範例:

    "profiles": [
       {
         "name": "vector-nasa-ebook-text-profile",
         "algorithm": "vector-nasa-ebook-text-algorithm",
         "vectorizer": "vector-nasa-ebook-text-vectorizer"
       }
     ],
     "vectorizers": [
       {
         "name": "vector-nasa-ebook-text-vectorizer",
         "kind": "azureOpenAI",
         "azureOpenAIParameters": {
           "resourceUri": "https://my-fake-azure-openai-resource.openai.azure.com",
           "deploymentId": "text-embedding-ada-002",
           "apiKey": "0000000000000000000000000000000000000",
           "authIdentity": null
         },
         "customWebApiParameters": null
       }
     ]
    
  6. 直接跳到 測試向量化工具 ,以在查詢執行期間進行文字到向量轉換。

定義向量化工具和向量配置檔

本節說明手動定義向量化工具之索引架構的修改。

  1. 使用 建立或更新索引 (預覽) 將 新增 vectorizers 至搜尋索引。

  2. 將下列 JSON 新增至您的索引定義。 vectorizers 區段會提供已部署內嵌模型的聯機資訊。 此步驟顯示兩個向量化工具範例,讓您可以比較 Azure OpenAI 內嵌模型和自定義 Web API 並排。

      "vectorizers": [
        {
          "name": "my_azure_open_ai_vectorizer",
          "kind": "azureOpenAI",
          "azureOpenAIParameters": {
            "resourceUri": "https://url.openai.azure.com",
            "deploymentId": "text-embedding-ada-002",
            "apiKey": "mytopsecretkey"
          }
        },
        {
          "name": "my_custom_vectorizer",
          "kind": "customWebApi",
          "customVectorizerParameters": {
            "uri": "https://my-endpoint",
            "authResourceId": " ",
            "authIdentity": " "
          }
        }
      ]
    
  3. 在相同的索引中,新增指定其中一個向量化工具的向量配置檔區段。 向量配置檔也需要 用來建立導覽結構的向量搜尋演算法

    "profiles": [ 
        { 
            "name": "my_vector_profile", 
            "algorithm": "my_hnsw_algorithm", 
            "vectorizer":"my_azure_open_ai_vectorizer" 
        }
    ]
    
  4. 將向量配置檔指派給向量欄位。 下列範例顯示欄位集合,其中包含必要的索引鍵欄位、標題字串欄位,以及具有向量配置檔指派的兩個向量欄位。

    "fields": [ 
            { 
                "name": "ID", 
                "type": "Edm.String", 
                "key": true, 
                "sortable": true, 
                "analyzer": "keyword" 
            }, 
            { 
                "name": "title", 
                "type": "Edm.String"
            }, 
            { 
                "name": "vector", 
                "type": "Collection(Edm.Single)", 
                "dimensions": 1536, 
                "vectorSearchProfile": "my_vector_profile", 
                "searchable": true, 
                "retrievable": true
            }, 
            { 
                "name": "my-second-vector", 
                "type": "Collection(Edm.Single)", 
                "dimensions": 1024, 
                "vectorSearchProfile": "my_vector_profile", 
                "searchable": true, 
                "retrievable": true
            }
    ]
    

測試向量化工具

使用搜尋用戶端透過向量化工具傳送查詢。 此範例假設 Visual Studio Code 搭配 REST 用戶端和 範例索引

  1. 在 Visual Studio Code 中,提供搜尋端點和 搜尋查詢 API 金鑰

     @baseUrl: 
     @queryApiKey: 00000000000000000000000
    
  2. 貼上 向量查詢要求。 請務必使用預覽 REST API 版本。

     ### Run a query
     POST {{baseUrl}}/indexes/vector-nasa-ebook-txt/docs/search?api-version=2023-10-01-preview  HTTP/1.1
         Content-Type: application/json
         api-key: {{queryApiKey}}
    
         {
             "count": true,
             "select": "title,chunk",
             "vectorQueries": [
                 {
                     "kind": "text",
                     "text": "what cloud formations exists in the troposphere",
                     "fields": "vector",
                     "k": 3,
                     "exhaustive": true
                 }
             ]
         }
    

    關於查詢的要點包括:

    • "kind": "text" 告知搜尋引擎輸入為文字字串,並使用與搜尋字段相關聯的向量化程式。

    • "text": "what cloud formations exists in the troposphere" 是要向量化的文字字串。

    • "fields": "vector" 是要查詢的功能變數名稱。 如果您使用精靈所產生的範例索引,產生的向量欄位會命名為 vector

  3. 傳送要求。 您應該會得到三 k 個結果,其中第一個結果最相關。

請注意,在查詢時間沒有可設定的向量化工具屬性。 查詢會根據索引中的向量配置檔欄位指派,讀取向量化工具屬性。

查看記錄

如果您已啟用搜尋服務的診斷記錄,請執行 Kusto 查詢以確認向量欄位上的查詢執行:

OperationEvent
| where TIMESTAMP > ago(30m)
| where Name == "Query.Search" and AdditionalInfo["QueryMetadata"]["Vectors"] has "TextLength"

最佳作法

如果您要設定 Azure OpenAI 向量化工具,請考慮我們建議用於 Azure OpenAI 內嵌技能的相同 最佳做法

另請參閱