AI Guardrail Gateway
Upstream AI-firewall / guardrail layer for an enterprise inference gateway: intercepts prompts, sanitizes Russian PII with grammatical-case awareness, enforces OWASP LLM Top-10 policies, and scores adversarial intent before anything reaches a downstream LLM.
Pipeline: deterministic pre-hook → Russian PII engine (Natasha/Yargy + pymorphy3; optional transformer backend) → OWASP guardrails (ML-graded LLM01, PII-scanning LLM06) → adversarial-intent classifier (ONNX int8, CPU).
Repo layout
src/ner/ PII extractor (rule-based + transformer ML backend), morphology, de-identification
src/guardrails/ OWASP checkers (ML-scored LLM01, NER-scanned LLM06), secrets scanner, pre-hook
src/classifier/ Attack classifier: rules fast-path + ONNX int8 inference
src/api/ SanitizePipeline orchestrator; FastAPI /v1/sanitize, /v1/validate
scripts/ train_classifier.py (parity-gated), quantize_onnx.py (torch-gated)
benchmarks/ pii_dataset, pii_eval, ner_transformer_eval, security_bench, latency_bench + results/
TECHNICAL_REPORT.md Full results, model-selection rationale, failure post-mortems
Quickstart
from src.api.pipeline import SanitizePipeline
pipe = SanitizePipeline() # classifier defaults to mvroz/prompt-attack-minilm onnx-int8
result = pipe.run("Передать Ивану Сергеевичу, что отчёт готов")
print(result.action, result.sanitized_text, result.attack)
# ML PII backend (pretrained Russian NER, ONNX int8):
# stage the model dir and point NER_MODEL_DIR at it, then:
pipe_ml = SanitizePipeline(pii_backend="transformer")
FastAPI: uvicorn src.api.app:app → POST /v1/sanitize (PII de-identification) and POST /v1/validate (attack verdict).
from src.ner import extract_pii # Module 1 directly
from src.guardrails.owasp import check_input, check_output
Headline results
| Metric | Result | Target |
|---|---|---|
| Russian PER recall, rule-based (all 6 cases) | 0.959 | ≥ 0.94 ✅ |
| Injection FPR / FNR (xtram1 held-out test, n=500) | 0.29% / 0.0% | ≤ 2% / ≤ 5% ✅ |
| Benign FPR (n=241) | 3.7% | ≤ 2% ⚠ (all FPs are Russian prompts) |
| Hijacking-attack FNR (TensorTrust, n=500) | 10.6% | ≤ 5% ⚠ (OOD) |
| Classifier latency, int8 ONNX (4 vCPU) | p50 6.2 ms / p95 52 ms | p95 < 45 ms ⚠ (fast-path rules: 0.04 ms) |
| Total pipeline | p50 30.4 ms / p95 153 ms (PII dominates) | — |
| Rules vs pretrained transformer NER (micro F1 / p50) | 0.826 / 14.5 ms vs 0.309 / 84.6 ms | comparison per ТЗ |
Checkpoint + quantized artifacts: mvroz/prompt-attack-minilm (onnx-fp32/ 470.8 MB, onnx-int8/ 118.2 MB; parity vs torch ground truth 0.0000 fp32 / 0.0003 int8).
Requirements
See requirements.txt (pinned: transformers==5.17.0, setuptools<81 for natasha's pymorphy2 bridge, natasha, pymorphy3, onnxruntime, fastapi, uvicorn).
License
All rights reserved. This codebase and its artifacts are proprietary; no reuse, redistribution, or derivative works without explicit permission.