Skip to content

aashishGitHub/RAG-app

Repository files navigation

RAG Chat Application

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.

RAG Chat App Next.js Couchbase OpenAI

πŸš€ Overview

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.

✨ Key Features

  • πŸ€– 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

πŸ› οΈ Tech Stack

Frontend

Backend

Development Tools

πŸ—οΈ Architecture

System Overview

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   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    β”‚
                       β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

RAG Pipeline: Text-to-Embeddings-to-Response Flow

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
Loading

πŸ”„ Detailed Process Flow

1. Query Processing Pipeline

  1. User Input β†’ English text question or query
  2. Text Preprocessing β†’ Clean and prepare text for embedding
  3. Embedding Generation β†’ Convert to 1536-dimensional vector using OpenAI
  4. Vector Search β†’ Find semantically similar documents in Couchbase
  5. Context Retrieval β†’ Get relevant document chunks with similarity scores
  6. Context Assembly β†’ Combine query with retrieved context
  7. LLM Processing β†’ Generate contextually aware response using GPT
  8. Response Delivery β†’ Return English text answer to user

2. Document Ingestion Pipeline

  1. Document Upload β†’ PDF, text files, or web URLs
  2. Content Extraction β†’ Parse and extract text content
  3. Text Chunking β†’ Split into manageable segments with overlap
  4. Embedding Generation β†’ Create vector representations
  5. Database Storage β†’ Store documents and embeddings in Couchbase

3. Key Technical Components

  • 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

🎯 Use Cases

πŸ“š Document Analysis & Research

  • Upload research papers, reports, or documentation
  • Ask questions about specific content and get contextual answers
  • Extract insights and summaries from large documents

πŸ’Ό Business Intelligence

  • Upload company documents, policies, or procedures
  • Enable employees to quickly find information through natural language queries
  • Create an intelligent knowledge base for customer support

πŸŽ“ Educational Support

  • Upload textbooks, lecture notes, or study materials
  • Get explanations, summaries, and answers to study questions
  • Create personalized learning experiences

πŸ“– Content Management

  • Organize and search through large document collections
  • Enable semantic search across multiple file types
  • Build intelligent content recommendation systems

πŸš€ Quick Start

Prerequisites

  • Node.js (v18 or higher)
  • Couchbase Server (v7.0 or higher)
  • OpenAI API Key (with credits)

1. Clone the Repository

git clone <repository-url>
cd RAG-app

2. Install Dependencies

# Install frontend dependencies
npm install

# Install backend dependencies
cd server
npm install
cd ..

3. Setup Couchbase

  1. Install Couchbase Server: Download from couchbase.com
  2. Create Cluster: Set up a new cluster with default settings
  3. Load Sample Data: Import the travel-sample bucket
  4. Create Vector Index: Follow the Couchbase Vector Search Guide

4. Configure Environment

# Copy environment template
cp server/env.example server/.env

# Edit the .env file with your settings

Required 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

5. Test the Setup

# Run the setup test script
node test-setup.js

This will verify:

  • βœ… Environment variables are configured
  • βœ… Couchbase connection is working
  • βœ… OpenAI API is accessible
  • βœ… Sample data is available

6. Start the Application

# Terminal 1: Start the backend server
cd server
npm run dev

# Terminal 2: Start the frontend (in a new terminal)
npm run dev

7. Access the Application

Open your browser and navigate to:

πŸ§ͺ Testing Guide

Automated Setup Testing

The project includes a comprehensive test script to verify your setup:

node test-setup.js

What it tests:

  • Environment variable configuration
  • Couchbase database connection
  • OpenAI API connectivity
  • Sample data availability
  • Vector search index status

Manual Testing

1. Document Upload Test

  • Click the upload button in the chat interface
  • Upload a PDF file or enter a web URL
  • Verify the document is processed successfully

2. Chat Functionality Test

  • Type a question about travel destinations (using sample data)
  • Example: "Tell me about hotels in Paris"
  • Verify you get relevant, contextual responses

3. Vector Search Test

  • Upload a custom document
  • Ask specific questions about the uploaded content
  • Verify the AI provides accurate answers based on your document

4. API Endpoint Testing

# 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"

Troubleshooting Common Issues

Couchbase Connection Issues

# Check if Couchbase is running
lsof -i :8091
lsof -i :11210

# Verify web UI access
curl http://localhost:8091

OpenAI API Issues

Port Conflicts

  • Frontend runs on port 3000
  • Backend runs on port 5000
  • Couchbase uses ports 8091, 11210

πŸ“ Project Structure

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

πŸ”§ Configuration Options

Couchbase Configuration

  • Connection String: Modify for remote Couchbase clusters
  • Bucket Name: Use different buckets for different environments
  • Vector Index: Configure custom vector search indexes

OpenAI Configuration

  • 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)

Application Settings

  • File Upload Limits: Configure maximum file sizes
  • Supported File Types: Add support for additional document types
  • UI Themes: Customize the interface appearance

🀝 Contributing

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

πŸ“„ License

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

πŸ†˜ Support

If you encounter any issues or have questions:

  1. Check the troubleshooting section in this README
  2. Run the test script: node test-setup.js
  3. Review the logs in both frontend and backend consoles
  4. Open an issue on GitHub with detailed error information

πŸ™ Acknowledgments

  • 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! πŸš€

About

LLM RAG Vector Database Couchbase

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •