Skip to content

srinivaskarre-sk/document-qa

Repository files navigation

Document Q&A SaaS Backend

A Node.js/TypeScript backend for a Document Q&A SaaS application that allows users to upload documents (PDF, DOCX, TXT) and ask questions about their content using AI.

Features

  • Document Processing: Support for PDF, DOCX, and TXT files
  • Text Chunking: Intelligent text splitting with overlap for better context
  • Vector Embeddings: Using transformers.js for local embedding generation
  • Q&A System: Local Llama model integration for intelligent question answering
  • RAG Pipeline: Retrieval-Augmented Generation for accurate answers
  • TypeScript: Full type safety with strict configuration
  • SQLite Database: Prisma ORM with SQLite for development

Tech Stack

  • Runtime: Node.js 18+ with TypeScript 5+
  • Framework: Express.js with TypeScript
  • Database: SQLite with Prisma ORM
  • Document Processing: pdf-parse, mammoth
  • Embeddings: @xenova/transformers (all-MiniLM-L6-v2)
  • Q&A: Local Llama model
  • File Upload: Multer with validation

Quick Start

Prerequisites

  • Node.js 18+
  • npm or yarn
  • Local Llama model file

Installation

  1. Clone and install dependencies:

    npm install
  2. Set up environment variables:

    cp .env.example .env

    Edit .env and configure your local Llama model:

    LLAMA_MODEL_PATH="./models/llama-2-7b-chat.gguf"
    LLAMA_MODEL_TYPE="llama-2-7b-chat"
  3. Set up the database:

    npm run db:generate
    npm run db:migrate
  4. Start the development server:

    npm run dev

The server will start on http://localhost:3001

API Endpoints

Health Check

  • GET /health - Server health status

Documents

  • GET /api/documents - List all documents
  • GET /api/documents/:id - Get specific document
  • POST /api/documents - Upload new document
  • DELETE /api/documents/:id - Delete document
  • GET /api/documents/:id/chunks - Get document chunks

Q&A

  • POST /api/qa/ask - Ask question about document
  • GET /api/qa/models - Get AI model information
  • POST /api/qa/batch - Ask multiple questions

Usage Examples

Upload a Document

curl -X POST http://localhost:3001/api/documents \
  -F "file=@document.pdf" \
  -F "userId=user123"

Ask a Question

curl -X POST http://localhost:3001/api/qa/ask \
  -H "Content-Type: application/json" \
  -d '{
    "documentId": "doc_id_here",
    "question": "What is the main topic of this document?",
    "userId": "user123"
  }'

Get Document List

curl http://localhost:3001/api/documents

Project Structure

src/
├── index.ts              # Main server entry point
├── types/                # TypeScript type definitions
│   └── index.ts
├── middleware/           # Express middleware
│   └── errorHandler.ts
├── services/             # Business logic services
│   ├── documentProcessor.ts
│   ├── embeddingService.ts
│   └── qaService.ts
├── routes/               # API route handlers
│   ├── documents.ts
│   └── qa.ts
└── prisma/               # Database schema
    └── schema.prisma

Development

Available Scripts

  • npm run dev - Start development server with hot reload
  • npm run build - Build for production
  • npm run start - Start production server
  • npm run type-check - TypeScript type checking
  • npm run db:migrate - Run database migrations
  • npm run db:studio - Open Prisma Studio
  • npm test - Run tests

Environment Variables

Variable Description Default
PORT Server port 3001
NODE_ENV Environment development
DATABASE_URL Database connection file:./dev.db
LLAMA_MODEL_PATH Path to Llama model file ./models/llama-2-7b-chat.gguf
LLAMA_MODEL_TYPE Llama model type llama-2-7b-chat
MAX_FILE_SIZE Max file size (bytes) 10485760 (10MB)
EMBEDDING_MODEL Embedding model Xenova/all-MiniLM-L6-v2

Database Schema

The application uses Prisma with the following models:

  • User: User accounts (basic implementation)
  • Document: Uploaded documents with metadata
  • Chunk: Text chunks with embeddings for vector search
  • Usage: Usage tracking for analytics

File Processing Pipeline

  1. Upload: File validation and storage
  2. Extraction: Text extraction based on file type
  3. Chunking: Intelligent text splitting with overlap
  4. Embedding: Vector embedding generation
  5. Storage: Database storage with relationships

Q&A Pipeline

  1. Question: User submits question
  2. Embedding: Generate embedding for question
  3. Search: Vector similarity search for relevant chunks
  4. Context: Prepare context from relevant chunks
  5. Answer: Generate answer using local Llama model
  6. Response: Return structured response with sources

Error Handling

The application includes comprehensive error handling:

  • Input validation with detailed error messages
  • File processing error handling
  • API error responses with appropriate HTTP status codes
  • Database error handling
  • Graceful server shutdown

Security

  • Helmet.js for security headers
  • CORS configuration
  • File type validation
  • File size limits
  • Input sanitization

Performance

  • Efficient text chunking with overlap
  • Local embedding generation (no external API calls)
  • Database indexing for fast queries
  • Memory-efficient file processing

Next Steps

This is the MVP backend. Future enhancements include:

  • User authentication and authorization
  • Rate limiting
  • Caching layer
  • Advanced vector database (Pinecone/Chroma)
  • Multi-document conversations
  • Usage analytics and billing
  • Admin dashboard
  • API documentation with Swagger

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Add tests
  5. Submit a pull request

License

MIT License - see LICENSE file for details

About

Chatbot to upload a file and ask questions

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors