Milvus 视角看重排序模型(Rerankers)

在信息检索和生成式人工智能领域,重排序器是优化初始搜索结果顺序的重要工具。重排序器与传统的嵌入模型不同,它将查询和文档作为输入,并直接返回相似度得分,而不是嵌入。该得分表示输入查询和文档之间的相关性。

重排序器通常在第一阶段检索之后使用,通常通过向量近似最近邻 (ANN) 技术完成。虽然 ANN 搜索能够高效地获取大量潜在相关的结果,但它们并不总是根据结果与查询的实际语义接近程度来确定优先级。此时,重排序器会通过更深入的上下文分析来优化结果顺序,通常会利用 BERT 或其他基于 Transformer 的高级机器学习模型。通过这种方式,重排序器可以显著提高呈现给用户的最终结果的准确性和相关性。

PyMilvus 模型库集成了重排序功能,用于优化初始搜索返回结果的排序。从 Milvus 检索到最近的嵌入后,您可以利用这些重排序工具来优化搜索结果,从而提高搜索结果的准确率。

Rerank FunctionAPI or Open-sourced
BGEOpen-sourced
Cross EncoderOpen-sourced
VoyageAPI
CohereAPI
Jina AIAPI

示例 1:使用 BGE rerank 函数根据查询对文档进行重新排序

在此示例中,我们演示了如何使用基于特定查询的BGE 重新排序器对搜索结果进行重新排序。

要将重新排序器与PyMilvus 模型库一起使用,首先安装 PyMilvus 模型库以及包含所有必要的重新排序实用程序的模型子包:

pip install pymilvus[model]
# or pip install "pymilvus[model]" for zsh.

要使用 BGE 重新排序器,首先导入BGERerankFunction类:

from pymilvus.model.reranker import BGERerankFunction

然后,创建一个BGERerankFunction重新排名的实例:

bge_rf = BGERerankFunction(model_name="BAAI/bge-reranker-v2-m3",  # Specify the model name. Defaults to `BAAI/bge-reranker-v2-m3`.device="cpu" # Specify the device to use, e.g., 'cpu' or 'cuda:0'
)

要根据查询对文档重新排序,请使用以下代码:

query = "What event in 1956 marked the official birth of artificial intelligence as a discipline?"documents = ["In 1950, Alan Turing published his seminal paper, 'Computing Machinery and Intelligence,' proposing the Turing Test as a criterion of intelligence, a foundational concept in the philosophy and development of artificial intelligence.","The Dartmouth Conference in 1956 is considered the birthplace of artificial intelligence as a field; here, John McCarthy and others coined the term 'artificial intelligence' and laid out its basic goals.","In 1951, British mathematician and computer scientist Alan Turing also developed the first program designed to play chess, demonstrating an early example of AI in game strategy.","The invention of the Logic Theorist by Allen Newell, Herbert A. Simon, and Cliff Shaw in 1955 marked the creation of the first true AI program, which was capable of solving logic problems, akin to proving mathematical theorems."
]bge_rf(query, documents)

预期输出类似于以下内容:

[RerankResult(text="The Dartmouth Conference in 1956 is considered the birthplace of artificial intelligence as a field; here, John McCarthy and others coined the term 'artificial intelligence' and laid out its basic goals.", score=0.9911615761470803, index=1),RerankResult(text="In 1950, Alan Turing published his seminal paper, 'Computing Machinery and Intelligence,' proposing the Turing Test as a criterion of intelligence, a foundational concept in the philosophy and development of artificial intelligence.", score=0.0326971950177779, index=0),RerankResult(text='The invention of the Logic Theorist by Allen Newell, Herbert A. Simon, and Cliff Shaw in 1955 marked the creation of the first true AI program, which was capable of solving logic problems, akin to proving mathematical theorems.', score=0.006514905766152258, index=3),RerankResult(text='In 1951, British mathematician and computer scientist Alan Turing also developed the first program designed to play chess, demonstrating an early example of AI in game strategy.', score=0.0042116724917325935, index=2)]

示例 2:使用重新排序器增强搜索结果的相关性

在本指南中,我们将探索如何利用search()PyMilvus 中的方法进行相似性搜索,以及如何使用重排序器增强搜索结果的相关性。我们的演示将使用以下数据集:

entities = [{'doc_id': 0, 'doc_vector': [-0.0372721,0.0101959,...,-0.114994], 'doc_text': "In 1950, Alan Turing published his seminal paper, 'Computing Machinery and Intelligence,' proposing the Turing Test as a criterion of intelligence, a foundational concept in the philosophy and development of artificial intelligence."}, {'doc_id': 1, 'doc_vector': [-0.00308882,-0.0219905,...,-0.00795811], 'doc_text': "The Dartmouth Conference in 1956 is considered the birthplace of artificial intelligence as a field; here, John McCarthy and others coined the term 'artificial intelligence' and laid out its basic goals."}, {'doc_id': 2, 'doc_vector': [0.00945078,0.00397605,...,-0.0286199], 'doc_text': 'In 1951, British mathematician and computer scientist Alan Turing also developed the first program designed to play chess, demonstrating an early example of AI in game strategy.'}, {'doc_id': 3, 'doc_vector': [-0.0391119,-0.00880096,...,-0.0109257], 'doc_text': 'The invention of the Logic Theorist by Allen Newell, Herbert A. Simon, and Cliff Shaw in 1955 marked the creation of the first true AI program, which was capable of solving logic problems, akin to proving mathematical theorems.'}
]

数据集组件

  • doc_id:每个文档的唯一标识符。
  • doc_vector:表示文档的向量嵌入。有关生成嵌入的指导,请参阅嵌入。
  • doc_text:文档的文本内容。

准备工作

在启动相似性搜索之前,您需要与 Milvus 建立连接,创建一个集合,并准备数据并将其插入到该集合中。以下代码片段演示了这些准备步骤。

from pymilvus import MilvusClient, DataTypeclient = MilvusClient(uri="http://10.102.6.214:19530" # replace with your own Milvus server address
)client.drop_collection('test_collection')# define schemaschema = client.create_schema(auto_id=False, enabel_dynamic_field=True)schema.add_field(field_name="doc_id", datatype=DataType.INT64, is_primary=True, description="document id")
schema.add_field(field_name="doc_vector", datatype=DataType.FLOAT_VECTOR, dim=384, description="document vector")
schema.add_field(field_name="doc_text", datatype=DataType.VARCHAR, max_length=65535, description="document text")# define index paramsindex_params = client.prepare_index_params()index_params.add_index(field_name="doc_vector", index_type="IVF_FLAT", metric_type="IP", params={"nlist": 128})# create collectionclient.create_collection(collection_name="test_collection", schema=schema, index_params=index_params)# insert data into collectionclient.insert(collection_name="test_collection", data=entities)# Output:
# {'insert_count': 4, 'ids': [0, 1, 2, 3]}

数据插入后,使用该方法进行相似性搜索search

# search results based on our queryres = client.search(collection_name="test_collection",data=[[-0.045217834, 0.035171617, ..., -0.025117004]], # replace with your query vectorlimit=3,output_fields=["doc_id", "doc_text"]
)for i in res[0]:print(f'distance: {i["distance"]}')print(f'doc_text: {i["entity"]["doc_text"]}')

预期输出类似于以下内容:

distance: 0.7235960960388184
doc_text: The Dartmouth Conference in 1956 is considered the birthplace of artificial intelligence as a field; here, John McCarthy and others coined the term 'artificial intelligence' and laid out its basic goals.
distance: 0.6269873976707458
doc_text: In 1950, Alan Turing published his seminal paper, 'Computing Machinery and Intelligence,' proposing the Turing Test as a criterion of intelligence, a foundational concept in the philosophy and development of artificial intelligence.
distance: 0.5340118408203125
doc_text: The invention of the Logic Theorist by Allen Newell, Herbert A. Simon, and Cliff Shaw in 1955 marked the creation of the first true AI program, which was capable of solving logic problems, akin to proving mathematical theorems.

使用重新排序器来增强搜索结果

然后,通过重新排序步骤来提高搜索结果的相关性。在本例中,我们使用CrossEncoderRerankFunction内置的 PyMilvus 对结果进行重新排序,以提高准确率。

# use reranker to rerank search resultsfrom pymilvus.model.reranker import CrossEncoderRerankFunctionce_rf = CrossEncoderRerankFunction(model_name="cross-encoder/ms-marco-MiniLM-L-6-v2",  # Specify the model name.device="cpu" # Specify the device to use, e.g., 'cpu' or 'cuda:0'
)reranked_results = ce_rf(query='What event in 1956 marked the official birth of artificial intelligence as a discipline?',documents=["In 1950, Alan Turing published his seminal paper, 'Computing Machinery and Intelligence,' proposing the Turing Test as a criterion of intelligence, a foundational concept in the philosophy and development of artificial intelligence.","The Dartmouth Conference in 1956 is considered the birthplace of artificial intelligence as a field; here, John McCarthy and others coined the term 'artificial intelligence' and laid out its basic goals.","In 1951, British mathematician and computer scientist Alan Turing also developed the first program designed to play chess, demonstrating an early example of AI in game strategy.","The invention of the Logic Theorist by Allen Newell, Herbert A. Simon, and Cliff Shaw in 1955 marked the creation of the first true AI program, which was capable of solving logic problems, akin to proving mathematical theorems."],top_k=3
)# print the reranked results
for result in reranked_results:print(f'score: {result.score}')print(f'doc_text: {result.text}')

预期输出类似于以下内容:

score: 6.250532627105713
doc_text: The Dartmouth Conference in 1956 is considered the birthplace of artificial intelligence as a field; here, John McCarthy and others coined the term 'artificial intelligence' and laid out its basic goals.
score: -2.9546022415161133
doc_text: In 1950, Alan Turing published his seminal paper, 'Computing Machinery and Intelligence,' proposing the Turing Test as a criterion of intelligence, a foundational concept in the philosophy and development of artificial intelligence.
score: -4.771512031555176
doc_text: The invention of the Logic Theorist by Allen Newell, Herbert A. Simon, and Cliff Shaw in 1955 marked the creation of the first true AI program, which was capable of solving logic problems, akin to proving mathematical theorems.

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/pingmian/81345.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

C语言:gcc 如何调用 Win32 打开文件对话框 ?

在 Windows 平台上使用 gcc 调用原生 Win32 API 实现文件打开对话框是可行的,但需要直接使用 Win32 的 GetOpenFileName 函数(位于 commdlg.h 头文件,依赖 comdlg32.lib 库)。以下是完整实现步骤和代码示例: 编写 file…

计算机视觉与深度学习 | Python实现EMD-SSA-VMD-LSTM时间序列预测(完整源码和数据)

EMD-SSA-VMD-LSTM混合模型 一、环境配置与依赖二、数据生成(示例数据)三、多级信号分解1. 经验模态分解(EMD)2. 奇异谱分析(SSA)3. 变分模态分解(VMD) 四、数据预处理1. 归一化处理2…

vue配置子路由,实现点击左侧菜单,内容区域显示不同的内容

文章目录 一、路由链路二、实现步骤准备二级路由下的.vue文件配置子路由声明router-view标签为菜单项 el-menu-item 设置index属性,设置点击后的路由路径 三、参考资料 一、路由链路 二、实现步骤 准备二级路由下的.vue文件 配置子路由 router/index.js import {…

ModuleNotFoundError: No module named ‘SDToolbox‘

(py311) C:>python Python 3.11.11 | packaged by Anaconda, Inc. | (main, Dec 11 2024, 16:34:19) [MSC v.1929 64 bit (AMD64)] on win32 Type “help”, “copyright”, “credits” or “license” for more information. from SDToolbox import PostShock_eq Tracebac…

Hi3516DV500刷写固件

hi3516DV500刷固件 1、硬件连接 2、软件准备 3、刷固件步骤 一、硬件连接 特别注意的是,串口的接线顺序 通过网线连接好笔记本和开发板后,需要确认一下网口水晶头是否闪烁,以确认网络物理是否连通 二、软件资源准备 固件包准备 打开工具…

正则表达式r前缀使用指南

正则表达式中的 r:解锁字符串转义的魔法 正则表达式是处理字符串的强大工具,但它常常伴随着转义字符的复杂性。如果你曾因 \n、\t 或 \\ 的使用而困惑,那么这篇文章将为你揭开谜底,解释为什么 r 是正则表达式中的「神奇武器」。本…

网络攻防模拟:城市安全 “数字预演”

在当今数字化快速发展的时代,网络安全和城市安全面临着前所未有的挑战。为有效应对这些挑战,利用先进的技术搭建模拟演练平台至关重要。图扑软件的 HT for Web 技术,为网络攻防模拟与城市安全演练提供了全面且高效的解决方案。 三维场景搭建&…

AI模型开发全流程笔记

一、训练数据准备阶段 数据采集标准 格式要求:严格QA对形式(1问1答) 数量基准: 基础量:500组QA对 优化量:800-1000组QA对 内容规范: 聚焦单一业务节点(如售后场景) …

1688 数据接口调用秘籍:高效获取商品实时信息的开发指南

在电商行业竞争白热化的当下,企业想要抢占市场先机,实时掌握商品信息至关重要。作为国内 B2B 电商巨头,1688 平台汇聚海量商品资源,通过高效调用其数据接口获取商品实时信息,能为企业价格策略制定、库存管理、竞品分析…

milvus学习笔记

本文主要由AI生成,请注意自己查看源代码校验。 Milvus v2.4 系统架构概览 Milvus 采用分布式微服务架构,将计算层(Proxy、QueryCoord、QueryNode、IndexCoord、DataCoord、DataNode 等)与存储层(Pulsar、MinIO/S3、e…

使用教程:8x16模拟开关阵列可级联XY脚双向导通自动化接线

以下通过点亮LED进行基本使用流程演示,实际可以连接复杂外设(SPI、CAN、ADC等) 单模块使用 RX、TX、5V和GND接到串口模块;X5接5V;Y2接LED;LED-接GND 串口模块插上电脑后,LED没有亮;因为此时模…

HarmonyOS NEXT~鸿蒙应用上架指南:HarmonyOS应用发布全流程解析

HarmonyOS NEXT~鸿蒙应用上架指南:HarmonyOS应用发布全流程解析 引言 随着华为鸿蒙操作系统(HarmonyOS)生态的快速发展,越来越多的开发者希望将自己的应用上架到鸿蒙应用市场。本文将详细介绍鸿蒙应用上架的全流程,帮助开发者顺…

20250517 我设想一个空间,无限大,空间不与其中物质进行任何作用,甚至这个空间能容纳可以伸缩的空间

1.我设想一个空间,无限大,空间不与其中物质进行任何作用,甚至这个空间能容纳可以伸缩的空间 您设想的这个空间具有一些有趣的特点: 无限大:空间本身没有边界或限制,理论上可以容纳无限多的物质或结构。非…

使用 Kaniko来构建镜像

使用 Kaniko来构建镜像 Kaniko 是一种专注于容器镜像构建的开源工具,其核心设计理念与 Docker 存在显著差异。以下从功能定位、技术实现和适用场景三方面进行对比分析: 一、Kaniko 的核心特性 无需 Docker 守护进程 Kaniko 直接在容器或 Kubernetes 集…

webman用nginx代理静态json文件的异步跨域

场景 有.json文件置于webman的public目录下,使用了nginx做代理,直接访问文件是可以正常加载的,但跨域浏览器就无法加载文件。 nginx配置 文件是否存在于跟目录,存在则设置请求头,不存在则将请求交给webman处理即可。…

JDK 21新特性全面解析

Java Development Kit (JDK) 21作为Oracle长期支持(LTS)版本,于2023年9月正式发布,带来了多项令人振奋的新特性和改进。本文将全面介绍JDK 21中的主要更新,帮助开发者了解如何利用这些新功能提升开发效率和代码质量。 一、虚拟线程(Virtual …

如何选择高性价比的 1T 服务器租用服务​

选择高性价比的 1T 服务器租用服务​,可参考以下内容: 1、根据需求选配置​ 明确自身业务需求是关键。若为小型网站或轻量级应用,数据存储与处理需求不高,选择基础配置服务器即可。如个人博客网站,普通的 Intel Xeon …

JavaScript性能优化实战(11):前沿技术在性能优化中的应用

引言 随着Web应用复杂度和性能需求不断提高,传统的JavaScript优化技术已经无法满足某些高性能计算场景的需求。本文将深入探讨前沿Web技术如何突破JavaScript的性能瓶颈,为Web应用提供接近原生应用的性能体验。从底层计算到图形渲染,从并发处理到动画优化,我们将通过实际案…

package.json 和 package-lock.json 的区别

package.json​​ ​​作用​​ ​​声明项目元数据​​:如项目名称、版本、描述、入口文件等。​​定义依赖范围​​:在 dependencies 和 devDependencies 中声明项目​​直接依赖​​的包及其​​版本范围​​(如 ^1.2.3)。​​…

Rollup入门与进阶:为现代Web应用构建超小的打包文件

我们常常面临Webpack复杂配置或是Babel转译后的冗余代码,结果导致最终的包体积居高不下加载速度也变得异常缓慢,而在众多打包工具中Rollup作为一个轻量且高效的选择,正悄然改变着这一切,本文将带你深入了解这个令人惊艳的打包工具…