Originally published at hrdnsh.com by Haradhan Sharma, Senior Enterprise Operations Leader & Chief Architect.
Retrieval-Augmented Generation (RAG) is the most practical, cost-effective enterprise AI architecture in existence. Instead of fine-tuning multi-billion parameter foundation models or paying massive SaaS subscriptions, RAG dynamically retrieves internal company records at query time and grounds the LLM in verified facts.
When properly architected, RAG boosts factual accuracy from ~60% (raw foundation LLM) to over 95%, while reducing hallucinations to near zero.
Here is the complete engineering blueprint for building an enterprise-grade, private RAG system.
1. System Architecture Overview
A production RAG infrastructure consists of 5 tightly integrated layers:
[Raw Documents (PDF, DB, Docs)]
│
▼
1. Document Ingestion (Semantic Chunking: 250-500 tokens, 15% overlap)
│
▼
2. Vector Embedding Engine (BGE-Large / text-embedding-3-small)
│
▼
3. Vector Storage & Relational Index (PostgreSQL + pgvector HNSW)
│
▼
4. Hybrid Retrieval & Re-ranking (Dense Vector + BM25 Sparse + Cross-Encoder)
│
▼
5. Private LLM Inference (vLLM / Ollama: LLaMA 3.3, Mistral) ──► Grounded Response
2. Ingestion & Semantic Chunking
Raw enterprise documents are messy: PDFs contain recurring running headers, footers, and complex multi-column tables.
Golden Rules of Enterprise Chunking:
- Never use fixed character chunking blindly: Chunking strictly by character count breaks sentences midway and fragments contextual logic.
- Target Token Range: The optimal sweet spot is 250 to 500 tokens per chunk with a 10% to 15% overlap.
-
Structured vs Unstructured: For legal agreements and policy PDFs, use
RecursiveCharacterTextSplitter. For industrial tabular records (ERP reports, BOM lists), extract data into Markdown tables before embedding to maintain column-row semantic affinity.
3. Embedding & Vector Storage with PostgreSQL
Store your embeddings natively inside PostgreSQL using the pgvector extension. This guarantees that document metadata, user access permissions (Row-Level Security), and vector indexes reside inside a single transactional boundary.
-- Create extension and documents table
CREATE EXTENSION IF NOT EXISTS vector;
CREATE TABLE enterprise_knowledge (
id BIGSERIAL PRIMARY KEY,
document_title VARCHAR(255) NOT NULL,
chunk_index INT NOT NULL,
content TEXT NOT NULL,
metadata JSONB DEFAULT '{}',
embedding vector(1536) -- Matches standard embedding dimensions
);
-- Build high-speed HNSW index for sub-5ms cosine retrieval
CREATE INDEX ON enterprise_knowledge
USING hnsw (embedding vector_cosine_ops)
WITH (m = 16, ef_construction = 64);
4. Advanced Retrieval: Hybrid Search & Re-ranking
Basic vector similarity search often fails on exact keyword matching (part numbers, invoice serials, specific employee names).
The Solution: Hybrid Search
Combine dense semantic search with sparse keyword search (BM25 or PostgreSQL tsvector):
-
Step 1: Retrieve top 20 candidate chunks via vector similarity (
<=>). -
Step 2: Retrieve top 20 candidate chunks via Full-Text Search (
tsvector @@ plainto_tsquery). - Step 3 (Reciprocal Rank Fusion): Merge candidate pools using RRF scoring.
-
Step 4 (Cross-Encoder Re-ranking): Pass the top 15 candidate chunks through a local re-ranker model (such as
bge-reranker-v2-m3) to calculate precise query-to-context relevance. Pass only the top 3-5 re-ranked chunks to the generator LLM.
5. LLM Prompt Guardrails & Hallucination Mitigation
The generator prompt must enforce strict boundaries:
You are a factual enterprise assistant. Answer the user's question ONLY using the provided context chunks below.
If the context does not contain sufficient facts to answer accurately, explicitly state: "I cannot find sufficient documentation in the knowledge base to verify this."
Do not extrapolate, assume, or utilize outside knowledge.
Context Chunks:
{context}
Question:
{question}
Set temperature between 0.0 and 0.2 for factual enterprise tasks.
6. Continuous Automated Evaluation (Ragas Framework)
Never deploy RAG without quantitative evaluation. Use frameworks like Ragas to track 3 core metrics continuously:
- Faithfulness: Quantifies whether every statement in the generated answer is grounded in the retrieved context (detects hallucinations).
- Answer Relevance: Quantifies whether the generated response directly addresses the user query.
- Context Precision: Quantifies the signal-to-noise ratio of your retrieval pipeline.
Looking to deploy a sovereign, private RAG pipeline or eliminate third-party AI SaaS fees? Explore production architectures at hrdnsh.com/services/agentic-ai-rag-orchestration/ or get in touch with Haradhan Sharma at hrdnsh.com.
Top comments (0)