2024工作周期安排
2024项目整体规划
沐曦测试(已完成)
沐曦性能测试研究
沐曦Benchmark相关测试
沐曦模型适配表
2024-02-26沐曦沟通报告
智能打标
数据打标服务Json样例
智能打标寒武纪大模型思路
业务层优先级排序
智能打标流程图
打标API接口文档
图像内容识别
其他
申报项目文本段落
研发链相关资料文档
国产GPU虚拟化培训介绍
RAG垂直落地
DB-GPT与TeleChat-7B搭建相关RAG知识库
ChatWithRTX
ChatRTX安装教程
ChatWithRTX 踩坑记录
ChatWithRTX 使用其他量化模型
ChatWithRTX介绍
RAG 相关资料
英伟达—大模型结合 RAG 构建客服场景自动问答
又一大模型技术开源!有道自研RAG引擎QAnything正式开放下载
收藏!RAG入门参考资料开源大总结:RAG综述、介绍、比较、预处理、RAG Embedding等
RAG调研
解决现代RAG实际生产问题
解决现代 RAG 系统中的生产问题-II
先进的Retriever技术来增强你的RAGs
高级RAG — 使用假设文档嵌入 (HyDE) 改进检索
提升 RAG:选择最佳嵌入和 Reranker 模型
LangGraph
增强型RAG:re-rank
大模型比赛
相关资料
智能填单_填单 启动命令
2024私人规划
ChatGPT API账号记录
公众号相关资料
基于 Docker 的深度学习环境:入门篇
ollama
-
+
首页
增强型RAG:re-rank
## 从原理到两种主流实现方法 重新排序在检索增强生成(RAG)过程中起着至关重要的作用。在简单的 RAG 方法中,可以检索大量上下文,但并非所有上下文都一定与问题相关。重新排序允许对文档进行重新排序和过滤,将相关文档置于最前面,从而提高 RAG 的有效性。 本文介绍了 RAG 的重新排名技术,并演示了如何使用两种方法合并重新排名功能。 # 重新排名简介 ![img](https://yg9538.kmgy.top/202405150659107.png)图 1:RAG 中的重新排序,重新排序的任务是评估这些上下文的相关性,并优先考虑最有可能提供准确且相关答案的上下文(红色框)。图片由作者提供。 如图1所示,重新排序的任务就像一个智能过滤器。当检索器从索引集合中检索多个上下文时,这些上下文可能与用户的查询具有不同的相关性。有些上下文可能非常相关(在图 1 中以红色框突出显示),而另一些上下文可能只是轻微相关甚至不相关(在图 1 中以绿色和蓝色框突出显示)。 重新排名的任务是评估这些上下文的相关性,并优先考虑最有可能提供准确且相关答案的上下文。这使得法学硕士能够在生成答案时优先考虑这些排名靠前的上下文,从而提高响应的准确性和质量。 简单来说,重新排名就像开卷考试时帮助你从一堆学习材料中选择最相关的参考文献,以便你更高效、更准确地回答问题。 本文介绍的重排序方法主要可以分为以下两种: - 重新排序模型:这些模型考虑文档和查询之间的交互特征,以更准确地评估它们的相关性。 - LLM:LLM的出现为重新排名开辟了新的可能性。通过彻底理解整个文档和查询,可以更全面地捕获语义信息。 # 使用重新排序模型作为重新排序器 与嵌入模型不同,重排序模型将查询和上下文作为输入,并直接输出相似度分数而不是嵌入。值得注意的是,重排序模型是使用交叉熵损失进行优化的,允许相关性分数不限于特定范围,甚至可以是负值。 目前,可用的重排序模型并不多。一种选择是Cohere的在线模型,可以通过 API 访问。还有开源模型,例如bge-reranker-base 和 bge-reranker-large等。 图 2 显示了使用命中率和平均倒数排名 (MRR) 指标的评估结果: ![img](https://yg9538.kmgy.top/202405150659125.png)图 2:使用命中率和平均倒数排名 (MRR) 指标的评估结果。资料来源:Boosting RAG:选择最佳嵌入和重新排序模型 从这个评测结果我们可以看出: - 无论使用哪种嵌入模型,重新排名都表现出更高的命中率和 MRR,表明重新排名的显着影响。 - 目前,最好的重新排名模型是Cohere,但它是一项付费服务。开源bge-reranker-large模型具有与 Cohere 类似的功能。 - 嵌入模型和重排序模型的组合也会产生影响,因此开发人员可能需要在实际过程中尝试不同的组合。 在本文中,将使用 bge-reranker-base 模型。 ## 环境配置 导入相关库,设置环境和全局变量 ``` import os os.environ["OPENAI_API_KEY"] = "YOUR_OPENAI_KEY" from llama_index import VectorStoreIndex, SimpleDirectoryReader from llama_index.postprocessor.flag_embedding_reranker import FlagEmbeddingReranker from llama_index.schema import QueryBundle dir_path = "YOUR_DIR_PATH" ``` 目录下只有一个PDF文件,使用的是论文《TinyLlama: An Open-Source Small Language Model》。 ``` (py) Florian:~ Florian$ ls /Users/Florian/Downloads/pdf_test/ tinyllama.pdf ``` ## 使用 LlamaIndex 构建一个简单的检索器 ``` documents = SimpleDirectoryReader(dir_path).load_data() index = VectorStoreIndex.from_documents(documents) retriever = index.as_retriever(similarity_top_k = 3) ``` ## 基本检索 ``` query = "Can you provide a concise description of the TinyLlama model?" nodes = retriever.retrieve(query) for node in nodes: print('----------------------------------------------------') display_source_node(node, source_length = 500) ``` display_source_node 函数改编自llama_index 源代码。原函数是为Jupyter笔记本设计的,因此修改如下: ``` from llama_index.schema import ImageNode, MetadataMode, NodeWithScore from llama_index.utils import truncate_text def display_source_node( source_node: NodeWithScore, source_length: int = 100, show_source_metadata: bool = False, metadata_mode: MetadataMode = MetadataMode.NONE, ) -> None: """Display source node""" source_text_fmt = truncate_text( source_node.node.get_content(metadata_mode=metadata_mode).strip(), source_length ) text_md = ( f"Node ID: {source_node.node.node_id} \n" f"Score: {source_node.score} \n" f"Text: {source_text_fmt} \n" ) if show_source_metadata: text_md += f"Metadata: {source_node.node.metadata} \n" if isinstance(source_node.node, ImageNode): text_md += "Image:" print(text_md) # display(Markdown(text_md)) # if isinstance(source_node.node, ImageNode) and source_node.node.image is not None: # display_image(source_node.node.image) ``` 基本检索结果如下,代表重排序前的前3个节点: ``` ---------------------------------------------------- Node ID: 438b9d91-cd5a-44a8-939e-3ecd77648662 Score: 0.8706055408845863 Text: 4 Conclusion In this paper, we introduce TinyLlama, an open-source, small-scale language model. To promote transparency in the open-source LLM pre-training community, we have released all relevant infor- mation, including our pre-training code, all intermediate model checkpoints, and the details of our data processing steps. With its compact architecture and promising performance, TinyLlama can enable end-user applications on mobile devices, and serve as a lightweight platform for testing a w... ---------------------------------------------------- Node ID: ca4db90f-5c6e-47d5-a544-05a9a1d09bc6 Score: 0.8624531691777889 Text: TinyLlama: An Open-Source Small Language Model Peiyuan Zhang∗Guangtao Zeng∗Tianduo Wang Wei Lu StatNLP Research Group Singapore University of Technology and Design {peiyuan_zhang, tianduo_wang, @sutd.edu.sg">luwei}@sutd.edu.sg guangtao_zeng@mymail.sutd.edu.sg Abstract We present TinyLlama, a compact 1.1B language model pretrained on around 1 trillion tokens for approximately 3 epochs. Building on the architecture and tok- enizer of Llama 2 (Touvron et al., 2023b), TinyLlama leverages various advances contr... ---------------------------------------------------- Node ID: e2d97411-8dc0-40a3-9539-a860d1741d4f Score: 0.8346160605298356 Text: Although these works show a clear preference on large models, the potential of training smaller models with larger dataset remains under-explored. Instead of training compute-optimal language models, Touvron et al. (2023a) highlight the importance of the inference budget, instead of focusing solely on training compute-optimal language models. Inference-optimal language models aim for optimal performance within specific inference constraints This is achieved by training models with more tokens... ``` ## 重新排名 要重新排列上述节点,请使用 bge-reranker-base 模型。 ``` print('------------------------------------------------------------------------------------------------') print('Start reranking...') reranker = FlagEmbeddingReranker( top_n = 3, model = "BAAI/bge-reranker-base", ) query_bundle = QueryBundle(query_str=query) ranked_nodes = reranker._postprocess_nodes(nodes, query_bundle = query_bundle) for ranked_node in ranked_nodes: print('----------------------------------------------------') display_source_node(ranked_node, source_length = 500) ``` 重新排序后的结果如下: ``` ------------------------------------------------------------------------------------------------ Start reranking... ---------------------------------------------------- Node ID: ca4db90f-5c6e-47d5-a544-05a9a1d09bc6 Score: -1.584416151046753 Text: TinyLlama: An Open-Source Small Language Model Peiyuan Zhang∗Guangtao Zeng∗Tianduo Wang Wei Lu StatNLP Research Group Singapore University of Technology and Design {peiyuan_zhang, tianduo_wang, @sutd.edu.sg">luwei}@sutd.edu.sg guangtao_zeng@mymail.sutd.edu.sg Abstract We present TinyLlama, a compact 1.1B language model pretrained on around 1 trillion tokens for approximately 3 epochs. Building on the architecture and tok- enizer of Llama 2 (Touvron et al., 2023b), TinyLlama leverages various advances contr... ---------------------------------------------------- Node ID: e2d97411-8dc0-40a3-9539-a860d1741d4f Score: -1.7028117179870605 Text: Although these works show a clear preference on large models, the potential of training smaller models with larger dataset remains under-explored. Instead of training compute-optimal language models, Touvron et al. (2023a) highlight the importance of the inference budget, instead of focusing solely on training compute-optimal language models. Inference-optimal language models aim for optimal performance within specific inference constraints This is achieved by training models with more tokens... ---------------------------------------------------- Node ID: 438b9d91-cd5a-44a8-939e-3ecd77648662 Score: -2.904750347137451 Text: 4 Conclusion In this paper, we introduce TinyLlama, an open-source, small-scale language model. To promote transparency in the open-source LLM pre-training community, we have released all relevant infor- mation, including our pre-training code, all intermediate model checkpoints, and the details of our data processing steps. With its compact architecture and promising performance, TinyLlama can enable end-user applications on mobile devices, and serve as a lightweight platform for testing a w... ``` 很明显,重新排序后,具有该 ID 的节点`ca4db90f-5c6e-47d5-a544–05a9a1d09bc6`的排名从 2 变为 1。这意味着最相关的上下文排在第一位。 # 使用 LLM 作为重新排序器 现有涉及LLM的重排序方法大致可以分为三类:通过重排序任务微调LLM、提示LLM进行重排序以及在训练过程中使用LLM进行数据增强。 提示LLM重新排名的方法成本较低。下面是使用RankGPT的演示,它已集成到 LlamaIndex 中。 RankGPT 的想法是使用 LLM(例如 ChatGPT 或 GPT-4 或其他 LLM)执行零样本列表通道重新排序。它应用排列生成方法和滑动窗口策略来有效地对段落进行重新排序。 如图3所示,论文提出了三种可行的方法。 ![img](https://yg9538.kmgy.top/202405150659131.png)图 3:使用 LLM 进行零样本通道重新排序的三种指令。灰色和黄色块表示模型的输入和输出。(a) 查询生成依赖于 LLM 的对数概率来根据段落生成查询。(b) 相关性生成指示法学硕士输出相关性判断。(c) 排列生成生成一组段落的排名列表。资料来源:https://arxiv.org/pdf/2304.09542.pdf 前两种方法是传统方法,给每个文档打分,然后根据这个分数对所有段落进行排序。 本文提出了第三种方法,排列生成。具体来说,该模型不依赖外部评分,而是直接对段落进行端到端排序。换句话说,它直接利用LLM的语义理解能力对所有候选段落进行相关性排序。 然而,通常候选文档的数量非常大,而法学硕士的输入却有限。因此,通常不可能一次输入所有文本。 ![img](https://yg9538.kmgy.top/202405150659094.png)图 4:使用窗口大小为 4、步长为 2 的滑动窗口对 8 个段落进行重新排序的图示。蓝色代表前两个窗口,黄色代表最后一个窗口。滑动窗口按从后到先的顺序应用,这意味着前一个窗口中的前 2 个段落将参与下一个窗口的重新排名。资料来源:https://arxiv.org/pdf/2304.09542.pdf 因此,如图4所示,引入了滑动窗口方法,该方法遵循冒泡排序的思想。每次只对最前面的`4`文本进行排序,然后移动窗口,对后续`4`文本进行排序。迭代整个文本后,我们可以获得性能最好的顶部文本。 请注意,为了使用 RankGPT,您需要安装更新版本的 LlamaIndex。我之前安装的版本 ( `0.9.29`) 不包含 RankGPT 所需的代码。因此,我使用 LlamaIndex version 创建了一个新的 conda 环境`0.9.45.post1`。 代码很简单,基于上一节的代码,只需将RankGPT设置为reranker即可。 ``` from llama_index.postprocessor import RankGPTRerank from llama_index.llms import OpenAI reranker = RankGPTRerank( top_n = 3, llm = OpenAI(model="gpt-3.5-turbo-16k"), # verbose=True, ) ``` 总体结果如下: ``` (llamaindex_new) Florian:~ Florian$ python /Users/Florian/Documents/rerank.py ---------------------------------------------------- Node ID: 20de8234-a668-442d-8495-d39b156b44bb Score: 0.8703492815379594 Text: 4 Conclusion In this paper, we introduce TinyLlama, an open-source, small-scale language model. To promote transparency in the open-source LLM pre-training community, we have released all relevant infor- mation, including our pre-training code, all intermediate model checkpoints, and the details of our data processing steps. With its compact architecture and promising performance, TinyLlama can enable end-user applications on mobile devices, and serve as a lightweight platform for testing a w... ---------------------------------------------------- Node ID: 47ba3955-c6f8-4f28-a3db-f3222b3a09cd Score: 0.8621633467539512 Text: TinyLlama: An Open-Source Small Language Model Peiyuan Zhang∗Guangtao Zeng∗Tianduo Wang Wei Lu StatNLP Research Group Singapore University of Technology and Design {peiyuan_zhang, tianduo_wang, @sutd.edu.sg">luwei}@sutd.edu.sg guangtao_zeng@mymail.sutd.edu.sg Abstract We present TinyLlama, a compact 1.1B language model pretrained on around 1 trillion tokens for approximately 3 epochs. Building on the architecture and tok- enizer of Llama 2 (Touvron et al., 2023b), TinyLlama leverages various advances contr... ---------------------------------------------------- Node ID: 17cd9896-473c-47e0-8419-16b4ac615a59 Score: 0.8343984516104476 Text: Although these works show a clear preference on large models, the potential of training smaller models with larger dataset remains under-explored. Instead of training compute-optimal language models, Touvron et al. (2023a) highlight the importance of the inference budget, instead of focusing solely on training compute-optimal language models. Inference-optimal language models aim for optimal performance within specific inference constraints This is achieved by training models with more tokens... ------------------------------------------------------------------------------------------------ Start reranking... ---------------------------------------------------- Node ID: 47ba3955-c6f8-4f28-a3db-f3222b3a09cd Score: 0.8621633467539512 Text: TinyLlama: An Open-Source Small Language Model Peiyuan Zhang∗Guangtao Zeng∗Tianduo Wang Wei Lu StatNLP Research Group Singapore University of Technology and Design {peiyuan_zhang, tianduo_wang, @sutd.edu.sg">luwei}@sutd.edu.sg guangtao_zeng@mymail.sutd.edu.sg Abstract We present TinyLlama, a compact 1.1B language model pretrained on around 1 trillion tokens for approximately 3 epochs. Building on the architecture and tok- enizer of Llama 2 (Touvron et al., 2023b), TinyLlama leverages various advances contr... ---------------------------------------------------- Node ID: 17cd9896-473c-47e0-8419-16b4ac615a59 Score: 0.8343984516104476 Text: Although these works show a clear preference on large models, the potential of training smaller models with larger dataset remains under-explored. Instead of training compute-optimal language models, Touvron et al. (2023a) highlight the importance of the inference budget, instead of focusing solely on training compute-optimal language models. Inference-optimal language models aim for optimal performance within specific inference constraints This is achieved by training models with more tokens... ---------------------------------------------------- Node ID: 20de8234-a668-442d-8495-d39b156b44bb Score: 0.8703492815379594 Text: 4 Conclusion In this paper, we introduce TinyLlama, an open-source, small-scale language model. To promote transparency in the open-source LLM pre-training community, we have released all relevant infor- mation, including our pre-training code, all intermediate model checkpoints, and the details of our data processing steps. With its compact architecture and promising performance, TinyLlama can enable end-user applications on mobile devices, and serve as a lightweight platform for testing a w... ``` 请注意,由于使用LLM,重新排名后的分数尚未更新。当然,这并不重要。 从结果中我们可以看到,重新排序后,排名第一的结果是包含答案的正确文本,这与之前使用重新排序模型得到的结果一致。 # 评估 我们可以使用本系列上一篇文章中介绍的方法: ## 高级 RAG 03:使用 RAGAs + LlamaIndex 进行 RAG 评估 ### 在本文中,我们首先介绍RAGAs(Retrieval Augmented Generation... 简单英语网 具体过程在本系列的上一篇文章中有介绍。修改后的代码如下: ``` reranker = FlagEmbeddingReranker( top_n = 3, model = "BAAI/bge-reranker-base", use_fp16 = False ) # or using LLM as reranker # from llama_index.postprocessor import RankGPTRerank # from llama_index.llms import OpenAI # reranker = RankGPTRerank( # top_n = 3, # llm = OpenAI(model="gpt-3.5-turbo-16k"), # # verbose=True, # ) query_engine = index.as_query_engine( # add reranker to query_engine similarity_top_k = 3, node_postprocessors=[reranker] ) # query_engine = index.as_query_engine() # original query_engine ``` 有兴趣的读者可以测试一下。 # 结论 总体来说,本文介绍了重排序的原理和两种主流方法。 其中,使用重排序模型的方法是轻量级的,开销较小。 另一方面,使用LLM的方法在多个基准上表现良好,但成本较高,并且仅在使用ChatGPT和GPT-4时表现良好,而在使用其他开源模型(例如FLAN)时表现不佳-T5 和 Vicuna-13B。 因此,在实际项目中,需要进行具体的权衡。 最后,如果大家还有什么问题,欢迎在评论区指出。
yg9538
2024年5月15日 06:47
376
转发文档
收藏文档
上一篇
下一篇
手机扫码
复制链接
手机扫一扫转发分享
复制链接
Markdown文件
PDF文档
PDF文档(打印)
分享
链接
类型
密码
更新密码