🚀 RAG/LLM 评估器 - DeepEval¶
本代码教程展示了如何轻松地将 DeepEval 与 LlamaIndex 集成。DeepEval 可以轻松地对你的 RAG/LLM 进行单元测试。
你可以在这里阅读更多关于 DeepEval 框架的信息:https://docs.confident-ai.com/docs/getting-started
欢迎在 GitHub 上查看我们的仓库:https://github.com/confident-ai/deepeval
设置和安装¶
我们建议通过 pip 进行设置和安装!
In [ ]
已复制!
!pip install -q -q llama-index
!pip install -U -q deepeval
!pip install -q -q llama-index !pip install -U -q deepeval
此步骤是可选的,仅当你需要一个服务器托管的仪表板时才执行!(嘘,我觉得你应该试试!)
In [ ]
已复制!
!deepeval login
!deepeval login
步骤 1 - 设置你的 LlamaIndex 应用程序¶
In [ ]
已复制!
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader
# Read LlamaIndex's quickstart on more details, you will need to store your data in "YOUR_DATA_DIRECTORY" beforehand
documents = SimpleDirectoryReader("YOUR_DATA_DIRECTORY").load_data()
index = VectorStoreIndex.from_documents(documents)
rag_application = index.as_query_engine()
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader # 阅读 LlamaIndex 快速入门了解更多细节,你需要事先将你的数据存储在 "YOUR_DATA_DIRECTORY" 中 documents = SimpleDirectoryReader("YOUR_DATA_DIRECTORY").load_data() index = VectorStoreIndex.from_documents(documents) rag_application = index.as_query_engine()
步骤 2 - 使用 DeepEval 的 RAG/LLM 评估器¶
DeepEval 开箱即用地提供了 6 种评估器,其中一些用于 RAG,一些直接用于 LLM 输出(尽管也适用于 RAG)。让我们试试忠实度评估器(用于评估 RAG 中的幻觉)
In [ ]
已复制!
from deepeval.integrations.llamaindex import DeepEvalFaithfulnessEvaluator
# An example input to your RAG application
user_input = "What is LlamaIndex?"
# LlamaIndex returns a response object that contains
# both the output string and retrieved nodes
response_object = rag_application.query(user_input)
evaluator = DeepEvalFaithfulnessEvaluator()
evaluation_result = evaluator.evaluate_response(
query=user_input, response=response_object
)
print(evaluation_result)
from deepeval.integrations.llamaindex import DeepEvalFaithfulnessEvaluator # RAG 应用程序的示例输入 user_input = "What is LlamaIndex?" # LlamaIndex 返回一个包含输出字符串和检索到的节点 # 的响应对象 response_object = rag_application.query(user_input) evaluator = DeepEvalFaithfulnessEvaluator() evaluation_result = evaluator.evaluate_response( query=user_input, response=response_object ) print(evaluation_result)
评估器完整列表¶
以下是你可以从 deepeval
导入所有 6 种评估器的方法
from deepeval.integrations.llama_index import (
DeepEvalAnswerRelevancyEvaluator,
DeepEvalFaithfulnessEvaluator,
DeepEvalContextualRelevancyEvaluator,
DeepEvalSummarizationEvaluator,
DeepEvalBiasEvaluator,
DeepEvalToxicityEvaluator,
)
有关所有评估器定义以及了解如何将其与 DeepEval 的测试套件集成,请点击此处。