SrivathsanVijayaraghavan/promptgate
GitHub: SrivathsanVijayaraghavan/promptgate
PromptGate 是一个部署在用户与 LLM 之间的 Python 中间件,通过多层信号检测和评分机制在输入到达模型前识别并拦截 prompt injection 攻击。
Stars: 0 | Forks: 0
# PromptGate
PromptGate 是一个开源的 Python middleware 库,它位于用户和 LLM 之间,并且**在模型看到输入之前检测 prompt injection 风险**。它是一个**风险分类器**,而不是道德裁判:它积累可解释的信号并应用策略阈值。
## 架构
```
user_input
→ parser (normalize text, metadata)
→ rule_based (substring pattern matching)
→ aggregator (map signals → threat categories)
→ scorer (sum unique signal severities, cap at 1.0)
→ policy (ALLOW / FLAG / REVIEW / BLOCK)
→ response (structured, explainable output)
```
当 PromptGate 被放置在请求路径前端时,LLM 永远不会接收到被阻止的 prompt。
## 安装
从项目根目录(`promptgate/`)开始:
```
pip install -e ".[dev]"
```
可编辑安装可以在开发过程中保持模式文件和源代码同步。
## 用法
```
from promptgate import PromptGate
gate = PromptGate()
result = gate.check("Please ignore previous instructions.")
print(result["decision"]) # BLOCK
print(result["confidence"]) # 0.95
print(result["message"]) # Human-readable explanation
print(result["signals"]) # Matched risk signals
```
自定义策略阈值:
```
gate = PromptGate(thresholds={"block": 0.80, "review": 0.60, "flag": 0.35})
result = gate.check(user_input)
```
## 可解释性理念
每个响应都包含:
- **decision** — ALLOW、FLAG、REVIEW 或 BLOCK
- **confidence** — 等于累积的风险分数(安全时为 0.0)
- **signals** — 匹配的内容,包含严重性和模式文本
- **signals_checked** — 扫描的信号类型、类别和模式文件
- **message** — 关于为何做出该决定的通俗语言解释
ALLOW 响应明确指出没有检测到超过阈值的 injection 模式或操纵框架。受限响应会指出匹配的信号和类别。
**信号积累是必需的。** 单独一个微弱的信号(例如,同情框架)不会导致阻止。多个信号通过 `score = min(1.0, sum(severities))` 进行组合。
## 威胁类别
| 类别 | 示例信号 |
|----------|-----------------|
| `direct_injection` | instruction_override, data_exfiltration |
| `jailbreak` | jailbreak_persona |
| `system_override` | system_override, system_prompt_leak |
| `social_engineering` | authority_claim, secrecy_request, urgency_framing |
| `encoding_attack` | encoding_trick |
严重性值和映射位于 `promptgate/config.py` 中。
## 默认策略阈值
| 分数范围 | 决定 |
|-------------|----------|
| 0.00 – 0.29 | ALLOW |
| 0.30 – 0.54 | FLAG |
| 0.55 – 0.74 | REVIEW |
| 0.75 – 1.00 | BLOCK |
## 本地测试
```
cd promptgate
pip install -e ".[dev]"
pytest -v
```
## 项目布局
```
promptgate/
├── promptgate/ # Python package
├── data/patterns/ # Seed pattern files
├── tests/
├── pyproject.toml
└── README.md
```
## 许可证
MIT
标签:DLL 劫持, DoH影响, Python, Windows日志分析, 中间件, 大语言模型, 提示词注入检测, 无后门, 逆向工具, 风险分类器