Skip to main content

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

PyPI version Python Ollama FAISS LangChain License


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

  1. 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
    
  2. Windows Users: Install the Microsoft Visual C++ Redistributable โ€” required for PyTorch/FAISS on Windows.

  3. 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:

  1. Upload โ€” drag & drop PDF, TXT, or DOCX files into the drop zone
  2. Configure โ€” choose your model, sensitivity tier, and optional PII redaction
  3. Process โ€” click "Process Documents" to build the vector index
  4. Chat โ€” switch to the Chat tab and start asking questions
  5. Citations โ€” every answer shows the source document and relevance score
  6. 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:

  • GuardRAGChain bypasses the history-aware LLM reformulation call on the first turn (saves ~1 LLM round-trip)
  • Documents are chunked with source metadata so citations always show the originating filename
  • Cloud LLMs use k=8 retrieved chunks; local models use k=4 to 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 .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

GitHub ยท PyPI ยท Issues

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

guard_rag-1.2.7.tar.gz (88.9 kB view details)

Uploaded Source

Built Distribution

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

guard_rag-1.2.7-py3-none-any.whl (74.5 kB view details)

Uploaded Python 3

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

Hashes for guard_rag-1.2.7.tar.gz
Algorithm Hash digest
SHA256 8531a25da67feb616738d1350c7851b409bc634cf2891411167e3fc8c67cd362
MD5 a2b889263b03b052e08da14178a407d0
BLAKE2b-256 3937dae6fa44b0e38b4e8f12b55706d3620f1cb2ed7a52a4713125e40ea1d5cc

See more details on using hashes here.

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

Hashes for guard_rag-1.2.7-py3-none-any.whl
Algorithm Hash digest
SHA256 34ca20ec7a383f83f2b1e36bf74c534522d1f77c8844b3a3118e29222e4a5c51
MD5 bbd20207eaa0604f1ee0d4700d0b57e9
BLAKE2b-256 830d37810b5902155ebac71f10ae21f15e526ad8768872edcacff2f2736eb458

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