A modern Retrieval-Augmented Generation (RAG) chat application built with Next.js and Couchbase, enabling intelligent conversations powered by vector search and OpenAI's language models.
This RAG Chat Application combines the power of large language models with your own data through vector search capabilities. Users can upload documents (PDFs, text files, web URLs) and have intelligent conversations about the content, making it perfect for document analysis, knowledge management, and AI-powered research assistance.
- π€ Intelligent Chat Interface: Natural language conversations with AI
- π Document Upload: Support for PDF files, text documents, and web URLs
- π Vector Search: Semantic search through uploaded documents using embeddings
- πΎ Persistent Storage: Documents and embeddings stored in Couchbase
- β‘ Real-time Responses: Streaming chat responses for better user experience
- π¨ Modern UI: Clean, responsive interface built with Tailwind CSS
- π± Mobile Friendly: Fully responsive design for all devices
- Next.js 14 - React framework with App Router
- React 18 - UI library with hooks and modern features
- TypeScript - Type-safe JavaScript
- Tailwind CSS - Utility-first CSS framework
- Lucide React - Beautiful icons
- React Markdown - Markdown rendering
- React Dropzone - File upload component
- Node.js - JavaScript runtime
- Express.js - Web application framework
- Couchbase - NoSQL database with vector search
- OpenAI API - GPT models and embeddings
- Multer - File upload middleware
- PDF Parse - PDF text extraction
- Cheerio - Web scraping for URL content
- Nodemon - Development server auto-restart
- ESLint - Code linting
- PostCSS - CSS processing
- Autoprefixer - CSS vendor prefixes
βββββββββββββββββββ βββββββββββββββββββ βββββββββββββββββββ
β Frontend β β Backend β β Database β
β (Next.js) βββββΊβ (Express) βββββΊβ (Couchbase) β
β β β β β β
β β’ Chat UI β β β’ REST API β β β’ Documents β
β β’ File Upload β β β’ File Processingβ β β’ Embeddings β
β β’ Responsive β β β’ Vector Search β β β’ Vector Index β
βββββββββββββββββββ βββββββββββββββββββ βββββββββββββββββββ
β
βΌ
βββββββββββββββββββ
β OpenAI API β
β β
β β’ GPT Models β
β β’ Embeddings β
βββββββββββββββββββ
The following diagram illustrates the complete journey of how English text gets converted to embeddings, processed through the RAG system, and returned as contextually aware responses:
graph TD
A["π€ User Input<br/>(English Text)"] --> B["π Text Preprocessing<br/>β’ Tokenization<br/>β’ Cleaning<br/>β’ Chunking"]
B --> C["π€ OpenAI Embedding API<br/>text-embedding-ada-002<br/>1536 dimensions"]
C --> D["π Query Vector<br/>(Numerical Embeddings)"]
D --> E["π Couchbase Vector Search<br/>β’ Cosine Similarity<br/>β’ K-Nearest Neighbors<br/>β’ Semantic Matching"]
E --> F["π Document Store<br/>Couchbase Database<br/>β’ Documents<br/>β’ Pre-computed Embeddings<br/>β’ Metadata"]
F --> G["π Retrieved Documents<br/>β’ Relevant Chunks<br/>β’ Similarity Scores<br/>β’ Context Window"]
G --> H["π§ Context Assembly<br/>β’ Combine Query + Documents<br/>β’ Format for LLM<br/>β’ Add Instructions"]
H --> I["π OpenAI GPT API<br/>GPT-4 / GPT-3.5-turbo<br/>β’ Context Understanding<br/>β’ Response Generation"]
I --> J["π Generated Response<br/>(English Text)<br/>β’ Contextually Aware<br/>β’ Source-grounded"]
J --> K["π€ User Receives Answer<br/>(English Text)"]
%% Document Ingestion Flow
L["π Document Upload<br/>PDF, Text, URL"] --> M["π§ Content Extraction<br/>β’ PDF Parse<br/>β’ Web Scraping<br/>β’ Text Processing"]
M --> N["βοΈ Text Chunking<br/>β’ Split into segments<br/>β’ Overlap handling<br/>β’ Size optimization"]
N --> O["π€ Generate Embeddings<br/>OpenAI API<br/>text-embedding-ada-002"]
O --> P["πΎ Store in Couchbase<br/>β’ Document chunks<br/>β’ Vector embeddings<br/>β’ Metadata indexing"]
P --> F
%% Styling
classDef userInput fill:#e1f5fe,stroke:#01579b,stroke-width:2px
classDef processing fill:#f3e5f5,stroke:#4a148c,stroke-width:2px
classDef embedding fill:#fff3e0,stroke:#e65100,stroke-width:2px
classDef database fill:#e8f5e8,stroke:#1b5e20,stroke-width:2px
classDef llm fill:#fce4ec,stroke:#880e4f,stroke-width:2px
classDef output fill:#e0f2f1,stroke:#004d40,stroke-width:2px
class A,K userInput
class B,H,M,N processing
class C,D,O embedding
class E,F,P database
class I llm
class G,J output
- User Input β English text question or query
- Text Preprocessing β Clean and prepare text for embedding
- Embedding Generation β Convert to 1536-dimensional vector using OpenAI
- Vector Search β Find semantically similar documents in Couchbase
- Context Retrieval β Get relevant document chunks with similarity scores
- Context Assembly β Combine query with retrieved context
- LLM Processing β Generate contextually aware response using GPT
- Response Delivery β Return English text answer to user
- Document Upload β PDF, text files, or web URLs
- Content Extraction β Parse and extract text content
- Text Chunking β Split into manageable segments with overlap
- Embedding Generation β Create vector representations
- Database Storage β Store documents and embeddings in Couchbase
- Embedding Model:
text-embedding-ada-002(1536 dimensions) - Vector Search: Cosine similarity with configurable K-nearest neighbors
- LLM Models: GPT-4 or GPT-3.5-turbo for response generation
- Database: Couchbase with vector indexing capabilities
- Chunking Strategy: Overlapping text segments for context preservation
- Upload research papers, reports, or documentation
- Ask questions about specific content and get contextual answers
- Extract insights and summaries from large documents
- Upload company documents, policies, or procedures
- Enable employees to quickly find information through natural language queries
- Create an intelligent knowledge base for customer support
- Upload textbooks, lecture notes, or study materials
- Get explanations, summaries, and answers to study questions
- Create personalized learning experiences
- Organize and search through large document collections
- Enable semantic search across multiple file types
- Build intelligent content recommendation systems
- Node.js (v18 or higher)
- Couchbase Server (v7.0 or higher)
- OpenAI API Key (with credits)
git clone <repository-url>
cd RAG-app# Install frontend dependencies
npm install
# Install backend dependencies
cd server
npm install
cd ..- Install Couchbase Server: Download from couchbase.com
- Create Cluster: Set up a new cluster with default settings
- Load Sample Data: Import the
travel-samplebucket - Create Vector Index: Follow the Couchbase Vector Search Guide
# Copy environment template
cp server/env.example server/.env
# Edit the .env file with your settingsRequired Environment Variables:
# Couchbase Configuration
COUCHBASE_CONNECTION_STRING=couchbase://localhost
COUCHBASE_USERNAME=Administrator
COUCHBASE_PASSWORD=your_password
COUCHBASE_BUCKET_NAME=travel-sample
# OpenAI Configuration
OPENAI_API_KEY=your_openai_api_key
# Server Configuration
PORT=5000
NODE_ENV=development# Run the setup test script
node test-setup.jsThis will verify:
- β Environment variables are configured
- β Couchbase connection is working
- β OpenAI API is accessible
- β Sample data is available
# Terminal 1: Start the backend server
cd server
npm run dev
# Terminal 2: Start the frontend (in a new terminal)
npm run devOpen your browser and navigate to:
- Frontend: http://localhost:3000
- Backend API: http://localhost:5000
The project includes a comprehensive test script to verify your setup:
node test-setup.jsWhat it tests:
- Environment variable configuration
- Couchbase database connection
- OpenAI API connectivity
- Sample data availability
- Vector search index status
- Click the upload button in the chat interface
- Upload a PDF file or enter a web URL
- Verify the document is processed successfully
- Type a question about travel destinations (using sample data)
- Example: "Tell me about hotels in Paris"
- Verify you get relevant, contextual responses
- Upload a custom document
- Ask specific questions about the uploaded content
- Verify the AI provides accurate answers based on your document
# Test chat endpoint
curl -X POST http://localhost:5000/api/chat \
-H "Content-Type: application/json" \
-d '{"message": "Hello, tell me about travel destinations"}'
# Test upload endpoint
curl -X POST http://localhost:5000/api/upload \
-F "file=@your-document.pdf"# Check if Couchbase is running
lsof -i :8091
lsof -i :11210
# Verify web UI access
curl http://localhost:8091- Verify your API key at OpenAI Platform
- Check your usage and billing at OpenAI Billing
- Frontend runs on port 3000
- Backend runs on port 5000
- Couchbase uses ports 8091, 11210
RAG-app/
βββ app/ # Next.js App Router
β βββ components/ # React components
β β βββ ChatMessage.tsx # Chat message component
β β βββ FileUpload.tsx # File upload component
β βββ globals.css # Global styles
β βββ layout.tsx # Root layout
β βββ page.tsx # Main chat page
β βββ types.ts # TypeScript definitions
βββ server/ # Backend Express server
β βββ routes/ # API routes
β β βββ chat.js # Chat endpoint
β β βββ upload.js # File upload endpoint
β βββ couchbase.js # Database connection & operations
β βββ server.js # Express server setup
β βββ .env # Environment variables
β βββ package.json # Backend dependencies
βββ test-setup.js # Setup verification script
βββ next.config.js # Next.js configuration
βββ tailwind.config.js # Tailwind CSS configuration
βββ package.json # Frontend dependencies
- Connection String: Modify for remote Couchbase clusters
- Bucket Name: Use different buckets for different environments
- Vector Index: Configure custom vector search indexes
- Model Selection: Switch between GPT-3.5-turbo and GPT-4
- Embedding Model: Use different embedding models for vector search
- Temperature: Adjust response creativity (0.0 - 1.0)
- File Upload Limits: Configure maximum file sizes
- Supported File Types: Add support for additional document types
- UI Themes: Customize the interface appearance
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
If you encounter any issues or have questions:
- Check the troubleshooting section in this README
- Run the test script:
node test-setup.js - Review the logs in both frontend and backend consoles
- Open an issue on GitHub with detailed error information
- Couchbase for the powerful NoSQL database and vector search capabilities
- OpenAI for the GPT models and embedding APIs
- Next.js team for the excellent React framework
- Vercel for the deployment platform
Happy coding! π