A production-ready RAG (Retrieval-Augmented Generation) system with built-in security guardrails for prompt injection detection and PII filtering.
- Semantic Search: Vector-based document search using OpenAI embeddings and Qdrant
- Security Guardrails:
- Input Guard: Detects prompt injection, role manipulation, jailbreak attempts
- Output Guard: Detects and redacts PII (emails, SSNs, credit cards, API keys)
- Rate Limiter: Protects against abuse and resource exhaustion
- Document Management: Upload, index, and search text documents
- Modern Stack: FastAPI, Next.js, PostgreSQL, Qdrant
- Docker and Docker Compose
- Python 3.11+
- Node.js 20+
- Clone the repository:
git clone https://github.com/musabdulai-io/secure-rag-kit.git
cd secure-rag-kit- Run the setup script:
./setup.sh- Update your environment:
# Edit .env and add your OPENAI_API_KEY
nano .env- Start the services:
docker compose up- Access the application:
- Frontend: http://localhost:3000
- Backend API: http://localhost:8000
- API Docs: http://localhost:8000/docs
docker compose exec backend python -m scripts.seeddocker compose exec backend python -m scripts.test_security┌─────────────────────────────────────────────────────────────┐
│ Frontend │
│ (Next.js + MUI) │
└─────────────────────────┬───────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────┐
│ API Layer │
│ (FastAPI) │
│ ┌──────────────┐ ┌──────────────┐ ┌──────────────┐ │
│ │ Input Guard │ │ Rate Limiter │ │ Output Guard │ │
│ └──────────────┘ └──────────────┘ └──────────────┘ │
└─────────────────────────┬───────────────────────────────────┘
│
┌───────────────┼───────────────┐
▼ ▼ ▼
┌─────────────────┐ ┌───────────┐ ┌─────────────────┐
│ PostgreSQL │ │ Qdrant │ │ Storage │
│ (Documents) │ │ (Vectors) │ │ (Local/GCS) │
└─────────────────┘ └───────────┘ └─────────────────┘
Detects and blocks:
- Role manipulation ("ignore previous instructions")
- Prompt extraction attempts
- Delimiter injection (```, ###)
- Jailbreak patterns
- Encoding attacks (base64)
Detects and redacts:
- Email addresses
- Phone numbers
- Social Security Numbers
- Credit card numbers
- API keys and tokens
- IP addresses
- Configurable requests per window
- In-memory for development
- Ready for Redis in production
| Method | Endpoint | Description |
|---|---|---|
| POST | /api/v1/rag/documents |
Upload a document |
| GET | /api/v1/rag/documents |
List all documents |
| GET | /api/v1/rag/documents/{id} |
Get document by ID |
| DELETE | /api/v1/rag/documents/{id} |
Delete a document |
| POST | /api/v1/rag/search |
Search documents |
| GET | /health |
Health check |
Environment variables (see .env.example):
| Variable | Description | Default |
|---|---|---|
OPENAI_API_KEY |
OpenAI API key | Required |
DATABASE_URL |
PostgreSQL connection string | See .env.example |
QDRANT_URL |
Qdrant server URL | http://localhost:6333 |
RATE_LIMIT_REQUESTS |
Max requests per window | 100 |
RATE_LIMIT_WINDOW |
Rate limit window (seconds) | 60 |
The ./setup.sh script sets up your local environment:
- Creates Python virtual environment with dependencies
- Installs Node.js dependencies
- Copies
.env.exampleto.env
For IDE intellisense, activate the backend venv:
source backend/venv/bin/activatedocker compose exec backend alembic revision --autogenerate -m "description"
docker compose exec backend alembic upgrade headMIT
Have questions or want to discuss this project?
- Email: hello@musabdulai.com
- Book a call: Schedule a meeting
- Website: musabdulai.com