AlgorithmicDhruv/SentinelEval_AI

GitHub: AlgorithmicDhruv/SentinelEval_AI

一个专为 RAG 管线设计的实时安全评估与红队测试框架,通过加权风险引擎实现幻觉检测、提示注入拦截和毒性审核的自动化防护。

Stars: 0 | Forks: 0

# 🛡️ SentinelEval AI ### **安全的 RAG 评估与红队测试框架** SentinelEval AI 是一个专为检索增强生成(RAG)系统设计的安全网关。它位于用户和 LLM 之间,利用加权风险引擎实时检测幻觉、拦截提示注入并审核有毒内容。 ## 📂 项目结构 ``` Project Root ├── app/ │ ├── main.py # FastAPI Entry point │ ├── analysis/ │ │ └── reasoning_engine.py # Interprets scores into human-readable reasons │ ├── api/ │ │ └── routes.py # evaluate endpoint orchestration │ ├── evaluation/ │ │ ├── hallucination.py # Semantic similarity & grounding checks │ │ ├── prompt_injection.py # Vector-based jailbreak detection │ │ ├── risk_engine.py # Weighted risk scoring logic │ │ └── toxicity.py # Toxic-BERT classification │ ├── experiments/ # Adversarial testing & automated red-teaming │ ├── policies/ │ │ └── enforcement.py # Response modification (BLOCK/WARN/FLAG) │ ├── services/ │ │ ├── llm_service.py # Groq/Llama-3 integration & system prompting │ │ └── retriever.py # Local document RAG retrieval │ └── utils/ │ └── logger.py # JSON event logging for history ├── dashboard/ │ └── app.py # Streamlit monitoring UI ├── data/ │ ├── knowledge/ # RAG source docs (e.g., policy.txt) │ └── eval_logs.json # Historical performance data └── requirements.txt # Project dependencies ``` ## 🚀 核心逻辑与工作流 ### **1. RAG 流水线** 当系统接收到一个提示时,它会遵循以下自动化路径: 1. **检索:** `retriever.py` 从 `data/knowledge/` 中提取文本。 2. **增强:** `llm_service.py` 构建一个“基于事实”的提示,指示 LLM *仅* 使用提供的上下文。 3. **生成:** LLM(通过 Groq 调用的 Llama-3)生成响应。 ### **2. 安全评估套件** 系统会对生成的输出并行运行三项检查: * **幻觉检查:** 使用 `SentenceTransformer` 将 LLM 的响应与源上下文进行对比。 * **注入检查:** 采用混合方法(模式匹配 + 向量相似度)来检测诸如 *"忽略之前的指令"* 等越狱尝试。 * **毒性检查:** 使用 HuggingFace 的 `toxic-bert` 流水线来识别有害语言。 ### **3. 加权风险引擎** 最终的安全性由以下公式决定: $$RiskScore = (0.3 \times Hallucination) + (0.2 \times Toxicity) + (0.5 \times Injection)$$ | 严重级别 | 动作 | 指示 | | --- | --- | --- | | **严重 (CRITICAL)** | **拦截 (BLOCK)** | 返回一条通用安全提示。 | | **高 (HIGH)** | **警告 (WARN)** | 在响应前附加警告。 | | **中 (MEDIUM)** | **标记 (FLAG)** | 为响应打上标签以供人工审核。 | | **低 (LOW)** | **允许 (ALLOW)** | 原样传递响应。 | ## 🛠️ 设置与安装 ### **1. 环境设置** ``` # 创建虚拟环境 python -m venv venv source venv/bin/activate # Or `venv\Scripts\activate` on Windows # 安装依赖 pip install -r requirements.txt ``` ### **2. API 配置** 在根目录下创建一个 `.env` 文件: ``` GROQ_API_KEY=your_groq_key_here ``` ### **3. 执行** 启动 API(后端): ``` uvicorn app.main:app --reload ``` 启动仪表盘(前端): ``` streamlit run dashboard/app.py ``` ## ⚙️ 开发工作流 (`.gitignore`) 该项目在保持核心架构的同时,已配置为保持仓库整洁: * **数据隐私:** 所有本地执行日志 (`eval_logs.json`) 和特定知识库文档均被忽略,以防止泄露敏感的 RAG 数据。 * **实验:** 本地的红队测试实验和对抗性结果均不纳入版本控制。 * **缓存:** 所有 `__pycache__` 和特定于 IDE 的文件(如 `.vscode`)均被忽略。 *为 AI 红队测试与企业 LLM 安全而开发。*
标签:AI内容安全, AI安全网关, AI越狱防御, AV绕过, CISA项目, Clair, FastAPI, Kubernetes, LLM应用防火墙, Python, RAG安全评估, Streamlit, Toxic-BERT, 主机安全, 内容风控, 大模型安全, 大语言模型安全, 安全策略执行, 对抗性测试, 提示注入防御, 无后门, 机密管理, 检索增强生成, 毒性内容过滤, 源代码安全, 访问控制, 语义相似度, 逆向工具, 风险评估引擎