在搜索索引中配置矢量器

重要

根据补充使用条款,此功能以公共预览版提供。 2023-10-01-Preview REST API 支持此功能。

在 Azure AI 搜索中,矢量器是执行矢量化的软件,例如在 Azure OpenAI 上部署的嵌入模型,它用于在查询执行期间将文本转换为矢量。

它在搜索索引中定义,适用于可搜索矢量字段,并在查询时用来为文本查询输入生成嵌入。 如果需要将文本矢量化作为索引进程的一部分,请参阅集成矢量化(预览)。 对于索引编制期间的内置矢量化,可以配置一个索引器和技能集,用于为原始文本内容调用 Azure OpenAI 嵌入模型。

若要将矢量器添加到搜索索引,则可以在 Azure 门户中使用索引设计器,或者调用创建或更新索引 2023-10-01-preview REST API,或使用更新的任何 Azure beta SDK 包来提供此功能。

先决条件

建议在搜索服务上启用诊断日志记录,以确认矢量查询执行。

用示例数据试用矢量器

导入和矢量化数据向导会从 Azure Blob 存储读取文件,创建包含分块和矢量化字段的索引,并添加矢量器。 根据设计,向导创建的矢量器被设置为用于为 Blob 内容编制索引的同一嵌入模型。

  1. 将示例数据文件上传到 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",
           "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",
            "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 嵌入技能的最佳做法

另请参阅