Priya123z/llm-chatbot-eval-suite
GitHub: Priya123z/llm-chatbot-eval-suite
基于 pytest 的 LLM 聊天机器人评估套件,整合幻觉检测、忠实度评估、提示注入红队测试和 CI 质量门禁,解决非确定性 LLM 输出难以用传统断言验证的问题。
Stars: 0 | Forks: 0
# LLM 聊天机器人评估套件
[](https://github.com/Priya123z/llm-chatbot-eval-suite/actions/workflows/eval.yml)
[](https://python.org)
[](https://github.com/confident-ai/deepeval)
[](LICENSE)
一个基于 **pytest 风格的评估套件**,用于在 QA 工程知识库上构建的 RAG 问答聊天机器人。
测试内容:幻觉 · 忠实度 · 答案相关性 · 提示注入红队测试 · CI 质量门禁。
## 为什么会有这个项目
传统的测试断言不适用于 LLM 输出 —— 系统是非确定性的。本仓库演示了生产级的评估模式:
| 问题 | 本仓库中的解决方案 |
|---|---|
| "LLM 是否产生了幻觉?" | DeepEval `HallucinationMetric`(LLM-as-judge) |
| "答案是否基于上下文?" | DeepEval `FaithfulnessMetric` |
| "答案是否与问题相关?" | DeepEval `AnswerRelevancyMetric` |
| "它是否能抵御提示注入?" | `test_red_team.py` 中的 15 个红队测试用例 |
| "这个 PR 是否应该被拦截?" | `test_deepeval_metrics.py` 中确定性的 CI 门禁 |
| "哪个 prompt / 模型更好?" | 比较 2 个模型提供商的 Promptfoo 配置 |
## 测试内容
```
tests/
├── test_retrieval.py # RAG retrieval accuracy (7 tests)
├── test_answer_quality.py # Deterministic answer quality (8 tests)
├── test_red_team.py # Prompt injection + boundary inputs (12 tests)
└── test_deepeval_metrics.py # LLM-as-judge + CI gate (5 tests)
```
**共计 32 个测试** —— 30 个测试在 CI 中运行时无需任何 API 密钥;当配置了 `DEEPEVAL_API_KEY` 时,将运行 3 个使用 DeepEval 的测试。
## 快速开始
```
# 1. Clone 并安装
git clone https://github.com/Priya123z/llm-chatbot-eval-suite.git
cd llm-chatbot-eval-suite
pip install -r requirements.txt
# 2. 运行所有测试(无需 API key)
pytest tests/ -v
# 3. 使用 Groq(免费)运行以获取真实 LLM 回答
export GROQ_API_KEY=your_groq_key_here
pytest tests/ -v
# 4. 运行 Promptfoo 模型对比(使用 Groq 免费)
export GROQ_API_KEY=your_groq_key_here
npx promptfoo eval --config promptfoo/promptfooconfig.yaml
npx promptfoo view
```
**在此获取免费的 Groq 密钥:** https://console.groq.com(无需信用卡)
## 架构
```
llm-chatbot-eval-suite/
├── chatbot/
│ ├── rag_chatbot.py # Target under test — simple RAG chatbot
│ └── knowledge_base.csv # QA engineering knowledge base (20 topics)
├── tests/
│ ├── conftest.py # Shared fixtures + ground-truth QA pairs
│ ├── test_retrieval.py # Retrieval accuracy tests
│ ├── test_answer_quality.py # Deterministic quality assertions
│ ├── test_red_team.py # Prompt injection + boundary input tests
│ └── test_deepeval_metrics.py # LLM-as-judge + CI quality gate
├── promptfoo/
│ └── promptfooconfig.yaml # 2-model, 2-prompt comparison config
├── reports/ # Eval report outputs
└── .github/workflows/eval.yml # CI: quality gate + LLM-judge job
```
## 演示的核心概念
### 忠实度 vs 幻觉
| 指标 | 衡量内容 | 阈值 |
|---|---|---|
| **Faithfulness** | 答案是否基于上下文 | ≥ 0.7 |
| **Hallucination** | 答案是否与上下文矛盾 | ≤ 0.3 |
| **Answer Relevancy** | 答案是否切题 | ≥ 0.7 |
### CI 质量门禁
`test_deterministic_answer_quality_no_api` 测试始终在 CI 中运行,并在以下情况拦截合并:
- 聊天机器人返回的答案不再包含预期的关键字
- 响应时间超过 5 秒
### Promptfoo 模型比较
`promptfoo/promptfooconfig.yaml` 比较了以下内容:
- **2 种提示词风格:** 简洁 vs 详细的系统提示词
- **2 个模型提供商:** Groq Llama3 8B vs Llama3 70B
- **5 个测试用例**,包含 2 个安全/红队场景
## 费用
在 CI 中运行**完全免费** —— 所有确定性测试均使用 mock 推理模式。添加您的免费 Groq 密钥即可获得真实的 LLM 响应。DeepEval 的 LLM-as-judge 需要 OpenAI 密钥(可选)。
## 技术栈
- **[DeepEval](https://github.com/confident-ai/deepeval)** — LLM 评估指标
- **[Promptfoo](https://github.com/promptfoo/promptfoo)** — 提示词和模型比较
- **[LangChain](https://python.langchain.com)** — RAG 基础设施
- **[Groq](https://groq.com)** — 免费 LLM 推理 (Llama3)
- **[pytest](https://pytest.org)** — 测试运行器
- **[GitHub Actions](https://docs.github.com/en/actions)** — CI/CD
## 作者
**Priya Bhagoriya** — 高级 QA 工程师 / SDET
[LinkedIn](https://linkedin.com/in/priya-bhagoriya) · [作品集](https://Priya123z.github.io) · [GitHub](https://github.com/Priya123z)
标签:AI质量保证, CI质量门禁, Clair, DeepEval, LLM-as-Judge, LLM评估, Ollama, pocsuite3, Promptfoo, Pytest, Python, RAG评估, 大模型幻觉检测, 安全测试, 安全规则引擎, 忠实度评估, 提示注入, 攻击性安全, 无后门, 模型对比, 答案相关性, 聊天机器人测试, 软件测试, 逆向工具, 问答系统, 集群管理