A Node.js + Express proof of concept that demonstrates Retrieval Augmented Generation (RAG) using MongoDB as a data source and OpenAI's GPT models for language model capabilities.
- Chat Web Interface: Beautiful, responsive chat UI for easy interaction
- RESTful API endpoint
/askfor question-answering - MongoDB integration using Mongoose ODM
- OpenAI integration using GPT-4o-mini model
- Environment-based configuration
- Graceful error handling and shutdown
- Health check endpoint
- Node.js 14.x or higher
- MongoDB instance (local or cloud)
- OpenAI API key
-
Clone the repository (or create the project directory):
cd /home/debojyoti/projects/personal/langchain-rag -
Install dependencies:
npm install
-
Set up OpenAI API Key:
- Sign up for an OpenAI account at https://platform.openai.com/
- Create an API key in your OpenAI dashboard
- Keep your API key secure and never commit it to version control
-
Configure environment variables:
Create a
.envfile in the project root with the following variables:# OpenAI Configuration OPENAI_API_KEY=your-openai-api-key # MongoDB Configuration MONGODB_URI=mongodb://localhost:27017 # or for MongoDB Atlas: mongodb+srv://username:password@cluster.mongodb.net/ MONGODB_DB_NAME=your-database-name MONGODB_COLLECTION_NAME=your-collection-name # Server Configuration (optional) PORT=3000
| Variable | Description | Required | Example |
|---|---|---|---|
OPENAI_API_KEY |
Your OpenAI API key | Yes | sk-... |
MONGODB_URI |
MongoDB connection string | Yes | mongodb://localhost:27017 |
MONGODB_DB_NAME |
Target database name | Yes | myDatabase |
MONGODB_COLLECTION_NAME |
Target collection name | Yes | documents |
PORT |
Server port (default: 3000) | No | 3000 |
npm run devnpm startThe server will start on the configured port (default: 3000) and connect to MongoDB automatically using Mongoose.
Open your browser and navigate to:
http://localhost:3000
This provides a beautiful, modern chat interface where you can:
- Ask questions about your MongoDB documents
- See real-time typing indicators
- View conversation history
- Get source information for each response
- Use on both desktop and mobile devices
GET /healthReturns:
{
"status": "ok",
"timestamp": "2024-01-10T12:00:00.000Z",
"database": "connected"
}POST /ask
Content-Type: application/json
{
"question": "What is the main topic of the documents?"
}Returns:
{
"answer": "Based on the documents in the collection...",
"source": "MongoDB collection: your-collection-name",
"documentsCount": 5
}curl -X POST http://localhost:3000/ask \
-H "Content-Type: application/json" \
-d '{
"question": "What information is available about product features?"
}'- Create a new POST request
- URL:
http://localhost:3000/ask - Headers:
Content-Type: application/json - Body (raw JSON):
{ "question": "Summarize the main points from the documents" }
const response = await fetch('http://localhost:3000/ask', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
question: 'What are the key insights from the data?'
})
});
const data = await response.json();
console.log(data.answer);-
Document Retrieval: When a question is received, the application connects to MongoDB using Mongoose and fetches all documents from the specified collection.
-
Context Building: Documents are processed to extract relevant fields (title, description, content, etc.) and combined into a single context blob.
-
LLM Processing: The question and context are sent to OpenAI's GPT-4o-mini model for processing.
-
Response Generation: The LLM's answer is returned along with the source information and document count.
The application handles various error scenarios:
- Missing or invalid request body
- MongoDB connection failures
- OpenAI API errors (authentication, rate limits, etc.)
- Missing environment variables
All errors return appropriate HTTP status codes and descriptive error messages.
The application uses a flexible Mongoose schema that accepts any document structure. It looks for these common fields in documents:
titlenamedescriptioncontenttextsummary
If none of these fields exist, the entire document is stringified and used as context (excluding MongoDB's _id and __v fields).
The application uses the gpt-4o-mini model, which provides:
- Cost-effective processing
- Good performance for question-answering tasks
- Support for large context windows
- Fast response times
- Verify your
MONGODB_URIis correct - Ensure MongoDB is running and accessible
- Check network/firewall settings
- Verify database and collection names
- Verify your
OPENAI_API_KEYis correct and active - Check your OpenAI account has sufficient credits
- Monitor rate limits (the app handles 429 errors gracefully)
- Ensure your API key has the necessary permissions
- Verify the database and collection names
- Ensure the collection contains documents
- Check MongoDB connection permissions
ISC