综合介绍
DeepEval是一个简单易用的开源LLM评估框架,用于评估和测试大语言模型系统。它类似于Pytest,但专注于LLM输出的单元测试。DeepEval结合最新的研究成果,通过G-Eval、幻觉检测、答案相关性、RAGAS等指标,对LLM输出进行评估。无论你的应用是通过RAG实现还是微调,DeepEval都能帮助你确定最佳超参数,从而提升模型性能。此外,它还可以生成合成数据集、无缝集成到任何CI/CD环境中,并提供40多种安全漏洞的红队测试功能。该框架还与Confident AI完全集成,支持整个平台的评估生命周期。
功能列表
- 多种LLM评估指标,如G-Eval、幻觉检测、答案相关性、RAGAS等
- 支持自定义评估指标,并自动集成到DeepEval生态系统中
- 生成合成数据集用于评估
- 无缝集成到任何CI/CD环境中
- 红队测试功能,检测40多种安全漏洞
- 基准测试,支持MMLU、HellaSwag、DROP等多个基准
- 与Confident AI完全集成,支持从数据集创建到评估结果调试的整个评估生命周期
使用帮助
安装
你可以通过pip安装DeepEval:
pip install -U deepeval
推荐创建一个账户以生成可共享的云端测试报告:
deepeval login
撰写测试用例
创建一个测试文件:
touch test_chatbot.py
在test_chatbot.py
中编写第一个测试用例:
import pytest
from deepeval import assert_test
from deepeval.metrics import AnswerRelevancyMetric
from deepeval.test_case import LLMTestCase
def test_case():
correctness_metric = GEval(
name="Correctness",
criteria="Determine if the 'actual output' is correct based on the 'expected output'.",
evaluation_params=[LLMTestCaseParams.ACTUAL_OUTPUT, LLMTestCaseParams.EXPECTED_OUTPUT],
threshold=0.5
)
test_case = LLMTestCase(
input="What if these shoes don't fit?",
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, [correctness_metric])
将你的OPENAI_API_KEY
设置为环境变量:
export OPENAI_API_KEY="..."
在CLI中运行测试文件:
deepeval test run test_chatbot.py
使用独立指标
DeepEval极具模块化,使得任何人都可以轻松使用其各项指标:
from deepeval.metrics import AnswerRelevancyMetric
from deepeval.test_case import LLMTestCase
answer_relevancy_metric = AnswerRelevancyMetric(threshold=0.7)
test_case = LLMTestCase(
input="What if these shoes don't fit?",
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."]
)
answer_relevancy_metric.measure(test_case)
print(answer_relevancy_metric.score)
print(answer_relevancy_metric.reason)
批量评估数据集
在DeepEval中,数据集只是测试用例的集合。以下是如何批量评估这些数据集:
import pytest
from deepeval import assert_test
from deepeval.metrics import HallucinationMetric, AnswerRelevancyMetric
from deepeval.test_case import LLMTestCase
from deepeval.dataset import EvaluationDataset
first_test_case = LLMTestCase(input="...", actual_output="...", context=["..."])
second_test_case = LLMTestCase(input="...", actual_output="...", context=["..."])
dataset = EvaluationDataset(test_cases=[first_test_case, second_test_case])
@pytest.mark.parametrize("test_case", dataset)
def test_customer_chatbot(test_case: LLMTestCase):
hallucination_metric = HallucinationMetric(threshold=0.3)
answer_relevancy_metric = AnswerRelevancyMetric(threshold=0.5)
assert_test(test_case, [hallucination_metric, answer_relevancy_metric])
在CLI中运行测试文件:
deepeval test run test_<filename>.py -n 4
使用Confident AI进行LLM评估
登录DeepEval平台:
deepeval login
运行测试文件:
deepeval test run test_chatbot.py
测试完成后,你将在CLI中看到一个链接,将其粘贴到浏览器中查看结果。