LLM Architecture · 2024-04-15 · 12 min read
RAG: Connecting LLMs to Private Data
The Knowledge Gap
LLMs are frozen in time: they only know what they were trained on. RAG (Retrieval-Augmented Generation) is the bridge that allows an AI to "read" your private documents, databases, or real-time web search results before generating an answer.
1. The RAG Pipeline
- Ingestion: Documents are broken into "chunks."
- Embedding: Each chunk is converted into a vector (a list of numbers) that represents its semantic meaning.
- Storage: These vectors are stored in a Vector Database (like Pinecone, Weaviate, or Chroma).
- Retrieval: When a user asks a question, we convert that question into a vector and find the most similar document chunks.
- Augmented Generation: We pass the question PLUS the retrieved chunks to the LLM, instructing it to answer using only the provided context.
2. Why RAG instead of Fine-Tuning?
Fine-tuning is like teaching a student for a year. RAG is like giving that student an open-book exam. RAG is cheaper, faster, and allows for real-time updates of the knowledge base without retraining the model.
PRACTICAL TIP: Chunk size matters. If your chunks are too small, they lose context. If they are too large, the LLM gets overwhelmed. Aim for 512-1024 tokens per chunk for most documents.
3. Advanced RAG: Re-ranking and Hybrid Search
Simple vector search can sometimes be inaccurate. Hybrid Search combines traditional keyword search (BM25) with vector search. Re-ranking takes the top results and uses a smaller, faster model to re-order them by relevance before sending them to the LLM.