开始使用免费开始使用

Ragas 上下文精确度评估

为了开始您的 RAG 评估之旅,您将先使用 ragas 框架评估 上下文精确度(context precision) 指标。回顾一下,上下文精确度用于衡量检索到的文档与输入查询的相关性有多高。

在本练习中,您将获得一个输入查询、由 RAG 应用检索出的文档,以及基于人类专家判断得出的真实答案(即最合适被检索的文档)。您将先基于这些字符串计算上下文精确度,然后在下一个练习中评估一个实际的 LangChain RAG 链。

为简洁起见,RAG 应用生成的文本已保存到变量 model_response 中。

本练习是课程的一部分

使用 LangChain 的 Retrieval Augmented Generation (RAG)

查看课程

练习说明

  • 定义一个 ragas 的上下文精确度链。
  • 评估给定输入查询所对应的已检索文档的上下文精确度;"ground_truth" 已经提供。

交互式实操练习

通过完成这段示例代码来试试这个练习。

from ragas.metrics import context_precision

# Define the context precision chain
context_precision_chain = ____(metric=____, llm=llm, embeddings=embeddings)

# Evaluate the context precision of the RAG chain
eval_result = ____({
  "question": "How does RAG enable AI applications?",
  "ground_truth": "RAG enables AI applications by integrating external data in generative models.",
  "contexts": [
    "RAG enables AI applications by integrating external data in generative models.",
    "RAG enables AI applications such as semantic search engines, recommendation systems, and context-aware chatbots."
  ]
})

print(f"Context Precision: {eval_result['context_precision']}")
编辑并运行代码