---
title: "RAG System"
description: "Understanding ChatRAG's high-performance RAG system with HNSW vector search"
url: "https://www.chatrag.ai/docs/rag-system"
type: "documentation"
---

# RAG System

ChatRAG's Retrieval-Augmented Generation system provides document-based AI responses with HNSW-optimized vector search.

## 15-28x Faster Performance

ChatRAG uses HNSW (Hierarchical Navigable Small World) vector indexes for semantic search, providing 15-28x faster performance compared to traditional IVFFlat indexes.

## How RAG Works

1. **Document Upload** - Upload documents (PDF, DOCX, TXT, etc.)
2. **Processing & Chunking** - LlamaCloud parses and chunks documents
3. **Embedding Generation** - OpenAI generates 1536-dimensional vectors
4. **Vector Storage** - Embeddings stored in Supabase with HNSW indexes
5. **Query & Retrieval** - User queries matched against stored embeddings
6. **Context Injection** - Relevant chunks injected into `{{context}}` placeholder
7. **AI Response** - LLM generates response based on context

## Performance Comparison

| Metric | Traditional (IVFFlat) | ChatRAG (HNSW) | Improvement |
|--------|----------------------|----------------|-------------|
| Single Query | 100-500ms | <50ms | 15-28x faster |
| 10 Concurrent Users | 800-2000ms | <100ms | 20x faster |
| 100k Documents | 1-3 seconds | <200ms | 15x faster |
| Accuracy | 95% | 98% | +3% |

## HNSW Index Parameters
- m=64: Number of connections per layer
- ef_construction=200: Size of dynamic candidate list
- Dimensions: 1536 (OpenAI text-embedding-3-small)

## Supported Document Types
- PDF (Portable Document Format)
- DOCX (Microsoft Word)
- TXT (Plain Text)
- HTML (Web Pages)
- RTF (Rich Text Format)
- EPUB (E-books)

## Key RAG Features
- Adaptive Retrieval
- Multi-Pass Search
- Adjacent Chunks
- Semantic Chunking
- Hybrid Reranking
- Result Caching

## Testing & Diagnostics

```bash
# Check RAG flow
node scripts/rag/check-rag-flow.js

# Decode RAG prompt
node scripts/rag/decode-rag-prompt.js

# Test RAG system
node scripts/rag/test-rag-system.js

# Reprocess documents
node scripts/rag/reprocess-documents.js
```

