要说 RAG 发展路上卡在哪,其实就两个关键问题。
第一,各种新型 RAG 算法之间,始终缺少一种全面且公平的横向比较。你用你的配置,我跑我的benchmark,结果放在一起根本没法直接对话。第二,LlamaIndex 和 LangChain 这些开源工具虽然好用,但它们做了太高级的抽象封装。这玩意儿的副作用就是内部逻辑成了黑箱,透明度严重缺失。想在上面折腾新算法、开发新评估指标?那可就费劲了。
这正是 RAGLAB 要解决的事。它是一个模块化的开源库,一口气复现了 6 种先进算法,给研究 RAG 算法的人搭了一个全面的生态系统。基于 RAGLAB,已经有人在 10 个不同的基准测试上,对这 6 种算法做了公平的对比。有了这玩意儿,研究人员既能高效地横向比较不同算法的性能,也能顺手开发新算法。
不同 RAG 库和框架的比较
这里需要先厘清一个概念。所谓的公平比较,说白了就是在评估过程中,把所有基本组件都对齐到同一个起跑线上——随机种子、生成器、检索器,甚至你给模型的指令(instruction)都得统一。而数据收集器,就是指有没有能力去收集或生成训练和测试数据,比如从现有的原始数据集里抽个样,或者直接拿 LLM 来构建标注数据。
回到 RAGLAB。它的模块化架构,让你能像搭积木一样,轻松替换和扩展算法的各个组成部分:检索器(retriever)、生成器(generator)和指令(instruction)。
RAGLAB 框架的架构和组件
具体拆开来看,RAGLAB 的核心组件包括这么几块:
检索器(Retriever):集成了 Contriever 和 ColBERT 这类基于 BERT 的模型,统一了查询接口,还搞了个客户端-服务器架构,配合检索缓存机制。简单说,就是查得快,还不容易打架。
语料库(Corpus):直接提供了预处理好的 Wikipedia 语料库,包括 2018 年和 2023 年两个版本,对应的索引和嵌入都打包好了。开箱即用。
生成器(Generator):集成了 Huggingface Transformers 和 VLLM,还支持量化和 LoRA(低秩适应)技术。这意味着你跑大模型的时候,显存没那么容易爆。
指令实验室(Instruction Lab):包含系统指令、任务指令和算法指令。说白了,你怎么调教模型生成结果,全看这里怎么组合指令。
训练器(Trainer):集成了 Accelerate 和 DeepSpeed 库,支持模型微调,包括 LoRA 和量化 LoRA。想自己训个定制版?挺方便。
数据集和度量(Dataset and Metric):一口气收集了 10 个广泛使用的基准数据集,覆盖 5 种不同类型的任务,还提供了灵活的数据适配机制和多样的评估指标。
一个使用 RAGLAB 来复现 Self-RAG 算法的脚本
光说不练假把式。RAGLAB 的实际效果如何?他们做了一组全面的实验,用不同的基础模型当生成器,同时死死按住其他组件不变,就为了看不同 RAG 算法在公平条件下到底谁更强。
实验分析了 Naive RAG、RRR、ITER-RETGEN、Self-Ask、Active RAG、Self-RAG 这几种算法在多个基准上的表现。结论挺有意思:Self-RAG 算法在搭配特定生成器时,表现显著碾压其他算法。


不同算法的指令
以下是各算法在实际代码中的指令示例,供你参考:
Naive RAG
# read process insruction
"### Instruction:n {task_instrucion} n## Input:nn{query}nn Now, based on the following passages
and your knowledge, please answer the question more succinctly and professionally. ### Background
Knowledge:n {passages} nn### Response:n"
RRR
# rewrite process instruction
"Provide a better search query for Wikipedia to answer the given question, end the query with '**'. nn
Question: Ezzard Charles was a world champion in which sport? nn Query: Ezzard Charles
champion** nn Question: What is the correct name of laughing gas? nn Query: laughing gas
name** nn Question: {query} nn Query: " # read process insruction
"### Instruction:n {task_instrucion} n## Input:nn{query}nn Now, based on the following passages
and your knowledge, please answer the question more succinctly and professionally. ### Background
Knowledge:n {passages} nn### Response:n"
ITER-RETGEN
# read process insruction
"### Instruction:n {task_instrucion} n## Input:nn{query}nn Now, based on the following passages
and your knowledge, please answer the question more succinctly and professionally. ### Background
Knowledge:n {passages} nn### Response:n"
Self ASK
# follow up question instruction
"Question: When does monsoon season end in the state the area code 575 is located? Are follow up
questions needed here: Yes. Follow up: Which state is the area code 575 located in? Intermediate
answer: The area code 575 is located in New Mexico. Follow up: When does monsoon season end in
New Mexico? Intermediate answer: Monsoon season in New Mexico typically ends in mid-September. So the final answer is: mid-September. n{query} Are follow up questions needed here:" # read process insruction
"### Instruction:n {task_instrucion} n## Input:nn{query}nn Now, based on the following passages
and your knowledge, please answer the question more succinctly and professionally. ### Background
Knowledge:n {passages} nn### Response:n"
Active RAG
# read process insruction
"### Instruction:n {task_instrucion} n## Input:nn{query}nn Now, based on the following passages
and your knowledge, please answer the question more succinctly and professionally. ### Background
Knowledge:n {passages} nn### Response:n"
Self-RAG
# read process instruction
"### Instruction:n{task_instruction}nn## Input:nn{query}nn### Response:n"
RAGLAB 系统用户评估问卷

想深入了解的,直接看论文和代码:
