Skip to content

Repository files navigation

🧠 PDF RAG Chatbot with Multi-User Support

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.


📦 Features

  • 📄 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 StorageMongoDB 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

🚀 Getting Started

1️⃣ Clone the Repository

git clone https://github.com/NKomrade/pdf-rag-bot.git
cd pdf-rag-bot

2️⃣ Install Dependencies

npm install

If you face dependency issues:

npm install --legacy-peer-deps

3️⃣ Configure Environment Variables

Create a .env.local file:

cp .env.example .env.local

Then 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 Stack

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

📸 Screenshot

Rag Bot Preview


📂 Project Structure

.
├── 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

💬 How It Works

🔄 Multi-User Session Flow:

  1. User visits → Gets unique session ID (stored in cookies)
  2. Upload PDF → Document chunks stored with session isolation
  3. Ask questions → Vector search within user's documents only
  4. AI responses → Generated using relevant context from user's PDFs
  5. Privacy ensured → Each user only sees their own documents

🏗️ RAG Architecture:

PDF Upload → Text Extraction → Chunking → Embeddings → MongoDB Storage
     ↓
User Query → Embedding → Vector Search → Context Retrieval → LLM Response

📱 Example Usage:

  1. Upload a PDF file using the uploader
  2. Wait for processing completion (chunks + embeddings created)
  3. Ask questions like: "What is the main idea of Chapter 2?"
  4. Get AI-powered responses based on your document content

🛠 To Do

  • 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

🙌 Acknowledgements


About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages