Chunking Documents for Efficient Retrieval
Introduction: Why Chunking is Needed for RAG
Welcome back! In the previous lesson, you learned how to represent and load documents using Mastra. Now, we will take the next step in building a smart email assistant: splitting documents into smaller, manageable pieces — a process called chunking.
Why is chunking important? When working with retrieval-augmented generation (RAG) systems, you often need to search for relevant information inside large documents. If you try to use the whole document at once, it can be too big for the model to handle, and you might miss important details. By breaking documents into smaller chunks, you make it easier to find and use the right information at the right time.
In this lesson, you will learn how to split a document into overlapping chunks using Mastra. This is a key step before you can retrieve and use information efficiently in your smart email assistant.
Quick Recall: Loading Documents in Mastra
Before we start chunking, let’s quickly review how to load a document in Mastra. You saw this in the previous lesson, but here’s a short reminder.
To load a document, you use the MDocument.fromText() method. This method takes a string (the text of your document) and creates a document object you can work with.
For example:
Here, doc is now a Mastra document containing your text. You will use this document in the next steps.
What is Chunking? Key Concepts
Now, let’s talk about chunking. Chunking means splitting a document into smaller pieces, called "chunks." Each chunk is a segment of the original text. This helps with searching and retrieving information, especially when the document is long.
There are two important ideas to understand:
- Chunk Size: This is how big each chunk is, usually measured in characters or words. If the chunk is too small, you might lose important context. If it’s too big, it might be hard to process.
- Overlap: Sometimes, you want chunks to share some content. This is called overlap. Overlapping chunks help make sure that important information at the edge of one chunk is not missed in the next chunk.
For example, if you have a chunk size of 100 characters and an overlap of 20, each new chunk will start 80 characters after the previous one, so the last 20 characters of the previous chunk are included in the next chunk.
