全面揭秘:Llama数据集深度解析

2023年12月07日 由 alex 发表 385 0

在构建生产用的 RAG(可重构聚合生成器)时,一大难题是评估。不同于传统的软件系统,LLM(大型语言模型)系统和更广泛意义上的 ML(机器学习)系统是旨在模拟噪声化真实世界信号的随机黑箱。这意味着开发人员无法轻松定义单元测试来断定确定性行为——可能总有一些输入会导致错误。因为开发人员不太清楚特定输入会产生什么结果,他们需要定义一个反映其生产用例的评估数据集,并使用一套评估指标来评估他们的系统。


但我们越来越发现,定义正确的评估数据集很难而且依赖于具体用例。在学术基准测试上进行评估,如 BEIR 和 HotpotQA,通常无法推广到特定的用例。某些在特定数据领域(例如,SEC 报告)表现良好的参数可能会在其他方面(例如,研究论文)失败。


这就是我们创造 Llama 数据集的灵感所在。我们没有强制规定你必须使用的数据,而是决定创建一个中心,你可以轻松挑选和选择适合你用例的正确数据集!


1


概览


包括LlamaHub上的一套Llama数据集、一个用于计算数据集上指标的RagEvaluatorPack辅助工具,以及可以单独使用的相应数据集抽象。


  • 要使用Llama数据集,请从LlamaHub下载并运行我们的RagEvaluatorPack(或运行你自己的评估模块)。
  • 要生成Llama数据集,请定义一个LabelledRagDataset,并附带一组LabelledRagDataExample对象。
  • 要贡献一个Llama数据集,请向LlamaHub提交一张“数据卡”,并将你的原始数据集文件上传到我们的llama_datasets仓库。


2


示例演练


让我们逐步了解使用/贡献 Llama 数据集的不同步骤。


下载并使用羊驼数据集 


下载数据集很简单,执行以下命令(这里我们下载的是Paul Graham)。


from llama_index.llama_dataset import download_llama_dataset
# download and install dependencies
rag_dataset, documents = download_llama_dataset(
    "PaulGrahamEssayDataset", "./paul_graham"
)


这会下载一个rag_dataset,它包含了QA对(+参考上下文)和文档,这些文档是源文档语料库。


让我们用to_pandas()来检查rag_dataset:


3


在RAG数据集上生成预测是直截了当的。你可以轻松地将任何查询引擎插入到amake_predictions_with中。


from llama_index import VectorStoreIndex
# a basic RAG pipeline, uses service context defaults
index = VectorStoreIndex.from_documents(documents=documents)
query_engine = index.as_query_engine()
# generate prediction dataset
prediction_dataset = await rag_dataset.amake_predictions_with(
    query_engine=query_engine, show_progress=True
)


prediction_dataset 是一个 RagPredictionDataset 对象,其样式如下:


4


鉴于 rag_dataset 和 prediction_dataset,你可以使用我们的评估模块在各种指标(例如:忠实度、正确性、相关性)上衡量性能。


for example, prediction in tqdm.tqdm(
    zip(rag_dataset.examples, prediction_dataset.predictions)
):
    correctness_result = judges["correctness"].evaluate(
        query=example.query,
        response=prediction.response,
        reference=example.reference_answer,
    )


为了消除编写所有这些评估模块的样板代码,我们还提供了一个LlamaPack,它将为你完成所有这些工作!


from llama_index.llama_pack import download_llama_pack
RagEvaluatorPack = download_llama_pack("RagEvaluatorPack", "./pack")
rag_evaluator = RagEvaluatorPack(
    query_engine=query_engine, rag_dataset=rag_dataset
)
benchmark_df = await rag_evaluator.arun()


生成Llama数据集


你可以使用我们的LabelledRagDataExample和LabelledRagDataset抽象来创建你自己的数据集。


以下是手动添加一个示例的例子。


from llama_index.llama_dataset import (
    LabelledRagDataExample,
    CreatedByType,
    CreatedBy,
)
# constructing a LabelledRagDataExample
query = "This is a test query, is it not?"
query_by = CreatedBy(type=CreatedByType.AI, model_name="gpt-4")
reference_answer = "Yes it is."
reference_answer_by = CreatedBy(type=CreatedByType.HUMAN)
reference_contexts = ["This is a sample context"]
rag_example = LabelledRagDataExample(
    query=query,
    query_by=query_by,
    reference_contexts=reference_contexts,
    reference_answer=reference_answer,
    reference_answer_by=reference_answer_by,
)


from llama_index.llama_dataset.rag import LabelledRagDatasetimport LabelledRagDataset
rag_dataset = LabelledRagDataset(examples=[rag_example, rag_example_2])


你还可以用 GPT-4 在任何文档语料库上合成生成一个数据集:


# generate questions against chunks
from llama_index.llama_dataset.generator import RagDatasetGenerator
from llama_index.llms import OpenAI
from llama_index import ServiceContext
# set context for llm provider
gpt_4_context = ServiceContext.from_defaults(
    llm=OpenAI(model="gpt-4", temperature=0.3)
)
# instantiate a DatasetGenerator
dataset_generator = RagDatasetGenerator.from_documents(
    documents,
    service_context=gpt_4_context,
    num_questions_per_chunk=2,  # set the number of questions per nodes
    show_progress=True,
)



文章来源:https://medium.com/llamaindex-blog/introducing-llama-datasets-aadb9994ad9e
欢迎关注ATYUN官方公众号
商务合作及内容投稿请联系邮箱:bd@atyun.com
评论 登录
热门职位
Maluuba
20000~40000/月
Cisco
25000~30000/月 深圳市
PilotAILabs
30000~60000/年 深圳市
写评论取消
回复取消