Skip to main content

Ensemble Prompt Injection Detection

Project description

PromptForest: Fast, Calibrated & Reliable Injection Detection

PyPI Downloads Apache License Open In Colab

PromptForest is an ensemble-based prompt injection detection system designed for high-throughput production environments. By aggregating predictions from multiple lightweight expert models, it achieves best-in-class calibration and competitive accuracy while maintaining significantly lower latency than comparable monolithic models.


1. Introduction

Large Language Models (LLMs) are vulnerable to prompt injection attacks where adversarial inputs manipulate the model into executing unintended commands. Traditional defenses often rely on single, large BERT-based classifiers which suffer from two key limitations:

  1. High Latency: Large transformer models introduce significant overhead in critical paths.
  2. Overconfidence: Single models tend to be overconfident in their predictions, even when incorrect (high Expected Calibration Error).

PromptForest addresses these issues through Ensemble Diversity. We combine the signals from diverse architectures (DeBERTa, XGBoost, etc.) to produce a discrepancy-weighted risk score. This allows for:

  • Robustness: Disagreement between models flags ambiguous inputs.
  • Efficiency: The total parameter count is <50% of the leading SOTA monolithic detector.
  • Calibration: Lower confidence on incorrect predictions reduces catastrophic failures.

2. Methodology

PromptForest utilizes a voting ensemble of three distinct lightweight models:

  1. Meta Llama Prompt Guard 86M: Meta's prompt injection detector, already very calibrated for its parameter count (ECE lowest for its parameter class in candidate models).
  2. Vijil Dome: Vijil's ModernBERT finetune, has the highest parameter efficiency in all candidate models.
  3. PromptForest-XGB: A custom XGBoost model trained on embedding features.

Predictions are aggregated using a weighted voting mechanism to form a sophisticated consensus mechanism that outperforms individual voters in calibration metrics.

3. Performance Evaluation

We benchmarked PromptForest against a wide range of detection models, with the SOTA model Qualifire Sentinel v2 being the best performing competitor model. You can view our benchmarking code in the benchmark folder.

3.1 Reliability & Calibration

PromptForest demonstrates superior calibration, meaning its reported confidence scores better reflect the true probability of correctness. Lower ECE (Expected Calibration Error) indicates more trustworthy probability outputs.

PromptForest Calibration Graph

Metric PromptForest (Ensemble) Qualifire Sentinel v2 (Baseline) Improvement
Accuracy 0.901 0.973 -
ECE (Lower is better) 0.070 0.096 +27%
Avg Conf. on Failure 0.642 0.760 +16%
Parameter Count ~237M ~600M 60% fewer

While Sentinel v2 achieves higher raw accuracy, PromptForest is significantly less confident when it makes mistakes (0.642 vs 0.760), making it safer for "Human-in-the-Loop" fallback systems.

3.2 Latency Benchmark

We compared the end-to-end inference time (including tokenization) on a standard Macbook CPU node on 3000 prompts of varying length. Our latency was significantly lower, despite our server overhead, showing that PromptForest is adequately optimised for real-world speed.

System Mean Latency (ms) P95 Latency (ms)
Qualifire Sentinel v2 ~225.77ms ~430.31ms
PromptForest ~141.07ms ~257.19ms

4. Quick Start

PromptForest is designed for immediate deployment.

# Install valid package
pip install promptforest

# Start the optimized inference server
promptforest serve --port 8000

The server automatically handles model downloading, caching, and ensemble orchestration.

5. Downstream Integration

PromptForest outputs the following JSON, where entries like is_malicious or confidence could be used in downstream tasks.

{
  "is_malicious": true/false,
  "confidence": float,
  "uncertainty": float,
  "malicious_score": float,
  "max_risk_score": float,
  "details": {
    "llama_guard": float,
    "vijil": float,
    "xgboost": float
  },
  "latency_ms": float
}

To configure the models or the output, please use a config.yaml.

6. Models & Attribution

PromptForest ensembles the following open-weights models:

Provider Model License
Meta Llama Prompt Guard 86M Llama Community
Vijil Vijil Dome Apache 2.0
Appleroll PromptForest-XGB Apache 2.0

7. Disclaimer

PromptForest is an evolving research project. It is not a standalone security solution and should be used as part of a defense-in-depth strategy alongside input sanitization (e.g., NeMo Guardrails) and system prompting.

License

Apache 2.0. See LICENSE and THIRD_PARTY_LICENSES for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

promptforest-0.7.5.tar.gz (655.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

promptforest-0.7.5-py3-none-any.whl (677.9 kB view details)

Uploaded Python 3

File details

Details for the file promptforest-0.7.5.tar.gz.

File metadata

  • Download URL: promptforest-0.7.5.tar.gz
  • Upload date:
  • Size: 655.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.9

File hashes

Hashes for promptforest-0.7.5.tar.gz
Algorithm Hash digest
SHA256 48726cbda36a4d829ae498cb7e7a2c8e354cdfe641faad67f3791912593d8bfb
MD5 37f22c6851d046ef1aab5e47b8d017f2
BLAKE2b-256 a6c1b9597274e86916352e0cb14d8e356255c4a4a3cc580441aee64ac4439757

See more details on using hashes here.

File details

Details for the file promptforest-0.7.5-py3-none-any.whl.

File metadata

  • Download URL: promptforest-0.7.5-py3-none-any.whl
  • Upload date:
  • Size: 677.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.9

File hashes

Hashes for promptforest-0.7.5-py3-none-any.whl
Algorithm Hash digest
SHA256 665b34c4e17bca73f05068207ded2895065a78d0d84ea1a0c02261415368fab2
MD5 da23a6fb23bfbf11a2f6bb9866633ef6
BLAKE2b-256 d75731f2414f38a96aa4521184c5e8d924200433eb739fb0d56817aa60a3cb5a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page