Skip to content

iparesh18/ChatGPT-Clone

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

11 Commits
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸš€ ChatGPT Clone (Realtime + Web Scraping + Hybrid Memory + Socket.io)

| REACTJS | MONGODB | NODEJS | EXPRESSJS | PINECONE | RAG | GEMINI | SOCKET.IO | REST API |

A complete production-level ChatGPT clone featuring:

  • ⚑ Real-time AI chat (Socket.io)
  • 🌐 Web Scraping (Cheerio + Puppeteer)
  • 🧠 Hybrid Memory (STM + LTM via Pinecone)
  • πŸ” Auth with JWT + Cookies
  • β™» Auto WebSocket reconnect + polling fallback
  • 🎨 ChatGPT-level UI/UX (almost identical)
  • πŸ“¦ Parallel optimized LLM pipeline (Promise.all)
  • 🧡 Hidden prompt engineering (avoids Gemini system-role limitations)

⭐ Features

βœ” Realtime communication using WebSockets

βœ” Hybrid STM + LTM memory system

βœ” Web scraping support

  • Static websites β†’ Cheerio
  • Dynamic JS-rendered β†’ Puppeteer

βœ” Strong authentication

βœ” Deleting chats + cascading delete for messages

βœ” Perfect mobile-responsive UI

βœ” Shimmer floating loader for AI response

βœ” Secure cookies + CORS + production deployment ready


🧩 Tech Stack

Frontend

  • React + Vite
  • TailwindCSS
  • Axios
  • Socket.io-client

Backend

  • Node.js
  • Express.js
  • Socket.io
  • MongoDB + Mongoose
  • Pinecone Vector DB
  • Gemini 2.0 Flash API
  • Cheerio
  • Puppeteer
  • JWT
  • Cookie-parser
  • CORS

πŸ“¦ Major Packages Used

Purpose Package
Real-time chat socket.io
Frontend socket socket.io-client
Static scraping cheerio
JS-rendered scraping puppeteer
Vector memory @pinecone-database/pinecone
AI model @google/genai
Auth jsonwebtoken, bcryptjs
Cookies cookie-parser
Backend express, cors
DB mongoose

🧠 Hybrid Memory System (Advanced)

Short-Term Memory (STM)

  • Last 10 messages of the current chat
  • Controls conversation flow
  • Stored in MongoDB

Long-Term Memory (LTM)

  • Stored in Pinecone
  • Every user message & bot response becomes a vector
  • On each new message:
    topMatches = pinecone.query(vectors)
    
  • Returned top 3 relevant memories

Hidden Context Merging (Gemini-safe)

Gemini does NOT allow system-role messages.
So we merge STM + LTM + scraped content inside a hidden user role prompt:

{ role: "user", text: hiddenContext + "User asked: " + userMessage }

The model sees memory + scraped content.
User sees only the answer.


🌐 Web Scraping System

Static Websites β†’ Cheerio

axios.get(url)
cheerio.load(html)
extract & clean text

JS Websites β†’ Puppeteer

puppeteer.launch()
page.goto(url, waitUntil:"networkidle0")
extract page content

Automatically selects Cheerio β†’ Puppeteer fallback.


⚑ Performance Optimizations

πŸ”₯ 1. Promise.all() Parallel Execution

Used throughout the backend:

const [message, vectors] = await Promise.all([
  messageModel.create(...),
  aiService.generateVector(...)
])

DB + vector embedding run simultaneously, reducing latency by 40–60%.


πŸ”Œ 2. Hybrid WebSocket + Polling

Render sometimes kills WebSocket connections.
Solution:

transports: ["websocket", "polling"]
reconnection: true
reconnectionAttempts: Infinity

Flawless stability.


πŸ”’ 3. Cookie-based JWT Authentication

  • User stays logged in after refresh
  • httpOnly + secure + sameSite=none
  • Accessible via both REST & Socket.io

πŸ— Flow of the System

1. User logs in β†’ HTTP cookie set
2. React loads chats via REST (cookie auto-sent)
3. User sends message β†’ socket.emit("ai-message")
4. Backend:
   a) Detect URL
   b) Scrape (Cheerio/Puppeteer)
   c) Save message
   d) Generate vector
   e) Store vector in Pinecone
   f) Query STM + LTM
   g) Merge into hidden prompt
   h) Generate response using Gemini
   i) Save response + vector
5. Backend β†’ emits "ai-response"
6. Frontend updates in real-time

πŸ§ͺ Chat Features

  • Create chat
  • Load all chats
  • Load messages
  • Delete chat
  • Auto-select next chat
  • Prevent sending messages if no chat exists

🎨 UI/UX

  • Fully responsive
  • ChatGPT-like sidebar
  • New chat popup
  • Floating AI shimmer loading bubble
  • Dark theme
  • Fixed input bar

πŸš€ Deployment Guide

Backend β†’ Render

  • Environment variables:
MONGO_URI=
JWT_SECRET=
GEMINI_API_KEY=
PINECONE_API_KEY=
  • Build command: npm install
  • Start command: node server.js

Frontend β†’ Vercel

Update Axios base URL:

https://your-backend.onrender.com


⭐ Support

If you like this project, ⭐ the repository.

About

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages