主题
DeepEval 为 AI 智能体与 LLM 应用提供单元测试。它为 LlamaIndex 用户提供了非常简单的接口来为 LLM 输出编写测试,帮助开发者在生产中捕捉破坏性变更。
DeepEval 提供了一套带有主张的响应度量框架,并且完全开源。
安装与配置
接入 DeepEval 很简单,无需任何配置。安装方式:
终端窗口
pip install -U deepeval
# Optional step: Login to get a nice dashboard for your tests later!
deepeval login安装完成后,你可以创建一个 test_rag.py 并开始编写测试。
test_rag.py
import pytest
from deepeval import assert_test
from deepeval.metrics import AnswerRelevancyMetric
from deepeval.test_case import LLMTestCase
def test_case():
answer_relevancy_metric = AnswerRelevancyMetric(threshold=0.5)
test_case = LLMTestCase(
input="What if these shoes don't fit?",
# Replace this with the actual output from your LLM application
actual_output="We offer a 30-day full refund at no extra costs.",
retrieval_context=[
"All customers are eligible for a 30 day full refund at no extra costs."
],
)
assert_test(test_case, [answer_relevancy_metric])随后可以像这样运行测试:
终端窗口
deepeval test run test_rag.py如果你已登录,就能在 deepeval 的仪表盘上分析评估结果:

指标类型
DeepEval 为 RAG 应用的单元测试提供了一套带主张的框架。它把评估拆分成测试用例,并为每个测试用例提供一批可自由选择的评估指标,包括:
- G-Eval
- Summarization
- Answer Relevancy
- Faithfulness
- Contextual Recall
- Contextual Precision
- Contextual Relevancy
- RAGAS
- Hallucination
- Bias
- Toxicity
DeepEval 把最新的研究成果融入它的评估指标。关于完整指标清单及其计算方式,可在这里了解更多。
为你的 LlamaIndex 应用评估 RAG
DeepEval 与 LlamaIndex 的 BaseEvaluator 类配合得很好。下面是把 DeepEval 的评估指标以 LlamaIndex 评估器形式使用的示例。
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader
from deepeval.integrations.llama_index import DeepEvalAnswerRelevancyEvaluator
# Read LlamaIndex's quickstart on more details
documents = SimpleDirectoryReader("YOUR_DATA_DIRECTORY").load_data()
index = VectorStoreIndex.from_documents(documents)
rag_application = index.as_query_engine()
# 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 = DeepEvalAnswerRelevancyEvaluator()随后可以像这样评估:
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 测试体系集成,请点击这里。