Ragas 情境精確度評估
為了開始你的 RAG 評估之旅,你會先用 ragas 框架評估 情境精確度(context precision) 這個 RAG 指標。回想一下,情境精確度基本上是在衡量擷取到的文件與輸入查詢的相關程度。
在這個練習中,我們提供了一個輸入查詢、由 RAG 應用程式擷取到的文件,以及標準答案(由人類專家判定最適合被擷取的文件)。你將先在這些字串上計算情境精確度,下一題再評估實際的 LangChain RAG chain。
為了簡潔,RAG 應用程式產生的文字已存入變數 model_response。
本練習屬於課程
使用 LangChain 的 Retrieval Augmented Generation(RAG)
練習說明
- 定義一個
ragas的情境精確度 chain。 - 針對提供給輸入查詢的擷取文件評估情境精確度;
"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']}")