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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# ๐ฌ RAG Visualizer
|
| 2 |
+
|
| 3 |
+
**An X-Ray machine for Retrieval-Augmented Generation pipelines.**
|
| 4 |
+
|
| 5 |
+
RAG Visualizer is an interactive, local-first tool that lets you **see** what happens inside a RAG pipeline โ from how your text gets chunked, to how those chunks land in vector space, to which chunks get retrieved for a given query. No cloud APIs, no black boxes. Everything runs on your machine with local Ollama models.
|
| 6 |
+
|
| 7 |
+
---
|
| 8 |
+
|
| 9 |
+
## โจ Features
|
| 10 |
+
|
| 11 |
+
### ๐งช Phase 1 โ Chunking Lab
|
| 12 |
+
|
| 13 |
+
Visualize and compare **5 chunking strategies** side-by-side:
|
| 14 |
+
|
| 15 |
+
| Strategy | Description |
|
| 16 |
+
| ---------------- | --------------------------------------------------------------------------------- |
|
| 17 |
+
| **Fixed Size** | Cuts text every N tokens with configurable overlap |
|
| 18 |
+
| **Sentence** | Splits on sentence boundaries using NLTK tokenizer |
|
| 19 |
+
| **Recursive** | Applies a hierarchy of separators (`\n\n` โ `\n` โ `. ` โ ` `) |
|
| 20 |
+
| **Parent-Child** | Two-level nested chunking โ large parent windows with smaller child chunks inside |
|
| 21 |
+
| **Semantic** | Detects topic shifts using embedding similarity + adaptive thresholding |
|
| 22 |
+
|
| 23 |
+
- **Document X-Ray Viewer** โ Original text with color-coded chunk boundaries and overlap regions
|
| 24 |
+
- **Chunk Inspector** โ Stats panel showing total chunks, average token count, and per-chunk metadata
|
| 25 |
+
|
| 26 |
+
### ๐ Phase 2 โ Embedding Lab
|
| 27 |
+
|
| 28 |
+
- Generate embeddings using **3 local Ollama embedding models** (Nomic Embed Text, Embedding Gemma, Qwen3 Embedding)
|
| 29 |
+
- **UMAP dimensionality reduction** projects high-dimensional embeddings down to 2D
|
| 30 |
+
- **Interactive Canvas** with pan, zoom, hover tooltips, and click-to-select
|
| 31 |
+
- Parent-child connection lines visualized in vector space
|
| 32 |
+
|
| 33 |
+
### ๐ Phase 3 โ Retrieval
|
| 34 |
+
|
| 35 |
+
- **ChromaDB** persistent vector store โ chunks are indexed on every run
|
| 36 |
+
- **Sonar Query Simulator** โ type a natural language query and watch the retrieval happen in real time
|
| 37 |
+
- Retrieved chunks render as ranked result cards with distance scores
|
| 38 |
+
- **Sonar Probe** โ click anywhere on the canvas to find the nearest chunks by 2D proximity
|
| 39 |
+
- **Document X-Ray Highlighting** โ retrieved chunks glow in the original text with rank-based styling (gold for Rank 1, dashed for Rank 2, dotted for Rank 3)
|
| 40 |
+
|
| 41 |
+
### ๐ Phase 4 โ Adaptive Thresholding
|
| 42 |
+
|
| 43 |
+
- Semantic chunking uses a **gradient derivative method** instead of a static threshold
|
| 44 |
+
- Computes mean + z-score-scaled standard deviation of inter-sentence embedding distances
|
| 45 |
+
- The slider controls the z-score multiplier, making the boundary detection adaptive to each document's unique distribution
|
| 46 |
+
|
| 47 |
+
---
|
| 48 |
+
|
| 49 |
+
## ๐๏ธ Architecture
|
| 50 |
+
|
| 51 |
+
```mermaid
|
| 52 |
+
graph TB
|
| 53 |
+
subgraph Frontend ["Frontend (Vanilla HTML/CSS/JS)"]
|
| 54 |
+
UI[index.html] --> JS[app.js]
|
| 55 |
+
UI --> CSS[styles.css]
|
| 56 |
+
JS -->|Canvas 2D API| Canvas[Vector Space Renderer]
|
| 57 |
+
JS -->|DOM Manipulation| XRay[Document X-Ray Viewer]
|
| 58 |
+
end
|
| 59 |
+
|
| 60 |
+
subgraph Backend ["Backend (FastAPI + Python)"]
|
| 61 |
+
API[FastAPI Server] --> ChunkRouter["/api/chunk"]
|
| 62 |
+
API --> RetrievalRouter["/api/retrieve"]
|
| 63 |
+
|
| 64 |
+
ChunkRouter --> ChunkEngine[Chunking Engine]
|
| 65 |
+
ChunkRouter --> EmbedEngine[Embedding Engine]
|
| 66 |
+
ChunkRouter --> Reducer[UMAP Reducer]
|
| 67 |
+
ChunkRouter --> VStore[Vector Store]
|
| 68 |
+
|
| 69 |
+
RetrievalRouter --> EmbedEngine
|
| 70 |
+
RetrievalRouter --> VStore
|
| 71 |
+
|
| 72 |
+
ChunkEngine -->|5 Strategies| Splitters[LangChain + NLTK Splitters]
|
| 73 |
+
EmbedEngine -->|HTTP| Ollama[Ollama API :11434]
|
| 74 |
+
Reducer --> UMAP[umap-learn]
|
| 75 |
+
VStore --> ChromaDB[(ChromaDB)]
|
| 76 |
+
end
|
| 77 |
+
|
| 78 |
+
JS -->|fetch /api/chunk| ChunkRouter
|
| 79 |
+
JS -->|fetch /api/retrieve| RetrievalRouter
|
| 80 |
+
API -->|Static Files| UI
|
| 81 |
+
```
|
| 82 |
+
|
| 83 |
+
### Data Flow
|
| 84 |
+
|
| 85 |
+
1. **User pastes text** โ selects strategy + embedding model โ clicks **Run Chunking**
|
| 86 |
+
2. **Backend** splits text into chunks โ generates embeddings via Ollama โ reduces to 2D via UMAP โ stores in ChromaDB
|
| 87 |
+
3. **Frontend** renders the chunk boundaries in the X-Ray viewer and plots particles on the 2D canvas
|
| 88 |
+
4. **User queries** โ backend embeds the query โ retrieves top-K from ChromaDB โ projects query point into 2D
|
| 89 |
+
5. **Frontend** draws sonar lines from query to retrieved chunks, highlights them in the document viewer
|
| 90 |
+
|
| 91 |
+
---
|
| 92 |
+
|
| 93 |
+
## ๐ Folder Structure
|
| 94 |
+
|
| 95 |
+
```
|
| 96 |
+
RAG-Visualizer/
|
| 97 |
+
โโโ backend/
|
| 98 |
+
โ โโโ __init__.py
|
| 99 |
+
โ โโโ main.py # FastAPI app, CORS, static file serving
|
| 100 |
+
โ โโโ constants.py # LLM prompt templates
|
| 101 |
+
โ โโโ engines/
|
| 102 |
+
โ โ โโโ __init__.py
|
| 103 |
+
โ โ โโโ chunking.py # 5 chunking strategies + ChunkingEngine
|
| 104 |
+
โ โ โโโ embedding.py # Ollama embedding adapter (httpx)
|
| 105 |
+
โ โ โโโ llm_client.py # Ollama LLM generation client
|
| 106 |
+
โ โ โโโ reducer.py # UMAP 2D dimensionality reducer
|
| 107 |
+
โ โโโ models/
|
| 108 |
+
โ โ โโโ __init__.py
|
| 109 |
+
โ โ โโโ schemas.py # Pydantic models (request/response schemas)
|
| 110 |
+
โ โโโ routers/
|
| 111 |
+
โ โ โโโ __init__.py
|
| 112 |
+
โ โ โโโ chunk_router.py # POST /api/chunk โ chunking + embedding + UMAP
|
| 113 |
+
โ โ โโโ retrieval_router.py # POST /api/retrieve โ query + ChromaDB retrieval
|
| 114 |
+
โ โโโ storage/
|
| 115 |
+
โ โโโ vector_store.py # ChromaDB persistent client wrapper
|
| 116 |
+
โโโ frontend/
|
| 117 |
+
โ โโโ index.html # Single-page app (3-column layout)
|
| 118 |
+
โ โโโ app.js # All frontend logic, canvas rendering, API calls
|
| 119 |
+
โ โโโ styles.css # Superman theme design system
|
| 120 |
+
โโโ store/ # ChromaDB persistent data (gitignored)
|
| 121 |
+
โโโ .gitignore
|
| 122 |
+
โโโ .python-version # Python 3.11
|
| 123 |
+
โโโ dev.bat # Dev server launcher
|
| 124 |
+
โโโ pyproject.toml # Project metadata & dependencies
|
| 125 |
+
โโโ uv.lock # Locked dependency versions
|
| 126 |
+
โโโ README.md
|
| 127 |
+
```
|
| 128 |
+
|
| 129 |
+
---
|
| 130 |
+
|
| 131 |
+
## ๐ Getting Started
|
| 132 |
+
|
| 133 |
+
### Prerequisites
|
| 134 |
+
|
| 135 |
+
| Tool | Version | Purpose |
|
| 136 |
+
| ------------------------------------ | ------- | ---------------------------------- |
|
| 137 |
+
| **Python** | โฅ 3.11 | Runtime |
|
| 138 |
+
| **[uv](https://docs.astral.sh/uv/)** | Latest | Fast Python package manager |
|
| 139 |
+
| **[Ollama](https://ollama.com/)** | Latest | Local LLM & embedding model server |
|
| 140 |
+
|
| 141 |
+
### 1. Install uv
|
| 142 |
+
|
| 143 |
+
```bash
|
| 144 |
+
# Windows (PowerShell)
|
| 145 |
+
powershell -ExecutionPolicy ByPass -c "irm https://astral.sh/uv/install.ps1 | iex"
|
| 146 |
+
|
| 147 |
+
# macOS / Linux
|
| 148 |
+
curl -LsSf https://astral.sh/uv/install.sh | sh
|
| 149 |
+
```
|
| 150 |
+
|
| 151 |
+
### 2. Clone the Repository
|
| 152 |
+
|
| 153 |
+
```bash
|
| 154 |
+
git clone https://github.com/<your-username>/RAG-Visualizer.git
|
| 155 |
+
cd RAG-Visualizer
|
| 156 |
+
```
|
| 157 |
+
|
| 158 |
+
### 3. Install Dependencies
|
| 159 |
+
|
| 160 |
+
```bash
|
| 161 |
+
uv sync
|
| 162 |
+
```
|
| 163 |
+
|
| 164 |
+
This reads `pyproject.toml` and `uv.lock`, creates a `.venv`, and installs all dependencies in seconds.
|
| 165 |
+
|
| 166 |
+
### 4. Pull Ollama Models
|
| 167 |
+
|
| 168 |
+
Make sure Ollama is running, then pull the required models:
|
| 169 |
+
|
| 170 |
+
```bash
|
| 171 |
+
# Embedding models (at least one required)
|
| 172 |
+
ollama pull nomic-embed-text
|
| 173 |
+
ollama pull qwen3-embedding:0.6b
|
| 174 |
+
|
| 175 |
+
# LLM model (for future features)
|
| 176 |
+
ollama pull gemma4:e2b
|
| 177 |
+
```
|
| 178 |
+
|
| 179 |
+
### 5. Run the Dev Server
|
| 180 |
+
|
| 181 |
+
```bash
|
| 182 |
+
# Using the dev script (Windows)
|
| 183 |
+
.\dev.bat
|
| 184 |
+
|
| 185 |
+
# Or directly with uv
|
| 186 |
+
uv run uvicorn backend.main:app --reload --port 8080
|
| 187 |
+
```
|
| 188 |
+
|
| 189 |
+
Open **http://localhost:8080** in your browser.
|
| 190 |
+
|
| 191 |
+
---
|
| 192 |
+
|
| 193 |
+
## ๐ฎ Usage Guide
|
| 194 |
+
|
| 195 |
+
### Chunking Lab
|
| 196 |
+
|
| 197 |
+
1. **Paste your text** into the input area on the left panel
|
| 198 |
+
2. **Select a chunking strategy** โ click one of the 5 strategy cards
|
| 199 |
+
3. **Tune parameters** โ adjust chunk size, overlap, or semantic threshold with the sliders
|
| 200 |
+
4. **Choose an embedding model** from the dropdown
|
| 201 |
+
5. Click **โก Run Chunking**
|
| 202 |
+
6. Explore:
|
| 203 |
+
- **Document Viewer tab** โ see color-coded chunk boundaries in your text
|
| 204 |
+
- **Vector Space 2D tab** โ see chunks plotted as interactive particles
|
| 205 |
+
- **Chunk Inspector** (right panel) โ browse individual chunks with metadata
|
| 206 |
+
|
| 207 |
+
### Sonar Query Simulator
|
| 208 |
+
|
| 209 |
+
1. Switch to the **Vector Space 2D** tab
|
| 210 |
+
2. Type a query in the **Sonar Query Simulator** bar (e.g., `"linear regression"`)
|
| 211 |
+
3. Click **๐ Query** โ watch the sonar ping animate across the canvas
|
| 212 |
+
4. Retrieved chunks appear as ranked cards with distance scores
|
| 213 |
+
5. The **Document Viewer** automatically highlights retrieved chunks with rank-based glow effects
|
| 214 |
+
|
| 215 |
+
---
|
| 216 |
+
|
| 217 |
+
## โ๏ธ API Reference
|
| 218 |
+
|
| 219 |
+
### `POST /api/chunk`
|
| 220 |
+
|
| 221 |
+
Chunks input text, generates embeddings, reduces to 2D, and stores in ChromaDB.
|
| 222 |
+
|
| 223 |
+
**Request Body:**
|
| 224 |
+
|
| 225 |
+
```json
|
| 226 |
+
{
|
| 227 |
+
"text": "Your input text...",
|
| 228 |
+
"runs": [
|
| 229 |
+
{
|
| 230 |
+
"strategy": "fixed_size",
|
| 231 |
+
"config": {
|
| 232 |
+
"chunk_size": 500,
|
| 233 |
+
"chunk_overlap": 20,
|
| 234 |
+
"tokenizer": "cl100k_base"
|
| 235 |
+
}
|
| 236 |
+
}
|
| 237 |
+
],
|
| 238 |
+
"embedding_model": "nomic-embed-text",
|
| 239 |
+
"n_neighbors": 15,
|
| 240 |
+
"min_dist": 0.1
|
| 241 |
+
}
|
| 242 |
+
```
|
| 243 |
+
|
| 244 |
+
**Response:** `ChunkResponse` with chunks, stats, 2D coordinates, and embeddings.
|
| 245 |
+
|
| 246 |
+
### `POST /api/retrieve`
|
| 247 |
+
|
| 248 |
+
Embeds a query and retrieves the top-K most similar chunks from ChromaDB.
|
| 249 |
+
|
| 250 |
+
**Request Body:**
|
| 251 |
+
|
| 252 |
+
```json
|
| 253 |
+
{
|
| 254 |
+
"search_text": "What is gradient descent?",
|
| 255 |
+
"embedding_model": "nomic-embed-text",
|
| 256 |
+
"strategy": "fixed_size",
|
| 257 |
+
"top_k": 3
|
| 258 |
+
}
|
| 259 |
+
```
|
| 260 |
+
|
| 261 |
+
**Response:** `QueryResponse` with query coordinates, retrieved chunks, and distance scores.
|
| 262 |
+
|
| 263 |
+
### `GET /api/strategies`
|
| 264 |
+
|
| 265 |
+
Returns the list of available chunking strategies.
|
| 266 |
+
|
| 267 |
+
---
|
| 268 |
+
|
| 269 |
+
## ๐ ๏ธ Tech Stack
|
| 270 |
+
|
| 271 |
+
| Layer | Technology | Role |
|
| 272 |
+
| ---------------------------- | ------------------------------ | ------------------------------------------------------- |
|
| 273 |
+
| **Frontend** | Vanilla HTML / CSS / JS | Single-page app, Canvas 2D rendering |
|
| 274 |
+
| **Backend** | FastAPI (Python 3.11) | REST API, async request handling |
|
| 275 |
+
| **Chunking** | LangChain Text Splitters, NLTK | 5 chunking strategy implementations |
|
| 276 |
+
| **Tokenization** | tiktoken (`cl100k_base`) | Token counting (OpenAI-compatible) |
|
| 277 |
+
| **Embeddings** | Ollama (local models) | `nomic-embed-text`, `EmbeddingGemma`, `qwen3-embedding` |
|
| 278 |
+
| **Dimensionality Reduction** | UMAP (`umap-learn`) | High-dim โ 2D projection for visualization |
|
| 279 |
+
| **Vector Database** | ChromaDB (persistent) | Cosine similarity search with HNSW index |
|
| 280 |
+
| **Package Manager** | uv | Dependency management & virtual environments |
|
| 281 |
+
|
| 282 |
+
## ๐ License
|
| 283 |
+
|
| 284 |
+
This project is for educational and personal use.
|
| 285 |
+
|
| 286 |
+
---
|
| 287 |
+
|
| 288 |
+
## ๐ Acknowledgements
|
| 289 |
+
|
| 290 |
+
- [Ollama](https://ollama.com/) โ Local LLM inference
|
| 291 |
+
- [ChromaDB](https://www.trychroma.com/) โ Open-source vector database
|
| 292 |
+
- [LangChain](https://www.langchain.com/) โ Text splitting utilities
|
| 293 |
+
- [UMAP](https://umap-learn.readthedocs.io/) โ Dimensionality reduction
|
| 294 |
+
- [FastAPI](https://fastapi.tiangolo.com/) โ Modern Python web framework
|