A Retrieval-Augmented Generation (RAG) chatbot built with Next.js, enabling multiple users to upload PDF files and query their contents in real-time using AI-powered language models and vector search.
Each user session is securely isolated, ensuring privacy and independent interactions.
- 📄 PDF Upload & Processing – Upload and extract text automatically
- 🔍 Smart Document Chunking – Intelligent segmentation for better retrieval
- 🧠 AI-Powered Embeddings – Generate semantic vectors via Hugging Face Transformers
- 💾 Flexible Storage – MongoDB Atlas for vector storage with local fallback
- 🤖 Advanced LLM Integration – Context-aware responses via OpenAI / OpenRouter
- 👥 Multi-User Support – Session-based isolation for concurrent users
- 🔒 Privacy & Security – Each user’s data remains private and independent
- ⚡ Real-Time Chat – Fast, responsive, and interactive interface
- 🧹 Auto-Cleanup – Sessions expire automatically after 24 hours
git clone https://github.com/NKomrade/pdf-rag-bot.git
cd pdf-rag-botnpm installIf you face dependency issues:
npm install --legacy-peer-deps3️⃣ Configure Environment Variables
Create a .env.local file:
cp .env.example .env.localThen edit .env.local with your credentials:
# === MongoDB Atlas (Primary Storage) ===
MONGODB_URI=mongodb+srv://<username>:<password>@cluster0.mongodb.net/<dbname>?retryWrites=true&w=majority
# === Hugging Face Transformers (Embeddings) ===
HUGGINGFACE_API_KEY=your-huggingface-api-key
# === OpenAI/OpenRouter (LLM Responses) ===
OPENAI_API_KEY=your-openai-or-openrouter-api-key
# === App Configuration ===
NEXT_PUBLIC_SITE_URL=http://localhost:3000| Tech | Purpose |
|---|---|
| Next.js 14 | React framework for frontend + API routes |
| TypeScript | Type-safe development |
| LangChain | Document loaders and text splitting |
| Hugging Face | Text embeddings with Transformers API |
| OpenAI/OpenRouter | Language model inference for responses |
| MongoDB Atlas | Vector storage + document metadata |
| TailwindCSS | Modern styling and responsive design |
| PDF-Parse | PDF text extraction and processing |
.
├── app/
│ ├── layout.tsx # App layout and metadata
│ ├── page.tsx # Main chat interface
│ ├── test-session/ # Session testing page
│ │ └── page.tsx
│ └── api/
│ ├── upload/ # PDF upload and processing
│ │ └── route.ts
│ ├── query/ # Chat queries and responses
│ │ └── route.ts
│ └── debug/ # System debugging info
│ └── route.ts
├── components/
│ ├── ChatWindow.tsx # Main chat interface
│ ├── MessageBubble.tsx # Individual message display
│ └── PdfUploader.tsx # PDF upload component
├── lib/
│ ├── embeddings.ts # Hugging Face embeddings
│ ├── llm.ts # OpenAI/OpenRouter LLM calls
│ ├── vectorStore.ts # Vector similarity search
│ └── sessionManager.ts # Multi-user session handling
├── temp/ # Temporary file storage
│ └── chunks.json # Local storage fallback
├── .env.local # Environment variables
├── package.json
└── README.md
- User visits → Gets unique session ID (stored in cookies)
- Upload PDF → Document chunks stored with session isolation
- Ask questions → Vector search within user's documents only
- AI responses → Generated using relevant context from user's PDFs
- Privacy ensured → Each user only sees their own documents
PDF Upload → Text Extraction → Chunking → Embeddings → MongoDB Storage
↓
User Query → Embedding → Vector Search → Context Retrieval → LLM Response
- Upload a PDF file using the uploader
- Wait for processing completion (chunks + embeddings created)
- Ask questions like: "What is the main idea of Chapter 2?"
- Get AI-powered responses based on your document content
- Add user authentication system
- Implement persistent chat history storage
- Support multiple PDF uploads per session
- Add document management (list, delete, switch)
- Implement conversation memory across sessions
- Add support for other file formats (DOCX, TXT)
- Optimize embedding model for better accuracy
- Add real-time collaboration features
- LangChain - Document processing and text splitting
- Hugging Face - Text embeddings and transformers
- OpenAI - Language model inference
- MongoDB Atlas - Vector storage and database
- Next.js - React framework and API routes
- TailwindCSS - Styling and design system
