Skip to content

Priyanshuthapliyal2005/chatpdf

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

16 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

An intelligent document analysis assistant powered by Gemini AI

Features Β· Architecture Β· Tech Stack Β· Installation Β· Usage Β· Deployment Β· Roadmap

Next.js Vercel AI SDK Gemini API TypeScript License


πŸš€ Features

  • Document Intelligence

    • Extract key information from PDFs automatically
    • Smart summarization of document contents
    • Context-aware question answering based on document content
    • Support for multiple document formats
  • Advanced UI/UX

    • 50-50 split screen display for simultaneous document and chat viewing
    • Real-time document analysis and chat interaction
    • Responsive design for desktop and mobile devices
    • Dark/light mode support
  • Modern Tech Stack

    • Next.js 14 App Router

      • Advanced routing for seamless navigation
      • React Server Components (RSCs) for optimized rendering
      • Server Actions for secure, direct database mutations
    • Vercel AI SDK

      • Streaming responses for real-time interaction
      • Unified API for generating text and structured outputs
      • Hooks for building dynamic chat interfaces
    • shadcn/ui

      • Beautifully designed components built with Radix UI and Tailwind
      • Fully accessible and customizable interface
      • Consistent design language throughout the application
  • Security & Performance

    • Role-based access control for document sharing
    • Document encryption for sensitive files
    • Optimized query handling for large documents
    • Rate limiting to prevent API abuse
  • Data Persistence

  • Authentication

    • NextAuth.js for secure user management
    • Multiple authentication providers (Google, GitHub, Email)
    • JWT token-based session management

πŸ—οΈ Architecture

Database Schema

erDiagram
    User ||--o{ Chat : "has many"
    
    User {
        uuid id PK
        varchar(64) email
        varchar(64) password
    }

    Chat {
        uuid id PK
        timestamp createdAt
        json messages
        uuid userId FK
    }
Loading

Basic Architecture

graph TD
    User[User] -->|Uploads Document / Asks Question| Frontend[Next.js Frontend]
    
    Frontend -->|Routes Requests| Backend[Next.js Backend/Server]
    
    Backend -->|Document Processing| AI[AI SDK with Gemini]
    Backend -->|User Authentication| Auth[NextAuth.js]
    Backend -->|Data Operations| Storage[Data Storage]
    
    Storage -->|User & Chat Data| Database[Vercel Postgres]
    Storage -->|Document Storage| BlobStore[Vercel Blob]
    
    AI -->|Document Analysis Results| Backend
    Auth -->|Authentication Status| Backend
    Database -->|Retrieved Data| Backend
    BlobStore -->|Retrieved Documents| Backend
    
    Backend -->|Response Data| Frontend
    Frontend -->|Display Results| User
Loading

High-Level Design (HLD)

graph TD
    %% Client Layer
    subgraph "Client Layer"
        CL1[Web Browser]
        CL2[Mobile Device]
    end
    
    %% Presentation Layer
    subgraph "Presentation Layer"
        PL1[Next.js App Router]
        PL2[React Components]
        PL3[Document Viewer]
        PL4[Chat Interface]
        PL5[shadcn/ui Components]
    end
    
    %% Application Layer
    subgraph "Application Layer"
        AL1[Next.js Server Actions]
        AL2[API Routes]
        AL3[Document Processor]
        AL4[Chat Controller]
    end
    
    %% AI/ML Layer
    subgraph "AI/ML Layer"
        ML1[AI SDK]
        ML2[Gemini Integration]
        ML3[Document Analyzer]
        ML4[Context Builder]
    end
    
    %% Data Access Layer
    subgraph "Data Access Layer"
        DL1[Authentication Service]
        DL2[Database Access]
        DL3[Blob Storage Access]
    end
    
    %% Storage Layer
    subgraph "Storage Layer"
        SL1[NextAuth.js]
        SL2[Vercel Postgres]
        SL3[Vercel Blob]
    end
    
    %% Connections
    CL1 <--> PL1
    CL2 <--> PL1
    
    PL1 --> PL2
    PL2 --> PL3
    PL2 --> PL4
    PL2 --> PL5
    
    PL3 --> AL1
    PL4 --> AL1
    
    AL1 --> AL2
    AL1 --> AL3
    AL1 --> AL4
    
    AL3 --> ML1
    AL4 --> ML1
    
    ML1 --> ML2
    ML1 --> ML3
    ML1 --> ML4
    
    AL1 --> DL1
    AL3 --> DL3
    AL4 --> DL2
    
    DL1 --> SL1
    DL2 --> SL2
    DL3 --> SL3
Loading

Low-Level Design (LLD)

sequenceDiagram
    actor User
    participant Frontend as Next.js Frontend
    participant Backend as Next.js Backend
    participant Auth as NextAuth.js
    participant DB as Vercel Postgres
    participant Blob as Vercel Blob
    participant AI as AI SDK (Gemini)

    %% Document Upload Flow
    User ->> Frontend: Uploads Document
    Frontend ->> Backend: Document Upload Request
    Backend ->> Auth: Verify User Session
    Auth -->> Backend: Session Valid
    Backend ->> Blob: Store Document
    Blob -->> Backend: Storage Confirmation
    Backend ->> DB: Record Document Metadata
    DB -->> Backend: Confirmation
    Backend -->> Frontend: Upload Success
    Frontend -->> User: Display Document

    %% Question Answering Flow
    User ->> Frontend: Asks Question
    Frontend ->> Backend: Process Question
    Backend ->> Auth: Verify User Session
    Auth -->> Backend: Session Valid
    Backend ->> DB: Retrieve Chat History
    DB -->> Backend: Chat History
    Backend ->> Blob: Retrieve Document
    Blob -->> Backend: Document Content
    Backend ->> AI: Process Query with Document Context
    AI -->> Backend: Generated Response
    Backend ->> DB: Save Interaction
    DB -->> Backend: Confirmation
    Backend -->> Frontend: Response Data
    Frontend -->> User: Display Answer
Loading

πŸš€ API Overview

This project provides a database utility module using Drizzle ORM and PostgreSQL to handle users and chats. Below is an overview of the available functions:

πŸ§‘β€πŸ’» User Management

Function Description
getUser(email: string) Fetches a user by email.
createUser(email: string, password: string) Creates a new user with a hashed password.

πŸ’¬ Chat Management

Function Description
saveChat({ id, messages, userId }) Saves or updates a chat session.
deleteChatById({ id }) Deletes a chat by its ID.
getChatsByUserId({ id }) Retrieves all chats for a given user, ordered by creation date.
getChatById({ id }) Retrieves a single chat by ID.

πŸ’» Tech Stack

  • Frontend

    • Next.js 14 (App Router)
    • React 18 with Server Components
    • Tailwind CSS for styling
    • shadcn/ui component library
    • TypeScript for type safety
  • Backend

    • Next.js API Routes and Server Actions
    • Vercel AI SDK for LLM integration
    • Gemini AI for document analysis
    • NextAuth.js for authentication
  • Database

    • Vercel Postgres (powered by Neon)
    • Drizzle ORM for type-safe queries
    • Vercel Blob for document storage
  • DevOps

    • Vercel for hosting and serverless functions
    • GitHub Actions for CI/CD
    • pnpm for package management

πŸ“¦ Installation

  1. Clone the repository:
git clone https://github.com/yourusername/chatpdf.git
cd chatpdf
  1. Install dependencies:
pnpm install
  1. Set up environment variables:
cp .env.example .env
  1. Configure your environment variables in .env file:
# Database
POSTGRES_URL=your_postgres_connection_string

# Authentication
NEXTAUTH_URL=http://localhost:3000
NEXTAUTH_SECRET=your_nextauth_secret
GOOGLE_CLIENT_ID=your_google_client_id
GOOGLE_CLIENT_SECRET=your_google_client_secret

# Storage
BLOB_READ_WRITE_TOKEN=your_vercel_blob_token

# AI
AI_API_KEY=your_gemini_api_key

πŸ”§ Usage

  1. Start the development server:
pnpm dev
  1. Access the application at http://localhost:3000

  2. Upload a document and start asking questions!

πŸš€ Deployment

Deploy to Vercel

The easiest way to deploy this application is using the Vercel Platform:

pnpm i -g vercel
vercel

Or connect your GitHub repository to Vercel for automatic deployments.

Manual Production Build

For a production build:

pnpm build
pnpm start

πŸ“ˆ Performance Optimizations

  • Document Chunking: Large documents are automatically chunked into manageable segments for efficient processing
  • Response Streaming: AI responses are streamed in real-time for better user experience
  • Semantic Caching: Similar questions use cached results to reduce API costs and improve response time
  • Progressive Loading: UI components load progressively for faster perceived performance

πŸ›£οΈ Roadmap

  • Multi-document analysis (comparing information across documents)
  • Custom knowledge base integration
  • Document annotation and collaborative editing
  • Mobile application with offline support
  • Webhooks for integration with third-party services
  • Advanced document analytics dashboard

πŸ“„ License

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

πŸ™ Acknowledgements

  • Vercel for their amazing platform and AI SDK
  • Google for the Gemini AI model
  • shadcn for the beautiful UI components
  • NextAuth.js team for the authentication system
  • All contributors who have helped shape this project

Built with ❀️ using Next.js, Gemini, and Vercel

About

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published