FlashRAG

LLM、AIGC、RAG 开发交流裙:377891973


一、关于 FlashRAG


FlashRAG 是一个用于复现和开发检索增强生成 (RAG) 研究的 Python 工具包。

工具包包括 32 个预处理的基准 RAG 数据集和 12 个最先进的 RAG 算法。

在这里插入图片描述

借助 FlashRAG 和提供的资源,您可以轻松地在 RAG 域中重现现有的 SOTA 作品或实现自定义的 RAG 流程和组件。

FlashRAG 是根据MIT 许可证授权的。


特点 ✨

  • 🛠 广泛且可定制的框架:包括 RAG 场景的基本组件,例如检索器、重新排序器、生成器和压缩器,允许灵活组装复杂的管道。
  • 🗂 全面的基准数据集:32 个预处理的 RAG 基准数据集的集合,用于测试和验证 RAG 模型的性能。
  • 🎯 预实现的高级 RAG 算法:基于我们的框架,具有 12 种先进的 RAG 算法,并报告了结果。可轻松在不同设置下重现结果。
  • 🧩 高效的预处理阶段:通过提供检索的语料库处理、检索索引构建和文档预检索等各种脚本,简化 RAG 工作流程准备。
  • 🚀 优化执行:通过 vLLM、用于 LLM 推理加速的 FastChat 和用于向量索引管理的 Faiss 等工具增强了库的效率。

🔧 安装

要开始使用 FlashRAG,只需从 Github 克隆并安装(需要 Python 3.9+):

git clone https://github.com/RUC-NLPIR/FlashRAG.git
cd FlashRAG
pip install -e . 

二、快速入门🏃

1、Toy Example

运行以下代码,使用提供的玩具数据集实现一个简单的 RAG 管道。

默认检索器是e5,默认生成器是llama2-7B-chat

您需要在以下命令中填写相应的模型路径。

如果您希望使用其他模型,请参阅下面的详细说明。

cd examples/quick_start
python simple_pipeline.py \
    --model_path=<LLAMA2-7B-Chat-PATH> \
    --retriever_path=<E5-PATH>

代码完成后可以在相应路径下的输出文件夹里查看运行的中间结果以及最终的评估分数。

注意: 此示例仅用于测试整个流程是否能正常运行,我们的示例检索文档仅包含 1000 条数据,因此可能无法获得良好的结果。


2、使用现成的管道

您可以使用我们已经构建好的pipeline类(如pipelines所示)来实现里面的RAG流程,这种情况下,只需要配置config,加载对应的pipeline即可。

首先加载整个流程的config,里面记录了RAG流程中需要用到的各种超参数,可以把yaml文件作为参数输入,也可以直接作为变量输入,变量作为输入的优先级高于文件。

from flashrag.config import Config

config_dict = {'data_dir': 'dataset/'}
my_config = Config(config_file_path = 'my_config.yaml',
                config_dict = config_dict)

您可以参考我们提供的基础yaml文件来设置自己的参数,具体参数名称及含义请参考config参数说明

接下来加载相应的数据集,并初始化管道。管道中的组件将被自动加载。

from flashrag.utils import get_dataset
from flashrag.pipeline import SequentialPipeline
from flashrag.prompt import PromptTemplate
from flashrag.config import Config

config_dict = {'data_dir': 'dataset/'}
my_config = Config(config_file_path = 'my_config.yaml',
                config_dict = config_dict)
all_split = get_dataset(my_config)
test_data = all_split['test']

pipeline = SequentialPipeline(my_config)

您可以使用以下方式指定自己的输入提示PromptTemplete

prompt_templete = PromptTemplate(
    config, 
    system_prompt = "Answer the question based on the given document. Only give me the answer and do not output any other words.\nThe following are given documents.\n\n{reference}",
    user_prompt = "Question: {question}\nAnswer:"
)
pipeline = SequentialPipeline(my_config, prompt_template=prompt_templete)

最后执行pipeline.run即可得到最终结果。

output_dataset = pipeline.run(test_data, do_eval=True)

包含output_dataset输入数据集中每项的中间结果和度量分数。

同时,包含中间结果和总体评估分数的数据集也将保存为文件(如果指定了save_intermediate_datasave_metric_score)。


3、建立自己的管道

有时你可能需要实现更复杂的RAG流程,你可以构建自己的流水线来实现,只需要继承BasicPipeline,初始化你需要的组件,完成run功能即可。

from flashrag.pipeline import BasicPipeline
from flashrag.utils import get_retriever, get_generator

class ToyPipeline(BasicPipeline):
  def __init__(self, config, prompt_templete=None):
    # Load your own components
    pass

  def run(self, dataset, do_eval=True):
    # Complete your own process logic

    # get attribute in dataset using `.`
    input_query = dataset.question
    ...
    # use `update_output` to save intermeidate data
    dataset.update_output("pred",pred_answer_list)
    dataset = self.evaluate(dataset, do_eval=do_eval)
    return dataset

了解您需要使用的组件的输入和输出形式 :

https://github.com/RUC-NLPIR/FlashRAG/blob/main/docs/basic_usage.md


4、只需使用组件

如果您已经有自己的代码,只想使用我们的组件来嵌入原有的代码,您可以参考组件的基本介绍来获取各个组件的输入输出格式。


三、组件⚙️

在 FlashRAG 中,我们构建了一系列常用的 RAG 组件,包括检索器、生成器、refiners 等。

基于这些组件,我们组装了多个管道来实现 RAG 工作流,同时还提供了灵活性,可以按自定义方式组合这些组件以创建您自己的管道。

1、RAG 组件
TypeModuleDescription
JudgerSKR JudgerJudging whether to retrieve using SKRmethod
RetrieverDense RetrieverBi-encoder models such as dpr, bge, e5, using faiss for search
BM25 RetrieverSparse retrieval method based on Lucene
Bi-Encoder RerankerCalculate matching score using bi-Encoder
Cross-Encoder RerankerCalculate matching score using cross-encoder
RefinerExtractive RefinerRefine input by extracting important context
Abstractive RefinerRefine input through seq2seq model
LLMLingua RefinerLLMLingua-series prompt compressor
SelectiveContext RefinerSelective-Context prompt compressor
GeneratorEncoder-Decoder GeneratorEncoder-Decoder model, supporting Fusion-in-Decoder (FiD)
Decoder-only GeneratorNative transformers implementation
FastChat GeneratorAccelerate with [FastChat](
vllm GeneratorAccelerate with vllm

2、管道

参考有关检索增强生成的调查,我们根据推理路径将 RAG 方法分为四类。

  • 顺序:RAG 过程的顺序执行,如查询-(检索前)-检索器-(检索后)生成器
  • 条件:针对不同类型的输入查询实现不同的路径
  • 分支:并行执行多个路径,合并每个路径的响应
  • 循环:迭代执行检索和生成

在每个类别中,我们都实现了相应的通用流程,部分流程还有相应的工作底稿。

TypeModuleDescription
SequentialSequential PipelineLinear execution of query, supporting refiner, reranker
ConditionalConditional PipelineWith a judger module, distinct execution paths for various query types
BranchingREPLUG PipelineGenerate answer by integrating probabilities in multiple generation paths
SuRe PipelineRanking and merging generated results based on each document
LoopIterative PipelineAlternating retrieval and generation
Self-Ask PipelineDecompose complex problems into subproblems using self-ask
Self-RAG PipelineAdaptive retrieval, critique, and generation
FLARE PipelineDynamic retrieval during the generation process

四、支持方法🤖

我们实施了 12 部作品,其一致设定如下:


对于开源方法,我们利用我们的框架实现了它们的流程。对于作者没有提供源代码的方法,我们会尽量遵循原论文中的方法进行实现。

对于某些方法特有的必要设置和超参数,我们在具体设置栏中进行了记录。更多详细信息,请查阅我们的代码

需要注意的是,为了确保一致性,我们使用了统一的设置。但是,此设置可能与该方法的原始设置不同,导致结果与原始结果有所不同。

MethodTypeNQ (EM)TriviaQA (EM)Hotpotqa (F1)2Wiki (F1)PopQA (F1)WebQA(EM)Specific setting
Naive GenerationSequential22.655.728.433.921.718.8
Standard RAGSequential35.158.935.321.036.715.7
AAR-contriever-kiltSequential30.156.833.419.836.116.1
LongLLMLinguaSequential32.259.237.525.038.717.5Compress Ratio=0.5
RECOMP-abstractiveSequential33.156.437.532.439.920.2
Selective-ContextSequential30.555.634.418.533.517.3Compress Ratio=0.5
Ret-RobustSequential42.968.235.843.457.233.7Use LLAMA2-13B with trained lora
SuReBranching37.153.233.420.648.124.2Use provided prompt
REPLUGBranching28.957.731.221.127.820.2
SKRConditional25.555.929.828.524.518.6Use infernece-time training data
Self-RAGLoop36.438.229.625.132.721.9Use trained selfrag-llama2-7B
FLARELoop22.555.828.033.920.720.2
Iter-Retgen, ITRGLoop36.860.138.321.637.918.2

五、支持数据集📓

我们收集并处理了 35 个在 RAG 研究中广泛使用的数据集,并对其进行了预处理,以确保格式一致,方便使用。对于某些数据集(例如 Wiki-asp),我们根据社区内常用的方法对其进行了调整,以适应 RAG 任务的要求。

对于每个数据集,我们将每个分割保存为一个jsonl文件,每行是一个字典,如下所示:

{
  'id': str,
  'question': str,
  'golden_answers': List[str],
  'metadata': dict
}

以下是数据集列表以及相应的样本大小:

TaskDataset NameKnowledge Source# Train# Dev# Test
QANQwiki79,1688,7573,610
QATriviaQAwiki & web78,7858,83711,313
QAPopQAwiki//14,267
QASQuADwiki87,59910,570/
QAMSMARCO-QAweb808,731101,093/
QANarrativeQAbooks and story32,7473,46110,557
QAWikiQAwiki20,3602,7336,165
QAWebQuestionsGoogle Freebase3,778/2,032
QAAmbigQAwiki10,0362,002/
QASIQA-33,4101,954/
QACommenseQA-9,7411,221/
QABoolQwiki9,4273,270/
QAPIQA-16,1131,838/
QAFermiwiki8,0001,0001,000
multi-hop QAHotpotQAwiki90,4477,405/
multi-hop QA2WikiMultiHopQAwiki15,00012,576/
multi-hop QAMusiquewiki19,9382,417/
multi-hop QABambooglewiki//125
Long-form QAASQAwiki4,353948/
Long-form QAELI5Reddit272,6341,507/
Open-Domain SummarizationWikiASPwiki300,63637,04637,368
multiple-choiceMMLU-99,8421,53114,042
multiple-choiceTruthfulQAwiki/817/
multiple-choiceHellaSWAGActivityNet39,90510,042/
multiple-choiceARC-3,3708693,548
multiple-choiceOpenBookQA-4,957500500
Fact VerificationFEVERwiki104,96610,444/
Dialog GenerationWOWwiki63,7343,054/
Entity LinkingAIDA CoNll-yagoFreebase & wiki18,3954,784/
Entity LinkingWNEDWiki/8,995/
Slot FillingT-RExDBPedia2,284,1685,000/
Slot FillingZero-shot REwiki147,9093,724/

六、其他常见问题解答 🙌


2024-05-24(五)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

编程乐园

请我喝杯伯爵奶茶~!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值