Objectives
- Split document pages and create a Chroma database.
- Wrap database query retrieval in a custom
@tool. - Set up a StateGraph that invokes LLM reasoning and retrieval tools sequentially.
Agent IV: RAG Agent
Goal
Build a RAG agent querying details from theStock_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
- Load and chunk the PDF document (
Stock_Market_Performance_2024.pdf). - Embed chunks using
OpenAIEmbeddingsand store them in a local Chroma vector database. - Define a retriever tool function (
retriever_tool) and bind it to the LLM. - 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
- Create a retriever tool
@toolnamedemail_retrieverreturning content from corporate email records. - Bind both
[retriever_tool, email_retriever]to the LLM. - Configure
take_actionto map and invoke both tools correctly. - Compile the graph, invoke with the sample query, and print the results.
Solution
Solution
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