Skip to content

vardaansinghal17/RAG-CHAT-MODEL

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

📄 RAG PDF Chat Model

A backend API that lets you upload PDF documents and chat with them using AI. Built with Retrieval-Augmented Generation (RAG), it extracts content from your PDFs, stores vector embeddings in Weaviate, and uses GPT-4o-mini via OpenRouter to answer your questions — strictly grounded in the document's content.


✨ Features

  • 📤 PDF Upload & Storage — Upload PDFs directly to Supabase Storage with signed URL generation
  • 🔍 Intelligent Text Extraction — Extracts and parses text from every page of the uploaded PDF using pdfjs-dist
  • ✂️ Automatic Text Chunking — Splits extracted text into manageable chunks for accurate retrieval
  • 🧠 Vector Embeddings — Generates semantic embeddings for each chunk using openai/text-embedding-3-small via OpenRouter
  • 🗄️ Dual Storage — Stores embeddings and chunks in both Weaviate (vector search) and Supabase (relational metadata)
  • 💬 Context-Aware Chat — Answers questions using only the retrieved context from the document; never hallucinates
  • Fast Similarity Search — Performs nearVector search in Weaviate filtered by document ID for precise, scoped retrieval

🛠️ Tech Stack

Layer Technology
Runtime Node.js (ESM)
Framework Express.js v5
LLM & Embeddings OpenRouter (GPT-4o-mini + text-embedding-3-small)
Vector Database Weaviate Cloud
Storage & DB Supabase (Storage + PostgreSQL)
PDF Parsing pdfjs-dist, pdf-parse
File Upload Multer (in-memory)

🚀 Getting Started

Prerequisites

  • Node.js v18+
  • A Supabase project with:
    • A storage bucket named pdfs
    • A pdf_documents table (for metadata)
    • A pdf_chunks table (for chunk storage)
  • A Weaviate Cloud instance with a Document collection
  • An OpenRouter API key

Installation

git clone https://github.com/vardaansinghal17/RAG-CHAT-MODEL.git
cd rag-chat-model
npm install

Environment Variables

Create a .env file in the root directory:

SUPABASE_URL=your_supabase_project_url
SUPABASE_SERVICE_ROLE_KEY=your_supabase_service_role_key
OPENROUTER_API_KEY=your_openrouter_api_key
WEAVIATE_API_KEY=your_weaviate_api_key
WEAVIATE_URL=your_weaviate_cluster_url

Running the Server

# Development (with hot reload)
npm run dev

# Production
npm start

Server starts on http://localhost:3000


📡 API Reference

Upload a PDF

Uploads a PDF, extracts its text, generates embeddings, and indexes it for chat.

POST /upload
Content-Type: multipart/form-data

pdf: <your-pdf-file>

Response:

{
  "message": "PDF uploaded & indexed successfully",
  "pdfId": "uuid-of-the-document"
}

Chat with a PDF

Ask a question about a previously uploaded PDF.

POST /chat
Content-Type: application/json

{
  "question": "What is the main topic of this document?",
  "pdfId": "uuid-of-the-document"
}

Response:

{
  "answer": "The document discusses..."
}

Note: The model will only answer based on the document's content. If the answer is not found in the context, it will say so.


🔄 How It Works

Upload Flow:
PDF File → Supabase Storage → Text Extraction (pdfjs-dist)
       → Chunking (500 chars) → Embeddings (OpenRouter)
       → Weaviate (vector store) + Supabase (chunk metadata)

Chat Flow:
Question → Embedding → Weaviate nearVector Search (top 3 chunks)
        → Context Assembly → GPT-4o-mini → Answer

📦 Scripts

Command Description
npm run dev Start development server with nodemon
npm start Start production server

📄 License

This project is licensed under the ISC License.

About

A Node.js RAG (Retrieval-Augmented Generation) backend that lets you upload PDF documents and chat with them using AI — powered by OpenRouter (GPT-4o-mini), Weaviate vector search, and Supabase storage.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors