# RAG and Agent-based Q&A System ## Table of Contents - [Introduction](#introduction) - [Features](#features) - [System Architecture](#system-architecture) - [Prerequisites](#prerequisites) - [Installation](#installation) - [Usage](#usage) - [API Endpoints](#api-endpoints) - [Frontend Interface](#frontend-interface) This project implements a sophisticated Question-Answering system that combines Retrieval-Augmented Generation (RAG) with an intelligent agent. The system is designed to answer queries related to NCERT textbooks chapters, specifically focusing on the Sound chapter, while also handling general queries using web search capabilities. Do not: **Don't forget to specify your open-ai key in the .env file** The application serves two main functionalities: - A RAG system that retrieves relevant information from a vector database containing NCERT textbook content. - An agent-based system that can perform smart actions based on the user's query, including invoking the RAG system when appropriate and using additional tools like web search. ## Introduction This project implements a sophisticated Question-Answering system that combines Retrieval-Augmented Generation (RAG) with an intelligent agent. The system is designed to answer queries related to NCERT textbooks, specifically focusing on the Sound chapter, while also handling general queries using web search capabilities. The application serves two main functionalities: 1. A RAG system that retrieves relevant information from a vector database containing NCERT textbook content. 2. An agent-based system that can perform smart actions based on the user's query, including invoking the RAG system when appropriate and using additional tools like web search. ## Features - **RAG System**: - Utilizes a vector database to store and retrieve relevant information from NCERT textbooks. - Provides accurate and concise answers to questions related to the Sound chapter. - **Intelligent Agent**: - Determines when to use the RAG system based on the query content. - Incorporates additional tools, including web search for non-textbook related queries. - Calculates word count of responses when requested. - **FastAPI Backend**: - Serves both RAG and Agent functionalities via separate endpoints. - Ensures efficient and scalable handling of requests. - **Gradio Frontend**: - Provides an intuitive user interface for interacting with both the RAG and Agent systems. - Allows easy testing and demonstration of the system's capabilities. ## System Architecture The system is built using the following key components: 1. **Vector Store**: Stores embeddings of NCERT textbook content for efficient retrieval. 2. **LangChain**: Facilitates the creation of the RAG chain and the agent. 3. **OpenAI's ChatGPT**: Powers the language model for generating responses. 4. **DuckDuckGo Search API**: Enables web search capabilities for the agent. 5. **FastAPI**: Provides the backend API framework. 6. **Gradio**: Creates the frontend user interface. ## Installation 1. Clone the repository and create a virtual env ```bash python -m venv venv source venv/bin/activate ``` 2.Install the required packages: ```bash pip install -r requirements.txt ``` 3. Setup up .env with all the environment variables ```bash OPEN_API_KEY=your_openai_api_key UVICORN_HOST = 127.0.0.1 UVICORN_PORT = 7860 SOURCE_DATA = "../pdf_data" VECTOR_STORE = "../chroma_langchain_db" ``` ## Usage To start the application run: ```python python3 main_app.py ``` This will start the FastAPI server and launch the Gradio interface. You can access the Gradio interface by navigating to `http://localhost:8000` in your web browser. ## API Endpoints The application exposes two main endpoints: 1. `/rag` (POST): For querying the RAG system - Request body: `{ "question": "Your question here" }` - Response: `{ "answer": "Generated answer" }` 2. `/agent` (POST): For interacting with the intelligent agent - Request body: `{ "question": "Your question here" }` - Response: `{ "answer": "Agent's response" }` ## Frontend Interface The Gradio interface provides two tabs: 1. **RAG System**: For asking questions related to the NCERT Sound chapter. 2. **Agent**: For general queries, including those that may require web search or other tools. Users can type their questions in the input box and receive answers in real-time. ### RAG System Interface ![RAG System Interface](images/RAG_app.png) ### Agent Interface ![Agent Interface](images/Agent_app.png) ### API Documentation The FastAPI automatic interactive API documentation is available at `/docs` endpoint: ![API Documentation](images/API_docs.png)