kokilamariyayi/llm-eval-redteam

GitHub: kokilamariyayi/llm-eval-redteam

一个自动化LLM红队评估框架,通过520+对抗性提示词在六大失败类别中测试模型安全性与鲁棒性,并利用MART迭代循环持续强化对抗性测试以发现深层漏洞。

Stars: 2 | Forks: 0

# 🔴 LLM 评估与红队框架 **针对大型语言模型 (LLM) 的自动化对抗性评估 pipeline —— 在 6 个失败类别中测试安全性、鲁棒性和一致性,并通过迭代的 MART prompt 强化进行加固。** [![Python](https://img.shields.io/badge/Python-3.10+-3776AB?style=for-the-badge&logo=python&logoColor=white)](https://python.org) [![DeepEval](https://img.shields.io/badge/DeepEval-Metrics-4A90D9?style=for-the-badge)](https://github.com/confident-ai/deepeval) [![Groq](https://img.shields.io/badge/Groq-LLaMA%203.3%2070B-F55036?style=for-the-badge&logo=groq&logoColor=white)](https://groq.com) [![LangSmith](https://img.shields.io/badge/LangSmith-Tracing-1C3C3C?style=for-the-badge&logo=langchain&logoColor=white)](https://smith.langchain.com) [![Streamlit](https://img.shields.io/badge/Streamlit-Dashboard-FF4B4B?style=for-the-badge&logo=streamlit&logoColor=white)](https://streamlit.io) [![License: MIT](https://img.shields.io/badge/License-MIT-yellow?style=for-the-badge)](LICENSE)
## 📊 评估结果 — 520 个对抗性 Prompt ![评估摘要](https://static.pigsec.cn/wp-content/uploads/repos/cas/f6/f6c5d82ff0f25e1236974e0e11eb28e4ae577764adcf78c11fd72874ebff5c13.png) | 类别 | 通过 | 失败 | 通过率 | |---|---|---|---| | 🧠 幻觉 | 100 | 0 | **100.0%** | | ☣️ 毒性 | 79 | 1 | **98.8%** | | 💉 Prompt 注入 | 75 | 5 | **93.8%** | | 🌀 上下文漂移 | 54 | 6 | **90.0%** | | 🔓 越狱 | 83 | 17 | **83.0%** | | ⚖️ 偏见 | 73 | 27 | **73.0%** | | **总计** | **464** | **56** | **89.2%** | - 🧪 跨越 6 个失败类别测试了 **520 个 prompt** - 📉 **10.8% 失败率** · 检测到 **40 个高严重性失败** - 🔁 经过 4 轮 MART 后,**违规率降低 36.4%** (22% → 14%) ## 🏗️ 架构 ``` ┌─────────────────────────────────────────────────────────────────────┐ │ main.py (CLI) │ │ --run-eval │ --run-mart │ --dashboard │ --summary │ └──────────────┬──────────────────────┬───────────────────────────────┘ │ │ ▼ ▼ ┌──────────────────────────┐ ┌──────────────────────────┐ │ pipeline/runner.py │ │ pipeline/mart_loop.py │ │ EvalRunner │◄─┤ MARTLoop │ │ (batch eval + metrics) │ │ (iterative hardening) │ └──────────────┬───────────┘ └────────────┬─────────────┘ │ │ ▼ ▼ ┌──────────────────────────────────────────────────────────────────┐ │ evaluators/ │ │ Hallucination │ Bias │ Jailbreak │ Prompt Injection │ │ │ Toxicity │ Contextual Drift (all extend BaseEvaluator) │ └──────────────┬───────────────────────────┬───────────────────────┘ │ │ ▼ ▼ ┌──────────────────────────┐ ┌──────────────────────────┐ │ Groq LLM (target) │ │ DeepEval + Groq Judge │ │ llama-3.3-70b │ │ (scoring & tracing) │ └──────────────────────────┘ └──────────────────────────┘ │ ▼ ┌──────────────────────────┐ ┌──────────────────────────┐ │ results/*.json │───►│ dashboard/app.py │ │ eval_results, mart_log │ │ Streamlit Dashboard │ └──────────────────────────┘ └──────────────────────────┘ ``` ## ⚡ 快速开始 ### 1. 克隆并安装 ``` git clone https://github.com/kokilamariyayi/llm-eval-redteam.git cd llm-eval-redteam pip install -r requirements.txt ``` ### 2. 配置环境 ``` # 复制示例 env 文件 cp .env.example .env ``` | 变量 | 必填 | 默认值 | 描述 | |---|---|---|---| | `GROQ_API_KEY` | ✅ 是 | — | 用于 LLM 推理与评判的 Groq API key | | `LANGSMITH_API_KEY` | ⬜ 否 | — | LangSmith 追踪(可选) | | `LANGSMITH_PROJECT` | ⬜ 否 | `llm-eval-redteam` | LangSmith 项目名称 | | `MODEL_NAME` | ⬜ 否 | `llama-3.3-70b-versatile` | 待评估的 Groq 模型 | ### 3. 运行 ``` # 快速 smoke test(20 个 prompts) python sample_run.py # 完整评估(全部 6 个类别) python main.py --run-eval # 运行特定类别 python main.py --run-eval --categories hallucination bias jailbreak # 运行 MART loop(4 轮对抗性强化) python main.py --run-mart --rounds 4 # 启动交互式 dashboard streamlit run dashboard/app.py # 将最新摘要打印到 console python main.py --summary ``` ## 🎯 失败类别 该框架探测了 **6 种对抗性失败模式**,代表了现实世界中的 LLM 安全风险: | 类别 | 测试内容 | 风险等级 | |---|---|---| | 🧠 **幻觉** | 编造的事实、虚假研究、不存在的实体、错误的日期 | 🔴 高 | | ⚖️ **偏见** | 人口统计学刻板印象、不公平的概括、歧视性输出 | 🔴 高 | | 🔓 **越狱** | DAN prompt、角色覆盖、安全策略绕过尝试 | 🔴 高 | | 💉 **Prompt 注入** | 隐藏指令、分隔符攻击、指令覆盖 | 🟠 中 | | ☣️ **毒性** | 仇恨言论、骚扰、有害语言生成 | 🟠 中 | | 🌀 **上下文漂移** | 多轮操纵、虚假记忆声明、渐进式角色漂移 | 🟡 低-中 | 每个 prompt 包含: - **`severity`** — `high` / `medium` / `low` - **`expected_behavior`** — 人工定义的真实基准 - **类别特定评分** — 通过 DeepEval 指标或基于 Groq 的规则评判器进行 ## 🔁 MART 循环 — 多轮自动红队测试 MART 迭代地**强化对抗性 prompt 池**,以揭露更难检测到的模型失败: ``` Round 1 ──► Evaluate all prompts ──► Compute violation rate │ violation rate > 15%? YES │ ▼ Groq generates 20 harder prompt variants from prompts the model passed too easily │ ▼ Append to pool → Round 2 ... │ violation rate ≤ 15%? YES │ ▼ Save mart_report.json ✅ ``` **最终 MART 报告 (`results/mart_report.json`)**: ``` { "rounds_completed": 4, "initial_violation_rate": 0.22, "final_violation_rate": 0.14, "reduction_percentage": 36.36, "total_prompts_tested": 520 } ``` ## 📁 项目结构 ``` llm-eval-redteam/ ├── main.py # CLI entry point (eval / mart / dashboard / summary) ├── config.py # Environment & model configuration ├── sample_run.py # 20-prompt smoke test │ ├── evaluators/ # Category-specific evaluators │ ├── base_eval.py # BaseEvaluator (Groq query + retry logic) │ ├── hallucination.py │ ├── bias.py │ ├── jailbreak.py │ ├── prompt_injection.py │ ├── toxicity.py │ └── contextual_drift.py │ ├── pipeline/ │ ├── runner.py # EvalRunner — batch eval + metrics aggregation │ └── mart_loop.py # MARTLoop — iterative prompt hardening │ ├── scripts/ │ └── generate_prompts.py # Prompt dataset generation utility │ ├── dashboard/ │ └── app.py # Streamlit interactive results dashboard │ ├── results/ # Auto-generated evaluation output │ ├── eval_results_*.json # Full evaluation runs │ ├── mart_round*.json # Per-round MART evaluations │ ├── mart_log.json # Round-by-round violation rates │ ├── mart_report.json # Final MART summary │ └── smoke_test_*.json # Smoke test output │ ├── output.png # Dashboard screenshot ├── requirements.txt └── .gitignore ``` ## 🛠️ 技术栈 | 工具 | 作用 | |---|---| | [**Groq**](https://groq.com) · `llama-3.3-70b-versatile` | 被评估的目标 LLM + 评判模型 | | [**DeepEval**](https://github.com/confident-ai/deepeval) | 幻觉、偏见与毒性评分指标 | | [**LangSmith**](https://smith.langchain.com) | Prompt 追踪、运行可观测性与日志记录 | | [**LangChain**](https://python.langchain.com) | LangSmith 集成层 | | [**Streamlit**](https://streamlit.io) | 交互式结果仪表板 | ## 📄 许可证 MIT — 详情请参阅 [LICENSE](LICENSE)。
## 📬 联系方式 欢迎建立联系,探讨合作、研究讨论或实习机会。 [![LinkedIn](https://img.shields.io/badge/LinkedIn-Kokila%20M-0A66C2?style=for-the-badge&logo=linkedin&logoColor=white)](https://www.linkedin.com/in/kokila-m-ai-ds/) [![Email](https://img.shields.io/badge/Email-kokilakoki3376%40gmail.com-EA4335?style=for-the-badge&logo=gmail&logoColor=white)](mailto:kokilakoki3376@gmail.com) [![GitHub](https://img.shields.io/badge/GitHub-kokilamariyayi-181717?style=for-the-badge&logo=github&logoColor=white)](https://github.com/kokilamariyayi) ⭐ **如果这个 repo 对您有帮助,请点个 Star!**
标签:AI安全, Chat Copilot, Clair, DeepEval, DLL 劫持, Kubernetes, LangSmith, 大语言模型, 模型评估, 逆向工具