你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn。
在搜索索引中配置矢量器
重要
根据补充使用条款,此功能以公共预览版提供。 2023-10-01-Preview REST API 支持此功能。
在 Azure AI 搜索中,矢量器是执行矢量化的软件(例如在 Azure OpenAI 上部署的嵌入模型),用于在查询执行期间将文本转换为矢量。
它在搜索索引中定义,适用于可搜索的矢量字段,并在查询时用于为文本查询输入生成嵌入。 如果是需要在索引编制过程中将文本矢量化,请参阅集成矢量化(预览版)。 对于索引编制期间的内置矢量化,可以配置一个索引器和技能集,用于为原始文本内容调用 Azure OpenAI 嵌入模型。
若要向搜索索引添加矢量器,可以通过使用 Azure 门户中的索引设计器、调用创建或更新索引 2023-10-01-preview REST API 或使用更新的任何 Azure beta 版本 SDK 包来提供此功能。
先决条件
Azure AI 搜索上的具有可搜索矢量字段的索引。
Azure OpenAI 上部署的嵌入模型,例如 text-embedding-ada-002。 它用于将查询矢量化。 它必须与用于在索引中生成嵌入的模型相同。
使用嵌入模型的权限。 如果你使用 Azure OpenAI,则调用方必须具有认知服务 OpenAI 用户权限。 或者,你可以提供 API 密钥。
具有 REST 客户端的 Visual Studio Code,用于发送查询并接受响应。
建议在搜索服务上启用诊断日志记录,以确认矢量查询执行。
使用示例数据试用矢量器
“导入数据并将其矢量化”向导会读取 Azure Blob 存储中的文件,创建包含分块字段和矢量化字段的索引,并添加矢量器。 根据设计,该向导创建的矢量器将设置为采用用于为 Blob 内容编制索引的同一嵌入模型。
将示例数据文件上传到 Azure 存储上的容器。 我们使用了美国宇航局地球书中的一些小文本文件来在免费的搜索服务中测试这些说明。
运行“导入数据并将其矢量化”向导,选择数据源的 Blob 容器。
选择现有部署“text-embedding-ada-002”。 此模型将在索引编制过程中生成嵌入,并用于配置在查询期间使用的矢量器。
完成该向导并完成所有索引器处理后,应该会有一个具有可搜索矢量字段的索引。 该字段的 JSON 定义如下所示:
{ "name": "vector", "type": "Collection(Edm.Single)", "searchable": true, "retrievable": true, "dimensions": 1536, "vectorSearchProfile": "vector-nasa-ebook-text-profile" }
还应具有矢量配置文件和矢量器,类似于以下示例:
"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 } ]
跳转到测试矢量器部分,以测试查询执行期间的文本到矢量转换。
定义矢量器和矢量配置文件
本部分介绍如何修改索引架构以手动定义矢量器。
使用创建或更新索引(预览版)将
vectorizers
添加到搜索索引。将以下 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": " " } } ]
在同一索引中添加一个矢量配置文件部分(即“profiles”),用于指定其中的一个矢量器。 矢量配置文件还需要用于创建导航结构的矢量搜索算法。
"profiles": [ { "name": "my_vector_profile", "algorithm": "my_hnsw_algorithm", "vectorizer":"my_azure_open_ai_vectorizer" } ]
将矢量配置文件分配给矢量字段。 以下示例显示了一个“fields”集合,其中包含必需的键字段、一个标题字符串字段和两个分配了矢量配置文件的矢量字段。
"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 客户端和示例索引。
在 Visual Studio Code 中,提供搜索终结点和搜索查询 API 密钥:
@baseUrl: @queryApiKey: 00000000000000000000000
粘贴到矢量查询请求中。 请务必使用预览版 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
。
发送请求。 你应获得三个
k
结果,其中第一个结果相关度最高。
请注意,在查询时没有要设置的矢量器属性。 查询将根据索引中的矢量配置文件字段分配来读取矢量器属性。
检查日志
如果为搜索服务启用了诊断日志记录,请运行 Kusto 查询来确认矢量字段上的查询执行:
OperationEvent
| where TIMESTAMP > ago(30m)
| where Name == "Query.Search" and AdditionalInfo["QueryMetadata"]["Vectors"] has "TextLength"
最佳做法
如果要设置 Azure OpenAI 矢量器,请考虑我们针对 Azure OpenAI 嵌入技能推荐的相同最佳做法。