DEV Community

Cover image for Build a RAG-Enabled Helpdesk Chatbot in 10 Minutes with MongoDB
Michael Lynn
Michael Lynn

Posted on

1 1

Build a RAG-Enabled Helpdesk Chatbot in 10 Minutes with MongoDB

Oh no... another RAG tutorial... right?

Hold on... this one's different. I'm going to show you how to get started with a RAG enabled chatbot app with a brand-new library created specifically to take the pain out of developing a RAG enabled Chatbot.

The best part? You'll have it up and running in less than 10 minutes!

Prefer to watch? Checkout the Video Walkthrough here.

Before we begin, if you need more information about RAG, check out this article.

Background

As part of my job as a Developer Advocate at MongoDB, I help customers make the best use of software, and data including MongoDB. A common request I encounter is around customers wanting to create an intelligent chatbot that knows the specifics of their company, customers, and personalization. This is a perfect use-case for Retrieval Augmented Generation, or RAG. To simplify the process, I've created a new library that will help developers create and deploy a custom RAG enabled chatbot in about 10 minutes.

What We're Building

MongoDB-RAG Helpdesk Chatbot

We'll create a modern helpdesk application that includes:

  • A sleek React frontend with a search interface
  • A robust Express backend API for processing queries
  • RAG-powered vector search using MongoDB Atlas Vector Search
  • Easy document ingestion for helpdesk articles
  • Automatic vector embedding generation

By the end, your chatbot will retrieve the most relevant support articles using semantic search!

Prerequisites

Before we dive in, ensure you have:

  • Node.js installed on your machine
  • A MongoDB Atlas account (Sign up for free)
  • An OpenAI API key for generating embeddings
  • Basic familiarity with JavaScript & Node.js

Step 1: Set Up MongoDB Atlas (3 minutes)

1. Create a Free Atlas Cluster

MongoDB Atlas

  1. Go to MongoDB Atlas and sign in
  2. Click "Create" to deploy a new cluster
  3. Select the FREE (M0) tier
  4. Choose your preferred cloud provider and region
  5. Click "Create Cluster" and wait for deployment

2. Configure Database Access

  1. Go to "Database Access" under the security tab
  2. Click "Add New Database User"
  3. Choose Password Authentication
  4. Set a secure username and password
  5. Assign "Read and write to any database" permissions

3. Set Network Access

  1. Go to "Network Access" and click "Add IP Address"
  2. Select "Allow Access from Anywhere" (for development)
  3. Confirm and wait for the change to propagate

4. Get Your Connection String

  1. Click "Connect" on your cluster
  2. Choose "Connect your application"
  3. Copy the connection string
  4. Replace <password> with your actual password

Step 2: Configure Your Environment

First, initialize your mongodb-rag configuration:

npx mongodb-rag init
Enter fullscreen mode Exit fullscreen mode

This creates a .mongodb-rag.json file with settings like:

{
  "mongoUrl": "mongodb+srv://your_user:your_password@your-cluster.mongodb.net/mongorag",
  "database": "helpdesk",
  "collection": "articles",
  "embedding": {
    "provider": "openai",
    "apiKey": "your-embedding-api-key",
    "model": "text-embedding-3-small",
    "dimensions": 1536
  },
  "search": {
    "maxResults": 5,
    "minScore": 0.7
  },
  "indexName": "vector_index"
}
Enter fullscreen mode Exit fullscreen mode

Then convert it to environment variables:

npx mongodb-rag create-env
Enter fullscreen mode Exit fullscreen mode

This creates a .env file with the correct variables:

MONGODB_URI=mongodb+srv://your_user:your_password@your-cluster.mongodb.net/mongorag
MONGODB_DATABASE=helpdesk
MONGODB_COLLECTION=articles
PORT=4001

# Embedding Configuration
EMBEDDING_PROVIDER=openai
EMBEDDING_API_KEY=your-embedding-api-key
EMBEDDING_MODEL=text-embedding-3-small
EMBEDDING_DIMENSIONS=1536

# Vector Search Configuration
VECTOR_INDEX=vector_index
MAX_RESULTS=5
Enter fullscreen mode Exit fullscreen mode

Step 3: Create a Vector Search Index (1 minute)

You have two options for creating your vector search index:

Option 1: Using the CLI (Recommended)

npx mongodb-rag create-index
Enter fullscreen mode Exit fullscreen mode

This will:

  • Create an index optimized for OpenAI's text-embedding-3-small model
  • Configure proper dimensions (1536) and similarity metrics (cosine)
  • Handle any existing index conflicts

Option 2: Using Atlas UI

  1. Navigate to your cluster in Atlas
  2. Click "Browse Collections"
  3. Click the "Search" tab
  4. Click "Create Search Index"
  5. Choose "JSON Editor" and select "Atlas Vector Search"
  6. Use this configuration:
{
  "name": "vector_index",
  "type": "vectorSearch",
  "definition": {
    "fields": [{
      "type": "vector",
      "path": "embedding",
      "numDimensions": 1536,
      "similarity": "cosine"
    }]
  }
}
Enter fullscreen mode Exit fullscreen mode

Step 4: Deploy Your Helpdesk Chatbot

Generate your application:

npx mongodb-rag create-rag-app my-helpdesk
cd my-helpdesk
Enter fullscreen mode Exit fullscreen mode

Your application will be structured as:

my-helpdesk/
├── frontend/           # React frontend
│   ├── src/
│   │   ├── components/
│   │   ├── App.jsx
│   │   └── main.jsx
├── backend/           # Express backend
│   ├── config/
│   ├── server.js
│   └── package.json
└── package.json      # Root workspace
Enter fullscreen mode Exit fullscreen mode

Install dependencies and start the servers:

npm install
npm run dev
Enter fullscreen mode Exit fullscreen mode

This will start:

Step 5: Add Your Helpdesk Documents

Create a helpdesk-docs directory with markdown files for common IT support issues. Here are some samples - but you should feel free to create your own, or use your company's helpdesk documents. Let's stick to markdown for this example implementation... but know that mongodb-rag supports ingestion of PDF documents.

wifi-connection.md:

### Wi-Fi Connection Issues

**Issue:** My Wi-Fi is not working.

**Solution:**
1. Restart your router and modem
2. Check if other devices can connect
3. Forget the Wi-Fi network and reconnect
4. If the issue persists, contact your ISP
Enter fullscreen mode Exit fullscreen mode

password-reset.md:

### Password Reset Guide

**Issue:** I forgot my password.

**Solution:**
1. Go to the login page and click "Forgot Password?"
2. Enter your registered email address
3. Check your email for a password reset link
4. Click the link and enter a new password
Enter fullscreen mode Exit fullscreen mode

software-install.md:

### Software Installation Issues

**Issue:** I can't install the application.

**Solution:**
1. Ensure your device meets the system requirements
2. Check if you have administrator privileges
3. Disable any antivirus software temporarily
4. Try reinstalling the application
Enter fullscreen mode Exit fullscreen mode

Ingest the Documents

Use the CLI to ingest all documents:

npx mongodb-rag ingest --directory ./helpdesk-docs
Enter fullscreen mode Exit fullscreen mode

Or use the API for programmatic ingestion:

await fetch('http://localhost:4001/api/ingest', {
  method: 'POST',
  headers: { 'Content-Type': 'application/json' },
  body: JSON.stringify({
    documents: [
      {
        id: 'wifi-help',
        content: fs.readFileSync('./helpdesk-docs/wifi-connection.md', 'utf8'),
        metadata: { category: 'network', topic: 'wifi' }
      }
    ]
  })
});
Enter fullscreen mode Exit fullscreen mode

🔍 Step 6: Test Your Chatbot

Try searching through the frontend interface or use the CLI:

npx mongodb-rag search "wifi not connecting"
npx mongodb-rag search "how to reset password"
npx mongodb-rag search "software installation failed"
Enter fullscreen mode Exit fullscreen mode

Available API endpoints:

  • GET /api/search?query=<text> - Search documents
  • POST /api/ingest - Ingest new documents
  • DELETE /api/documents/:id - Remove documents
  • GET /api/health - Health check endpoint

🔧 Troubleshooting

Vector Index Creation Failures

  1. Invalid Index Definition

    • Error: "Invalid index specification"
    • Solution: Double-check your index JSON syntax. The most common issues are:
      • Missing type": "vectorSearch" field
      • Incorrect nesting of fields array
      • Wrong similarity metric spelling
  2. Dimension Mismatch

    • Error: "Vector dimensions do not match index definition"
    • Solution: Ensure your vector dimensions match your embedding model's output (1536 for text-embedding-3-small)
  3. Permission Issues

    • Error: "Insufficient permissions to create index"
    • Solution: Ensure you have Project Data Access Admin or higher role

Ingestion Issues

  1. Embedding Generation Failures

    • Error: "Failed to generate embeddings"
    • Solution:
      • Verify your OpenAI API key is valid
      • Check OpenAI service status
      • Ensure text content is within token limits
  2. Batch Processing Errors

    • Error: "Batch ingestion failed"
    • Solution:
      • Reduce batch size
      • Check for malformed documents
      • Verify network connectivity

Search Issues

  1. No Results Returned

    • Possible causes:
      • Vector index still building (check status)
      • Query vector dimensions don't match
      • Similarity score threshold too high
    • Solution: Monitor index status and verify query parameters
  2. Performance Issues

    • If searches are slow:
      • Verify index is built and active
      • Check your numCandidates setting
      • Consider adjusting maxResults

Next Steps

To enhance your chatbot, consider:

  • Adding authentication for admin functions
  • Implementing conversation history
  • Adding support for file uploads
  • Customizing the chunking strategy for your documents
  • Setting up automated document ingestion

Resources

Final Thoughts

In just about 10 minutes, we've built a professional, full-stack helpdesk chatbot that leverages the power of MongoDB Atlas Vector Search and RAG architecture. The mongodb-rag library and CLI tools make it incredibly easy to get started, while providing the flexibility to customize and enhance your application as needed.

I hope you enjoyed this... if you have ideas for enhancements to the mongodb-rag NPM library, please reach out - you can find me on linkedIn!


Have you built something similar? Share your experience in the comments below! And don't forget to follow for more tutorials on MongoDB, AI, and web development.

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

Top comments (1)

Collapse
 
ai_joddd profile image
Vinayak Mishra

MongoDB is great to build RAG, nice content Michael! I came across one more blog on Build a RAG application using MongoDB and Maxim AI - it's nice, you can give a read.

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay