Privacy-first, fully offline AI document assistant secured by tiered safety guardrails
Project description
๐ก๏ธ GuardRAG
Privacy-First, Fully Offline AI Document Assistant
Secured by a Tiered Safety Guardrails System
Upload any document. Ask anything. Get grounded, source-cited answers โ
entirely on your machine or using secure cloud endpoints.
Every answer is guarded by a tiered safety engine that prevents jailbreaks, PII leaks, and credential exposure.
โจ Features
| Feature | Description |
|---|---|
| ๐ง RAG Pipeline | Retrieval-Augmented Generation with FAISS, LangChain, and HuggingFace embeddings |
| ๐ก๏ธ 4-Tier Safety Guardrails | Jailbreak detection, PII protection, credential blocking โ fully offline |
| ๐ Web UI | Clean, dark-mode browser interface with chat history, tabs, and document library |
| ๐ป CLI Mode | Terminal-based interactive chat โ no browser needed |
| ๐ Document Library | Persist FAISS indices and reload sessions without re-uploading |
| ๐ Source Citations | Every answer cites which document and chunk it came from |
| ๐ PII Redaction | Automatic context-aware redaction of names, emails, SSNs before indexing |
| โ๏ธ Manual Redactions | Specify custom keywords to scrub from documents before embedding |
| ๐ Dynamic Model Switching | Swap LLM model or Ollama host mid-session without restarting |
| โ๏ธ Cloud API Support | Works with Groq, OpenRouter, OpenAI, Anthropic, Cohere via OpenAI-compatible API |
| ๐ Pluggable Vector Stores | FAISS (default), Qdrant, or Chroma โ switch from the settings panel |
| ๐ก LAN Sharing | Access from any device on your local network (auto-detected LAN IP on startup) |
| ๐ Smart Retrieval | Bypasses LLM reformulation on first turn; auto-tunes k for local vs cloud LLMs |
| ๐งช Fully Tested | 60-test suite covering safety, RAG, CLI parsing, and Ollama utilities |
๐ก Use Cases
GuardRAG is designed for professionals and organizations that handle sensitive data and need the power of LLMs without sacrificing privacy.
๐ Secure Contract & Legal Document Analysis
Lawyers and compliance officers can query confidential contracts, NDAs, and legal briefs entirely on a local machine. No data ever leaves your network. The Confidential or Restricted sensitivity tiers automatically block queries and answers that would expose sensitive clauses.
๐ฅ Healthcare & Clinical Research
Analyze patient records, clinical trial PDFs, or research papers with PII redaction enabled. GuardRAG replaces names, SSNs, and medical identifiers with tokens before indexing โ the LLM never sees raw patient data.
๐ฆ Financial & Audit Reports
Query earnings reports, internal financial statements, or audit PDFs. The Restricted tier blocks any attempt to extract account numbers, salary data, or financial model details through either the input or output channel.
๐ Enterprise Internal Knowledge Base
Build an internal Q&A system over HR policies, onboarding docs, or engineering runbooks. Run it on a company server (LAN mode) so the entire team can access it from their browsers โ no external API calls needed.
๐ Research & Academic Literature
Academics can index large corpora of papers and query across them with exact citations. The source-citation feature shows which paper each answer was drawn from.
๐ป Developer Documentation Assistant
Index local API docs, architecture decision records (ADRs), or README files and ask questions in natural language โ much faster than Ctrl+F searching.
๐ Air-Gapped Environments
Once HuggingFace embeddings and Ollama models are downloaded, GuardRAG operates with zero internet dependency. Perfect for classified or air-gapped systems.
โ๏ธ Data Sensitivity Tiers
The 4-tier safety engine runs entirely offline โ no cloud safety API is called.
| Tier | Badge | What it blocks |
|---|---|---|
| Public | ๐ข | Jailbreaks, prompt injections, DAN-mode, ignore instructions |
| Internal | ๐ต | + API keys, bearer tokens, passwords, credentials |
| Confidential | ๐ก | + SSNs, emails, phone numbers, credit card numbers |
| Restricted | ๐ด | + Medical records, diagnoses, HIPAA/GDPR data, salary info |
Both input (user question) and output (LLM answer) are independently checked. A blocked response is replaced with a [REDACTED] message and logged in the Security Sandbox Auditor.
๐ฅ Installation
pip install guard-rag
Prerequisites
-
Ollama (for local/offline use): Install from ollama.com then pull a model:
ollama pull gemma3:1b # lightweight โ good for most docs ollama pull llama3.1 # more capable โ requires more RAM
-
Windows Users: Install the Microsoft Visual C++ Redistributable โ required for PyTorch/FAISS on Windows.
-
Cloud API (optional): Set your API key as an environment variable:
# .env file or shell export OPENAI_API_KEY=sk-... GROQ_API_KEY=gsk_... OPENROUTER_API_KEY=sk-or-...
๐ Quick Start
Option A โ Web Interface (recommended)
Run with no arguments to launch the browser UI:
guard-rag
The terminal will print your access URLs:
ACCESS (Local): http://127.0.0.1:8000
ACCESS (LAN): http://10.0.0.5:8000 โ share with your team
Web UI Workflow:
- Upload โ drag & drop PDF, TXT, or DOCX files into the drop zone
- Configure โ choose your model, sensitivity tier, and optional PII redaction
- Process โ click "Process Documents" to build the vector index
- Chat โ switch to the Chat tab and start asking questions
- Citations โ every answer shows the source document and relevance score
- Library โ reopen past document sessions without re-uploading
Option B โ CLI Mode
Chat with a single document directly in your terminal:
guard-rag --pdf path/to/document.pdf
๐ CLI Reference
guard-rag [OPTIONS]
| Option | Description | Default |
|---|---|---|
| (no args) | Launch the Web UI | โ |
--pdf <file> |
Path to document (PDF, TXT, DOCX) | Required for CLI mode |
--model <name> |
LLM model name (Ollama or cloud) | gemma3:1b |
--ollama-host <url> |
Ollama or OpenAI-compatible endpoint | http://localhost:11434 |
--sensitivity <level> |
Public / Internal / Confidential / Restricted |
Internal |
--chunk-size <int> |
Token size per document chunk | 1000 |
--chunk-overlap <int> |
Overlap tokens between adjacent chunks | 200 |
--no-guardrails |
Disable safety checks entirely | False |
--help |
Show help and exit | โ |
CLI Examples
# Local model, Confidential-level safety
guard-rag --pdf contracts/nda.pdf --model llama3.1 --sensitivity Confidential
# Cloud model via Groq (fast inference)
guard-rag --pdf research.pdf --model llama-3.1-8b-instant \
--ollama-host https://api.groq.com --sensitivity Internal
# Cloud model via OpenRouter
guard-rag --pdf report.pdf --model openai/gpt-4o \
--ollama-host https://openrouter.ai/api --sensitivity Restricted
# Tighter chunking for dense technical documents
guard-rag --pdf api_docs.pdf --chunk-size 500 --chunk-overlap 100
๐ Python SDK
Use GuardRAG directly in your Python code:
from guardrag import build_rag_chain, load_stored_rag_chain
# โโ Build a new RAG chain from documents โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
db_id, chain = build_rag_chain(
file_paths=["report.pdf", "policy.docx"],
model="gemma3:1b",
ollama_host="http://localhost:11434",
sensitivity="Confidential", # optional: passed through to check_input_safety
redact_pii=True, # auto-redact names, emails, SSNs before indexing
manual_redactions=["ProjectX"], # custom words to scrub from documents
system_prompt=None, # use the default GuardRAG prompt
)
# โโ Query the chain โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
result = chain.invoke({
"input": "What are the key obligations of the licensee?",
"chat_history": []
})
print(result["answer"])
for citation in result.get("context", []):
print(f" Source: {citation.metadata['source']}")
# โโ Reload a persisted session later (no re-upload) โโโโโโโโโโโโโโโโโโโโโโโโโโ
chain = load_stored_rag_chain(db_id=db_id, model="gemma3:1b")
Safety Utilities
from guardrag.utils.safety import check_input_safety, check_output_safety
# Returns None if clean, or a block message string if flagged
blocked = check_input_safety(
user_input="What is the patient's diagnosis?",
sensitivity="Restricted",
enabled=True,
custom_rules=["ProjectX", "salary breakdown"],
)
if blocked:
print(f"Blocked: {blocked}")
๐๏ธ Architecture
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ GuardRAG โ
โ โ
โ โโโโโโโโโโโโ โโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโโโ โ
โ โ Frontend โโโโโถโ FastAPI API โโโโโถโ Safety Engine โ โ
โ โ (Web UI) โ โ /api/upload โ โ Input + Output โ โ
โ โโโโโโโโโโโโ โ /api/chat โ โ 4-Tier Guardrail โ โ
โ โโโโโโโโฌโโโโโโโโ โโโโโโโโโโโโโโโโโโโโโ โ
โ โ โ
โ โโโโโโโโผโโโโโโโโโโโโโโโโโโโโโ โ
โ โ RAG Core โ โ
โ โ โโโโโโโโโโโโโโโโโโโโโโโ โ โ
โ โ โ Document Loaders โ โ โ
โ โ โ PDF / TXT / DOCX โ โ โ
โ โ โโโโโโโโโโฌโโโโโโโโโโโโโ โ โ
โ โ โ โ โ
โ โ โโโโโโโโโโผโโโโโโโโโโโโโ โ โ
โ โ โ PII Redactor โ โ โ
โ โ โ (optional) โ โ โ
โ โ โโโโโโโโโโฌโโโโโโโโโโโโโ โ โ
โ โ โ โ โ
โ โ โโโโโโโโโโผโโโโโโโโโโโโโ โ โ
โ โ โ Text Splitter โ โ โ
โ โ โ RecursiveChar โ โ โ
โ โ โโโโโโโโโโฌโโโโโโโโโโโโโ โ โ
โ โ โ โ โ
โ โ โโโโโโโโโโผโโโโโโโโโโโโโ โ โ
โ โ โ Vector Store โ โ โ
โ โ โ FAISS / Qdrant / โ โ โ
โ โ โ Chroma โ โ โ
โ โ โโโโโโโโโโฌโโโโโโโโโโโโโ โ โ
โ โ โ โ โ
โ โ โโโโโโโโโโผโโโโโโโโโโโโโ โ โ
โ โ โ GuardRAGChain โ โ โ
โ โ โ LLM (Ollama/Cloud) โ โ โ
โ โ โโโโโโโโโโโโโโโโโโโโโโโ โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Key design decisions:
GuardRAGChainbypasses the history-aware LLM reformulation call on the first turn (saves ~1 LLM round-trip)- Documents are chunked with
sourcemetadata so citations always show the originating filename - Cloud LLMs use
k=8retrieved chunks; local models usek=4to avoid context overflow - Word-boundary pattern matching in the safety engine prevents false positives on substring matches
๐ Supported Models & Endpoints
| Provider | Endpoint URL | Example Model |
|---|---|---|
| Ollama (local) | http://localhost:11434 |
gemma3:1b, llama3.1, mistral |
| Groq | https://api.groq.com |
llama-3.1-8b-instant |
| OpenRouter | https://openrouter.ai/api |
openai/gpt-4o, meta-llama/llama-3-8b |
| OpenAI | https://api.openai.com |
gpt-4o, gpt-4o-mini |
| Anthropic | https://api.anthropic.com |
claude-3-5-sonnet |
| Cohere | https://api.cohere.ai |
command-r |
| Any OpenAI-compatible | Custom URL | Any model name |
๐ Supported File Types
| Format | Extension | Notes |
|---|---|---|
.pdf |
Full text extraction via pypdf |
|
| Plain Text | .txt |
UTF-8 encoding |
| Word Documents | .docx, .doc |
Via docx2txt |
๐งช Running Tests
# Run the full test suite (60 tests)
python -m pytest
# Verbose output with coverage
python -m pytest -v --cov=guardrag
๐ค Contributing
Contributions, bug reports, and feature requests are welcome!
See CONTRIBUTING.md for guidelines.
Built with โค๏ธ by Sowmiyan S
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file guard_rag-1.2.7.tar.gz.
File metadata
- Download URL: guard_rag-1.2.7.tar.gz
- Upload date:
- Size: 88.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8531a25da67feb616738d1350c7851b409bc634cf2891411167e3fc8c67cd362
|
|
| MD5 |
a2b889263b03b052e08da14178a407d0
|
|
| BLAKE2b-256 |
3937dae6fa44b0e38b4e8f12b55706d3620f1cb2ed7a52a4713125e40ea1d5cc
|
File details
Details for the file guard_rag-1.2.7-py3-none-any.whl.
File metadata
- Download URL: guard_rag-1.2.7-py3-none-any.whl
- Upload date:
- Size: 74.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
34ca20ec7a383f83f2b1e36bf74c534522d1f77c8844b3a3118e29222e4a5c51
|
|
| MD5 |
bbd20207eaa0604f1ee0d4700d0b57e9
|
|
| BLAKE2b-256 |
830d37810b5902155ebac71f10ae21f15e526ad8768872edcacff2f2736eb458
|