ragas官方文档中文版(二十六)

ragas官方文档中文版(二十六) 评估一个简单的 RAG 系统在本教程中我们将编写一个简单的评估管道来评估 RAG检索增强生成Retrieval-Augmented Generation系统。本教程结束时您将学会如何使用评估驱动开发evaluation-driven development来评估和迭代 RAG 系统。我们将从编写一个简单的 RAG 系统开始该系统从语料库中检索相关文档并使用 LLM 生成答案。python -m ragas_examples.rag_eval.rag接下来我们将为 RAG 系统编写几个示例查询和预期输出然后将它们转换为 CSV 文件。importpandasaspd 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,indexFalse)为了评估 RAG 系统的性能我们将定义一个基于 LLM 的指标该指标将 RAG 系统的输出与评分标准grading_notes进行比较并据此输出通过pass或失败fail。fromragas.metricsimportDiscreteMetric my_metricDiscreteMetric(namecorrectness,promptCheck 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()asyncdefrun_experiment(row):responserag_client.query(row[query])scoremy_metric.score(llmllm,responseresponse.get(answer, ),grading_notesrow[grading_notes])experiment_view{**row,response:response.get(answer,),score:score.value,log_file:response.get(logs, ),}returnexperiment_view现在每当您对 RAG 管道进行修改时都可以运行实验观察它如何影响 RAG 的性能。端到端运行示例设置 OpenAI API 密钥export OPENAI_API_KEYyour_openai_api_key运行评估python -m ragas_examples.rag_eval.evals完成您已成功使用 Ragas 运行了首次评估。现在可以通过打开 experiments/experiment_name.csv 文件来查看结果。