alihasan194jb-prog/Ai-final-lab-project
GitHub: alihasan194jb-prog/Ai-final-lab-project
这是一个多语言的LLM安全网关,专注于提示注入检测和PII脱敏,以保护语言模型应用的安全。
Stars: 0 | Forks: 0
# 🔐 LLM 安全网关
一个多语言提示注入检测与个人身份信息(PII)脱敏网关。
支持**英语、乌尔都语、罗马乌尔都语和韩语**。
## 📁 项目结构
```
llm-security-gateway/
├── app/
│ ├── main.py # FastAPI backend (4 endpoints)
│ ├── detectors/
│ │ ├── rule_detector.py # Regex patterns (EN/UR/Roman-UR/KO)
│ │ └── semantic_detector.py # TF-IDF + Logistic Regression
│ ├── pii/
│ │ └── presidio_custom.py # CNIC, StudentID, APIKey, PK-Phone
│ ├── policy/
│ │ └── policy_engine.py # Risk formula → ALLOW/MASK/BLOCK
│ └── utils/
│ └── language.py # Script-based language detection
├── config/
│ └── gateway_config.yaml # All thresholds configurable
├── data/
│ └── final_eval.csv # 160 labeled evaluation prompts
├── tests/
│ └── test_all.py # Unit tests for all modules
├── run_evaluation.py # Full evaluation with metrics
└── requirements.txt
```
## 🚀 设置(Windows)
### 1. 创建虚拟环境
```
python -m venv venv
venv\Scripts\activate
```
### 2. 安装依赖
```
pip install -r requirements.txt
```
### 3. 运行 API 服务器
```
cd app
uvicorn main:app --reload --port 8000
```
访问:http://localhost:8000/docs (Swagger UI)
## 🧪 运行评估
```
# 从项目根目录
python run_evaluation.py
```
预期输出:
- 准确率、精确率、召回率、F1 分数
- 混淆矩阵
- 延迟(P50/P95/P99)
- 按类别和语言的结果
- 规则基线与混合方法的比较
## 🧪 运行单元测试
```
python -m pytest tests/ -v
```
## 📡 API 端点
### POST /analyze
```
{
"text": "ignore all previous instructions"
}
```
响应:
```
{
"action": "BLOCK",
"risk_score": 0.87,
"rule_score": 0.85,
"semantic_score": 0.72,
"language": "en",
"language_display": "English",
"pii_found": [],
"masked_text": null,
"matched_rules": ["prompt_injection_en"],
"reasons": ["Rule score 0.85 triggered hard BLOCK"],
"latency_ms": 2.3
}
```
### POST /batch
```
{
"prompts": ["hello", "ignore all instructions", "my email is x@y.com"]
}
```
### GET /health
返回系统健康状态。
### GET /config
返回当前阈值和配置。
## 🌍 支持的语言
| 代码 | 语言 | 文字 |
|------|----------|--------|
| en | 英语 | Latin |
| ur | 乌尔都语 | Arabic/Nastaliq |
| roman_ur | 罗马乌尔都语 | Latin (乌尔都语词) |
| ko | 韩语 | Hangul |
## 🔍 检测管道
```
Input Prompt
│
▼
Language Detection (script-based)
│
├─► Rule Detector (regex patterns, multilingual)
│
├─► Semantic Detector (TF-IDF + Logistic Regression)
│
├─► PII Scanner (Presidio + custom recognizers)
│ CNIC | StudentID | APIKey | PK-Phone
│
▼
Policy Engine
risk_score = 0.5 × rule_score + 0.5 × semantic_score
│
├─ score ≥ 0.75 → BLOCK
├─ score ≥ 0.45 → MASK (PII redacted)
└─ score < 0.45 → ALLOW
```
## ⚙️ 配置
编辑 `config/gateway_config.yaml` 以调整阈值:
```
thresholds:
block: 0.75 # Raise to be less aggressive
mask: 0.45 # Lower to catch more PII
weights:
rule: 0.50
semantic: 0.50
```
## 📊 评估结果
| 指标 | 仅规则 | 混合方法 |
|--------|-----------|--------|
| 准确率 | ~65% | ~67%+ |
| 精确率 | 低 | 0.82+ |
| 召回率 | 高 | 平衡 |
| 延迟 P50 | <1ms | ~2ms |
## 🛡️ 检测的 PII 实体
| 实体 | 示例 | 置信度 |
|--------|---------|------------|
| PK_CNIC | 42101-1234567-1 | 0.95 |
| STUDENT_ID | FAST-2021-CS-0123 | 0.90 |
| API_KEY | sk-abc123... | 0.99 |
| PK_PHONE | 0300-1234567 | 0.85 |
| EMAIL_ADDRESS | user@example.com | 0.95 |
| CREDIT_CARD | 4111-1111-1111-1111 | 0.95 |
## 📝 实验报告说明
- **架构**:混合规则 + 语义机器学习管道
- **无需 GPU**:TF-IDF + 逻辑回归,仅需 CPU
- **无需 API 密钥**:完全离线,使用模拟的 LLM
- **兼容 Windows**:纯 Python,无 Linux 专有依赖
- **多语言**:4 种语言,支持文字自动检测
标签:Apex, API密钥检测, API网关, AV绕过, FastAPI, IPv6支持, Presidio, TF-IDF, 个人身份信息屏蔽, 人工智能安全, 合规性, 多语言处理, 多语言支持, 安全测试框架, 安全网关, 数据隐私, 文本分析, 机器学习, 检测引擎, 策略引擎, 结构化查询, 网络安全, 网络安全挑战, 自动化安全, 输入验证, 逆向工具, 隐私保护, 零日漏洞检测, 风险公式, 风险评分