改进向量搜索-使用PostgresML和LlamaIndex重新排名

news2024/9/21 14:28:03

改进向量搜索-使用PostgresML和LlamaIndex重新排名

搜索和重新排名:提高结果相关性

搜索系统通常采用两种主要方法:关键字和语义。关键字搜索将精确的查询词与索引数据库内容匹配,而语义搜索使用 NLP 和机器学习来理解查询上下文和意图。许多有效的系统结合了这两种方法以获得最佳结果。

初始检索后,重新排序可以进一步提高结果相关性。传统的重新排序依赖于历史用户交互数据,但这种方法难以处理新内容,并且需要大量数据才能有效训练。一种先进的替代方法是使用交叉编码器,它直接比较查询结果对的相似性。

交叉编码器直接比较两段文本并计算相似度得分。与传统的语义搜索方法不同,我们无法预先计算交叉编码器的嵌入并在以后重复使用它们。相反,我们必须对每一对想要比较的文本运行交叉编码器,这使得这种方法在计算上非常昂贵,并且不适用于大规模搜索。但是,它对于重新排序我们数据集的子集非常有效,因为它擅长评估新的、未见过的数据,而无需大量用户交互数据进行微调。

alt

交叉编码器弥补了传统重排序系统在深度文本分析方面的局限性,尤其是针对新颖或高度特定内容。它们不依赖大量用户交互数据集进行训练(尽管此类数据仍然很有用),并且擅长处理新数据和以前从未见过的数据。这使得交叉编码器成为在重排序环境中增强搜索结果相关性的绝佳选择。

实施重新排名

我们将使用 LlamaIndex 和 PostgresML 托管索引实现一个简单的重新排名示例。有关 PostgresML 托管索引的更多信息。查看我们关于 LlamaIndex 的公告: 使用 LlamaIndex + PostgresML 简化您的 RAG 应用程序架构 。

安装所需的依赖项以开始使用:

pip install llama_index llama-index-indices-managed-postgresml

我们将使用 Paul Graham 数据集,可以通过 curl 下载:

mkdir data

curl -o data/paul_graham_essay.txt https://raw.githubusercontent.com/run-llama/llama_index/main/docs/docs/examples/data/paul_graham/paul_graham_essay.txt

PostgresML 托管索引将处理存储、拆分、嵌入和查询我们的文档。我们需要的只是一个数据库连接字符串。如果您还没有, 请创建您的 PostgresML 帐户 。完成您的个人资料后,您将获得 100 美元的免费积分。

设置 PGML_DATABASE_URL 环境变量:

export PGML_DATABASE_URL="{YOUR_CONNCECTION_STRING}"

让我们创建索引:


  
  from llama_index.core.readers import SimpleDirectoryReader
from llama_index.indices.managed.postgresml import PostgresMLIndex


documents = SimpleDirectoryReader("data").load_data()
index = PostgresMLIndex.from_documents(
    documents, collection_name="llama-index-rerank-example"
)

请注意,collection_name 用于唯一标识您正在使用的索引。

这里我们使用 SimpleDirectoryReader 来加载文档,然后从这些文档构造 PostgresMLIndex。

此工作流程不需要文档预处理。相反,文档会直接发送到 PostgresML,并根据管道规范进行存储、拆分和嵌入。这是使用 PostgresML 托管索引的独特品质。

现在让我们搜索一下!我们可以执行语义搜索,并通过从索引中创建检索器来获取前 2 个结果。


  
  retriever = index.as_retriever(limit=2)
docs = retriever.retrieve("What did the author do as a child?")
for doc in docs:
    print("---------")
    print(f"Id: {doc.id_}")
    print(f"Score: {doc.score}")
    print(f"Text: {doc.text}")

这样做我们得到:


  
  ---------

Id: de01b7e1-95f8-4aa0-b4ec-45ef64816e0e

Score: 0.7793415653313153

Text: Wow, I thought, there's an audience. If I write something and put it on the web, anyone can read it. That may seem obvious now, but it was surprising then. In the print era there was a narrow channel to readers, guarded by fierce monsters known as editors. The only way to get an audience for anything you wrote was to get it published as a book, or in a newspaper or magazine. Now anyone could publish anything.



This had been possible in principle since 1993, but not many people had realized it yet. I had been intimately involved with building the infrastructure of the web for most of that time, and a writer as well, and it had taken me 8 years to realize it. Even then it took me several years to understand the implications. It meant there would be a whole new generation of essays. [11]



In the print era, the channel for publishing essays had been vanishingly small. Except for a few officially anointed thinkers who went to the right parties in New York, the only people allowed to publish essays were specialists writing about their specialties. There were so many essays that had never been written, because there had been no way to publish them. Now they could be, and I was going to write them. [12]



I'
ve worked on several different things, but to the extent there was a turning point where I figured out what to work on, it was when I started publishing essays online. From then on I knew that whatever else I did, I'd always write essays too.



---------

Id: de01b7e1-95f8-4aa0-b4ec-45ef64816e0e

Score: 0.7770352826735559

Text: Asterix comics begin by zooming in on a tiny corner of Roman Gaul that turns out not to be controlled by the Romans. You can do something similar on a map of New York City: if you zoom in on the Upper East Side, there'
s a tiny corner that's not rich, or at least wasn'in 1993. It's called Yorkville, and that was my new home. Now I was a New York artist — in the strictly technical sense of making paintings and living in New York.



I was nervous about money, because I could sense that Interleaf was on the way down. Freelance Lisp hacking work was very rare, and I didn'
t want to have to program in another language, which in those days would have meant C++ if I was lucky. So with my unerring nose for financial opportunity, I decided to write another book on Lisp. This would be a popular book, the sort of book that could be used as a textbook. I imagined myself living frugally off the royalties and spending all my time painting. (The painting on the cover of this book, ANSI Common Lisp, is one that I painted around this time.)



The best thing about New York for me was the presence of Idelle and Julian Weber. Idelle Weber was a painter, one of the early photorealists, and I'd taken her painting class at Harvard. I've never known a teacher more beloved by her students. Large numbers of former students kept in touch with her, including me. After I moved to New York I became her de facto studio assistant.

这些结果还不错,但并不完美。让我们尝试使用交叉编码器进行重新排序。


  
  retriever = index.as_retriever(
    limit=2,
    rerank={
        "model""mixedbread-ai/mxbai-rerank-base-v1",
        "num_documents_to_rerank": 100
    }
)
docs = retriever.retrieve("What did the author do as a child?")
for doc in docs:
    print("---------")
    print(f"Id: {doc.id_}")
    print(f"Score: {doc.score}")
    print(f"Text: {doc.text}")

在这里,我们将检索器配置为返回排名前两个的文档,但这次,我们添加了一个重新排名参数以使用mixedbread-ai/mxbai-rerank-base-v1模型。这意味着我们的初始语义搜索将返回 100 个结果,然后由mixedbread-ai/mxbai-rerank-base-v1模型对这些结果进行重新排名,并且仅显示排名前两个的结果。

运行此输出:


  
  Id: de01b7e1-95f8-4aa0-b4ec-45ef64816e0e
Score: 0.17803585529327393
Text: What I Worked On

February 2021

Before college the two main things I worked on, outside of school, were writing and programming. I didn't write essays. I wrote what beginning writers were supposed to write then, and probably still are: short stories. My stories were awful. They had hardly any plot, just characters with strong feelings, which I imagined made them deep.

The first programs I tried writing were on the IBM 1401 that our school district used for what was then called "data processing." This was in 9th grade, so I was 13 or 14. The school district'
s 1401 happened to be in the basement of our junior high school, and my friend Rich Draves and I got permission to use it. It was like a mini Bond villain's lair down there, with all these alien-looking machines — CPU, disk drives, printer, card reader — sitting up on a raised floor under bright fluorescent lights.

The language we used was an early version of Fortran. You had to type programs on punch cards, then stack them in the card reader and press a button to load the program into memory and run it. The result would ordinarily be to print something on the spectacularly loud printer.


---------
Id: de01b7e1-95f8-4aa0-b4ec-45ef64816e0e
Score: 0.1057136133313179
Text: I wanted not just to build things, but to build things that would last.

In this dissatisfied state I went in 1988 to visit Rich Draves at CMU, where he was in grad school. One day I went to visit the Carnegie Institute, where I'
d spent a lot of time as a kid. While looking at a painting there I realized something that might seem obvious, but was a big surprise to me. There, right on the wall, was something you could make that would last. Paintings didn't become obsolete. Some of the best ones were hundreds of years old.

And moreover this was something you could make a living doing. Not as easily as you could by writing software, of course, but I thought if you were really industrious and lived really cheaply, it had to be possible to make enough to survive. And as an artist you could be truly independent. You wouldn'
t have a boss, or even need to get research funding.

I had always liked looking at paintings. Could I make them? I had no idea. I'd never imagined it was even possible. I knew intellectually that people made art — that it didn't just appear spontaneously — but it was as if the people who made it were a different species. They either lived long ago or were mysterious geniuses doing strange things in profiles in Life magazine. The idea of actually being able to make art, to put that verb before that noun, seemed almost miraculous.

这些结果好多了!我们可以看到,最上面的文档回答了用户的问题。请注意,我们不必指定用于重新排名的第三方 API。再一次,PostgresML 使用数据库中的交叉编码器来处理重新排名。

我们可以在 RAG 中直接使用重新排序:


  
  query_engine = index.as_query_engine(
    streaming=True,
    vector_search_limit=2,
    vector_search_rerank={
        "model""mixedbread-ai/mxbai-rerank-base-v1",
        "num_documents_to_rerank": 100,
    },
)
results = query_engine.query("What did the author do as a child?")
for text in results.response_gen:
    print(text, end="", flush=True)

运行此输出:

Based on the context information, as a child, the author worked on writing (writing short stories) and programming (on the IBM 1401 using Fortran) outside of school.

这正是我们想要的答案!

重新排序可带来更好的结果

搜索可能很复杂。使用交叉编码器进行重新排序可以通过比较文本对并有效处理新数据来改进搜索。使用 LlamaIndex 和 PostgresML 实现重新排序可以改进搜索结果,在检索增强生成应用程序中提供更精确的答案。

要开始使用 PostgresML 和 LlamaIndex,您可以按照 PostgresML 入门 指南 设置您的帐户,并将上述示例与您自己的数据一起使用。

原文链接:https://www.llamaindex.ai/blog/improving-vector-search-reranking-with-postgresml-and-llamaindex

本文由 mdnice 多平台发布

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

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

相关文章

【踩坑系列-Docker】基于Alibaba Cloud Linux3基础镜像安装Nginx

Author:赵志乾 Date:2024-07-26 Declaration:All Right Reserved!!! 1. 问题描述 使用Alibaba Cloud Linux3作为基础镜像,在其上安装Nginx,对应的Dockerfile内容如下: …

使用 From File 模块加载数据

目录 检查模型 创建时间和信号数据 加载 timeseries 数据 加载数组数据 加载总线数据 此示例说明如何使用 From File 模块从 MAT 文件加载仿真输入数据,包括如何创建和格式化输入数据。可以通过编程方式创建您加载的数据,加载从另一个仿真中记录的数据,或加载从…

栈和队列<数据结构 C版>

目录 栈(Stack) 栈的结构体 初始化 销毁 入栈 判空 出栈 取栈顶元素 获取栈个数 测试: 队列(Queue) 队列的结构体 单个结点 队列 初始化 销毁 入队列,队尾 判空 出队列,队头 …

【YashanDB知识库】开源调度框架Quartz写入Boolean值到YashanDB报错

问题现象 Quartz 是一个广泛应用于企业级应用中的开源作业调度框架,它主要用于在Java环境中管理和执行任务。 为了任务调度,Quartz的数据模型中使用了大量的布尔值记录任务、流程的各种状态,如: Quartz使用JDBC写入任务状态代码…

【资料分享】2024第三届钉钉杯大学生大数据挑战赛B题思路解析+双语言代码

2024钉钉杯大学生大数据挑战赛,B题解题思路和双语言代码分享,资料预览:

制作excel模板,用于管理后台批量导入船舶数据

文章目录 引言I 数据有效性:基于WPS在Excel中设置下拉框选择序列内容II 数据处理:基于easyexcel工具实现导入数据的持久化2.1 自定义枚举转换器2.2 ExcelDataConvertExceptionIII 序列格式化: 基于Sublime Text 文本编辑器进行批量字符操作引言 需求: excel数据导入模板制…

【MySQL进阶之路 | 高级篇】表级锁之S锁,X锁,意向锁

1. 从数据操作的粒度划分:表级锁,页级锁,行锁 为了尽可能提高数据库的并发度,每次锁定的数据范围越小越好,理论上每次只锁定当前操作的数据的方案会得到最大的并发度,但是管理锁是很耗资源的事情&#xff…

前端开发:HTML与CSS

文章目录 前言1.1、CS架构和BS架构1.2、网页构成 HTML1.web开发1.1、最简单的web应用程序1.2、HTTP协议1.2.1 、简介1.2.2、 http协议特性1.3.3、http请求协议与响应协议 2.HTML概述3.HTML标准结构4.标签的语法5.基本标签6.超链接标签6.1、超链接基本使用6.2、锚点 7.img标签8.…

【网络安全的神秘世界】文件包含漏洞

🌝博客主页:泥菩萨 💖专栏:Linux探索之旅 | 网络安全的神秘世界 | 专接本 | 每天学会一个渗透测试工具 一、概述 文件包含:重复使用的函数写在文件里,需要使用某个函数时直接调用此文件,而无需再…

【学习日记】函数调用 和 全局变量 如何实现 位置无关码

问题来源 在 I.MX6ull 的启动流程中,u-boot会将自身从内存一开始的位置拷贝到其他位置,以便给linux留出内存空间,防止 u-boot被覆盖如果代码中包含直接引用其链接时地址的指令,那么当代码被移动到新的地址时,这些引用…

聊聊RNNLSTM

RNN 用于解决输入数据为,序列到序列(时间序列)数据,不能在传统的前馈神经网络(FNN)很好应用的问题。时间序列数据是指在不同时间点上收集到的数据,这类数据反映了某一事物、现象等随时间的变化状态或程度,即输入内容的上下文关联…

工业现场实测,焦化厂导烟车与装煤车风机实现无人作业

一、项目背景 作为我国重要的能源行业之一,焦化行业在国民经济中扮演着重要角色,焦化工艺是高温、高压、有毒物质等因素共同作用下进行的,因此存在着安全隐患,并伴有环境污染,改善焦化工艺的安全和环保问题是当前亟待…

优选算法之前缀和(下)

目录 一、和为 k 的子数组 1.题目链接:560. 和为 K 的子数组 2.题目描述: 3.解法(前缀和 哈希表) 🌻算法思路: 🌻算法代码: 二、和可被 k 整除的子数组 1.题目链接&#xff…

MySQL中多表查询之外连接

首先先来介绍一下我做的两个表,然后再用他们两个举例说明。 -- 创建教师表 create table teachers( id_t int primary key auto_increment, -- 老师编号 name_t varchar(5) -- 姓名 ); -- 创建学生表 create table students( id_s int primary key auto_increment,…

Android APK混淆处理方案分析

这里写目录标题 一、前言1.1 相关工具二、Apk 分析2.1 apk 解压文件2.2 apk 签名信息2.3 apk AndroidManifest.xml2.4 apk code三、Apk 处理3.1 添加垃圾文件3.2 AndroidManifest.xml 处理3.3 dex 混淆处理3.4 zipalign对齐3.5 apk 重新签名3.6 apk 安装测试四、总结一、前言 提…

使用Astro+Vercel+Cloudflare一天时间开发部署上线一个知识博客网站,简直简简单单

大家好,这里是程序猿代码之路。在当今数字化时代,拥有一个个人博客网站对于分享知识、展示个人品牌变得越来越重要。然而,许多非技术背景的用户对于搭建和维护一个网站可能会感到望而却步。幸运的是,随着低代码和无代码平台的兴起…

Spring高手之路21——深入剖析Spring AOP代理对象的创建

文章目录 创建代理对象核心动作的三个步骤1. 判断 Bean 是否需要增强(源码分析时序图说明)2. 匹配增强器 Advisors(源码分析时序图说明)3. 创建代理对象(源码分析时序图说明) 创建代理对象核心动作的三个步…

C++模版基础知识与STL基本介绍

目录 一. 泛型编程 二. 函数模板 1. 概念 2. 函数模版格式 3. 函数模版的原理 4. 模版函数的实例化 (1). 隐式实例化 (2.) 显式实例化 5. 模版参数的匹配原则 三. 类模板 1. 类模板的定义格式 2. 类模板的实例化 四. STL的介绍 1. 什么是STL? 2. STL的版…

netty入门-7 ByteBuf

文章目录 前言ByteBuf结构池化与非池化创建(直接内存/堆内存)写入和读取释放零拷贝,slice,duplicate, copy,Composite 结语 前言 ByteBuf这部分视频讲的更为详细。 主要参考视频。 ByteBuf 结构 首先这…

C# Task.WaitAll 的用法

目录 简介 1.WaitAll(Task[], Int32, CancellationToken) 2.WaitAll(Task[]) 3.WaitAll(Task[], Int32) 4.WaitAll(Task[], CancellationToken) 5.WaitAll(Task[], TimeSpan) 结束 简介 Task.WaitAll 是 C# 中用于并行编程的一个的方法,它属于 System.Threa…