Skip to content

Getting started with RAG system using Langchain in Node.js. This project uses OpenAI for embedding and Pinecone for Vector DB.

License

Notifications You must be signed in to change notification settings

uyenvoaero/rag-langchain-nodejs

 
 

Repository files navigation

RAG Document Chat System

A modern web application that allows you to upload PDF documents and chat with them using AI. Built with Node.js, Express, and a beautiful responsive frontend.

Initial Interface Clean, modern interface ready for document upload

Features

  • 📁 File Upload: Upload PDF documents with drag & drop support
  • 🤖 AI Chat: Chat with your documents using OpenAI's GPT models
  • 🗂️ Multiple Documents: Manage multiple documents with separate conversations
  • 🎯 Namespace Isolation: Each document gets its own Pinecone namespace for accurate retrieval
  • 💬 Conversation History: Maintain separate chat histories for each document
  • 📱 Responsive Design: Beautiful, modern UI that works on all devices
  • Real-time Processing: Fast document processing and embedding generation
  • 🌍 Multi-language Support: Ask questions in English, Vietnamese, or other languages
  • 🔄 Auto-clean Data: Automatically clears old data on startup for fresh sessions

Tech Stack

Backend

  • Node.js with Express.js
  • LangChain for AI integration
  • OpenAI for embeddings and chat
  • Pinecone for vector database
  • Multer for file uploads
  • PDF-parse for document processing

Frontend

  • Vanilla JavaScript with modern ES6+
  • Responsive CSS with gradient design
  • Drag & Drop file upload
  • Real-time chat interface

Setup Instructions

1. Install Dependencies

npm install

2. Environment Variables

Create a .env file in the root directory with the following variables:

OPENAI_API_KEY=your_openai_api_key_here
PINECONE_API_KEY=your_pinecone_api_key_here
PORT=3001

3. Run the Application

# Start the server
npm start

# Or for development with auto-restart
npm run dev

The application will be available at http://localhost:3001

Demo

Chat Interface Example of chatting with a Vietnamese AWS Machine Learning document

How It Works

  1. Document Upload: Users upload PDF files through the web interface
  2. Processing: The system extracts text, chunks it, and generates embeddings
  3. Storage: Embeddings are stored in Pinecone with unique namespaces per document
  4. Chat: Users can select documents and ask questions
  5. Retrieval: The system retrieves relevant chunks from the specific document's namespace
  6. Generation: AI generates answers based on the retrieved context

API Endpoints

  • GET /api/files - Get list of uploaded files
  • POST /api/upload - Upload and process a PDF file
  • POST /api/chat - Send a chat message for a specific document
  • DELETE /api/files/:fileId - Delete a file and its data

File Structure

├── server.js              # Express server with API endpoints
├── public/
│   └── index.html         # Frontend application
├── src/
│   ├── chunk-texts.js     # Text chunking logic
│   ├── embed-texts.js     # Embedding generation
│   ├── generate-answer.js # AI answer generation
│   ├── parse-pdf.js       # PDF text extraction
│   └── vector-db.js       # Pinecone operations
├── uploads/               # Uploaded files storage
└── package.json

Usage

  1. Upload Documents: Click "Upload Document" or drag & drop PDF files
  2. Select Document: Click on any document in the sidebar to start chatting
  3. Ask Questions: Type your questions in the chat input (supports multiple languages)
  4. Switch Documents: Click different documents to have separate conversations
  5. Delete Documents: Use the delete button to remove documents

Multi-language Support

The system supports cross-language queries:

  • Upload a Vietnamese document and ask questions in English
  • Upload an English document and ask questions in Vietnamese
  • The AI will respond in the same language as your question

Configuration

The system uses the following default configurations:

  • Chunk Size: 1000 characters
  • Chunk Overlap: 200 characters
  • Embedding Model: text-embedding-3-large (3072 dimensions)
  • Chat Model: gpt-4o-mini
  • Top K Results: 10 relevant chunks (with similarity filtering)
  • File Size Limit: 10MB
  • Similarity Threshold: 0.7 for cross-language search

Troubleshooting

Common Issues

  1. "No matching chunks found": The document might not contain relevant information for your query
  2. Upload fails: Check file size (max 10MB) and ensure it's a valid PDF
  3. API errors: Verify your OpenAI and Pinecone API keys are correct
  4. Dimension mismatch errors: The system automatically handles this by recreating the Pinecone index
  5. Cross-language queries not working: Try asking in the same language as the document first

Environment Setup

Make sure you have:

  • Node.js 16+ installed
  • Valid OpenAI API key with sufficient credits
  • Valid Pinecone API key with an active project

Original Terminal Version

The original terminal-based version is still available in index.js. You can run it with:

node index.js

This will:

  • Create index if not exists
  • Process sample PDF for the first time
  • Accept queries from terminal and generate answers from PDF

License

MIT License - feel free to use this project for your own applications!

About

Getting started with RAG system using Langchain in Node.js. This project uses OpenAI for embedding and Pinecone for Vector DB.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • HTML 63.5%
  • JavaScript 36.5%