Skip to content

IRONalways17/RAG-ChatBot

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Advanced RAG Q&A Chatbot System

A sophisticated Retrieval-Augmented Generation (RAG) chatbot built with Node.js and Express, featuring real-time document processing, semantic search, and multiple LLM provider support.

Author

Aaryan Choudhary

Features

  • Multi-Format Document Support: PDF, DOCX, TXT, CSV, HTML, JSON
  • Advanced Text Processing: Intelligent chunking with overlap and semantic boundaries
  • Real-time Communication: WebSocket-based streaming responses
  • Multiple LLM Providers: OpenAI GPT, Google Gemini, Anthropic Claude, Cohere, HuggingFace
  • Semantic Search: Vector-based document retrieval with cosine similarity
  • Professional UI: Clean, responsive interface with drag-and-drop file upload
  • Configurable Settings: Adjustable similarity thresholds and result limits
  • Source Attribution: Transparent citation of information sources
  • Rate Limiting: Built-in protection against abuse
  • Comprehensive Logging: Detailed system monitoring and debugging

Technology Stack

  • Backend: Node.js, Express.js
  • Real-time: WebSocket (ws)
  • Document Processing: pdf-parse, mammoth, csv-parser
  • Vector Operations: Custom implementation with cosine similarity
  • Security: Helmet, CORS, rate limiting
  • Frontend: Vanilla JavaScript, TailwindCSS
  • Architecture: Modular service-based design

Project Structure

rag-qa-chatbot/
├── src/
│   ├── services/
│   │   ├── index.js              # Service initialization
│   │   ├── documentProcessor.js  # Document parsing and chunking
│   │   ├── embeddingService.js    # Text embedding generation
│   │   ├── vectorStore.js         # Vector storage and similarity search
│   │   ├── llmService.js          # LLM provider integration
│   │   └── retrievalService.js    # RAG pipeline orchestration
│   ├── routes/
│   │   ├── index.js              # Route registration
│   │   ├── documents.js          # Document management endpoints
│   │   ├── chat.js               # Chat and query endpoints
│   │   └── admin.js              # Administrative endpoints
│   ├── middleware/
│   │   ├── rateLimiter.js        # Rate limiting middleware
│   │   ├── errorHandler.js       # Global error handling
│   │   └── auth.js               # Authentication middleware
│   ├── websocket/
│   │   └── chatSocket.js         # WebSocket message handling
│   └── utils/
│       ├── logger.js             # Logging utilities
│       └── textProcessor.js      # Text processing utilities
├── public/
│   ├── index.html                # Main application interface
│   ├── css/
│   │   └── styles.css            # Application styling
│   └── js/
│       └── app.js                # Frontend application logic
├── uploads/                      # Document storage directory
├── .env.example                  # Environment configuration template
├── .gitignore                    # Git ignore rules
├── package.json                  # Project dependencies and scripts
├── server.js                     # Main application entry point
└── README.md                     # Project documentation

Installation

Prerequisites

  • Node.js (v18 or higher)
  • npm or yarn package manager

Quick Start

  1. Clone the repository

    git clone https://github.com/yourusername/rag-qa-chatbot.git
    cd rag-qa-chatbot
  2. Install dependencies

    npm install
  3. Configure environment variables

    cp .env.example .env

    Edit .env file with your API keys:

    # Required for enhanced features
    OPENAI_API_KEY=your_openai_api_key_here
    GOOGLE_API_KEY=your_google_api_key_here
    ANTHROPIC_API_KEY=your_anthropic_api_key_here
    COHERE_API_KEY=your_cohere_api_key_here
    HUGGINGFACE_API_KEY=your_huggingface_api_key_here
    
    # Server configuration
    PORT=3000
    NODE_ENV=development
    
    # Security
    RATE_LIMIT_WINDOW_MS=900000
    RATE_LIMIT_MAX_REQUESTS=100
  4. Start the application

    npm start
  5. Access the application Open your browser and navigate to http://localhost:3000

Alternative Installation Methods

Minimal Installation (Core Features Only)

npm run install-minimal
npm start

Development Mode

npm install -g nodemon
npm run dev

Usage

Document Upload

  1. Via Web Interface: Drag and drop files onto the upload zone or click to browse
  2. Supported Formats: PDF, DOCX, TXT, CSV, HTML, JSON files
  3. Automatic Processing: Documents are automatically chunked and indexed

Querying Documents

  1. Natural Language Queries: Ask questions in plain English
  2. Context-Aware Responses: Answers include source citations
  3. Real-time Streaming: Watch responses generate in real-time
  4. Follow-up Questions: Maintain conversation context

Configuration

Access the settings panel to adjust:

  • Retrieval Count: Number of relevant chunks to retrieve (1-20)
  • Similarity Threshold: Minimum relevance score (0.0-1.0)
  • Streaming Mode: Enable/disable real-time response streaming
  • LLM Provider: Switch between different AI models

API Endpoints

Document Management

  • POST /api/documents/upload - Upload and process documents
  • GET /api/documents/ - List all processed documents
  • DELETE /api/documents/:id - Remove document and its chunks

Chat Interface

  • POST /api/chat/query - Send query and receive response
  • POST /api/chat/stream - Streaming query endpoint
  • POST /api/chat/related - Get suggested follow-up questions

Administration

  • POST /api/admin/llm/provider - Change LLM provider
  • GET /api/admin/stats - System statistics
  • POST /api/admin/clear - Clear all documents and chat history

WebSocket Events

Client to Server

{
  "type": "chat",
  "data": {
    "message": "What is the main topic?",
    "sessionId": "session-id",
    "options": {
      "topK": 5,
      "threshold": 0.7
    }
  }
}

Server to Client

{
  "type": "chat_response",
  "data": {
    "message": "Based on the documents...",
    "sources": [
      {
        "filename": "document.pdf",
        "preview": "relevant text excerpt",
        "similarity": 0.85
      }
    ]
  }
}

Configuration Options

Environment Variables

Variable Description Default Required
PORT Server port 3000 No
NODE_ENV Environment mode development No
OPENAI_API_KEY OpenAI API key - Optional
GOOGLE_API_KEY Google Gemini API key - Optional
ANTHROPIC_API_KEY Anthropic Claude API key - Optional
COHERE_API_KEY Cohere API key - Optional
HUGGINGFACE_API_KEY HuggingFace API key - Optional

System Settings

  • Maximum File Size: 50MB per document
  • Chunk Size: 500-1000 characters with 100 character overlap
  • Vector Dimensions: 384 (sentence-transformers compatible)
  • Maximum Documents: No hard limit (memory dependent)
  • Supported Languages: Multi-language support via LLM providers

Development

Project Architecture

The application follows a modular, service-oriented architecture:

  1. Service Layer: Core business logic and data processing
  2. Route Layer: HTTP endpoint handling and validation
  3. WebSocket Layer: Real-time communication management
  4. Middleware Layer: Cross-cutting concerns (auth, logging, rate limiting)
  5. Frontend Layer: User interface and client-side logic

Adding New Features

  1. New Document Format: Extend documentProcessor.js service
  2. New LLM Provider: Add integration to llmService.js
  3. Custom Embedding: Modify embeddingService.js
  4. UI Enhancements: Update public/ directory files

Testing

# Run basic functionality test
npm test

# Test document upload
curl -X POST -F "document=@test.pdf" http://localhost:3000/api/documents/upload

# Test query endpoint
curl -X POST -H "Content-Type: application/json" \
  -d '{"message":"What is this document about?"}' \
  http://localhost:3000/api/chat/query

Deployment

Local Production

NODE_ENV=production npm start

Docker Deployment

FROM node:18-alpine
WORKDIR /app
COPY package*.json ./
RUN npm ci --only=production
COPY . .
EXPOSE 3000
CMD ["npm", "start"]

Cloud Deployment

The application can be deployed to any Node.js hosting platform:

  • Heroku
  • Vercel
  • Railway
  • Digital Ocean
  • AWS Elastic Beanstalk

Troubleshooting

Common Issues

  1. Port Already in Use

    Error: listen EADDRINUSE :::3000

    Solution: Change PORT in .env or kill process using port 3000

  2. Large File Upload Fails

    Error: File too large

    Solution: Adjust file size limits in server.js

  3. API Key Errors

    Error: Unauthorized - Invalid API key

    Solution: Verify API keys in .env file

  4. Memory Issues with Large Documents

    Error: JavaScript heap out of memory

    Solution: Increase Node.js memory limit or process documents in smaller chunks

Performance Optimization

  • Enable compression middleware for faster response times
  • Implement document caching for frequently accessed files
  • Use connection pooling for database operations
  • Configure appropriate rate limiting based on usage patterns

Contributing

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/new-feature)
  3. Commit your changes (git commit -am 'Add new feature')
  4. Push to the branch (git push origin feature/new-feature)
  5. Create a Pull Request

License

This project is licensed under the MIT License - see the LICENSE file for details.

Acknowledgments

  • Built with modern Node.js and Express.js
  • Vector similarity search implementation
  • Multiple LLM provider integrations
  • Professional UI design with TailwindCSS

Support

For questions, issues, or feature requests, please open an issue on GitHub or contact the developer.


Aaryan Choudhary - Professional RAG Q&A Chatbot System

About

CSI Week-8 | 2025

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published