Introduction: The Power of Context in Email Assistants

Welcome to the first lesson of the course, "Document Context and Retrieval-Augmented Generation." In this lesson, you will learn how external documents and context can make an email assistant much smarter and more helpful.

Imagine you are using an email assistant at work. If the assistant only knows what is in your current email, it can answer simple questions. But what if you want to know about your company’s meeting policy, your coworker’s availability, or how to handle urgent requests? For the assistant to answer these questions, it needs access to more information — such as company policies, calendars, and FAQs. These are called context documents.

By the end of this lesson, you will know what a context document is, why it matters, and how to load one into your assistant using Mastra. This is the first step in making your assistant truly useful.

Quick Recall: What is RAG?

Before we dive in, let's quickly remind ourselves about Retrieval-Augmented Generation, or RAG. If this is your first time hearing about it, don't worry — here's a simple explanation.

RAG is a method that combines two things:

  • Retrieval: Finding useful information from external documents by searching through them based on the user's question or request.
  • Generation: Using that retrieved information, along with the user's question, to create a helpful and accurate response through a language model.

For example, if your assistant is asked, "When is Emily available for a meeting?" it can look up Emily's calendar (retrieval), find the relevant availability information, and then use a language model to write a clear, natural-sounding answer (generation) like "Emily is available on weekdays from 10am to 4pm."

The key advantage of RAG is that it allows the assistant to provide accurate, up-to-date information from your documents, rather than relying only on what the language model learned during training.

In this lesson, we will focus on the first part: how to give your assistant access to the right documents so it can retrieve useful information.

Understanding Context Documents

A context document is any piece of information that helps your assistant answer questions better. Here are some examples:

Document TypeExample Content
Company Policy"All meetings must be scheduled 24 hours in advance."
Calendar"Emily is available on weekdays from 10am to 4pm."
FAQ"For urgent requests, mark your email as 'High Priority'."

These documents are important because they give your assistant the background knowledge it needs. Without them, the assistant can only guess or give generic answers.

For a smart email assistant, context documents might include:

  • Company rules and policies
  • Employee calendars
  • Frequently asked questions (FAQs)
  • Contact lists

By loading these documents, you make your assistant much more capable.

Loading a Document with Mastra

Now, let’s see how you can load a context document using Mastra. We will do this step by step.

Step 1: Import the `MDocument` Class

First, you need to import the MDocument class from the @mastra/rag library. This class helps you work with documents in your assistant.

This line tells your code to use the MDocument class, which is designed for handling documents in RAG systems.

Step 2: Create a Document from Text

Next, you can create a document by passing the text you want to use. For example, let’s say you have a company policy, a calendar entry, and an FAQ. You can combine them into one document like this:

Here’s what’s happening:

  • MDocument.fromText() takes a string (your document text) and creates a document object.
  • The text can include different types of information, separated by new lines or sections.

Now, doc is an object that represents your context document. You can use this object in your assistant to help answer questions.

Note: On CodeSignal, the Mastra library is already installed, so you don’t need to worry about setup. If you use your own computer, you may need to install it first.

Preparing Documents for Chunking and Embedding

Once you have loaded your document, the next step is to prepare it for use in a RAG system. This usually means two things: chunking and embedding.

  • Chunking means breaking the document into smaller pieces, or "chunks." This helps the assistant find the most relevant part of the document when answering a question.
  • Embedding means turning each chunk into a set of numbers (a vector) that the assistant can understand and compare.

For now, just remember:

  • You load the document as one big piece of text.
  • Later, the system will break it into chunks and create embeddings so it can search and use the information efficiently.

You don’t need to do the chunking and embedding by hand — Mastra and other RAG tools handle this for you. But it’s important to know why this step is needed: it helps the assistant quickly find the right information from your documents.

Summary and What’s Next

In this lesson, you learned:

  • Why context documents are important for making email assistants smarter.
  • What kinds of documents you might use (like policies, calendars, and FAQs).
  • How to load a document using Mastra’s MDocument.fromText method.
  • The basics of preparing documents for chunking and embedding.

Next, you will get a chance to practice loading and preparing your own documents. This hands-on practice will help you get comfortable with the process and see how it works in real code. When you’re ready, move on to the practice exercises to try it yourself!

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal