Reewd/llm-prompt-guard
GitHub: Reewd/llm-prompt-guard
一个学术性的提示注入检测基准评测项目,在消费级硬件上对比小型本地长上下文 LLM 与专用检测器的检测效果。
Stars: 0 | Forks: 0
# 用于提示注入检测的小型本地长上下文 LLM
罗马大学 Natural Language Processing 课程的作业项目 (HWp)。
它在 11 GB 消费级 GPU 预算下,在完整的 BrowseSafe
和 NotInject 文档上,将小型本地 LLM 与专用的提示注入检测器进行了比较。这是一个实验性基准测试,并非
生产级安全系统。
## 要求
- Python 3.11
- [`uv`](https://docs.astral.sh/uv/)
- 仅使用 CPU:`cpu` 依赖组
- GPU 实验:NVIDIA GTX 1080 Ti、CUDA 12.6 以及 `cu126` 组
- 本地 LLM 实验:Ollama 以及 `configs/models/` 中指定的模型标签
- 需要具备 Hugging Face 访问权限以获取受限检测器模型
## 安装
安装用于 CPU 或 GPU 执行的依赖组:
```
uv sync --group cpu
uv sync --group cu126
```
对于 QLoRA fine-tuning,请包含 Unsloth 依赖项:
```
uv sync --group cu126 --group qlora-unsloth
```
## 运行实验
### 准备数据集
下载并标准化已锁定的数据集:
```
uv run python scripts/download_data.py --config configs/datasets/browsesafe_bench.json
uv run python scripts/download_data.py --config configs/datasets/notinject.json
uv run python scripts/build_balanced_subset.py \
--config configs/datasets/browsesafe_test_balanced_500_hf_token_budget.json
```
### 生成预测
在共享的 BrowseSafe 子集上运行 regex 基线:
```
uv run python scripts/run_baseline.py \
--config configs/experiments/baseline_browsesafe_balanced500_hf_token_budget.json \
--input data/processed/browsesafe_bench/test_balanced_500_hf_token_budget.parquet \
--output results/raw_predictions/baseline_browsesafe_balanced500_hf_token_budget_v1.jsonl
```
通过选择另一个已提交的配置,来运行本地 LLM 或专用检测器:
```
uv run python scripts/run_local_llm.py \
--config configs/experiments/local_llm_qwen3_instruct_browsesafe_balanced500_test.json \
--resume
uv run python scripts/run_detector.py \
--config configs/experiments/detector_prompt_guard_browsesafe_test.json \
--resume
```
### 评估与分析
评估预测产物:
```
uv run python scripts/evaluate.py \
--predictions results/raw_predictions/baseline_browsesafe_balanced500_hf_token_budget_v1.jsonl \
--output results/metrics/baseline_browsesafe_balanced500_hf_token_budget_v1.json
```
根据已完成的预测产物构建比较表:
```
uv run python scripts/analyze.py \
--dataset data/processed/browsesafe_bench/test_balanced_500_hf_token_budget.parquet \
--predictions results/raw_predictions/baseline_browsesafe_balanced500_hf_token_budget_v1.jsonl \
--predictions results/raw_predictions/detector_prompt_guard_browsesafe_balanced500_hf_token_budget_v1.jsonl \
--native-token-config configs/analysis/native_token_counts.json \
--output-directory results/tables/example
```
## QLoRA fine-tuning 与 GGUF 导出
### 训练与验证
Qwen QLoRA 运行器将数据准备、模型加载检查、训练和验证
分为独立阶段。这些命令遵循主要的 step-100 工作流:
```
uv run python scripts/run_qwen_qlora.py \
--config configs/experiments/qlora_qwen3_06b_non_thinking_unsloth_feasibility.json \
--stage prepare
uv run python scripts/run_qwen_qlora.py \
--config configs/experiments/qlora_qwen3_06b_non_thinking_unsloth_feasibility.json \
--stage load-check
uv run python scripts/run_qwen_qlora.py \
--config configs/experiments/qlora_qwen3_06b_non_thinking_unsloth_feasibility.json \
--stage train --capacity-start 4096 --attempt-id oom-only-4096-v2
uv run python scripts/run_qwen_qlora.py \
--config configs/experiments/qlora_qwen3_06b_non_thinking_validation.json \
--stage validate
```
在中断的训练或评估命令中使用 `--resume`。指定的尝试名称和初始
容量与已提交的验证和导出配置所引用的路径相匹配。
### 构建 llama.cpp
GGUF 导出需要 Git、CMake、C++ 编译器,以及位于导出配置所锁定
版本的本地仓库 `llama.cpp` 检出:
```
mkdir -p runtime
git clone --depth 1 --branch b9632 \
https://github.com/ggml-org/llama.cpp runtime/llama.cpp
cmake -S runtime/llama.cpp -B runtime/llama.cpp/build \
-DCMAKE_BUILD_TYPE=Release
cmake --build runtime/llama.cpp/build --config Release \
--target llama-quantize -j
ln -s build/bin/llama-quantize runtime/llama.cpp/llama-quantize
```
### 导出 GGUF
将训练好的 adapter 导出为 Ollama 实验所使用的 Q4_K_M GGUF:
```
uv run python scripts/run_qwen_qlora.py \
--config configs/experiments/qlora_qwen3_06b_non_thinking_step100_gguf_q4_k_m.json \
--stage export-gguf
```
## 开发
运行静态检查:
```
uv run ruff check .
uv run mypy src
```
标签:AI风险缓解, Bash脚本, DLL 劫持, IaC 扫描, PyTorch, Vectored Exception Handling, 大语言模型, 逆向工具