Last translated: 12 Jun 2025

Translation Not Available Yet

This repository's README hasn't been translated yet. Once translated, it will be easier to read and understand in your native language (中文).

Once translated, you'll be able to submit a PR to the repository.

Once translated, you'll be able to submit a PR to the repository.

DeepEval Logo

LLM 评估框架

discord-invite

文档 | 指标与功能 | 快速开始 | 集成方案 | DeepEval 平台

GitHub release Try Quickstart in Colab License

Deutsch | Español | français | 日本語 | 한국어 | Português | Русский | 中文

DeepEval 是一个简单易用的开源 LLM 评估框架,专为测试和评估大语言模型系统而设计。它类似于 Pytest,但专注于对 LLM 输出进行单元测试。DeepEval 融合了最新研究成果,基于 G-Eval、幻觉度、答案相关性、RAGAS 等指标来评估 LLM 输出,这些评估使用 LLM 和各种其他 NLP 模型在您的本地机器上运行

无论您的 LLM 应用是 RAG 管道、聊天机器人、AI 代理,还是通过 LangChain 或 LlamaIndex 实现,DeepEval 都能满足需求。借助它,您可以轻松确定最佳模型、提示和架构,以改进 RAG 管道、代理工作流,防止提示漂移,甚至自信地从 OpenAI 过渡到托管自己的 Deepseek R1。

[!IMPORTANT] 需要一个地方来存放您的 DeepEval 测试数据 🏡❤️?注册 DeepEval 平台,比较 LLM 应用的不同迭代版本,生成并分享测试报告等。

演示 GIF

想讨论 LLM 评估、需要帮助选择指标,或者只是打个招呼?加入我们的 Discord。


🔥 指标与功能

🥳 您现在可以直接在 Confident AI 的基础设施上共享 DeepEval 的测试结果

  • 支持端到端和组件级的 LLM 评估。
  • 提供大量现成的 LLM 评估指标(均附有解释),由您选择的任何 LLM、统计方法或在本地机器上运行的 NLP 模型驱动:
    • G-Eval
    • DAG(深度无环图
    • RAG 指标:
      • 答案相关性
      • 忠实度
      • 上下文召回率
      • 上下文精确度
      • 上下文相关性
      • RAGAS
    • 代理指标:
      • 任务完成度
      • 工具正确性
    • 其他指标:
      • 幻觉度
      • 摘要质量
      • 偏见
      • 毒性
    • 对话指标:
      • 知识保留
      • 对话完整性
      • 对话相关性
      • 角色一致性
    • 等等。
  • 构建自定义指标,自动集成到 DeepEval 生态系统中。
  • 生成用于评估的合成数据集。
  • 无缝集成到任何 CI/CD 环境中。
  • 通过几行代码对 LLM 应用进行红队测试,检测 40+ 种安全漏洞,包括:
    • 毒性
    • 偏见
    • SQL 注入
    • 等等,使用 10+ 种高级攻击增强策略,如提示注入。
  • 轻松在流行的 LLM 基准测试中比较任何 LLM,代码不超过 10 行,包括:
    • MMLU
    • HellaSwag
    • DROP
    • BIG-Bench Hard
    • TruthfulQA
    • HumanEval
    • GSM8K
  • 100% 与 Confident AI 集成,实现完整的评估生命周期:
    • 在云端整理/标注评估数据集
    • 使用数据集对 LLM 应用进行基准测试,并与之前的迭代进行比较,实验哪些模型/提示效果最佳
    • 微调指标以获得自定义结果
    • 通过 LLM 跟踪调试评估结果
    • 监控和评估产品中的 LLM 响应,利用真实数据改进数据集
    • 重复直至完美

[!NOTE] Confident AI 是 DeepEval 平台。在此创建账户


🔌 集成方案


🚀 快速开始

假设您的 LLM 应用是一个基于 RAG 的客户支持聊天机器人;以下是 DeepEval 如何帮助测试您构建的内容。

安装

pip install -U deepeval

创建账户(强烈推荐)

使用 deepeval 平台可以在云端生成可共享的测试报告。它是免费的,无需额外代码设置,我们强烈建议尝试一下。

要登录,运行:

deepeval login

按照 CLI 中的说明创建账户,复制您的 API 密钥,并将其粘贴到 CLI 中。所有测试用例将自动记录(有关数据隐私的更多信息,请参见此处)。

编写第一个测试用例

创建一个测试文件:

touch test_chatbot.py

打开 test_chatbot.py,编写第一个测试用例,使用 DeepEval 进行端到端评估,将您的 LLM 应用视为黑盒:

import pytest
from deepeval import assert_test
from deepeval.metrics import GEval
from deepeval.test_case import LLMTestCase, LLMTestCaseParams

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?",
        # Replace this with the actual output from your LLM application
        actual_output="You have 30 days to get a full refund at no extra cost.",
        expected_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 中运行 test_chatbot.py

deepeval test run test_chatbot.py

恭喜!您的测试用例应该已通过 ✅ 让我们分解一下发生了什么。

  • 变量 input 模拟用户输入,actual_output 是您的应用基于此输入应该输出的占位符。
  • 变量 expected_output 表示给定 input 的理想答案,GEvaldeepeval 提供的一个基于研究的指标,用于以类似人类的准确性评估您的 LLM 输出。
  • 在此示例中,指标 criteria 是基于提供的 expected_outputactual_output 的正确性。
  • 所有指标分数范围为 0 - 1,threshold=0.5 阈值最终决定您的测试是否通过。

阅读我们的文档 了解更多关于运行端到端评估的选项、如何使用其他指标、创建自定义指标,以及如何与 LangChain 和 LlamaIndex 等其他工具集成的教程。


评估嵌套组件

如果您希望评估 LLM 应用中的各个组件,您需要运行组件级评估——这是评估 LLM 系统中任何组件的强大方法。

只需使用 @observe 装饰器跟踪 LLM 调用、检索器、工具调用和代理等“组件”,即可在组件级别应用指标。使用 deepeval 进行跟踪是非侵入性的(了解更多此处),帮助您避免仅为评估而重写代码库:

from deepeval.tracing import observe, update_current_span
from deepeval.test_case import LLMTestCase
from deepeval.dataset import Golden
from deepeval.metrics import GEval
from deepeval import evaluate

correctness = GEval(name="Correctness", criteria="Determine if the 'actual output' is correct based on the 'expected output'.", evaluation_params=[LLMTestCaseParams.ACTUAL_OUTPUT, LLMTestCaseParams.EXPECTED_OUTPUT])

@observe(metrics=[correctness])
def inner_component():
    # Component can be anything from an LLM call, retrieval, agent, tool use, etc.
    update_current_span(test_case=LLMTestCase(input="...", actual_output="..."))
    return

@observe
def llm_app(input: str):
    inner_component()
    return

evaluate(observed_callback=llm_app, goldens=[Golden(input="Hi!")])

您可以在此了解有关组件级评估的一切。


不使用 Pytest 集成进行评估

或者,您可以在不使用 Pytest 的情况下进行评估,这更适合笔记本环境。

from deepeval import evaluate
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?",
    # 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."]
)
evaluate([test_case], [answer_relevancy_metric])

使用独立指标

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?",
    # 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."]
)

answer_relevancy_metric.measure(test_case)
print(answer_relevancy_metric.score)
# All metrics also offer an explanation
print(answer_relevancy_metric.reason)

请注意,有些指标适用于 RAG 管道,而其他指标适用于微调。请确保使用我们的文档为您的用例选择合适的指标。

批量评估数据集/测试用例

在 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])
# Run this in the CLI, you can also add an optional -n flag to run tests in parallel
deepeval test run test_<filename>.py -n 4

或者,尽管我们推荐使用 deepeval test run,但您也可以在不使用 Pytest 集成的情况下评估数据集/测试用例:

from deepeval import evaluate
...

evaluate(dataset, [answer_relevancy_metric])
# or
dataset.evaluate([answer_relevancy_metric])

使用 Confident AI 进行 LLM 评估

完整的 LLM 评估生命周期只能通过 DeepEval 平台 实现。它允许您:

  1. 在云端整理/标注评估数据集
  2. 使用数据集对 LLM 应用进行基准测试,并与之前的迭代进行比较,实验哪些模型/提示效果最佳
  3. 微调指标以获得自定义结果
  4. 通过 LLM 跟踪调试评估结果
  5. 监控和评估产品中的 LLM 响应,利用真实数据改进数据集
  6. 重复直至完美

Confident AI 上的所有内容,包括如何使用 Confident,都可在此处找到。

首先,从 CLI 登录:

deepeval login

按照说明登录,创建您的账户,并将 API 密钥粘贴到 CLI 中。

现在,再次运行您的测试文件:

deepeval test run test_chatbot.py

测试运行完成后,您应该在 CLI 中看到一个链接。将其粘贴到浏览器中查看结果!

演示 GIF


贡献

请阅读 CONTRIBUTING.md 了解我们的行为准则,以及向我们提交拉取请求的流程。


路线图

功能:

  • 与 Confident AI 集成
  • 实现 G-Eval
  • 实现 RAG 指标
  • 实现对话指标
  • 评估数据集创建
  • 红队测试
  • DAG 自定义指标
  • 护栏

作者

由 Confident AI 的创始人构建。如有任何问题,请联系 [email protected]


许可证

DeepEval 采用 Apache 2.0 许可证 - 详情请见 LICENSE.md 文件。