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
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
- Go to MongoDB Atlas and sign in
- Click "Create" to deploy a new cluster
- Select the FREE (M0) tier
- Choose your preferred cloud provider and region
- Click "Create Cluster" and wait for deployment
2. Configure Database Access
- Go to "Database Access" under the security tab
- Click "Add New Database User"
- Choose Password Authentication
- Set a secure username and password
- Assign "Read and write to any database" permissions
3. Set Network Access
- Go to "Network Access" and click "Add IP Address"
- Select "Allow Access from Anywhere" (for development)
- Confirm and wait for the change to propagate
4. Get Your Connection String
- Click "Connect" on your cluster
- Choose "Connect your application"
- Copy the connection string
- Replace
<password>
with your actual password
Step 2: Configure Your Environment
First, initialize your mongodb-rag configuration:
npx mongodb-rag init
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"
}
Then convert it to environment variables:
npx mongodb-rag create-env
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
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
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
- Navigate to your cluster in Atlas
- Click "Browse Collections"
- Click the "Search" tab
- Click "Create Search Index"
- Choose "JSON Editor" and select "Atlas Vector Search"
- Use this configuration:
{
"name": "vector_index",
"type": "vectorSearch",
"definition": {
"fields": [{
"type": "vector",
"path": "embedding",
"numDimensions": 1536,
"similarity": "cosine"
}]
}
}
Step 4: Deploy Your Helpdesk Chatbot
Generate your application:
npx mongodb-rag create-rag-app my-helpdesk
cd my-helpdesk
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
Install dependencies and start the servers:
npm install
npm run dev
This will start:
- Frontend at http://localhost:3000
- Backend API at http://localhost:4001
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
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
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
Ingest the Documents
Use the CLI to ingest all documents:
npx mongodb-rag ingest --directory ./helpdesk-docs
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' }
}
]
})
});
🔍 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"
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
-
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
- Missing
-
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)
-
Permission Issues
- Error: "Insufficient permissions to create index"
- Solution: Ensure you have Project Data Access Admin or higher role
Ingestion Issues
-
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
-
Batch Processing Errors
- Error: "Batch ingestion failed"
- Solution:
- Reduce batch size
- Check for malformed documents
- Verify network connectivity
Search Issues
-
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
- Possible causes:
-
Performance Issues
- If searches are slow:
- Verify index is built and active
- Check your
numCandidates
setting - Consider adjusting
maxResults
- If searches are slow:
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.
Top comments (1)
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.