跳转到内容

使用DeepEval进行LLMs/RAG单元测试

DeepEval 为AI智能体和LLM驱动的应用程序提供单元测试。它为LlamaIndex用户提供了一个非常简单的界面来编写LLM输出测试,并帮助开发人员捕捉生产环境中的破坏性变更。

DeepEval 提供了一个有主见的框架来衡量响应,并且完全开源。

添加 DeepEval 非常简单且无需任何设置。安装方法如下:

Terminal window
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])

然后您可以按如下方式运行测试:

Terminal window
deepeval test run test_rag.py

如果您已登录,您将能够在 deepeval 的仪表板上分析评估结果:

Sample dashboard

DeepEval 提供了一个专为RAG应用单元测试设计的框架。它将评估分解为测试用例,并为每个测试用例提供一系列可自由选择的评估指标,包括:

  • G-Eval
  • 摘要
  • 答案相关性
  • 忠实度
  • 上下文记忆
  • 上下文精确度
  • 上下文相关性
  • RAGAS
  • 幻觉
  • 偏差
  • 毒性

DeepEval 将最新研究融入其评估指标中。您可以在此处了解更多关于完整指标列表及其计算方式的信息。

评估您的LlamaIndex应用程序的RAG系统

Section titled “Evaluating RAG for Your LlamaIndex Application”

DeepEval 与 LlamaIndex 的 BaseEvaluator 类无缝集成。以下是以 LlamaIndex 评估器形式使用 DeepEval 评估指标的示例。

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 测试套件集成,请点击此处。