ragas官方文档中文版(二十六)
·
评估一个简单的 RAG 系统
在本教程中,我们将编写一个简单的评估管道来评估 RAG(检索增强生成,Retrieval-Augmented Generation)系统。本教程结束时,您将学会如何使用评估驱动开发(evaluation-driven development)来评估和迭代 RAG 系统。

我们将从编写一个简单的 RAG 系统开始,该系统从语料库中检索相关文档,并使用 LLM 生成答案。
python -m ragas_examples.rag_eval.rag
接下来,我们将为 RAG 系统编写几个示例查询和预期输出,然后将它们转换为 CSV 文件。
import pandas as pd
samples = [
{"query": "What is Ragas 0.3?", "grading_notes": "- Ragas 0.3 is a library for evaluating LLM applications."},
{"query": "How to install Ragas?", "grading_notes": "- install from source - install from pip using ragas[examples]"},
{"query": "What are the main features of Ragas?", "grading_notes": "organised around - experiments - datasets - metrics."}
]
pd.DataFrame(samples).to_csv("datasets/test_dataset.csv", index=False)
为了评估 RAG 系统的性能,我们将定义一个基于 LLM 的指标,该指标将 RAG 系统的输出与评分标准(grading_notes)进行比较,并据此输出通过(pass)或失败(fail)。
from ragas.metrics import DiscreteMetric
my_metric = DiscreteMetric(
name="correctness",
prompt = "Check if the response contains points mentioned from the grading notes and return 'pass' or 'fail'.\nResponse: {response} Grading Notes: {grading_notes}",
allowed_values=["pass", "fail"],
)
接下来,我们将编写实验循环,在测试数据集上运行 RAG 系统,使用该指标进行评估,并将结果存储在 CSV 文件中。
@experiment()
async def run_experiment(row):
response = rag_client.query(row["query"])
score = my_metric.score(
llm=llm,
response=response.get("answer", " "),
grading_notes=row["grading_notes"]
)
experiment_view = {
**row,
"response": response.get("answer", ""),
"score": score.value,
"log_file": response.get("logs", " "),
}
return experiment_view
现在,每当您对 RAG 管道进行修改时,都可以运行实验,观察它如何影响 RAG 的性能。
端到端运行示例
- 设置 OpenAI API 密钥
export OPENAI_API_KEY="your_openai_api_key"
- 运行评估
python -m ragas_examples.rag_eval.evals
完成!您已成功使用 Ragas 运行了首次评估。现在可以通过打开 experiments/experiment_name.csv 文件来查看结果。
更多推荐


所有评论(0)