An optimized, full-stack Retrieval-Augmented Generation (RAG) architecture utilizing hybrid vector search and ONNX-based cross-encoder reranking to deliver highly accurate, document-grounded technical intelligence.
This system was specifically engineered to query dynamic, version-dependent automotive specifications (e.g., BMW Indonesia documentation) while strictly adhering to a 512MB memory constraint in a serverless cloud environment.
The pipeline is split into two primary workflows:
- Ingestion Pipeline: Scrapes dynamic web pages and PDFs (using Playwright and BeautifulSoup), parses them into contextual markdown chunks, generates 384-dimensional dense embeddings via
fastembed, and stores them in a Supabase PostgreSQL database. - Execution Pipeline: Processes user queries through a contextual rewriting agent, retrieves relevant documents via a hybrid search algorithm, reranks the candidates using a Cross-Encoder, and streams a hallucination-free response via the Groq LLM API.
flowchart TD
%% Missing Node A defined here
A[(BMW Documentation<br/>Oto.com)]
subgraph INGEST["PHASE 1 — DATA INGESTION"]
direction TB
B["Scraping & Extraction<br/>(Playwright / BeautifulSoup)"]
C["Cleaning & Chunking<br/>(Context-Aware Markdown)"]
D["Embedding Generation<br/>(fastembed + ONNX MiniLM)"]
E[("Supabase PostgreSQL<br/>(Text + Metadata + pgvector)")]
end
A --> B --> C --> D --> E
F(["User Query"])
subgraph QUERY["PHASE 2 — QUERY EXECUTION"]
direction TB
G["Contextual Query Rewriting<br/>(gpt-oss-120b)"]
H{"Hybrid Retrieval"}
I["Dense Search<br/>(pgvector)"]
J["Lexical Search<br/>(PostgreSQL/BM25)"]
K["Reciprocal Rank Fusion<br/>(RRF)"]
L["Top 10 Candidates"]
M["Cross-Encoder Reranking<br/>(MS MARCO)"]
N["Top K Relevant Chunks"]
O["Generation LLM<br/>(gpt-oss-120b)"]
P(["Grounded Answer<br/>(Citations + Latency Metrics)"])
end
F --> G --> H
H -->|Semantic| I
H -->|Keyword| J
I --> K
J --> K
K --> L --> M --> N --> O --> P
%% Connecting the database to the query phase
E -.->|Supplies Indexed Data| H
- Frontend: Next.js, TailwindCSS, Vercel
- Backend: FastAPI, Python, Render
- Vector Database: PostgreSQL,
pgvector, Supabase - Machine Learning:
fastembed(ONNX Runtime), Hugging Face (MiniLM-L6-v2) - LLM Engine: Groq API (Llama / OSS Models)
- Hybrid Dense/Sparse Retrieval: Merges semantic similarity (pgvector dense embeddings) with exact keyword matching (PostgreSQL Full-Text Search / BM25) using Reciprocal Rank Fusion (RRF) to handle highly specific alphanumeric queries (e.g., "M256 engine").
- Cross-Encoder Reranking: Implements an MS-MARCO Cross-Encoder to evaluate the logical relevance of retrieved chunks against the query before passing context to the LLM.
- Extreme Memory Optimization: Stripped all heavy PyTorch dependencies and replaced them with lightweight ONNX runtimes. This reduced the active RAM footprint from ~550MB to under 100MB, eliminating Out-Of-Memory (OOM) crashes on Render's free tier.
- Automated CI/CD Evaluation: Integrated GitHub Actions to run a 75-query benchmark suite against the retrieval pipeline on every push to ensure metric stability.
Evaluated against a ground-truth dataset of 100 complex and diverse technical queries:
| Metric | Score | Note |
|---|---|---|
| Context Recall@5 | 99.0% | Successfully retrieves the correct documentation chunk within the top 5 results. |
| MRR (Mean Reciprocal Rank) | 0.8133 | High precision in placing the most relevant document at the top of the stack. |
| Retrieval Latency | 396ms P50 / 918ms P95 | End-to-end database query and Cross-Encoder execution time. |
- Python 3.11+
- PostgreSQL database with
pgvectorenabled - Groq API Key
- Clone the repository:
git clone [https://github.com/yourusername/version-rag-engine.git](https://github.com/yourusername/version-rag-engine.git) cd version-rag-engine
2. **Install dependencies:**
```bash
pip install -r requirements.txt
- Environment Variables:
Create a
.envfile in the root directory:
SUPABASE_URL=your_supabase_url
SUPABASE_KEY=your_supabase_key
GROQ_API_KEY=your_groq_api_key
- Run the Database Ingestion (Optional):
python src/pipeline.py
- Start the API Server:
uvicorn src.api:app --reload --port 8000