A comprehensive Retrieval-Augmented Generation (RAG) chatbot system with a Next.js frontend and multiple FastAPI microservices, featuring document processing, vector embeddings, and user authentication.
This project implements a complete RAG system that allows users to query information from processed documents. The system processes documents into chunks, embeds them into vector space, and retrieves relevant context to generate accurate responses using LLM technology.
The system is built on a microservices architecture with the following components:
- Frontend: Next.js application with authentication and chat interface
- Authentication Service: Handles user management and JWT authentication
- Chunker Service: Processes documents into semantic chunks for better retrieval
- Embedding Service: Converts text chunks into vector embeddings
- Vector Database: Stores and enables semantic search of document embeddings
- LLM Service: Interfaces with Gemini API for response generation
- RAG Engine: Orchestrates the retrieval and generation process
-
User Management
- User authentication (login/register)
- Secure password storage with bcrypt encryption
- JWT-based authentication
-
Document Processing
- Semantic chunking of documents
- Support for multiple file types (PDF, DOCX, TXT)
- Document metadata tracking
-
Chat Interface
- Markdown rendering for rich responses
- Auto-scrolling to the latest message
- Loading indicators
- Error handling
- Reset chat functionality
- Typing animation for bot responses
-
RAG Capabilities
- Semantic search for relevant context
- Context-aware responses
- Citation of source documents
- Next.js (React + TypeScript)
- Tailwind CSS
- ShadCN UI components
- Axios for API requests
- React Markdown
- FastAPI (Python)
- MongoDB (document storage)
- ChromaDB (vector database)
- Sentence Transformers (embeddings)
- Gemini API (LLM)
- JWT for authentication
- Docker and Docker Compose
- MongoDB for data persistence
- Node.js 16.8 or later
- Python 3.8+
- MongoDB
- Docker and Docker Compose (for containerized setup)
- Gemini API key
- npm or yarn
-
Clone the repository:
git clone <repository-url> cd chatbot
-
Add your Gemini API key to the
docker-compose.yml
file:environment: - GEMINI_API_KEY=your_api_key_here
-
Build and start the containers:
docker-compose up -d
-
Import sample data (optional):
./import_mongo_docker.sh
-
Access the application at http://localhost:3000
-
Set up virtual environments and install dependencies for each service:
# Authentication Service cd backend python -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate pip install -r requirements.txt # Chunker Service cd ../chunker_service python -m venv venv source venv/bin/activate pip install -r requirements.txt # Embedding Service cd ../embedding_service python -m venv venv source venv/bin/activate pip install -r requirements.txt # LLM Service cd ../llm_service python -m venv venv source venv/bin/activate pip install -r requirements.txt # RAG Engine cd ../rag_engine python -m venv venv source venv/bin/activate pip install -r requirements.txt
-
Configure MongoDB:
- Make sure MongoDB is running
- Update the
.env
files in each service directory if needed
-
Start each service:
# In separate terminals cd backend && python main.py # Runs on port 8005 cd chunker_service && python main.py # Runs on port 8002 cd embedding_service && python main.py # Runs on port 8000 cd llm_service && python main.py # Runs on port 8001 cd rag_engine && python main.py # Runs on port 8003
-
Navigate to the project root directory
-
Install dependencies:
npm install # or yarn install
-
Run the development server:
npm run dev # or yarn dev
-
Open http://localhost:3000 in your browser to see the application.
The system requires MongoDB data for users and processed documents. You can import sample data using one of the provided scripts:
./import_mongo_data.sh
./import_mongo_docker.sh
Alternatively, you can use the Python script:
python import_mongo_data.py
The import scripts will populate:
users
collection with sample user accountsprocessed_documents
collection with document metadata and chunk references
POST /auth/login
: Authenticate a userPOST /auth/register
: Register a new userGET /auth/me
: Get current user information
POST /api/process
: Process a document into chunksGET /api/documents
: List processed documentsGET /api/documents/{document_id}
: Get document details
POST /api/embed
: Generate embeddings for textPOST /api/search
: Search for similar documents
POST /api/generate
: Generate text using LLM
POST /api/v1/query
: Send a query to the RAG engine
src/
: Frontend codeapp/
: Next.js app router pagescomponents/
: React componentslib/
: Utility functions, API service, and types
backend/
: Authentication servicechunker_service/
: Document processing serviceembedding_service/
: Vector embedding servicellm_service/
: LLM integration servicerag_engine/
: RAG orchestration servicextra/
: Sample data for importdocker-compose.yml
: Docker configurationimport_mongo_*.sh
: Data import scripts
MIT