Ragas 真实性评估
在本练习中,您将评估您在第 1 章末创建的 RAG 架构的真实性。该链已为您重新定义,并通过变量 chain 提供。
您将使用给定的 query、该链的输出以及检索到的结果,借助 ragas 框架来评估真实性。
所需的类已为您导入。
本练习是课程的一部分
使用 LangChain 的 Retrieval Augmented Generation (RAG)
练习说明
- 使用提供的
query查询retriever,并用列表推导式从每个检索到的文档中提取文档文本。 - 定义一个
ragas的真实性评估链。 - 评估可用的 RAG
chain的真实性;您需要调用该链以生成答案。
交互式实操练习
通过完成这段示例代码来试试这个练习。
from ragas.metrics import faithfulness
# Query the retriever using the query and extract the document text
query = "How does RAG improve question answering with LLMs?"
retrieved_docs = [doc.____ for doc in retriever.____(____)]
# Define the faithfulness chain
faithfulness_chain = ____(____, llm=llm, embeddings=embeddings)
# Evaluate the faithfulness of the RAG chain
eval_result = ____({
"question": ____,
"answer": ____.____(query),
"contexts": ____
})
print(eval_result)