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ilaa-1505/Explainable-RAG-System)**
|
| 10 |
+
> If the Space is sleeping, it may take ~30 seconds to wake up on first visit. Startup may also be slow as the embedding model and reranker weights load into memory.
|
| 11 |
+
|
| 12 |
+
Most RAG systems are black boxes. You type a question, get an answer, and have no idea what happened in between.
|
| 13 |
+
|
| 14 |
+
Which documents were retrieved. Why one chunk ranked above another. How the query was even interpreted.
|
| 15 |
+
|
| 16 |
+
This project opens that box. Every stage of the retrieval pipeline is visible and interactive:
|
| 17 |
+
- how your query gets tokenized and embedded
|
| 18 |
+
- how BM25 and vector search disagree
|
| 19 |
+
- how MMR trades off relevance for diversity
|
| 20 |
+
- why the reranker promotes some chunks and drops others
|
| 21 |
+
|
| 22 |
+
Ask a question. See exactly how the answer was built.
|
| 23 |
+
|
| 24 |
+
---
|
| 25 |
+
|
| 26 |
+
## Demo
|
| 27 |
+
|
| 28 |
+
> 
|
| 29 |
+
|
| 30 |
+
---
|
| 31 |
+
|
| 32 |
+
## What's inside
|
| 33 |
+
|
| 34 |
+
### Answer
|
| 35 |
+
Ask anything about the HuggingFace Transformers documentation. The system retrieves relevant chunks, reranks them, and generates an answer using Llama 3.1 via Groq.
|
| 36 |
+
|
| 37 |
+
> 
|
| 38 |
+
|
| 39 |
+
---
|
| 40 |
+
|
| 41 |
+
### Latency Timeline
|
| 42 |
+
Every stage of the pipeline: embed, vector search, BM25, hybrid fusion, MMR, rerank, LLM, broken down by time. You can see exactly where the bottleneck is.
|
| 43 |
+
|
| 44 |
+
Below it, query analysis shows each token's IDF score, its position in the embedding space, and an overall complexity rating.
|
| 45 |
+
|
| 46 |
+
> 
|
| 47 |
+
> 
|
| 48 |
+
|
| 49 |
+
---
|
| 50 |
+
|
| 51 |
+
### Retrieval Comparison
|
| 52 |
+
A table showing every candidate chunk with its Vector, BM25, Hybrid, and Reranker scores side by side. You can see which chunks got promoted, which got dropped, and by how much.
|
| 53 |
+
|
| 54 |
+
> 
|
| 55 |
+
|
| 56 |
+
---
|
| 57 |
+
|
| 58 |
+
### MMR Visualization
|
| 59 |
+
MMR (Maximal Marginal Relevance) balances relevance and diversity when selecting chunks. This tab shows that tradeoff visually, a UMAP scatter of all candidates, a similarity matrix, and a live Ξ» slider to see how the selection changes in real time.
|
| 60 |
+
|
| 61 |
+
> 
|
| 62 |
+
|
| 63 |
+
---
|
| 64 |
+
|
| 65 |
+
### Context Chunks
|
| 66 |
+
The final chunks passed to the LLM, each with its reranker score and source URL.
|
| 67 |
+
|
| 68 |
+
> 
|
| 69 |
+
|
| 70 |
+
---
|
| 71 |
+
|
| 72 |
+
## Retrieval Pipeline
|
| 73 |
+
|
| 74 |
+
```
|
| 75 |
+
Query β Embed β Vector Search + BM25 β Hybrid Fusion β MMR β Rerank β LLM
|
| 76 |
+
```
|
| 77 |
+
|
| 78 |
+
- **Vector search** β BAAI/bge-small-en-v1.5 embeddings via ChromaDB
|
| 79 |
+
- **BM25** β keyword search with BM25Okapi
|
| 80 |
+
- **Hybrid fusion** β weighted combination of both (Ξ± = 0.7)
|
| 81 |
+
- **MMR** β removes redundant chunks while preserving relevance
|
| 82 |
+
- **Reranker** β cross-encoder/ms-marco-MiniLM-L-6-v2 for final scoring
|
| 83 |
+
- **LLM** β Llama 3.1 8B via Groq API
|
| 84 |
+
|
| 85 |
+
---
|
| 86 |
+
|
| 87 |
+
## Stack
|
| 88 |
+
|
| 89 |
+
- **Backend** β Flask
|
| 90 |
+
- **Embeddings** β sentence-transformers, ChromaDB
|
| 91 |
+
- **Retrieval** β rank-bm25, UMAP
|
| 92 |
+
- **Reranker** β CrossEncoder (ms-marco-MiniLM-L-6-v2)
|
| 93 |
+
- **LLM** β Llama 3.1 via Groq
|
| 94 |
+
- **Frontend** β Vanilla JS
|
| 95 |
+
|
| 96 |
+
---
|
| 97 |
+
|
| 98 |
+
## Run it yourself
|
| 99 |
+
|
| 100 |
+
```bash
|
| 101 |
+
git clone https://github.com/ilaa-1505/Explainable-RAG-System
|
| 102 |
+
cd Explainable-RAG-System
|
| 103 |
+
pip install -r requirements.txt
|
| 104 |
+
```
|
| 105 |
+
|
| 106 |
+
Set up your Groq API key:
|
| 107 |
+
```bash
|
| 108 |
+
echo "GROQ_API_KEY=your_key_here" > .env
|
| 109 |
+
```
|
| 110 |
+
|
| 111 |
+
Build the index:
|
| 112 |
+
```bash
|
| 113 |
+
python src/ingestion/fetch_docs.py
|
| 114 |
+
python src/ingestion/chunk.py
|
| 115 |
+
python src/retrieval/embed_store.py
|
| 116 |
+
```
|
| 117 |
+
|
| 118 |
+
Run:
|
| 119 |
+
```bash
|
| 120 |
+
python app.py
|
| 121 |
+
```
|
| 122 |
+
|
| 123 |
+
> First startup takes a minute, the embedding model (BAAI/bge-small-en-v1.5) and reranker (ms-marco-MiniLM-L-6-v2) weights load into memory on first query.
|
| 124 |
+
|
| 125 |
+
---
|
| 126 |
+
|
| 127 |
+
## Things I learned building this
|
| 128 |
+
|
| 129 |
+
- BM25 consistently outranks vector search on exact keyword matches, hybrid fusion is genuinely worth the complexity
|
| 130 |
+
- MMR's Ξ» parameter matters more than expected, at Ξ»=1.0 the top chunks are nearly identical; at Ξ»=0.5 the diversity is visible in the UMAP
|
| 131 |
+
- The reranker and vector search frequently disagree on rank. the retrieval comparison table makes this obvious
|
| 132 |
+
|
| 133 |
+
---
|
| 134 |
+
|
| 135 |
+
## What's next
|
| 136 |
+
|
| 137 |
+
- Support for other documentation sources beyond HuggingFace Transformers
|
| 138 |
+
- Side by side comparison of retrieval strategies on the same query
|