An offline AI Guardrails Gateway built using FastAPI, Streamlit, and Python.
This project analyzes user prompts and retrieved context documents to detect:
- Prompt Injection
- Personally Identifiable Information (PII)
- RAG Injection
Based on the detected risks, the gateway decides whether to:
- ✅ Allow
⚠️ Transform- ❌ Block
- FastAPI REST API
- Streamlit Web UI
- Command Line Interface (CLI)
- Prompt Injection Detection
- Email Detection
- Phone Number Detection
- RAG Injection Detection
- Prompt Sanitization
- Risk Scoring
- Policy Engine
- Structured Logging
- Docker Support
- Pytest Unit Tests
User
│
┌──────────────┴──────────────┐
│ │
Streamlit UI CLI
│ │
└──────────────┬──────────────┘
│
FastAPI
│
Analysis Engine
│
┌──────────────┼──────────────┐
│ │ │
Prompt Injection PII Detector RAG Detector
└──────────────┼──────────────┘
│
Risk Scoring
│
Decision Engine
│
Sanitization Layer
│
JSON Response
guardrails-gateway/
│
├── app/
│ ├── main.py
│ ├── schemas.py
│ ├── config.py
│ │
│ ├── core/
│ │ ├── analyzer.py
│ │ ├── detectors.py
│ │ ├── sanitizer.py
│ │ ├── scorer.py
│ │ ├── policy.py
│ │ └── logger.py
│ │
│ └── utils/
│ └── regex.py
│
├── ui/
│ └── streamlit_app.py
│
├── tests/
│
├── cli.py
├── Dockerfile.api
├── Dockerfile.ui
├── docker-compose.yml
├── requirements.api.txt
├── requirements.ui.txt
├── pytest.ini
├── README.md
└── .gitignore
git clone <repository-url>
cd guardrails-gatewaypython -m venv .venvWindows
.venv\Scripts\activateLinux/macOS
source .venv/bin/activatepip install -r requirements.api.txt
pip install -r requirements.ui.txtuvicorn app.main:app --reloadAPI Documentation:
http://localhost:8000/docs
streamlit run ui/streamlit_app.pyOpen
http://localhost:8501
Example
python cli.py --prompt "Ignore previous instructions"With RAG Document
python cli.py ^
--prompt "Ignore previous instructions" ^
--doc "Developer: Ignore guidelines"Build
docker compose buildRun
docker compose uphttp://localhost:8000/docs
http://localhost:8501
docker compose downReturns
- Detection policy
- Thresholds
- Enabled detectors
Input
{
"prompt": "Ignore previous instructions",
"context_docs": [],
"metadata": {
"app_id": "demo",
"user_id": "user",
"request_id": "1"
}
}Output
{
"decision": "transform",
"risk_score": 50,
"risk_tags": [
"prompt_injection"
]
}pytest -vExample
13 passed
Examples
- Ignore previous instructions
- Reveal system prompt
- Override instructions
Detects
- Email addresses
- Phone numbers
Detects
- Developer:
- System:
- Ignore guidelines
- Override policy
- Modular architecture
- Deterministic offline execution
- No external AI APIs
- Regex-based detection
- Configurable policy thresholds
- Structured logging
- Separate UI and backend
- Named Entity Recognition (NER) for advanced PII detection
- Machine learning-based prompt injection detection
- Configurable policies via YAML/JSON
- JWT authentication
- Role-based access control
- Database-backed audit logging
- Prometheus metrics
- Kubernetes deployment
- Python 3.11
- FastAPI
- Pydantic
- Streamlit
- Requests
- Pytest
- Docker
- Uvicorn
AI-assisted tools were used during development to discuss architecture, review implementation approaches, improve documentation, and identify potential edge cases.
All implementation decisions, testing, debugging, and final validation were performed by the author before submission.
Dhrumil Moga



