Objectives
- Load a pre-existing Chroma vector database using
ChromaandOpenAIEmbeddings. - Configure history-aware retrievers using
create_history_aware_retriever. - Wrap RAG retrieval chains in a
Toolconstructor and execute them within an agent loop.
Implementation Plan
Goal
Set up a RAG pipeline querying a local database, wrap it inside a custom tool, and bind it to a ReAct agent to answer domain-specific questions in an interactive chat session.Sample Input
Sample Output
An AI assistant response generated using context retrieved from the database files.Plan
- Retrieve path configuration to load the Chroma vector database.
- Initialize
OpenAIEmbeddingsand load the persistent vector database instance. - Configure search settings (e.g. similarity search with ) to build a retriever.
- Establish a contextualization prompt to convert chat inputs into standalone queries.
- Create a
history_aware_retrieverand link it to a document-stuffing QA chain usingcreate_retrieval_chainto obtainrag_chain. - Define a custom
Toolwrapper mapping tool invocation to therag_chain.invokeendpoint. - Pull the ReAct prompt
hwchase17/reactfrom the hub, instantiate the ReAct agent, and build the interaction loops.
Step-by-Step Implementation
Step 1: Load Vector Store and Embeddings
We configure the directory variables and load our pre-populated vector database.Step 2: Set Up Retrieval Q&A Chain
We set up a history-aware retriever to formulate standalone questions and combine it with a documents chain.Step 3: Wrap RAG Chain as a Tool
We define a custom tool named"Answer Question" that maps its execution function to run the RAG chain pipeline.
Step 4: Create ReAct Agent and Execute Loop
We pull the prompt, construct the agent usingcreate_react_agent, and run the interactive loop.
Complete Combined Code
Below is the complete, consolidated Python script uniting all of the steps above:Practice & Exercises
To practice querying local document stores using agents, open the interactive notebook:Practice & Exercises
Practice loading vector database retrievers, writing history-aware retrievers, and setting up retriever-agent wrappers.💻 VS Code | 🚀 Colab | 📥 Download