Vasanth6 commited on
Commit
5fbafd7
ยท
1 Parent(s): d0649b4

update changes

Browse files
Files changed (1) hide show
  1. README.md +294 -0
README.md CHANGED
@@ -0,0 +1,294 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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