A comprehensive Retrieval-Augmented Generation (RAG) chatbot system that provides intelligent responses to news queries using real-time data retrieval and AI-powered answer generation.
🌐 Live API: rag-chatbot-backend-oxlx.onrender.com
🔗 Frontend Application: rag-chatbot-frontend-lake.vercel.app
- Overview
- Features
- Architecture
- Technology Stack
- Prerequisites
- Installation
- Configuration
- API Documentation
- Database Schema
- Usage
- WebSocket Events
- Deployment
- Performance
- Troubleshooting
- Contributing
- License
This project implements a sophisticated RAG (Retrieval-Augmented Generation) pipeline that enables users to query news articles and receive AI-generated responses based on relevant context. The system combines real-time news retrieval, semantic search, and intelligent response generation to create a comprehensive news chatbot experience.
- Real-time news article retrieval and processing
- Intelligent semantic search using vector embeddings
- AI-powered response generation with context awareness
- Multi-tier search strategy (Database → Vector Search → External APIs)
- Session-based chat management with persistent storage
- Real-time WebSocket communication
- Multi-layered Search: Database → Qdrant Vector Search → News API fallback
- Semantic Understanding: Jina AI embeddings for contextual matching
- Relevance Scoring: Advanced article ranking based on query relevance
- Context-Aware Generation: Google Gemini AI for intelligent responses
- Source Attribution: Transparent source referencing and citation
- Fallback Mechanisms: Graceful degradation when AI services are unavailable
- WebSocket Integration: Instant message delivery and status updates
- Session Management: Persistent chat sessions with unique identifiers
- Live Status Updates: Real-time search progress indicators
- Hybrid Storage: Redis for active sessions, PostgreSQL for persistence
- Vector Database: Qdrant for semantic search capabilities
- Automatic Backup: Session data backup on disconnection
- Duplicate Prevention: Advanced message deduplication across all layers
- Error Recovery: Comprehensive error handling and retry mechanisms
- Performance Optimization: Efficient batch processing and caching
┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐
│ Frontend │ │ Backend │ │ External │
│ (Socket.IO) │◄──►│ (Express.js) │◄──►│ Services │
└─────────────────┘ └─────────────────┘ └─────────────────┘
│ │
┌────────▼────────┐ │
│ WebSocket Layer │ │
│ (Real-time) │ │
└────────┬────────┘ │
│ │
┌───────────▼───────────┐ │
│ Search Pipeline │ │
│ 1. Database Search │ │
│ 2. Vector Search │ │
│ 3. News API Fetch │ │
└───────────┬───────────┘ │
│ │
┌───────────────────────┼───────────────────────┼──────────────┐
│ │ │ │
┌───────▼────────┐ ┌────────▼────────┐ ┌─────────▼──────┐ ┌──▼─────┐
│ PostgreSQL │ │ Redis Cache │ │ Qdrant Vector │ │ AI APIs│
│ (Persistence) │ │ (Sessions) │ │ Database │ │ │
└────────────────┘ └─────────────────┘ └────────────────┘ └────────┘
- Runtime: Node.js 18+
- Framework: Express.js
- Real-time: Socket.IO
- Database: PostgreSQL with Prisma ORM
- Cache: Redis (with fallback to in-memory)
- Vector DB: Qdrant
- Embeddings: Jina AI (jina-embeddings-v3)
- LLM: Google Gemini 1.5 Flash
- Search: Semantic vector similarity
- News Data: News API
- Vector Operations: Qdrant Cloud/Self-hosted
- Process Management: PM2 (recommended)
- Monitoring: Morgan logging
- Environment: dotenv
- HTTP Client: Axios
- Node.js: Version 18.0 or higher
- PostgreSQL: Version 13 or higher
- Redis: Version 6 or higher (optional, has fallback)
- Qdrant: Vector database instance
- News API key from newsapi.org
- Jina AI API key from jina.ai
- Google Gemini API key from ai.google.dev
- Qdrant instance (cloud or self-hosted)
- Platform: Render
- URL: rag-chatbot-backend-oxlx.onrender.com
- Health Check: /health endpoint
- Features:
- Automatic deployments from main branch
- PostgreSQL database integration
- Environment variables management
- SSL/HTTPS enabled
- 24/7 uptime monitoring
- Platform: Vercel
- URL: rag-chatbot-frontend-lake.vercel.app
- WebSocket Connection: Real-time communication with deployed backend
git clone <repository-url>
cd rag-news-chatbot
npm install
# Generate Prisma client
npx prisma generate
# Run database migrations
npx prisma migrate deploy
# (Optional) Seed database
npx prisma db seed
Create a .env
file in the root directory:
# Database
POSTGRES_URL="postgresql://username:password@localhost:5432/rag_chatbot"
# Redis (optional - has fallback)
REDIS_HOST="localhost"
REDIS_PORT="6379"
REDIS_PASSWORD=""
# API Keys
NEWS_API_KEY="your_news_api_key"
NEWS_API="https://newsapi.org/v2/"
JINA_API_KEY="your_jina_api_key"
JINA_API_URL="https://api.jina.ai/v1/embeddings"
GEMINI_API_KEY="your_gemini_api_key"
# Qdrant
QDRANT_URL="https://your-cluster-url.qdrant.tech"
QDRANT_API_KEY="your_qdrant_api_key"
# Server
PORT="5000"
NODE_ENV="production"
FRONTEND_URL="http://localhost:3000"
# Development
npm run dev
# Production
npm start
Currently, no authentication is required. Each session is identified by a unique session ID.
# Production API
https://rag-chatbot-backend-oxlx.onrender.com/api/v1
# Local Development
http://localhost:5000/api/v1
Method | Endpoint | Description |
---|---|---|
GET |
/chat/sessions |
List all chat sessions |
POST |
/chat/sessions |
Create new chat session |
GET |
/chat/sessions/:id/history |
Get session message history |
DELETE |
/chat/sessions/:id |
Delete entire session |
DELETE |
/chat/sessions/:id/cache |
Clear session cache only |
POST |
/chat/sessions/:id/backup |
Manual session backup |
Method | Endpoint | Description |
---|---|---|
GET |
/fetch-news?q={query} |
Fetch and store news articles |
GET |
/all-articles |
Get paginated articles list |
POST |
/search?q={query} |
Search articles with AI response |
POST |
/jina-sync |
Sync articles to vector database |
# Production API
curl -X POST https://rag-chatbot-backend-oxlx.onrender.com/api/v1/chat/sessions \
-H "Content-Type: application/json"
# Local Development
curl -X POST http://localhost:5000/api/v1/chat/sessions \
-H "Content-Type: application/json"
# Production API
curl -X POST "https://rag-chatbot-backend-oxlx.onrender.com/api/v1/search?q=climate%20change" \
-H "Content-Type: application/json"
# Local Development
curl -X POST "http://localhost:5000/api/v1/search?q=climate%20change" \
-H "Content-Type: application/json"
# Production API
curl -X GET https://rag-chatbot-backend-oxlx.onrender.com/api/v1/chat/sessions/{sessionId}/history \
-H "Content-Type: application/json"
# Local Development
curl -X GET http://localhost:5000/api/v1/chat/sessions/{sessionId}/history \
-H "Content-Type: application/json"
CREATE TABLE "Article" (
"id" UUID PRIMARY KEY DEFAULT gen_random_uuid(),
"author" TEXT,
"title" TEXT NOT NULL,
"description" TEXT,
"urlToImage" TEXT,
"url" TEXT NOT NULL,
"content" TEXT,
"published_at" TIMESTAMP,
"created_at" TIMESTAMP DEFAULT NOW(),
"source_name" TEXT,
"is_synced" BOOLEAN DEFAULT FALSE,
"synced_at" TIMESTAMP
);
CREATE TABLE "chat_sessions" (
"id" UUID PRIMARY KEY DEFAULT gen_random_uuid(),
"session_id" TEXT UNIQUE NOT NULL,
"created_at" TIMESTAMP DEFAULT NOW(),
"updated_at" TIMESTAMP DEFAULT NOW()
);
CREATE TABLE "chat_messages" (
"id" UUID PRIMARY KEY DEFAULT gen_random_uuid(),
"session_id" TEXT NOT NULL,
"message_id" TEXT NOT NULL,
"type" TEXT NOT NULL,
"content" TEXT NOT NULL,
"sender" TEXT NOT NULL,
"timestamp" TIMESTAMP NOT NULL,
"metadata" TEXT,
UNIQUE("session_id", "message_id")
);
chat_messages(session_id, timestamp)
- Fast session message retrievalArticle(is_synced)
- Efficient sync status queriesArticle(published_at)
- Chronological article sorting
- Connect via WebSocket:
// Production WebSocket connection
const socket = io('https://rag-chatbot-backend-oxlx.onrender.com');
// Local development
// const socket = io('http://localhost:5000');
// Join a session
socket.emit('join_session', {
sessionId: 'optional-existing-id' // If not provided, new session is created
});
- Send Messages:
socket.emit('send_message', {
sessionId: 'your-session-id',
message: 'What are the latest news about climate change?'
});
- Listen for Responses:
socket.on('new_message', (message) => {
console.log('New message:', message);
});
socket.on('status_update', (status) => {
console.log('Status:', status);
});
- Database Search: Query existing articles for keyword matches
- Vector Search: If no results, perform semantic search using Qdrant
- External Fetch: If still no results, fetch fresh articles from News API
- AI Processing: Generate contextual response using most relevant article
- Response Delivery: Stream response with source attribution
Event | Payload | Description |
---|---|---|
join_session |
{sessionId?} |
Join or create chat session |
send_message |
{sessionId, message} |
Send user message |
get_history |
{sessionId, limit?, offset?} |
Request message history |
clear_session |
{sessionId} |
Clear session cache |
ping |
{} |
Health check |
Event | Payload | Description |
---|---|---|
session_joined |
{sessionId, history} |
Session join confirmation |
new_message |
{id, content, sender, timestamp} |
New chat message |
status_update |
{status, message, visible} |
Search progress updates |
session_cleared |
{sessionId} |
Cache clear confirmation |
pong |
{timestamp, serverTime} |
Health check response |
initializing
- Search startinganalyzing
- Processing query semanticallyfetching
- Retrieving external datafound
- Results discoveredprocessing
- Selecting relevant contentgenerating
- AI response creationcomplete
- Process finishederror
- Error occurred
- Backend: Deployed on Render - rag-chatbot-backend-oxlx.onrender.com
- Frontend: Deployed on Vercel - rag-chatbot-frontend-lake.vercel.app
- Database: PostgreSQL on Render
- Cache: Redis (with in-memory fallback)
- Create Dockerfile:
FROM node:18-alpine
WORKDIR /app
COPY package*.json ./
RUN npm ci --only=production
COPY . .
RUN npx prisma generate
EXPOSE 5000
CMD ["npm", "start"]
- Docker Compose:
version: '3.8'
services:
app:
build: .
ports:
- "5000:5000"
environment:
- NODE_ENV=production
depends_on:
- postgres
- redis
postgres:
image: postgres:15
environment:
POSTGRES_DB: rag_chatbot
POSTGRES_USER: postgres
POSTGRES_PASSWORD: your_password
volumes:
- postgres_data:/var/lib/postgresql/data
redis:
image: redis:7-alpine
command: redis-server --appendonly yes
volumes:
- redis_data:/data
volumes:
postgres_data:
redis_data:
- Environment variables configured
- Database migrations applied
- SSL certificates installed
- Reverse proxy configured (Nginx/Cloudflare)
- Process manager setup (PM2)
- Monitoring configured
- Backup strategy implemented
- Log rotation configured
# render.yaml
services:
- type: web
name: rag-chatbot-backend
env: node
buildCommand: npm install && npx prisma generate && npx prisma migrate deploy
startCommand: npm start
healthCheckPath: /health
envVars:
- key: NODE_ENV
value: production
- key: POSTGRES_URL
fromDatabase:
name: rag-chatbot-db
property: connectionString
- key: REDIS_HOST
fromService:
type: redis
name: rag-chatbot-redis
property: host
NODE_ENV=production
LOG_LEVEL=error
POSTGRES_URL=postgresql://user:pass@hostname:port/database
FRONTEND_URL=https://rag-chatbot-frontend-lake.vercel.app
- Database Indexing: Optimized indexes for session and article queries
- Connection Pooling: Prisma connection pooling for database efficiency
- Redis Caching: Session data cached for fast retrieval
- Vector Search: Efficient semantic search with Qdrant
- Batch Processing: Articles processed in configurable batches
- Message Processing: ~2-5 seconds end-to-end
- Database Queries: <100ms for indexed queries
- Vector Search: ~200-500ms for semantic matching
- AI Response: 2-8 seconds depending on context size
- Concurrent Sessions: Supports 100+ concurrent users
- Horizontal Scaling: Stateless design enables easy horizontal scaling
- Database Sharding: Session data can be sharded by session_id
- Cache Distribution: Redis cluster for distributed caching
- Load Balancing: WebSocket sticky sessions recommended
Error: WebSocket connection failed
Solutions:
- Check CORS configuration
- Verify port accessibility
- Review firewall settings
- Check for proxy interference
Error: Can't reach database server
Solutions:
- Verify POSTGRES_URL format
- Check PostgreSQL service status
- Review connection pool limits
- Validate credentials
Warning: Redis unavailable, using fallback mode
Behavior: Application continues with in-memory storage Solutions:
- Check Redis connection parameters
- Verify Redis service status
- Review authentication settings
Error: Gemini API overloaded (503)
Behavior: Automatic retry with exponential backoff Solutions:
- Wait for service recovery
- Check API key quotas
- Review rate limits
Error: Qdrant collection not found
Solutions:
- Check Qdrant URL and API key
- Verify collection exists
- Review embedding dimensions
Enable debug logging:
NODE_ENV=development
LOG_LEVEL=debug
Monitor application health:
# Production API
curl https://rag-chatbot-backend-oxlx.onrender.com/health
# Local Development
curl http://localhost:5000/health
Expected response:
{
"status": "ok",
"timestamp": "2024-01-15T10:30:00.000Z",
"services": {
"server": "running",
"redis": "connected",
"socket": "active",
"database": "connected"
}
}
- Fork the repository
- Create a feature branch:
git checkout -b feature/amazing-feature
- Make changes and test thoroughly
- Commit changes:
git commit -m 'Add amazing feature'
- Push to branch:
git push origin feature/amazing-feature
- Open a Pull Request
- ESLint: Follow provided linting rules
- Prettier: Use for code formatting
- Naming: Use camelCase for JavaScript, snake_case for database
- Comments: Document complex logic and API interactions
# Run tests
npm test
# Run with coverage
npm run test:coverage
# Integration tests
npm run test:integration
This project is open source and available under the MIT License.
Copyright (c) 2024 RAG-Powered News Chatbot
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
For technical support and questions:
- Create an issue in the GitHub repository
- Check the troubleshooting section above
- Review the health endpoint:
/health
- Verify environment variables
- Check application logs