Preparing Data for RAG
Complete the full lesson to earn 25 points
Work through each section, then tap “Mark as Complete” on the last one.
✦ Skip the page breaks and see fewer ads — read each lesson on a single page with Pro
Preparing Data for Retrieval Augmented Generation (RAG)
Introduction: The Foundation of Intelligent Systems
Retrieval Augmented Generation, or RAG, has emerged as the primary architecture for connecting Large Language Models (LLMs) to private, proprietary, or rapidly changing data. While the model itself provides the reasoning and linguistic capability, the quality of the output is fundamentally constrained by the quality of the data retrieved. If you feed an LLM irrelevant, poorly formatted, or noisy information, the resulting answer will be inaccurate, regardless of how "smart" the underlying model is. This concept is often summarized by the phrase "garbage in, garbage out."
In this lesson, we will explore the critical phase of RAG development known as data preparation. This stage involves transforming raw, often unstructured information into a machine-readable format that a vector database can index and a retrieval system can query effectively. Preparing data is not merely a technical preprocessing step; it is a strategic exercise in information architecture. By understanding how to clean, chunk, and embed your data, you bridge the gap between static documents and dynamic, actionable intelligence.
The Data Lifecycle in RAG
Before diving into specific techniques, it is helpful to view data preparation as a multi-stage pipeline. Each stage in this pipeline serves a specific purpose in ensuring that the information is discoverable and semantically meaningful to the model.
- Ingestion: Collecting raw files (PDFs, HTML, Markdown, SQL databases) from disparate sources.
- Cleaning: Removing noise, formatting errors, and irrelevant metadata that might distract the embedding model.
- Chunking: Breaking large documents into smaller, manageable segments that fit within the context window and capture specific concepts.
- Embedding: Converting the text chunks into numerical vectors (embeddings) that represent the semantic meaning of the content.
- Indexing: Storing these vectors in a specialized database for efficient similarity searching.
Missing any of these steps, or performing them incorrectly, leads to "retrieval drift," where the system retrieves chunks that are syntactically similar but semantically useless for the user's query.
Callout: Retrieval vs. Generation It is important to distinguish between the two halves of RAG. Retrieval is the process of finding the right needle in a haystack of data. Generation is the process of summarizing or answering based on that needle. If your data preparation is poor, your retrieval fails, making the generation phase irrelevant. You cannot "fix" bad data with a better prompt or a larger model.
Step 1: Cleaning and Normalization
Raw data is rarely ready for machine consumption. Documents often contain headers, footers, boilerplate legal text, image captions, and formatting artifacts that introduce noise into your vector space.
Removing Noise
If you are processing PDF documents, you will frequently encounter "ghost" text or repetitive elements like page numbers, document IDs, and disclaimers. These elements should be stripped out because they create false signals. If an embedding model sees "Page 42" on every single chunk, it may start associating that phrase with every concept in your document, diluting the unique semantic signature of the content.
Standardizing Formats
If your data comes from a mix of sources (e.g., Markdown files, CSVs, and HTML), you must normalize them into a consistent structure. Converting everything to a clean Markdown or plain text format is a common industry practice because these formats are easily parsed and contain minimal structural overhead.
Handling Multi-modal Content
Modern RAG systems often need to handle tables and images. A table converted to a raw, unformatted string of text becomes unreadable to an LLM. You should convert tables into structured formats like Markdown tables or JSON objects. For images, you might use a vision model to generate a descriptive summary (a caption) and index that caption instead of the image data itself.
Step 2: The Art and Science of Chunking
Chunking is the most critical decision in the RAG pipeline. It determines the granularity of the information your system can retrieve. If your chunks are too small, you lose the surrounding context necessary to understand the text. If your chunks are too large, they introduce too much noise, potentially burying the relevant information within a sea of irrelevant details.
Fixed-Size Chunking
This is the simplest approach, where you split text into blocks of a fixed number of characters or tokens (e.g., 500 characters). While easy to implement, it is rarely optimal because it ignores the semantic structure of the document. A fixed-size chunk might split a sentence in half or cut a paragraph right in the middle of an argument.
Recursive Character Splitting
A better approach is to use a recursive splitter that attempts to split text based on a hierarchy of separators: first paragraphs (\n\n), then lines (\n), then sentences (.), and finally words. This ensures that chunks stay as close as possible to natural language boundaries.
# Example of Recursive Character Splitting using LangChain
from langchain.text_splitter import RecursiveCharacterTextSplitter
text = "Your long document content goes here..."
text_splitter = RecursiveCharacterTextSplitter(
chunk_size=1000,
chunk_overlap=200,
length_function=len,
separators=["\n\n", "\n", " ", ""]
)
chunks = text_splitter.split_text(text)
The Role of Overlap
Notice the chunk_overlap parameter in the example above. Overlap is essential because it ensures that information at the edge of one chunk is not lost. By having a 10-20% overlap, you provide the model with the context needed to understand the transition between segments.
Tip: Choosing Chunk Size Aim for a chunk size that aligns with the typical length of an answer you expect to retrieve. If your users ask specific, factual questions, smaller chunks (200-500 tokens) are often better. If your users ask for summaries or complex analysis, larger chunks (800-1500 tokens) are required.
Step 3: Semantic Enrichment (Metadata)
One common mistake is indexing only the raw text of a chunk. If you have a massive corpus, the system might retrieve a chunk that is semantically relevant but lacks context regarding where it came from. Metadata is your best friend here.
Essential Metadata Fields
When you store a chunk in your vector database, you should attach metadata that describes the source. This allows you to filter your search results before performing the similarity search.
- Source ID: The document name or URL.
- Timestamp: When the document was created (crucial for time-sensitive data).
- Category/Tag: Labels like "HR Policy," "Technical Documentation," or "Customer Support."
- Page Number: For PDF-based RAG, this helps the user verify the source.
Adding Contextual Summaries
Sometimes, a chunk on its own is ambiguous. For example, a sentence saying "The price is $50" is useless without knowing which product it refers to. To solve this, prepend the document title or a brief summary of the entire document to every chunk. This "context-aware" chunking ensures that every piece of data carries its own identity.
Step 4: Embedding Models and Vectorization
Once your data is cleaned and chunked, it must be converted into numerical vectors. This is done using an embedding model—a neural network trained to map text into a high-dimensional space where similar concepts are located close together.
Selecting the Right Model
Choosing an embedding model involves balancing performance, cost, and latency.
- OpenAI
text-embedding-3-small: A great general-purpose, cost-effective choice. - HuggingFace
BGEorE5models: Excellent open-source options that you can run locally for data privacy. - Domain-specific models: If your data is highly technical (e.g., legal or medical), look for models trained on those specific domains.
Normalizing Vectors
Most vector databases perform cosine similarity searches. Ensure your embedding model outputs normalized vectors. If the model does not normalize them, you should perform this step during the indexing process to maintain consistent search results.
Callout: Dimensionality and Storage The number of dimensions in your vectors (e.g., 768, 1536) affects both the accuracy of the search and the storage cost. Higher dimensions capture more nuance but require more memory and compute. Always check the requirements of your vector database (e.g., Pinecone, Milvus, Weaviate) to ensure they support the dimensionality of your chosen embedding model.
Best Practices for Data Quality
To ensure your RAG application remains performant, follow these established industry practices.
1. Maintain a "Gold Standard" Evaluation Set
You cannot improve what you cannot measure. Create a set of 50-100 question-answer pairs based on your data. Every time you change your chunking strategy or embedding model, run your retrieval system against this set to see if the "hit rate" (the frequency with which the right chunk is retrieved) improves or degrades.
2. Version Your Data
Data in real-world applications changes. Policies are updated, products are discontinued, and new documents are added. Implement versioning so that you can trace a retrieved answer back to the specific version of the document that was indexed at that time.
3. Handle PII (Personally Identifiable Information)
Before indexing data, use a PII redaction tool to identify and mask sensitive information like email addresses, phone numbers, or social security numbers. While RAG is powerful, it can inadvertently expose sensitive data if the underlying document contains it.
4. Optimize for Retrieval, Not Just Generation
Remember that the retrieval step is a search problem. Use keywords in your chunks if necessary. Some advanced RAG systems use "Hybrid Search," which combines vector similarity (semantic) with BM25 (keyword-based) search. Preparing your data to support keyword lookups (e.g., by ensuring specific terminology is preserved) can significantly improve performance for technical queries.
Common Pitfalls and How to Avoid Them
Even with the best intentions, developers often fall into common traps during the data preparation phase.
Pitfall 1: Over-chunking
If you split text into chunks that are too small, you break the semantic links between sentences. A chunk that just says "However, this is not true" is useless.
- Solution: Use context-aware splitting, where you ensure each chunk has at least a few sentences of context.
Pitfall 2: Ignoring Data Refresh
A RAG system is only as current as its database. If you index your data once and never update it, the system will provide outdated answers.
- Solution: Build a pipeline that automatically triggers a re-indexing process when a source document is modified.
Pitfall 3: Inconsistent Formatting
If your training data is in Markdown but your production data is a mix of messy HTML and raw text, your embedding model will behave inconsistently.
- Solution: Enforce a strict document ingestion pipeline that converts all inputs into a standardized format before they hit the chunking phase.
Pitfall 4: Neglecting Metadata
Searching through millions of vectors can lead to "semantic collision," where unrelated documents have similar vector representations.
- Solution: Use metadata filtering. If the user asks about "the Q3 report," your system should filter by the "Report Type" metadata field before searching the vectors.
Comparison: Chunking Strategies
| Strategy | Pros | Cons | Best For |
|---|---|---|---|
| Fixed Size | Simple, fast, predictable | Ignores context, potential for broken sentences | Simple text, logs |
| Recursive | Respects natural breaks | Slightly more complex to configure | General documents, reports |
| Semantic | Groups by meaning | Computationally expensive | Complex narratives, books |
| Agentic | High precision | Very slow, complex to build | Specialized technical manuals |
Step-by-Step Implementation Guide
If you are just starting, follow this workflow to build your first data pipeline:
- Define your document types: List all sources (PDFs, Wikis, etc.).
- Choose a library: Use
LangChainorLlamaIndexto handle the heavy lifting of document loading and text splitting. - Sanitize: Write a simple regex-based script to remove headers, footers, and repeated boilerplate text.
- Split: Use
RecursiveCharacterTextSplitterwith a 1000-character chunk size and 200-character overlap. - Embed: Select a model (e.g.,
text-embedding-3-small) and batch your chunks to save on API costs. - Index: Insert the chunks, along with their metadata, into a vector database like
ChromaDBorPinecone. - Test: Perform 10-20 queries and manually inspect the retrieved chunks to ensure they make sense to a human reader.
The Future of Data Preparation: Automated Pipelines
As the field matures, we are moving away from manual data preparation toward automated "ETL for AI" pipelines. These tools automatically detect the structure of a document, determine the optimal chunk size based on the content, and even generate synthetic questions for each chunk to improve retrieval accuracy.
While these tools are powerful, they do not replace the need for an understanding of the underlying principles. You must still be the one to define the "Gold Standard" and ensure that the data being fed into the system is accurate and representative of the answers you want the LLM to provide.
Note: Always consider the "Document Source Reliability." If you are indexing a mix of internal expert-reviewed documents and unverified internal chat logs, add a "Source Reliability" metadata field. You can then instruct your RAG system to prioritize high-reliability sources during the retrieval phase.
Summary of Key Takeaways
- Data Quality is Paramount: The intelligence of your RAG system is capped by the quality of your retrieved data. Invest heavily in the cleaning and normalization phase.
- Chunking Strategy Defines Granularity: Choose your chunking strategy based on the nature of the queries you expect. Smaller chunks for specific facts; larger chunks for broader summaries.
- Metadata is Essential: Never index raw text alone. Attach metadata (source, date, category) to enable filtering and improve retrieval precision.
- Context Matters: Use overlapping chunks to prevent information loss at the boundaries of your segments.
- Iterative Evaluation: Build a "Gold Standard" dataset to measure the effectiveness of your data preparation pipeline.
- Standardize Your Format: Transform disparate data sources into a uniform format before processing to ensure consistent embedding results.
- Monitor and Refresh: Data is dynamic. Ensure your pipeline includes mechanisms for periodic re-indexing and version control to prevent "knowledge rot."
By adhering to these principles, you move from building a basic prototype to constructing a robust, scalable retrieval system that can reliably support complex AI applications. Preparing data is the most unglamorous part of the process, yet it is where the most significant gains in performance and reliability are found. Treat your data as your most valuable asset, and your RAG application will reflect that care in the quality of its outputs.
Continue the course
Enjoying the courses?
Everything stays free. Pro shows fewer ads, doubles your daily points limit so you progress twice as fast, and lets you read each lesson on one page.
- ✓ Fewer advertisements
- ✓ 2× daily points limit
- ✓ Distraction-free lessons