A modular Retrieval-Augmented Generation (RAG) engine built from scratch using FastAPI, PostgreSQL (pgvector), Sentence Transformers, and OpenRouter.
Upload a PDF document to extract text, generate embeddings, and index it for retrieval.
Ask natural language questions against the indexed documents.
The engine retrieves relevant chunks using Hybrid Retrieval (Dense Retrieval + BM25 + RRF), reranks them with a Cross-Encoder, and generates a grounded answer with source citations.
- PDF document ingestion
- Metadata-aware chunking
- Semantic embeddings
- PostgreSQL + pgvector vector search
- BM25 lexical retrieval
- Hybrid Retrieval (RRF)
- Cross-Encoder reranking
- Context construction
- Prompt construction
- Grounded LLM responses
- Source citations
- Query observability
PDF Documents
│
▼
Metadata Chunking
│
▼
Embedding Generation
│
▼
PostgreSQL + pgvector
──────────────────────────────────────────────
User Question
│
┌───────────┴───────────┐
▼ ▼
Query Embedding BM25 Retrieval
│ │
▼ ▼
Vector Search Lexical Search
└───────────┬───────────┘
▼
Reciprocal Rank Fusion
│
▼
Cross-Encoder Reranker
│
▼
Context Builder
│
▼
Prompt Builder
│
▼
OpenRouter LLM
│
▼
Grounded Response
│
▼
Source Citations & Metrics
| Method | Endpoint | Description |
|---|---|---|
| POST | /ingest |
Upload and index PDF documents |
| POST | /search |
Hybrid retrieval and grounded question answering |
- FastAPI
- PostgreSQL
- pgvector
- SQLAlchemy
- Sentence Transformers
- PyMuPDF
- OpenRouter
- Dense Retrieval
- Sparse Retrieval (BM25)
- Hybrid Retrieval
- Reciprocal Rank Fusion (RRF)
- Cross-Encoder Reranking
- Semantic Search
- Vector Databases
- Context Engineering
- Prompt Engineering
- Grounded Generation
- Source Attribution
- Retrieval Observability
- Extract text from PDF documents.
- Split documents into metadata-aware chunks.
- Generate semantic embeddings.
- Store embeddings in PostgreSQL using pgvector.
- Generate the query embedding.
- Retrieve semantic candidates using vector search.
- Retrieve lexical candidates using BM25.
- Fuse both rankings using Reciprocal Rank Fusion (RRF).
- Rerank the fused candidates using a Cross-Encoder.
- Build contextual prompts from the highest-ranked chunks.
- Generate grounded responses using OpenRouter.
- Return answers with source citations and observability metrics.


