Integrating Retrieved Context into Agent Responses
Introduction: Making Agent Responses Smarter with Context
Welcome back! So far, you have learned how to prepare documents, chunk them, create embeddings, and use those embeddings to find relevant information for a given email. In this lesson, you will see how to use that retrieved context to make your smart email assistant even more helpful and accurate.
Adding context to your agent's prompt allows it to generate responses that are more relevant and personalized. This is a key step in building a truly smart assistant. By the end of this lesson, you will know how to pass the retrieved context to your agent and update your workflow to use this information when generating summaries and replies.
Recall: How We Retrieve Relevant Context
Let's quickly remind ourselves how we get relevant context for an email. In the previous lesson, you learned to:
- Embed the email using a model like OpenAI's embedding model.
- Search a vector database (like LibSQL) for the most similar document chunks.
- Collect the top results as your context.
This process is called Retrieval-Augmented Generation (RAG). It helps your agent "remember" important information from your documents when responding to emails.
Visualizing the RAG Pipeline
Here's how the entire RAG pipeline works from start to finish:
This diagram shows the complete flow:
- Incoming Email — The user's email arrives
- Embed Email — The email text is converted into a vector using an embedding model
- Vector DB Search — The email vector is compared against all document chunks in the database to find the most similar ones
- Top Chunks Retrieved — The most relevant chunks (usually 3-5) are selected based on similarity scores
- Agent Prompt — The original email and the retrieved chunks are combined into a single prompt
- Agent Response — The agent generates a contextual reply based on both the email and the retrieved information
