# Dhigrowth RAG Chatbot A Retrieval-Augmented Generation (RAG) chatbot system built for Dhigrowth, providing accurate answers based on company knowledge base using vector similarity search and fine-tuned language models. ## ๐Ÿš€ Features - **RAG Architecture**: Combines vector similarity search with LLM generation for accurate, context-aware responses - **Vector Database**: ChromaDB for efficient document retrieval - **Fine-tuned Model**: Qwen2.5-0.5B model fine-tuned on Dhigrowth data - **RESTful API**: FastAPI-based backend with comprehensive error handling - **Input Validation**: Pydantic-based validation for robust API requests - **Comprehensive Logging**: File and console logging for debugging and monitoring - **Health Monitoring**: Health check endpoints for system status - **Error Handling**: Graceful error handling with informative responses ## ๐Ÿ“‹ Table of Contents - [Installation](#installation) - [Project Structure](#project-structure) - [Configuration](#configuration) - [Usage](#usage) - [API Documentation](#api-documentation) - [Testing](#testing) - [Deployment](#deployment) - [Troubleshooting](#troubleshooting) ## ๐Ÿ› ๏ธ Installation ### Prerequisites - Python 3.8 or higher - 4GB+ RAM recommended - 2GB+ disk space for models and embeddings ### Step 1: Clone the Repository ```bash git clone cd DHIGROWTH_RAG ``` ### Step 2: Create Virtual Environment **Windows:** ```bash python -m venv venv source venv/Scripts/activate ``` **Linux/Mac:** ```bash python3 -m venv venv source venv/bin/activate ``` ### Step 3: Install Dependencies ```bash pip install -r requirements.txt ``` ### Step 4: Set Up Vector Database Create the vector database from your data: ```bash python src/embed_loader.py ``` This will: - Load data from `data/dhigrowth_final_rag_ft.jsonl` - Create embeddings using SentenceTransformer - Store documents in ChromaDB at `./embeddings` ## ๐Ÿ“ Project Structure ``` DHIGROWTH_RAG/ โ”‚ โ”œโ”€โ”€ app.py # FastAPI application โ”œโ”€โ”€ config.py # Configuration module โ”œโ”€โ”€ requirements.txt # Python dependencies โ”œโ”€โ”€ README.md # This file โ”‚ โ”œโ”€โ”€ models/ # LLM model files โ”‚ โ””โ”€โ”€ Dhi_Qwen2P5_0_5B_Q4_K_M.gguf โ”‚ โ”œโ”€โ”€ data/ # Training/knowledge base data โ”‚ โ””โ”€โ”€ dhigrowth_final_rag_ft.jsonl โ”‚ โ”œโ”€โ”€ embeddings/ # ChromaDB persistent storage โ”‚ โ””โ”€โ”€ (auto-generated) โ”‚ โ””โ”€โ”€ src/ # Source code โ”œโ”€โ”€ embed_loader.py # Vector database creation โ”œโ”€โ”€ retriever.py # Document retrieval module โ”œโ”€โ”€ llm_wrapper.py # LLM model wrapper โ”œโ”€โ”€ rag_pipeline.py # Main RAG pipeline โ””โ”€โ”€ logger_config.py # Logging configuration ``` ## โš™๏ธ Configuration Configuration is managed through environment variables or `config.py`. Create a `.env` file in the project root: ```env # Model paths MODEL_PATH=./models/Dhi_Qwen2P5_0_5B_Q4_K_M.gguf EMBEDDINGS_PATH=./embeddings DATA_PATH=./data/dhigrowth_final_rag_ft.jsonl # Embedding model EMBEDDING_MODEL=all-MiniLM-L6-v2 # Generation settings MAX_TOKENS=400 TOP_K=3 # LLM settings N_CTX=4096 TEMPERATURE=0.0 TOP_P=1.0 # Server settings PORT=8001 ``` ### Configuration Options - `MODEL_PATH`: Path to the GGUF model file - `EMBEDDINGS_PATH`: ChromaDB storage directory - `DATA_PATH`: Path to JSONL knowledge base file - `EMBEDDING_MODEL`: SentenceTransformer model name - `MAX_TOKENS`: Maximum tokens for LLM generation - `TOP_K`: Number of documents to retrieve - `N_CTX`: Context window size - `TEMPERATURE`: Sampling temperature (0.0 = deterministic) - `TOP_P`: Nucleus sampling parameter - `PORT`: Server port (default: 8001) ## ๐Ÿš€ Usage ### Starting the API Server ```bash # Activate virtual environment source venv/Scripts/activate # Windows: venv\Scripts\activate # Start the server uvicorn app:app --reload --port 8001 ``` The API will be available at `http://localhost:8001` ### Interactive API Documentation Once the server is running, visit: - **Swagger UI**: http://localhost:8001/docs - **ReDoc**: http://localhost:8001/redoc ### Using the RAG Pipeline Directly ```python from src.rag_pipeline import rag_chat # Ask a question answer = rag_chat("What services does Dhigrowth offer?") print(answer) ``` ## ๐Ÿ“ก API Documentation ### Endpoints #### `GET /` Root endpoint with API information. **Response:** ```json { "message": "Dhigrowth RAG Chatbot API", "version": "1.0.0", "endpoints": { "health": "/health", "ask": "/ask (POST)" } } ``` #### `GET /health` Health check endpoint. **Response:** ```json { "status": "healthy", "service": "Dhigrowth RAG Chatbot", "version": "1.0.0" } ``` #### `POST /ask` Ask a question to the chatbot. **Request Body:** ```json { "question": "What services does Dhigrowth offer?" } ``` **Response:** ```json { "question": "What services does Dhigrowth offer?", "answer": "Dhigrowth provides website development, application development, digital marketing, SEO improvement, business automation, and custom business growth strategies." } ``` **Error Responses:** - `400 Bad Request`: Invalid input - `422 Unprocessable Entity`: Validation error - `500 Internal Server Error`: Server error ### Example API Calls **Using cURL:** ```bash curl -X POST http://localhost:8001/ask \ -H "Content-Type: application/json" \ -d '{"question": "How do I book a consultation?"}' ``` **Using Python:** ```python import requests response = requests.post( "http://localhost:8001/ask", json={"question": "What services does Dhigrowth offer?"} ) print(response.json()) ``` **Using JavaScript:** ```javascript fetch("http://localhost:8001/ask", { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ question: "What services does Dhigrowth offer?", }), }) .then((response) => response.json()) .then((data) => console.log(data)); ``` ## ๐Ÿงช Testing ### Running Tests Test the RAG pipeline directly: ```bash python -c "from src.rag_pipeline import rag_chat; print(rag_chat('What services does Dhigrowth offer?'))" ``` Test the API using FastAPI TestClient: ```bash python -c "from app import app; from fastapi.testclient import TestClient; client = TestClient(app); print(client.get('/health').json())" ``` ### Test Cases The system has been tested with: - โœ… Valid questions about Dhigrowth - โœ… Out-of-scope questions (returns "I don't have that information") - โœ… Input validation (empty/whitespace questions) - โœ… Error handling See `TEST_RESULTS.md` for detailed test results. ## ๐Ÿšข Deployment ### Option 1: Local Development ```bash uvicorn app:app --reload --port 8001 ``` ### Option 2: Production with Uvicorn ```bash uvicorn app:app --host 0.0.0.0 --port 8001 --workers 4 ``` ### Option 3: Docker (Recommended for Production) Create a `Dockerfile`: ```dockerfile FROM python:3.11-slim WORKDIR /app COPY requirements.txt . RUN pip install --no-cache-dir -r requirements.txt COPY . . EXPOSE 8001 CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "8001"] ``` Build and run: ```bash docker build -t dhigrowth-rag . docker run -p 8001:8001 dhigrowth-rag ``` ### Option 4: Cloud Deployment **VPS Deployment:** 1. Upload project files 2. Install dependencies 3. Run with process manager (systemd, supervisor, or PM2) 4. Configure reverse proxy (nginx) **Platform-as-a-Service:** - Deploy to Heroku, Railway, or Render - Configure environment variables - Set up persistent storage for embeddings ## ๐Ÿ”ง Troubleshooting ### Common Issues **Issue: Model file not found** - **Solution**: Ensure `Dhi_Qwen2P5_0_5B_Q4_K_M.gguf` is in the `models/` directory **Issue: ChromaDB collection empty** - **Solution**: Run `python src/embed_loader.py` to create the vector database **Issue: Import errors** - **Solution**: Ensure virtual environment is activated and dependencies are installed **Issue: Port already in use** - **Solution**: Change port: `uvicorn app:app --port 8001` **Issue: Slow response times** - **Solution**: - First request is slower (model loading) - Subsequent requests are faster - Consider using GPU for faster inference ### Logs Check logs in `dhigrowth_rag.log` for detailed error information. ## ๐Ÿ“Š Performance - **Model Loading**: ~2-3 seconds (first request only) - **Embedding Model**: ~5 seconds (first request only) - **Query Processing**: ~2-3 seconds per query - **Vector Database**: 1,601 documents indexed ## ๐Ÿ”’ Security Considerations - Input validation prevents injection attacks - CORS can be configured for specific origins in production - Consider adding authentication for production deployment - Rate limiting recommended for public APIs ## ๐Ÿ“ License [Add your license information here] ## ๐Ÿ‘ฅ Contributing [Add contribution guidelines if applicable] ## ๐Ÿ“ง Contact For questions or support, contact [your contact information] ## ๐Ÿ™ Acknowledgments - Qwen2.5 model by Alibaba Cloud - ChromaDB for vector database - SentenceTransformers for embeddings - FastAPI for the web framework --- **Version**: 1.0.0 **Last Updated**: November 2024