Skip to main content
In this section, you will learn how to integrate a complete RAG (Retrieval-Augmented Generation) pipeline as a custom query tool inside a ReAct agent.

Objectives

  1. Load a pre-existing Chroma vector database using Chroma and OpenAIEmbeddings.
  2. Configure history-aware retrievers using create_history_aware_retriever.
  3. Wrap RAG retrieval chains in a Tool constructor 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

  1. Retrieve path configuration to load the Chroma vector database.
  2. Initialize OpenAIEmbeddings and load the persistent vector database instance.
  3. Configure search settings (e.g. similarity search with k=3k=3) to build a retriever.
  4. Establish a contextualization prompt to convert chat inputs into standalone queries.
  5. Create a history_aware_retriever and link it to a document-stuffing QA chain using create_retrieval_chain to obtain rag_chain.
  6. Define a custom Tool wrapper mapping tool invocation to the rag_chain.invoke endpoint.
  7. Pull the ReAct prompt hwchase17/react from 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 using create_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