Skip to content

Some1Uknow/chatgpt-cloned

Repository files navigation

ChatGPT Clone - Full-Stack AI Chat Application | Next.js 14, AISDK, mem0, OpenAI

A production-ready MVP ChatGPT clone with advanced features like multimodal AI, persistent memory, file attachments, and real-time streaming responses.

Next.js TypeScript OpenAI MongoDB Vercel

πŸ“– Table of Contents

🎯 Overview

ChatGPT Clone is a sophisticated, full-stack AI chat application that replicates and extends OpenAI's ChatGPT interface. Built with Next.js 14, TypeScript, and OpenAI GPT-4, this project demonstrates modern web development practices and enterprise-grade architecture patterns.

Perfect for developers looking to understand:

  • Real-time AI streaming with Vercel AI SDK
  • Full-stack TypeScript development
  • Multimodal AI integration (text, images, PDFs)
  • Persistent chat memory with MongoDB
  • Production-ready authentication with Clerk
  • Enterprise architecture patterns

πŸŽͺ Live Demo

πŸ”— Try the Live Application | πŸ“š Documentation

✨ Key Features

πŸ€– Advanced AI Capabilities

  • Real-Time Streaming Responses - Token-by-token AI responses using Vercel AI SDK
  • Multimodal AI Support - Text, image, PDF, and document processing
  • Long-Term Memory - Persistent conversation context with mem0.ai
  • Message Editing & Regeneration - Edit any message and regenerate responses
  • Context-Aware Conversations - Maintains conversation history across sessions

πŸ’Ό Enterprise Features

  • Production-Ready Authentication - Secure user management with Clerk
  • Scalable Database Architecture - MongoDB with optimized queries and indexing
  • File Upload & Processing - PDF, DOCX, images with text extraction
  • Responsive Design - Mobile-first UI with Tailwind CSS
  • TypeScript Throughout - Full type safety across frontend and backend

πŸ”§ Developer Experience

  • Modern Stack - Next.js 14 App Router, React Server Components
  • API Routes - RESTful endpoints with streaming support
  • Custom Hooks - Reusable logic for chat, attachments, and state management
  • Component Library - Modular, accessible components with Radix UI
  • Error Handling - Comprehensive error boundaries and user feedback

πŸ› οΈ Tech Stack

Frontend & Framework

Backend & AI

Database & Storage

Authentication & Security

  • Clerk - Authentication and user management
  • Svix - Webhook handling

File Processing

πŸš€ Quick Start

Prerequisites

  • Node.js 18+
  • MongoDB (local or cloud)
  • API Keys: OpenAI, Clerk, Uploadcare, mem0.ai

1-Minute Setup

# Clone the repository
git clone https://github.com/yourusername/chatgpt-clone.git
cd chatgpt-clone

# Install dependencies
npm install

# Copy environment variables
cp .env.example .env.local

# Configure your API keys in .env.local
# Start development server
npm run dev

Environment Variables

Create .env.local with your API keys:

# Authentication
NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY=pk_test_...
CLERK_SECRET_KEY=sk_test_...

# Database
MONGODB_URI=mongodb://localhost:27017/chatgpt-clone

# AI Services
OPENAI_API_KEY=sk-...
MEM0_API_KEY=m0-...

# File Upload
NEXT_PUBLIC_UPLOADCARE_PUBLIC_KEY=demopublickey
UPLOADCARE_SECRET_KEY=demosecretkey

# Application
NEXT_PUBLIC_APP_URL=http://localhost:3000

πŸ”— Quick Links:

πŸ“ Project Structure

src/
β”œβ”€β”€ app/                          # Next.js 14 App Router
β”‚   β”œβ”€β”€ api/                      # API routes (serverless functions)
β”‚   β”‚   β”œβ”€β”€ chat/                 # Main chat endpoint with streaming
β”‚   β”‚   β”œβ”€β”€ upload/               # File upload processing
β”‚   β”‚   β”œβ”€β”€ memory/               # mem0.ai integration
β”‚   β”‚   └── webhooks/             # External service webhooks
β”‚   β”œβ”€β”€ chat/                     # Chat interface pages
β”‚   └── layout.tsx                # Root layout with providers
β”œβ”€β”€ components/                   # React components
β”‚   β”œβ”€β”€ ui/                       # Reusable UI components
β”‚   β”œβ”€β”€ messages/                 # Message-specific components
β”‚   └── chat-*.tsx                # Chat interface components
β”œβ”€β”€ hooks/                        # Custom React hooks
β”‚   β”œβ”€β”€ use-chat-with-attachments.ts  # Main chat logic
β”‚   └── use-chat-data.ts          # Data fetching
β”œβ”€β”€ lib/                          # Utility libraries
β”‚   β”œβ”€β”€ mongodb.ts                # Database connection
β”‚   └── utils.ts                  # General utilities
β”œβ”€β”€ models/                       # Database models
β”‚   └── Chat.ts                   # Chat schema
└── types/                        # TypeScript definitions

πŸ”Œ API Documentation

Core Endpoints

POST /api/chat

Real-time streaming chat endpoint

// Request
{
  messages: CoreMessage[];
  chatId?: string;
  experimental_attachments?: FileAttachment[];
}

// Response: Streaming with X-Chat-Id header

POST /api/upload

File upload with text extraction

// Request: FormData with file
// Response
{
  url: string;
  name: string;
  type: string;
  textContent?: string;
}

GET/POST/DELETE /api/memory

Long-term memory management

🎨 Core Components

useChatWithAttachments Hook

Main chat logic with file support

const {
  messages,
  input,
  attachments,
  isLoading,
  handleSubmit,
  handleFileUpload,
  handleEditMessage,
} = useChatWithAttachments({ chatId });

ChatLayout Component

Complete chat interface orchestration

<ChatLayout
  messages={messages}
  input={input}
  isLoading={isLoading}
  onSubmit={handleSubmit}
  attachments={attachments}
  onFileUpload={handleFileUpload}
/>

🀝 Contributing

We welcome contributions! This project is perfect for learning:

  • Modern React patterns with Next.js 14
  • AI integration with streaming responses
  • Full-stack TypeScript development
  • Database design and optimization
  • Authentication and security

Getting Started

  1. Fork the repository
  2. Create a feature branch: git checkout -b feature/amazing-feature
  3. Make your changes with proper TypeScript types
  4. Add tests for new functionality
  5. Submit a pull request

🎯 Good First Issues:

  • Add new file type support (CSV, JSON)
  • Improve error handling and user feedback
  • Add keyboard shortcuts for better UX
  • Implement message search functionality
  • Add export chat functionality

πŸ“„ License

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

⭐ Show Your Support

If this project helped you learn or build something awesome, please give it a ⭐! It helps others discover this educational resource.

πŸ”— Connect & Share:

  • Star this repo if you found it useful
  • Follow me for more AI and full-stack projects
  • Share with developers who want to learn AI integration
  • Contribute to make this project even better