大模型测评从入门到精通-初识DeepEval

📅 2026/6/22 11:13:45
大模型测评从入门到精通-初识DeepEval
文章目录初识DeepEval1.1 为什么需要 DeepEvalDeepEval 是什么DeepEval vs 传统测试框架1.2 安装 DeepEval配置 LLM 提供商登录 Confident AI可选但推荐1.3 快速体验5 分钟上手初识DeepEval1.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 传统测试框架特性传统测试 (Pytest)DeepEval测试对象确定性函数概率性 LLM 输出通过标准精确匹配语义相似度 (0-1 分数)评估维度单一 (对/错)多维 (相关性、忠实度、安全性…)需要 LLM否是 (作为评判者)适用场景传统软件RAG、Agent、Chatbot1.2 安装 DeepEval# 创建虚拟环境推荐python-mvenv deepeval_envsourcedeepeval_env/bin/activate# Linux/Macdeepeval_env\Scripts\activate# Windows# 安装 DeepEvalpipinstall-Udeepeval# 验证安装deepeval--version配置 LLM 提供商DeepEval 需要 LLM 来执行评估LLM-as-a-Judge。默认使用 OpenAI# 设置 OpenAI API KeyexportOPENAI_API_KEYsk-your-key-here# 或者使用 Azure OpenAIdeepeval set-azure-openai\--openai-endpointhttps://your-resource.openai.azure.com/\--openai-api-keyyour-key\--deployment-nameyour-deployment\--openai-api-version2024-02-01# 使用本地 Ollama 模型免费deepeval set-ollama llama3.2# 使用其他本地模型 (LM Studio, vLLM 等)deepeval set-local-model\--model-nameyour-model\--base-urlhttp://localhost:1234/v1/\--api-keyyour-key登录 Confident AI可选但推荐# 注册账号并登录评估结果将同步到云端deepeval login1.3 快速体验5 分钟上手创建你的第一个测试文件test_quickstart.py#!/usr/bin/env python# -*- coding: utf-8 -*- Date : 2026/6/22 File : demo-firstDemo.py Author : liwei68 Description : fromdeepevalimportassert_testfromdeepeval.test_caseimportLLMTestCasefromdeepeval.test_case.llm_test_caseimportSingleTurnParamsfromdeepeval.metrics.g_eval.g_evalimportGEvalfromdeepeval.modelsimportOllamaModel# 使用本地 Ollama 模型进行评估llm_modelOllamaModel(modeldeepseek-r1:14b)deftest_correctness(): 测试 LLM 回答的正确性 场景用户问了一个医学问题我们检查 AI 的回答是否正确 correctness_metricGEval(nameCorrectness,criteriaDetermine if the actual output is correct based on the expected output.,evaluation_params[SingleTurnParams.ACTUAL_OUTPUT,SingleTurnParams.EXPECTED_OUTPUT],threshold0.5,modelllm_model# 使用本地模型)test_caseLLMTestCase(inputI have a persistent cough and fever. Should I be worried?,actual_outputA persistent cough and fever could be a viral infection or something more serious. See a doctor if symptoms worsen or dont improve in a few days.,expected_outputA 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])运行测试运行deepevaltestrun test_quickstart.py#以下为运行结果testsession startscollecting... collected1item demo-firstDemo.py::test_correctness PASSED[100%]Running teardown with pytest sessionfinish...1passedin59.14sProcess finished withexitcode0看到绿色的 ✅ 了吗恭喜你你的第一个 LLM 评估测试通过了划重点assert_test()就像 pytest 的assert但专门用于 LLM 测试想了解 pytest 看我 pytest 的帖子threshold0.5表示分数 0.5 就算通过你可以根据需求调整第一次运行可能需要几秒因为 DeepEval 在后台调用 LLM 进行评分常见坑点⚠️坑 1本地模型需要 7B 以上至少我第一次用 qianwen0.6b 调试好久发现是模型太小不适合做模型的测评