ajithreddy777/postgresql-rag-assistant
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
π PostgreSQL Enterprise RAG β Design & Architecture
Overview
This project implements an enterprise-grade Retrieval-Augmented Generation (RAG) system designed to answer PostgreSQL documentation questions safely, transparently, and reliably.
The core design principle is:
Correctness and groundedness are more important than always returning an answer.
The system explicitly prefers rejection or partial answers over hallucinated or weakly supported responses.
Design Goals
The system was built with the following goals:
- Zero hallucinations for out-of-domain or unsupported questions
- Clear confidence signaling for every answer
- Explainable rejection behavior
- Scalable retrieval architecture
- Production-aligned failure modes
This is not a βchatbot demoβ; it is a decision-driven RAG system.
High-Level Architecture
The system follows a multi-stage retrieval and evaluation pipeline:
User Question
β
Local Vector Retrieval (FAISS)
β
Strict Generation (Context-Only)
β
LLM-based Evaluation (Groundedness Scoring)
β
Decision Routing
ββ Accept (High confidence)
ββ Fallback to Official Docs (Partial coverage)
ββ Reject (Insufficient grounding)Each stage is explicitly controlled and observable.
Stage 1: Local Retrieval (Primary Source)
The first retrieval stage uses a FAISS vector index built from PostgreSQL documentation.
Why local first?
- Fast and deterministic
- No network dependency
- Known data boundary
- Cost-efficient
Only semantic similarity search is used β no keyword filtering β to avoid false negatives for expert-level queries (e.g., internal PostgreSQL terms like TOAST).
Stage 2: Strict Generation (No Speculation)
The generation step is intentionally conservative.
Rules enforced at generation time:
- The model may answer only using retrieved context
- No procedural instructions (e.g., βgo to the websiteβ)
- No speculation or inference
- If information is missing, the model must explicitly say so
This prevents plausible-sounding but unsupported answers.
Stage 3: Evaluation (LLM-as-a-Critic)
Instead of trusting the generated answer, the system uses a second LLM pass to evaluate:
- Factual correctness
- Completeness
- Grounding in retrieved documentation
The evaluator produces a numerical confidence score (0β1).
This score is used for routing decisions, not just display.
Stage 4: Intelligent Routing
Based on the evaluation score and retrieval coverage, the system chooses one of three paths:
β Accept (High Confidence)
- Score β₯ threshold (default: 0.8)
- Answer is shown as authoritative
β οΈ Partial / Fallback (Category 2 Questions)
- Documentation covers parts of the topic
- System explicitly explains limitations
- Optionally falls back to official PostgreSQL online documentation
This is critical for broad questions like:
- βWhat changed between PostgreSQL 10 and 11?β
- βWhat are the limitations of logical replication?β
β Reject (Safety First)
- Score below threshold
- Unsupported or out-of-domain queries
- Hallucination traps
The system explains why the question was rejected.
Official Documentation Fallback
If local documentation is insufficient, the system optionally fetches content from:
https://www.postgresql.org/docs/Important constraints:
- Only official PostgreSQL documentation is used
- No general web search
- No blogs, forums, or third-party sources
This extends coverage without compromising trust.
Category-Aware Behavior
The system intentionally handles different question types differently:
This distinction is by design, not accidental.
Why This Design Works in Production
1. No Blind Trust in the LLM
The model does not decide correctness β the system does.
2. Explicit Failure Modes
Every rejection is intentional and explainable.
3. Data-Bound Reasoning
The system cannot answer beyond what is indexed or officially retrieved.
4. Enterprise-Safe Defaults
- Conservative thresholds
- Preference for false negatives over false positives
- Transparency over confidence theater
What This System Is (and Is Not)
β Is
- Production-aligned RAG architecture
- Trustworthy documentation assistant
- Interview-ready system design
- Open-source friendly
β Is Not
- A generic chatbot
- A prompt-only solution
- A system that βalways answersβ
- A black box
Key Takeaway
RAG systems should not optimize for fluency. They should optimize for trust.
This project demonstrates how to build a RAG system that:
- Knows what it knows
- Knows what it doesnβt know
- Communicates that clearly to users
