文章目录
-
- 初识DeepEval
-
- 1.1 为什么需要 DeepEval?
-
- DeepEval 是什么?
- DeepEval vs 传统测试框架
- 1.2 安装 DeepEval
-
- 配置 LLM 提供商
- 登录 Confident AI(可选但推荐)
- 1.3 快速体验:5 分钟上手
初识DeepEval

1.1 为什么需要 DeepEval?
想象你开了一家餐厅,厨师(LLM)每天为客人做菜(生成回答)。你怎么知道菜做得好不好?
- 传统软件测试: 像检查汉堡里有没有放番茄一样简单直接 —— 输入 A,期望输出 B,对比即可。
- LLM 应用测试: 像品鉴一道法式料理 —— 需要评价味道、摆盘、创意、温度、食材新鲜度… 而且每次出品可能都有微妙差异。
这就是 DeepEval 存在的意义:它是专为 LLM 应用设计的"米其林评审团"。
DeepEval 是什么?
DeepEval 是一个开源的 LLM(大语言模型)评估框架,由 Confident AI 团队开发。它让你能够:
- 像写 Pytest 一样写 LLM 测试 —— 熟悉的 @pytest.mark.parametrize 语法
- 使用 50+ 开箱即用的评估指标 —— 从 RAG 到 Agent,从安全到多模态
- 在本地运行评估 —— 数据不出境,隐私有保障
- 一键集成 CI/CD —— 每次代码提交自动跑评估
- 生成合成测试数据 —— 没有数据集?没关系,AI 帮你造
- 进行红队安全测试 —— 模拟黑客攻击你的 AI 系统
DeepEval vs 传统测试框架
| 测试对象 | 确定性函数 | 概率性 LLM 输出 |
| 通过标准 | 精确匹配 | 语义相似度 (0-1 分数) |
| 评估维度 | 单一 (对/错) | 多维 (相关性、忠实度、安全性…) |
| 需要 LLM | 否 | 是 (作为评判者) |
| 适用场景 | 传统软件 | RAG、Agent、Chatbot |
1.2 安装 DeepEval
# 创建虚拟环境(推荐)
python -m venv deepeval_env
source deepeval_env/bin/activate # Linux/Mac
deepeval_env\\Scripts\\activate # Windows
# 安装 DeepEval
pip install -U deepeval
# 验证安装
deepeval –version
配置 LLM 提供商
DeepEval 需要 LLM 来执行评估(LLM-as-a-Judge)。默认使用 OpenAI:
# 设置 OpenAI API Key
export OPENAI_API_KEY="sk-your-key-here"
# 或者使用 Azure OpenAI
deepeval set-azure-openai \\
–openai-endpoint=https://your-resource.openai.azure.com/ \\
–openai-api-key=your-key \\
–deployment-name=your-deployment \\
–openai-api-version=2024-02-01
# 使用本地 Ollama 模型(免费!)
deepeval set-ollama llama3.2
# 使用其他本地模型 (LM Studio, vLLM 等)
deepeval set-local-model \\
–model-name=your-model \\
–base-url=http://localhost:1234/v1/ \\
–api-key=your-key
登录 Confident AI(可选但推荐)
# 注册账号并登录,评估结果将同步到云端
deepeval login
1.3 快速体验:5 分钟上手
创建你的第一个测试文件 test_quickstart.py:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
@Date : 2026/6/22
@File : demo-firstDemo.py
@Author : liwei68
@Description :
"""
from deepeval import assert_test
from deepeval.test_case import LLMTestCase
from deepeval.test_case.llm_test_case import SingleTurnParams
from deepeval.metrics.g_eval.g_eval import GEval
from deepeval.models import OllamaModel
# 使用本地 Ollama 模型进行评估
llm_model = OllamaModel(model="deepseek-r1:14b")
def test_correctness():
"""
测试 LLM 回答的正确性
场景:用户问了一个医学问题,我们检查 AI 的回答是否正确
"""
correctness_metric = GEval(
name="Correctness",
criteria="Determine if the 'actual output' is correct based on the 'expected output'.",
evaluation_params=[
SingleTurnParams.ACTUAL_OUTPUT,
SingleTurnParams.EXPECTED_OUTPUT
],
threshold=0.5,
model=llm_model # 使用本地模型
)
test_case = LLMTestCase(
input="I have a persistent cough and fever. Should I be worried?",
actual_output="A persistent cough and fever could be a viral infection or something more serious. See a doctor if symptoms worsen or don't improve in a few days.",
expected_output="A persistent cough and fever could indicate a range of illnesses, from a mild viral infection to more serious conditions like pneumonia or COVID-19. You should seek medical attention if your symptoms worsen, persist for more than a few days, or are accompanied by difficulty breathing, chest pain, or other concerning signs."
)
assert_test(test_case, [correctness_metric])
运行测试: 运行:
deepeval test run test_quickstart.py
#以下为运行结果
============================= test session starts ==============================
collecting ... collected 1 item
demo-firstDemo.py::test_correctness PASSED [100%]
Running teardown with pytest sessionfinish...
============================== 1 passed in 59.14s ==============================
Process finished with exit code 0
看到绿色的 ✅ 了吗?恭喜你,你的第一个 LLM 评估测试通过了!
划重点 🎯
常见坑点 ⚠️
- 坑 1 本地模型需要 7B 以上至少,我第一次用 qianwen:0.6b 调试好久发现是模型太小不适合做模型的测评





