CoolFace
Apppublic

ajithreddy777/postgresql-rag-assistant

sourceHugging Faceupdated 7mo agoView on Hugging Face
0likes
App README

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:

CategoryExampleBehavior
Narrow factualβ€œWhat data types support TOAST?”High-confidence answer
Broad / synthesisβ€œWhat are the limitations of logical replication?”Partial explanation or fallback
Out-of-domainβ€œWho is the CEO of Google?”Rejected
Hallucination trapsβ€œPostgreSQL AI optimizer”Rejected

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