共用方式為


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

在 Azure AI 搜尋服務,向量化工具是執行向量化的軟體,例如在 Azure OpenAI 部署的內嵌模型,會在查詢執行期間將文字 (或影像) 轉換為向量。

向量化工具定義於搜尋索引、套用至可搜尋向量欄位,並在查詢時用來產生文字或影像查詢輸入的內嵌。 如果您需要的是在編製索引流程將內容向量化,請參閱整合向量化 (預覽版)。 若要在編製索引期間進行內建向量化,您可以設定索引子和技能集,為原始文字內容呼叫內嵌模型。

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

必要條件

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

以範例資料試用向量化工具

匯入和向量化資料精靈從 Azure Blob 記憶體讀取檔案、建立具有區塊化和向量化欄位的索引,以及新增向量化工具。 根據設計,精靈所建立的向量化工具,設定使用編製 Blob 內容索引的同一個內嵌模型。

  1. 將範例資料檔案上傳至 Microsoft Azure 儲存體上的容器。 我們使用NASA 衛星影像集的部分小型文字檔,在免費的搜尋服務測試這些指令。

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

    [連線到您的資料] 頁面的螢幕擷取畫面。

  3. 選擇 text-embedding-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",
           "modelName": "text-embedding-ada-002",
           "apiKey": "0000000000000000000000000000000000000",
           "authIdentity": null
         },
         "customWebApiParameters": null
       }
     ]
    
  6. 在查詢執行期間跳過,直接為文字到向量轉換測試向量化工具

定義向量化工具和向量設定檔

本節說明手動定義向量化工具之索引結構描述的修改內容。

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

  2. 將下列 JSON 新增至您的索引定義。 向量化工具區段提供已部署內嵌模型的連線資訊。 此步驟顯示兩個向量化工具範例,讓您可以並排比較 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",
            "modelName": "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 內嵌技能的相同最佳做法

另請參閱