A production-ready Retrieval-Augmented Generation (RAG) chatbot built with Next.js, PostgreSQL (pgvector), and OpenAI.
Upload documents and ask questions - the AI answers based on your custom knowledge base, not generic training data.
RAG combines document retrieval with AI generation to create chatbots that answer questions based on your specific documents. Instead of relying on the AI's training data:
- Store your documents in a vector database
- Retrieve relevant chunks when you ask questions
- Generate accurate answers grounded in your actual data
- π Custom Knowledge Base - Upload PDFs and text files
- π Vector Similarity Search - Fast semantic search with pgvector
- π€ AI-Powered Responses - GPT-4o generates contextual answers
- π Document Management - Track and manage uploaded documents
- π¨ Modern UI - Clean, responsive interface with Tailwind CSS
- β‘ Fast Responses - 3-5 second query time with concise answers
- π Secure - Built with security best practices from day one
- Frontend: Next.js 16 (App Router), React 19, Tailwind CSS
- Backend: Node.js serverless functions
- Database: PostgreSQL with pgvector extension (HNSW indexing)
- AI/ML: OpenAI Embeddings API (text-embedding-3-small) + GPT-4o
- Text Processing: pdf-parse for document extraction
- Extract text from uploaded PDFs/TXT files
- Split text into semantic chunks (500 tokens with 50-token overlap)
- Generate 1536-dimension embeddings via OpenAI
- Store chunks + embeddings in PostgreSQL with pgvector
- User asks a question
- Generate embedding for the question
- Vector similarity search (cosine distance) finds top 3 relevant chunks
- Build prompt with retrieved context + user question
- GPT-4o generates concise answer (2-3 sentences)
- Return response with source citations
- Node.js 18+
- PostgreSQL database with pgvector extension enabled
- OpenAI API key
# Clone the repository
git clone https://github.com/cameronobriendev/rag-chatbot.git
cd rag-chatbot
# Install dependencies
npm install
# Set up environment variables
cp .env.local.example .env.local
# Edit .env.local with your credentialsRequired variables in .env.local:
# PostgreSQL with pgvector
DATABASE_URL=postgresql://user:password@host/database?sslmode=require
# OpenAI API
OPENAI_API_KEY=sk-proj-...
# Optional: Session secret for CSRF protection
SESSION_SECRET=your-secret-here-min-32-charsEnable pgvector extension in your PostgreSQL database:
CREATE EXTENSION IF NOT EXISTS vector;Run the schema migration to create required tables:
documents- Document metadatachunks- Text chunks with embeddingsquery_history- Query logs and performance metrics
npm run devOpen http://localhost:3000 to see the app.
rag-chatbot/
βββ app/
β βββ api/
β β βββ chat/ # RAG query endpoint
β β βββ documents/ # Document management
β β βββ csrf-token/ # CSRF protection
β βββ layout.js # Root layout
β βββ page.js # Chat interface
β βββ globals.css # Global styles
βββ components/
β βββ ChatInterface.js # Main chat UI component
βββ lib/
β βββ db.js # Database connection
β βββ embeddings.js # OpenAI embedding utilities
βββ package.json
Uses PostgreSQL's pgvector extension with HNSW indexing for fast approximate nearest neighbor search:
SELECT content, 1 - (embedding <=> query_embedding) as similarity
FROM chunks
ORDER BY embedding <=> query_embedding
LIMIT 3Smart text splitting that:
- Targets 500 tokens per chunk (2000 chars)
- 50-token overlap between chunks (prevents context loss)
- Breaks at natural boundaries (sentences, paragraphs)
- Handles null bytes and special characters
- 3 chunks retrieved (not 5) - 40% less context overhead
- GPT-4o with 150 max tokens - fast, concise responses
- Temperature 0.3 - focused answers, less rambling
- Session isolation - users only see their own documents
For portfolio/demo use (~100 queries/month):
- OpenAI Embeddings: ~$2/month
- OpenAI GPT-4o: ~$3-5/month
- Database: Free tier available (Neon, Supabase)
- Hosting: Free tier (Vercel, Netlify)
Total: $5-10/month for personal projects
For production (~1000 queries/month): ~$30-50/month
# Install Vercel CLI
npm i -g vercel
# Deploy
vercel --prodAdd environment variables in Vercel dashboard:
DATABASE_URLOPENAI_API_KEYSESSION_SECRET
- Neon - Serverless PostgreSQL with pgvector (free tier available)
- Supabase - PostgreSQL with pgvector support
- DigitalOcean - Managed PostgreSQL + self-hosted pgvector
- CSRF protection on all state-changing endpoints
- Input validation and sanitization
- Parameterized SQL queries (no SQL injection)
- Session isolation (users can't access others' documents)
- Environment variable protection (.gitignore blocks all .env files)
- Generic error messages (no information leakage)
- Customer Support - Answer questions from product documentation
- Research - Query academic papers and research notes
- Legal/Compliance - Search contracts and policy documents
- Education - Study aids based on textbooks and lectures
- Personal Knowledge Management - Your own AI assistant
This RAG chatbot demonstrates:
- AI/ML Integration - OpenAI embeddings + GPT-4o
- Vector Databases - PostgreSQL pgvector with HNSW indexing
- Modern Web Stack - Next.js 16, React 19, Tailwind CSS
- API Design - RESTful endpoints for upload, query, document management
- Security Best Practices - CSRF protection, input validation, session isolation
- Performance Optimization - Fast queries, concise responses
Ideal for freelance work: Custom RAG implementations typically bill at $100-150/hr.
MIT License - feel free to use for your own projects!
Cameron O'Brien GitHub
Built with Claude Code