Skip to main content
The first phase of the RAG pipeline is Ingestion, which handles loading files into memory and partitioning them into semantically cohesive chunks that models can query.

1. Document Loading

Before chunking, raw files (PDFs, Markdown, HTML, JSON) must be loaded into LangChain Document objects containing page_content (text) and metadata (source, page number, title). LangChain provides specialized Document Loaders for different file types:
  • TextLoader: Reads plain text files.
  • PyPDFLoader: Parses and reads PDF files page-by-page.
  • UnstructuredHTMLLoader: Loads and extracts text clean of HTML tags.
  • JSONLoader: Selectively extracts fields from structured JSON files using JSONPath.

2. Chunking Strategies

We cannot feed massive documents to LLMs due to context window limits. Splitting them into topic-focused chunks makes similarity searches far more accurate. Different chunking strategies are suited for different tasks:

2.1 Character Splitting

Splits text by a fixed character count (e.g., every 500 characters).
  • Pros: Simple to calculate.
  • Cons: Cuts words, sentences, or paragraphs in half, destroying context.
Splits text using a list of separator characters hierarchically (paragraphs \n\n, then lines \n, then spaces , and finally empty strings "").
  • Pros: Keeps paragraphs and sentences intact wherever possible.
  • Cons: Still requires tuning chunk sizes and overlap margins.

2.3 Token-Based Splitting

Splits text by token counts instead of characters.
  • Pros: Directly matches the LLM’s token limits, preventing context window overflow.
  • Cons: Harder to read visually for humans.

2.4 Semantic Chunking

Analyzes the semantic meaning of sentences (using embeddings) and splits text only when there is a significant shift in meaning.
  • Pros: Highly accurate; groups topics dynamically.
  • Cons: Requires executing an embedding model for every sentence during ingestion, making it slow and computationally expensive.

3. Ingestion Python Implementation

Below is a complete implementation that loads a local text file and chunks it recursively.

3.1 Creating a Sample File

First, let’s write a small sample database policy file (knowledge.txt):

3.2 Loading and Chunking Code

Now, load this file and split it using RecursiveCharacterTextSplitter:
Output:

4. Practice Exercises

Practice 1: PDF Document Loading Setup

Assume you have a PDF file named "report.pdf". Write the code to load it page-by-page using LangChain’s PyPDFLoader and print the content of the first page. Instructions:
  1. Import PyPDFLoader from langchain_community.document_loaders.
  2. Initialize it with "report.pdf".
  3. Call .load() to get the document list.
  4. Access the first page document and print its page_content.