maxtonhuang/Week6_Mini_Project_Customer_Service_RAG_Support_Chatbot
GitHub: maxtonhuang/Week6_Mini_Project_Customer_Service_RAG_Support_Chatbot
一个面向客服 RAG 聊天机器人的可信 AI 教学项目,通过智能体红队攻击与蓝队防御流水线评估和提升系统的鲁棒性。
Stars: 0 | Forks: 2
# RAGGuard — 面向客服 RAG 聊天机器人的智能体红队/蓝队流水线
可信 AI 迷你项目。我们构建了一个故意包含漏洞的客服 RAG
聊天机器人,使用智能体工具套件对其进行**攻击**,随后进行**防御**,并评估
准确性与鲁棒性之间的权衡。完整的计划、评分标准映射和威胁模型,请参阅 [`MINI_PROJECT_PLAN.md`](./MINI_PROJECT_PLAN.md)。
## 受害者系统
| 组件 | 模型 / 工具 |
|---|---|
| 生成器 | `Qwen/Qwen3-8B`(禁用思考模式) |
| 嵌入器 | `sentence-transformers/all-MiniLM-L6-v2` |
| 索引 | FAISS `IndexFlatIP` |
| 语料库 | `bitext/Bitext-customer-support-llm-chatbot-training-dataset` + 约 40 个植入的**金丝雀** (canary) 文档 |
## 目录结构
```
ragguard/
schemas.py # dataclasses & enums (Doc, AttackCase, Decision, RagResponse, RunRecord, ...)
interfaces.py # LLM / Retriever / Pipeline / Judge protocols; Attack / Defense base classes
config.py # model ids, sizes, paths, dials (FAST_MODE, N_PER_ATTACK), canary-token contract
testing.py # offline test-doubles: ScriptedLLM, KeywordRetriever, tiny_corpus
textnorm.py # A6 obfuscation encoders <-> D6 normaliser (kept as inverses)
metrics.py # ASR / utility / FRR / Pareto frontier / knee point
detect.py # refusal detection, PII regexes, shared canary regex
prompts.py # victim system prompt (+ secrets), hardened prompt, context formatters
canary.py # planted confidential documents with unique canary tokens
corpus.py # load Bitext, subsample, build knowledge base + benign eval set
rag.py # QwenLLM, EmbeddingRetriever, RagPipeline
judge.py # RuleJudge — rule-based attack-success oracle
attacks/ # A1-A6 static attacks + A7 adaptive attacker agent
defenses/ # D1-D6 defenses (3 hook points)
orchestrator.py # attack x defense sweep, two-stage stack search, Pareto selection
governance.py # NIST AI RMF scorecard (baseline vs defended)
report.py # tables & plots
app.py # Gradio UI (4 tabs)
```
## 设计原则:接口背后的模型
流水线从不直接导入模型——它依赖于 `LLM` / `Retriever`
协议。在 Colab 上,真实的 Qwen/FAISS 类实现了这些协议;而在离线环境中,则由轻量级的
`ScriptedLLM` / `KeywordRetriever` 替身来实现。这意味着
**整个** 攻击→防御→评估→编排逻辑无需 GPU 或任何重型依赖即可运行和测试。
## 运行
### 在 Google Colab 上(使用真实模型 — 推荐)
1. 上传此文件夹(或克隆),打开 `00_MAIN.ipynb`。
2. 运行环境 → 切换至 GPU (L4)。执行 `pip install -r requirements.txt`。
3. 运行所有单元格。`FAST_MODE` 可在约 8 分钟内验证整个流水线;完整运行将生成
报告数据。产出物将持久化保存至 Drive。
4. `01_DEMO.ipynb` 会加载缓存的产出物,并在约 2 分钟内启动 Gradio UI。
**使用 UI:** 新手操作指南请参阅 [`UI_GUIDE.md`](./UI_GUIDE.md)(包括四步演示、每个控件的作用,以及如何阅读判定标签)。在 Live Demo 标签页内还提供了一个“ℹ️ 如何使用”面板。
### 离线逻辑测试(无需网络,无重型依赖)
```
python run_tests.py # run every test with stdlib only
python run_tests.py test_rag # run one module
```
在 Colab 上,你也可以改用 `pytest`(参见 `requirements.txt`)。
## 调节参数(环境变量)
- `RAGGUARD_FAST=1` — 使用小样本量进行快速的端到端验证
- `RAGGUARD_N=50` — 每种攻击的攻击用例数量
- `RAGGUARD_ARTIFACTS=/path` — 索引/结果的持久化存储路径
标签:AI安全, Chat Copilot, DLL 劫持, FAISS, RAG, 大语言模型, 对抗性攻击, 逆向工具