Skip to content

HabitualCoder/document-assistant-ai

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

59 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

๐Ÿค– Intelligent Document Assistant

Intelligent Document Assistant Screenshot

A production-ready AI application built with Next.js 15, TypeScript, and Google Gemini that demonstrates advanced RAG (Retrieval-Augmented Generation) implementation with Supabase vector store and LangSmith observability for document processing and intelligent question answering.

๐ŸŽฏ Project Purpose

This project is created for educational and portfolio purposes to demonstrate:

  • Advanced AI engineering skills with LangChain.js and Google Gemini
  • Full-stack development with modern technologies (Next.js 15, TypeScript)
  • Production-ready application architecture and best practices
  • Complete RAG pipeline implementation with Supabase vector store
  • LangSmith observability and tracing for AI applications
  • Professional software development practices and documentation

โš ๏ธ Important Notice

This project is for educational and portfolio use only. Commercial use is prohibited without explicit permission.

๐Ÿš€ Key Features

Core Functionality

  • ๐Ÿ“„ Document Upload: Support for PDF, TXT, DOCX, and Markdown files with robust parsing
  • ๐Ÿค– AI-Powered Q&A: Ask questions about uploaded documents using Google Gemini 2.0 Flash
  • ๐Ÿ“š Source Citations: Get answers with references to specific document sections
  • ๐Ÿ—‚๏ธ Document Management: View, filter, and manage uploaded documents
  • โšก Real-time Processing: Live status updates during document processing
  • ๐Ÿ’ฌ Chat Interface: Modern chat UI with typing indicators and message history
  • ๐Ÿ”„ Error Recovery: Comprehensive error handling with retry logic and graceful degradation

Technical Excellence

  • ๐Ÿ”’ Type-Safe: Full TypeScript implementation with strict mode
  • ๐Ÿ“ฑ Responsive Design: Mobile-first approach with Tailwind CSS
  • ๐Ÿ›ก๏ธ Error Handling: Comprehensive error handling with retry logic
  • โšก Performance: Optimized with proper loading states and caching
  • ๐Ÿ” Security: Input validation and file type restrictions

AI & RAG Implementation

  • ๐Ÿง  Smart Chunking: LangChain RecursiveCharacterTextSplitter with sentence boundary detection
  • ๐Ÿ” Vector Search: Supabase pgvector with HNSW indexing for efficient similarity search
  • ๐ŸŽฏ Embedding Generation: Google Gemini embeddings (3072 dimensions) for semantic understanding
  • ๐Ÿ“Š Relevance Scoring: Confidence and relevance metrics for answers
  • ๐Ÿ”„ Complete RAG Pipeline: Query โ†’ Embedding โ†’ Vector Search โ†’ Context Retrieval โ†’ AI Generation โ†’ Citation
  • ๐Ÿ“ˆ Observability: LangSmith tracing for AI pipeline monitoring and debugging

๐Ÿ› ๏ธ Tech Stack

Frontend

  • Next.js 15 with App Router
  • TypeScript (strict mode)
  • Tailwind CSS for styling
  • React 19 with modern hooks

Backend

  • Next.js API Routes for backend logic
  • LangChain.js for AI functionality and RAG pipeline
  • Google Gemini models (gemini-2.0-flash-exp, gemini-embedding-001)
  • Supabase with pgvector extension for vector storage
  • Prisma ORM with Supabase PostgreSQL for metadata
  • Zod for validation

AI & ML

  • RAG Architecture with LangChain document chunking
  • Supabase Vector Store with pgvector for efficient similarity search
  • Google Gemini Embeddings (3072 dimensions) for semantic understanding
  • LangChain Expression Language (LCEL) for chain composition
  • Context-Aware Generation with source citations
  • LangSmith Integration for observability and tracing

Development & Testing

  • Jest for unit testing
  • Cypress for E2E testing
  • ESLint for code quality
  • TypeScript for type safety

๐Ÿ“ Project Architecture

document-assistant-ai/
โ”œโ”€โ”€ app/
โ”‚   โ”œโ”€โ”€ api/                    # API routes
โ”‚   โ”‚   โ”œโ”€โ”€ health/             # Health check endpoint
โ”‚   โ”‚   โ”œโ”€โ”€ upload/             # File upload endpoint
โ”‚   โ”‚   โ”œโ”€โ”€ process/            # Document processing endpoint
โ”‚   โ”‚   โ”œโ”€โ”€ query/              # Query processing endpoint
โ”‚   โ”‚   โ””โ”€โ”€ documents/          # Document management endpoint
โ”‚   โ”œโ”€โ”€ client/                 # Client-side components
โ”‚   โ””โ”€โ”€ layout.tsx              # Root layout
โ”œโ”€โ”€ components/                 # Reusable UI components
โ”‚   โ”œโ”€โ”€ FileUpload.tsx          # Drag-and-drop file upload
โ”‚   โ”œโ”€โ”€ DocumentList.tsx        # Document management interface
โ”‚   โ”œโ”€โ”€ QueryInterface.tsx     # Question input interface
โ”‚   โ”œโ”€โ”€ QueryResults.tsx        # AI response display
โ”‚   โ””โ”€โ”€ ...                     # Other UI components
โ”œโ”€โ”€ lib/                        # Core business logic
โ”‚   โ”œโ”€โ”€ types.ts                # TypeScript type definitions
โ”‚   โ”œโ”€โ”€ utils.ts                # Utility functions
โ”‚   โ”œโ”€โ”€ ai-services.ts          # AI service integration with LangSmith
โ”‚   โ”œโ”€โ”€ database.ts             # Database service layer
โ”‚   โ”œโ”€โ”€ supabase.ts             # Supabase client configuration
โ”‚   โ”œโ”€โ”€ vector-store.ts         # Supabase vector store integration
โ”‚   โ”œโ”€โ”€ document-loaders.ts     # LangChain document processing
โ”‚   โ”œโ”€โ”€ langchain-chains.ts     # RAG chains with LCEL
โ”‚   โ”œโ”€โ”€ langsmith.ts            # LangSmith observability setup
โ”‚   โ”œโ”€โ”€ config.ts               # Application configuration
โ”‚   โ””โ”€โ”€ error-handling.ts       # Error handling utilities
โ”œโ”€โ”€ prisma/                     # Database schema and migrations
โ”‚   โ””โ”€โ”€ schema.prisma           # Database models
โ”œโ”€โ”€ __tests__/                  # Unit tests
โ”œโ”€โ”€ cypress/                    # E2E tests
โ””โ”€โ”€ docs/                       # Documentation

๐Ÿš€ Getting Started

Prerequisites

  • Node.js 18+
  • npm or yarn
  • Google AI API key
  • Supabase account (for vector storage)
  • LangSmith account (optional, for observability)

Installation

  1. Clone the repository

    git clone <your-repo-url>
    cd document-assistant-ai
  2. Install dependencies

    npm install
  3. Set up environment variables

    cp env.example .env.local

    Edit .env.local and add your API keys:

    GOOGLE_API_KEY=your_google_api_key_here
    DATABASE_URL="postgres://postgres.[YOUR-PROJECT-REF]:[YOUR-PASSWORD]@aws-0-us-east-1.pooler.supabase.com:5432/postgres"
    SUPABASE_URL=https://[YOUR-PROJECT-REF].supabase.co
    SUPABASE_ANON_KEY=your_supabase_anon_key
    SUPABASE_SERVICE_ROLE_KEY=your_supabase_service_role_key
    LANGCHAIN_TRACING_V2=true
    LANGCHAIN_API_KEY=your_langsmith_api_key
    LANGCHAIN_PROJECT=document-assistant-ai
  4. Set up Supabase

    • Create a new Supabase project at supabase.com
    • Go to Settings โ†’ Database โ†’ Connect
    • Copy the Supavisor Session pooler connection string
    • Run the SQL script in supabase-setup.sql to create the vector store
  5. Set up LangSmith (Optional)

    • Create a LangSmith account at smith.langchain.com
    • Get your API key from the settings
    • Add the API key to .env.local
  6. Set up the database

    npx prisma generate
    npx prisma db push
  7. Get Google AI API Key

    • Go to Google AI Studio
    • Create a new API key
    • Copy the key to your .env.local file
  8. Run the development server

    npm run dev
  9. Open your browser Navigate to http://localhost:3000

๐Ÿ“– Usage

1. Upload Documents

  • Click on the "Upload Documents" tab
  • Drag and drop files or click "browse files"
  • Supported formats: PDF, TXT, DOCX, MD
  • Maximum file size: 10MB

2. Manage Documents

  • View uploaded documents in the "My Documents" tab
  • Filter by status (uploading, processing, processed, error)
  • Select specific documents for targeted queries

3. Ask Questions

  • Switch to the "Ask Questions" tab
  • Type your question in the text area
  • Use example questions or create your own
  • Get AI-powered answers with source citations
  • View chat history with typing indicators

๐Ÿ”ง API Endpoints

Health Check

GET /api/health

Returns the status of all services (AI, Database, File Storage).

File Upload

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

file: [File]

Uploads and processes a document file with real text extraction.

Document Processing

POST /api/process
Content-Type: application/json

{
  "documentId": "string",
  "forceReprocess": boolean
}

Processes a document and creates searchable chunks with embeddings.

Query Processing

POST /api/query
Content-Type: application/json

{
  "question": "string",
  "documentIds": ["string"],
  "maxResults": number,
  "includeSources": boolean
}

Processes a user query and returns AI-generated answers with sources.

Document Management

GET /api/documents

Retrieves all uploaded documents with their processing status.

๐Ÿงช Testing

Unit Tests

npm run test

Tests with Coverage

npm run test:coverage

E2E Tests

npm run test:e2e

All Tests

npm run test:all

๐Ÿš€ Deployment

Vercel (Recommended)

  1. Push your code to GitHub
  2. Connect your repository to Vercel
  3. Add environment variables in Vercel dashboard:
    • GOOGLE_API_KEY: Your Google AI API key
    • DATABASE_URL: Your database connection string
  4. Deploy!

Manual Deployment

npm run build
npm start

๐Ÿ”’ Environment Variables

Variable Description Required Example
GOOGLE_API_KEY Google AI API key Yes AIzaSy...
DATABASE_URL Supabase PostgreSQL connection string (pooler) Yes postgres://postgres.[PROJECT-REF]:[PASSWORD]@aws-0-us-east-1.pooler.supabase.com:5432/postgres
SUPABASE_URL Supabase project URL Yes https://xxx.supabase.co
SUPABASE_ANON_KEY Supabase anonymous key Yes eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...
SUPABASE_SERVICE_ROLE_KEY Supabase service role key Yes eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...
LANGCHAIN_TRACING_V2 Enable LangSmith tracing No true
LANGCHAIN_API_KEY LangSmith API key No ls__xxx
LANGCHAIN_PROJECT LangSmith project name No document-assistant-ai

๐Ÿ—๏ธ Architecture Deep Dive

RAG Pipeline Implementation

  1. Document Upload & Processing

    File Upload โ†’ Text Extraction (pdf2json/mammoth) โ†’ LangChain Chunking โ†’ Gemini Embeddings โ†’ Supabase Vector Store
    
  2. Query Processing

    User Query โ†’ Query Embedding โ†’ Supabase Vector Search โ†’ Context Retrieval โ†’ Gemini Generation โ†’ Response with Citations
    
  3. Vector Similarity Search

    • Supabase pgvector with HNSW indexing for efficient similarity search
    • Google Gemini embeddings (3072 dimensions) for semantic understanding
    • Relevance scoring and ranking with confidence metrics
    • Context window management for optimal response generation

Database Schema

  • Supabase PostgreSQL: Documents, DocumentChunks, Queries, QuerySources for metadata
  • Supabase Vector Store: Document embeddings and vector similarity search
  • Unified Architecture: All data in Supabase for optimal performance and scalability

Error Handling & Resilience

  • Exponential backoff retry logic
  • Comprehensive error boundaries with React ErrorBoundary
  • Graceful degradation with fallback UI
  • User-friendly error messages
  • Global error handlers to prevent page reloads
  • Robust PDF parsing with pdf2json fallback

๐Ÿ“Š Performance Features

Optimization Strategies

  • Lazy Loading: Components loaded on demand
  • Caching: API responses cached where appropriate
  • Chunking: Large documents processed in chunks with LangChain
  • Debouncing: Search inputs debounced to reduce API calls
  • Vector Search: Efficient similarity calculations with Supabase pgvector
  • Progress Indicators: Real-time feedback for long-running operations

Monitoring & Observability

  • Health Checks: Regular service health monitoring
  • Error Tracking: Comprehensive error logging
  • Performance Metrics: Response time tracking
  • Processing Status: Real-time status updates
  • LangSmith Tracing: AI pipeline observability and debugging
  • Vector Store Monitoring: Supabase performance metrics

๐Ÿ”ง Customization

Adding New File Types

  1. Update DocumentType in lib/types.ts
  2. Add extraction logic in app/api/upload/route.ts
  3. Update validation in components/FileUpload.tsx

Modifying AI Behavior

  1. Adjust parameters in lib/ai-services.ts
  2. Update prompts in lib/langchain-chains.ts
  3. Modify chunking strategy in lib/document-loaders.ts
  4. Configure LangSmith tracing in lib/langsmith.ts

Styling Changes

  1. Update Tailwind classes in components
  2. Modify color scheme in tailwind.config.js
  3. Add custom CSS for specific components

๐ŸŽฏ Key Technical Achievements

AI Engineering

  • โœ… Complete RAG pipeline implementation with LangChain
  • โœ… Supabase vector store with pgvector for efficient similarity search
  • โœ… Google Gemini embeddings (3072 dimensions) for semantic understanding
  • โœ… LangChain Expression Language (LCEL) for chain composition
  • โœ… Context-aware AI responses with source citations
  • โœ… LangSmith observability and tracing integration

Full-Stack Development

  • โœ… Type-safe API development with TypeScript
  • โœ… Modern React patterns with hooks and context
  • โœ… Unified Supabase architecture (PostgreSQL + Vector Store)
  • โœ… File processing with robust PDF parsing (pdf2json)
  • โœ… Error handling with React ErrorBoundary
  • โœ… Real-time UI updates and progress indicators

Production Readiness

  • โœ… Comprehensive testing suite (Jest + Cypress)
  • โœ… Error handling and retry logic
  • โœ… Performance optimization with vector indexing
  • โœ… Security considerations and input validation
  • โœ… Professional documentation and setup guides
  • โœ… LangSmith observability for production monitoring

๐Ÿ“ˆ Performance Metrics

  • Document Processing: 5-15 seconds depending on file size
  • Query Response: 2-5 seconds with Supabase vector search
  • Vector Search: Sub-second similarity calculations with pgvector
  • UI Responsiveness: <100ms for user interactions
  • LangSmith Tracing: Real-time observability with minimal overhead

๐Ÿ”ฎ Future Enhancements

  • Multi-language Support: Process documents in different languages
  • Advanced Chunking: Semantic chunking based on content structure
  • User Authentication: Multi-user support with document privacy
  • API Rate Limiting: Production-grade rate limiting
  • Caching Layer: Redis integration for improved performance
  • Advanced Analytics: Document usage analytics and insights
  • Custom Embeddings: Fine-tuned embeddings for specific domains

๐Ÿค Contributing

This project is currently not accepting contributions. See CONTRIBUTING.md for more information.

๐Ÿ“„ License

This project is licensed under the MIT License with commercial use restrictions. See LICENSE file for details.

๐Ÿ“ž Contact

For questions about this project or professional opportunities, please reach out through GitHub Issues or LinkedIn.


Built with โค๏ธ for AI Engineer Portfolio

โš ๏ธ Commercial Use Prohibited - This project is for educational and portfolio purposes only.