Skip to main content
In this section, we will see how to build a RAG (Retrieval-Augmented Generation) agent that can load a PDF document, store embeddings in a vector database, and retrieve info using custom tools.

Objectives

  1. Split document pages and create a Chroma database.
  2. Wrap database query retrieval in a custom @tool.
  3. Set up a StateGraph that invokes LLM reasoning and retrieval tools sequentially.

Agent IV: RAG Agent

Goal

Build a RAG agent querying details from the Stock_Market_Performance_2024.pdf document.

Sample Input

Sample Output

Outputs summarizing tech sector performance: In 2024, the tech sector gained 25% driven by AI breakthroughs. (cited from the document).

Plan

  1. Load and chunk the PDF document (Stock_Market_Performance_2024.pdf).
  2. Embed chunks using OpenAIEmbeddings and store them in a local Chroma vector database.
  3. Define a retriever tool function (retriever_tool) and bind it to the LLM.
  4. Set up a StateGraph containing an LLM call node, a retriever action node, and conditional routing edges.

Code Implementation

1. Load and Chunk PDF

We split document text to feed to embeddings model:

2. Setup ChromaDB Vector Store

We embed the split documents and store them in a Chroma DB:

3. Define Retriever Tool

We wrap retrieval search in a custom tool and bind it to the model:

4. Define Nodes and Build Graph

We define the graph node calls and setup the graph structure:

5. Invoke the Agent

Exercise: Multi-Document RAG with Document Type Router 📂

Goal

Add a second Corporate Email records search tool to the RAG Agent, allowing the model to choose the correct resource (Stock Performance DB or Email database) dynamically based on user query.

Sample Input

Sample Output

Outputs citing the email document (e.g., "The user was unable to attend due to a conflicting doctor's appointment.").

Plan

  1. Create a retriever tool @tool named email_retriever returning content from corporate email records.
  2. Bind both [retriever_tool, email_retriever] to the LLM.
  3. Configure take_action to map and invoke both tools correctly.
  4. Compile the graph, invoke with the sample query, and print the results.

Practice & Exercises

To reinforce what you’ve learned in this section, practice with the interactive notebook:

Practice & Exercises

Practice loading source documents, chunking text, setting up a local vector database retriever, and routing questions dynamically.💻 VS Code | 🚀 Colab | 📥 Download