Azure AI 搜索提供相关性优化策略,用于改进经典 RAG 解决方案中搜索结果的相关性。 相关性优化是交付符合用户期望的 RAG 解决方案的一个重要因素。
注释
现在建议对 RAG 工作流进行 代理检索 ,但经典 RAG 更简单。 如果它满足应用程序要求,它仍然是一个不错的选择。
在 Azure AI 搜索中,相关性优化包括 L2 语义排名和评分配置文件。 要实现这些功能,需要重新访问索引架构,为语义排序和计分概要文件添加配置。 然后使用新结构来重新运行查询。
在本教程中,你将修改现有的搜索索引和查询,以便使用:
- L2 语义排名
- 文档提升功能的评分配置文件
本教程更新索引管道创建的搜索索引。 更新不会影响现有内容,因此无需重新生成,也无需重新运行索引器。
先决条件
Azure AI 搜索、基本层或更高版本,用于托管标识和语义排名。
下载示例
示例笔记本包含更新的索引和查询请求。
更新语义排名和计分概要文件的索引
在之前的教程中,你为 RAG 工作负荷设计了一个索引架构。 我们特意省略了该架构中的相关性增强功能,以便你可以专注于基础知识。 将相关性视为一项独立的任务进行,这样可以在更新后对搜索结果的质量进行对比,分析前后的变化。
更新导入语句,以包括用于语义排名和评分配置文件的类。
from azure.identity import DefaultAzureCredential from azure.identity import get_bearer_token_provider from azure.search.documents.indexes import SearchIndexClient from azure.search.documents.indexes.models import ( SearchField, SearchFieldDataType, VectorSearch, HnswAlgorithmConfiguration, VectorSearchProfile, SearchIndex, SemanticConfiguration, SemanticPrioritizedFields, SemanticField, SemanticSearch, ScoringProfile, TagScoringFunction, TagScoringParameters )在搜索索引中添加以下语义配置。 此示例可在笔记本中的更新架构步骤中找到。
# New semantic configuration semantic_config = SemanticConfiguration( name="my-semantic-config", prioritized_fields=SemanticPrioritizedFields( title_field=SemanticField(field_name="title"), keywords_fields=[SemanticField(field_name="locations")], content_fields=[SemanticField(field_name="chunk")] ) ) # Create the semantic settings with the configuration semantic_search = SemanticSearch(configurations=[semantic_config])语义配置有一个名称和一个优先字段列表,有助于优化语义排序器的输入。 有关详细信息,请参阅配置语义排名。
接下来,添加评分配置定义。 与语义配置一样,计分概要文件可随时添加到索引架构中。 此示例也在笔记本的更新架构步骤中,紧随语义配置之后。
# New scoring profile scoring_profiles = [ ScoringProfile( name="my-scoring-profile", functions=[ TagScoringFunction( field_name="locations", boost=5.0, parameters=TagScoringParameters( tags_parameter="tags", ), ) ] ) ]此配置使用标签功能,能够提升在位置字段中找到匹配项的文档的评分。 回想一下,搜索索引有一个矢量字段和多个非矢量字段,分别代表标题、块和位置。 位置字段是一个字符串集合,而字符串集合可以使用计分概要文件中的标记函数进行增强。 有关详细信息,请参阅添加计分概要文件和通过文档提升增强搜索相关性(博客文章)。
更新搜索服务上的索引定义。
# Update the search index with the semantic configuration index = SearchIndex(name=index_name, fields=fields, vector_search=vector_search, semantic_search=semantic_search, scoring_profiles=scoring_profiles) result = index_client.create_or_update_index(index) print(f"{result.name} updated")