Skip to content

Repository files navigation

WA Support AI - RAG

A Retrieval-Augmented Generation (RAG) powered WhatsApp AI Assistant

An AI-powered WhatsApp customer support assistant built with NestJS and RAG. It maintains a persistent WhatsApp Web session, ingests your knowledge base into vector embeddings, and uses context-aware generation to answer customer queries intelligently via WhatsApp.

What is RAG?

Retrieval-Augmented Generation (RAG) is an AI architecture that combines information retrieval with text generation to produce accurate, grounded responses. Instead of relying solely on the LLM's training data, RAG retrieves relevant documents from your knowledge base at query time and feeds them as context to the model.

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ RAG Pipeline ──────────────────┐
β”‚                                                  β”‚
β”‚  1. RETRIEVE                                     β”‚
β”‚     User query β†’ Embed β†’ Vector search           β”‚
β”‚     Find the most relevant knowledge chunks      β”‚
β”‚                                                  β”‚
β”‚  2. AUGMENT                                      β”‚
β”‚     Inject retrieved context into the prompt     β”‚
β”‚     "Answer using ONLY this information..."      β”‚
β”‚                                                  β”‚
β”‚  3. GENERATE                                     β”‚
β”‚     LLM produces a grounded, cited response      β”‚
β”‚     based on actual knowledge base data          β”‚
β”‚                                                  β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Why RAG over fine-tuning?

  • No model retraining needed β€” just upload documents
  • Always up-to-date β€” add/remove knowledge at any time
  • Transparent β€” responses cite exact sources
  • Cost-effective β€” works with any LLM (GPT-4o-mini, Claude, etc.)

Built for SetNGo Holidays β€” adaptable to any business.

Disclaimer: This project uses only publicly available data from the SetNGo Holidays website as a demonstration example. It is not an official product of SetNGo Holidays. The system prompt and knowledge base can be replaced with any business's data to build a similar AI assistant.

Screenshots

Dashboard Dashboard

3D Vector Space Visualization 3D Vector Space

2D Vector Space Visualization 2D Vector Space

Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                        WhatsApp (Customer)                       β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                 β”‚
                    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                    β”‚   WA Channel Module     β”‚
                    β”‚  (whatsapp-web.js)      β”‚
                    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                 β”‚
              β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
              β”‚           RAG Pipeline              β”‚
              β”‚                                    β”‚
              β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”‚
              β”‚  β”‚ Embedding β”‚β†’ β”‚ Vector Search β”‚  β”‚
              β”‚  β”‚ (OpenAI)  β”‚  β”‚  (MongoDB)    β”‚  β”‚
              β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜  β”‚
              β”‚                        β”‚          β”‚
              β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”  β”‚
              β”‚  β”‚     LLM (GPT-4o-mini)       β”‚  β”‚
              β”‚  β”‚  + Retrieved Context        β”‚  β”‚
              β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β”‚
              β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                 β”‚
              β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
              β”‚         MongoDB Atlas               β”‚
              β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”‚
              β”‚  β”‚  Chunks +  β”‚ β”‚ Conversations β”‚  β”‚
              β”‚  β”‚ Embeddings β”‚ β”‚   & Tickets   β”‚  β”‚
              β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β”‚
              β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Features

Core

  • RAG-powered responses β€” answers grounded in your uploaded knowledge base with source citations
  • WhatsApp integration β€” persistent headless session via Puppeteer, QR-based auth, typing indicators
  • Document ingestion β€” upload PDF, TXT, CSV, or Markdown; auto-chunked and embedded
  • MongoDB Atlas Vector Search β€” native similarity search with cosine fallback for local dev
  • Automated ticket system β€” AI creates support tickets when it can't resolve issues
  • Conversation logging β€” full audit trail of every exchange with retrieved context and token usage

AI Agent Capabilities

  • Greets first-time customers automatically
  • Answers from knowledge base with source citations
  • Raises tickets: complaints, cancellations, update requests, callback requests
  • Deduplicates tickets β€” checks existing open tickets before creating new ones
  • Enforced boundaries β€” only responds to business-related queries

Developer Tools

  • Web chat interface β€” test the AI via browser at /chat
  • 3D vector space visualization β€” explore embeddings at /embedding/visualize-3d
  • 2D vector space visualization β€” scatter plot at /embedding/visualize
  • Swagger API docs β€” full REST documentation at /api/docs
  • Real-time dashboard β€” connection status + QR code at /

Tech Stack

Layer Technology
Framework NestJS 11, TypeScript
LLM OpenAI GPT-4o-mini via LangChain
Embeddings OpenAI text-embedding-3-small (1536 dims)
Vector Search MongoDB Atlas $vectorSearch
Database MongoDB 7 / Mongoose
WhatsApp whatsapp-web.js + Puppeteer
API Docs Swagger UI
Containerization Docker (multi-stage build)

Getting Started

Prerequisites

  • Node.js 20+
  • MongoDB (local Docker or Atlas free tier)
  • OpenAI API key
  • A machine where Chromium can run (for WhatsApp Web)

1. Clone and install

git clone https://github.com/your-username/wa-support-ai.git
cd wa-support-ai
npm install

2. Start MongoDB (local development)

docker compose -f infra-setup/docker-compose.yml up -d

This starts MongoDB 7 on localhost:27017 with Mongo Express GUI on localhost:8081.

3. Configure environment

cp .env.example .env

Fill in your values:

PORT=3000
OPENAI_API_KEY=sk-...
OPENAI_MODEL=gpt-4o-mini
OPENAI_EMBEDDING_MODEL=text-embedding-3-small
MONGODB_URI=mongodb://localhost:27017/wa_support_ai
WA_SEND_API_USER=your-api-user
WA_SEND_API_PASS=your-api-pass

4. Start the application

npm run start:dev

5. Connect WhatsApp

Open http://localhost:3000 and scan the QR code with your phone.

6. Ingest knowledge base

Upload documents via the API or Swagger UI:

curl -X POST http://localhost:3000/document/upload \
  -F "file=@your-document.pdf"

Embeddings are generated automatically after upload.

7. (Atlas only) Create vector search index

In MongoDB Atlas β†’ Search β†’ Create Index:

{
  "type": "vectorSearch",
  "fields": [{
    "path": "embedding",
    "numDimensions": 1536,
    "similarity": "cosine",
    "type": "vector"
  }]
}

Index name: autoembed_index on collection document_chunks.

API Reference

WhatsApp Channel

Method Endpoint Description
GET /wa-channel/status Connection status
GET /wa-channel/qr QR code as PNG
GET /wa-channel/events SSE stream (status + QR)
POST /wa-channel/send-message Send outbound message (auth required)
GET /wa-channel/users List unique users
GET /wa-channel/conversations/:phone Conversation history
GET /wa-channel/tickets List all tickets

Documents

Method Endpoint Description
POST /document/upload Upload and ingest a document
GET /document List all documents
GET /document/:id/chunks View chunks
DELETE /document/:id Delete a document

Embeddings

Method Endpoint Description
POST /embedding/process Embed all unprocessed chunks
POST /embedding/query Embed text (for testing)
GET /embedding/visualize 2D visualization
GET /embedding/visualize-3d 3D visualization

Chat (Browser)

Method Endpoint Description
GET /chat Chat UI
POST /chat/send Send message (full response)
POST /chat/stream Send message (SSE stream)

Full Swagger documentation available at /api/docs.

Project Structure

src/
β”œβ”€β”€ main.ts                          # Bootstrap + Swagger
β”œβ”€β”€ app.module.ts                    # Root module
β”œβ”€β”€ database/
β”‚   β”œβ”€β”€ database.module.ts           # MongoDB connection
β”‚   └── schemas/                     # Mongoose schemas
β”‚       β”œβ”€β”€ document-chunk.schema.ts
β”‚       β”œβ”€β”€ document-metadata.schema.ts
β”‚       β”œβ”€β”€ conversation-log.schema.ts
β”‚       └── ticket.schema.ts
β”œβ”€β”€ wa-channel/                      # WhatsApp transport + AI handler
β”‚   β”œβ”€β”€ wa-channel.module.ts
β”‚   β”œβ”€β”€ wa-channel.service.ts        # Message handling, RAG integration, tickets
β”‚   └── wa-channel.controller.ts     # REST + SSE endpoints
β”œβ”€β”€ rag/                             # RAG orchestration
β”‚   β”œβ”€β”€ rag.module.ts
β”‚   └── rag.service.ts              # Query β†’ Embed β†’ Search β†’ Generate
β”œβ”€β”€ llm/                             # LLM provider (OpenAI via LangChain)
β”‚   β”œβ”€β”€ llm.module.ts
β”‚   └── llm.service.ts
β”œβ”€β”€ embedding/                       # Vector embedding generation
β”‚   β”œβ”€β”€ embedding.module.ts
β”‚   β”œβ”€β”€ embedding.service.ts
β”‚   β”œβ”€β”€ embedding.controller.ts
β”‚   └── pages/                       # Visualization UIs
β”œβ”€β”€ document/                        # Document ingestion pipeline
β”‚   β”œβ”€β”€ document.module.ts
β”‚   β”œβ”€β”€ document.service.ts
β”‚   β”œβ”€β”€ document.controller.ts
β”‚   β”œβ”€β”€ chunking/                    # Text splitting
β”‚   └── loaders/                     # PDF, TXT, CSV parsers
β”œβ”€β”€ chat/                            # Web chat interface
β”‚   β”œβ”€β”€ chat.module.ts
β”‚   β”œβ”€β”€ chat.service.ts
β”‚   β”œβ”€β”€ chat.controller.ts
β”‚   └── pages/
└── frontend/
    └── dashboard.controller.ts      # Main dashboard UI

MongoDB Collections

Collection Purpose
document_chunks Chunked text with embedding vectors
documents Document metadata (filename, size, status)
conversation_logs Full message logs with RAG context
tickets Support tickets (complaints, cancellations, etc.)

Deployment

Docker

docker compose up -d --build

The app image uses a multi-stage build with Alpine + Chromium. WhatsApp sessions persist via Docker volumes.

Environment

For production, use MongoDB Atlas for vector search and set NODE_ENV=production.

Scripts

Command Description
npm run start:dev Development mode with hot reload
npm run build Compile TypeScript
npm run start:prod Run compiled build
npm run lint ESLint with auto-fix
npm run test Run unit tests

Message Flow

Customer sends WhatsApp message
  β†’ Typing indicator shown
  β†’ Load conversation history (last 10 messages)
  β†’ Check existing open tickets (dedup)
  β†’ Embed user query (OpenAI)
  β†’ Vector search for relevant chunks (MongoDB Atlas)
  β†’ Build augmented prompt (system prompt + context + history)
  β†’ LLM generates response (GPT-4o-mini)
  β†’ If response contains ticket JSON β†’ create ticket in DB
  β†’ Log conversation (user msg + AI response + sources + tokens)
  β†’ Send response to customer

License

UNLICENSED β€” private project.

About

No description, website, or topics provided.

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages