A RESTful API for a rule-based chatbot, built with Flask and MongoDB. Users can sign up, log in, send messages, and retrieve chat history. The chatbot responds based on predefined rules stored in rules.json
, with conversation data stored in MongoDB. This project showcases backend development, database integration, and introductory NLP skills.
- User authentication (signup, login) with password hashing (bcrypt).
- Rule-based chatbot with predefined responses (e.g., greetings, FAQs).
- CRUD operations for chat history (create, read).
- MongoDB storage using PyMongo.
- Python 3.x
- MongoDB (local or MongoDB Atlas)
- Flask, PyMongo, bcrypt (listed in
requirements.txt
)
-
Clone the Repository:
git clone https://github.com/Opikadash/chatbot-api.git cd chatbot-api
-
Set Up Virtual Environment:
python -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate
-
Install Dependencies:
pip install -r requirements.txt
-
Configure MongoDB:
- Update the MongoDB URI in
app.py
(line 10) with your URI. - Ensure MongoDB is running.
- Update the MongoDB URI in
-
Run the Application:
python app.py
The API will be available at
http://localhost:5001
.
- POST /signup: Create a new user.
- Request:
{"username": "chatuser", "password": "chatpass"}
- Response:
{"message": "User created successfully"}
- Request:
- POST /login: Authenticate a user.
- Request:
{"username": "chatuser", "password": "chatpass"}
- Response:
{"message": "Login successful", "username": "chatuser"}
- Request:
- POST /chat/: Send a message and get a response.
- Request:
{"message": "hello"}
- Response:
{"message": "hello", "response": "Hello! How can I assist you today?"}
- Request:
- GET /chat/: Retrieve recent chat history.
- Response: List of chats (e.g.,
[{"message": "hello", "response": "...", "timestamp": "..."}]
).
- Response: List of chats (e.g.,
Test the endpoints using Postman, curl
(on Unix-like systems), or PowerShell.
- Signup:
curl -X POST http://localhost:5001/signup -H "Content-Type: application/json" -d '{"username": "chatuser", "password": "chatpass"}'
- Login:
curl -X POST http://localhost:5001/login -H "Content-Type: application/json" -d '{"username": "chatuser", "password": "chatpass"}'
- Send Message:
curl -X POST http://localhost:5001/chat/chatuser -H "Content-Type: application/json" -d '{"message": "hello"}'
- Get Chat History:
curl http://localhost:5001/chat/chatuser
- Signup:
Invoke-WebRequest -Uri http://localhost:5001/signup -Method POST -Headers @{"Content-Type"="application/json"} -Body '{"username": "chatuser", "password": "chatpass"}'
- Login:
Invoke-WebRequest -Uri http://localhost:5001/login -Method POST -Headers @{"Content-Type"="application/json"} -Body '{"username": "chatuser", "password": "chatpass"}'
- Send Message:
Invoke-WebRequest -Uri http://localhost:5001/chat/chatuser -Method POST -Headers @{"Content-Type"="application/json"} -Body '{"message": "hello"}'
- Get Chat History:
Invoke-WebRequest -Uri http://localhost:5001/chat/chatuser -Method GET
- User signup/login with valid/invalid credentials.
- Chatbot responses for defined rules and unknown inputs.
- Chat history retrieval.
A RESTful API for an advanced chatbot, built with Flask and MongoDB, featuring intent recognition using an LSTM model (TensorFlow/Keras) and dynamic response generation via RAG (Retrieval-Augmented Generation). Users can sign up, log in, send messages, and retrieve chat history. The LSTM model classifies user intents (e.g., greeting, help, question), while the RAG system retrieves relevant answers from a MongoDB knowledge base using sentence embeddings. This project demonstrates backend development, advanced NLP with LSTM, and RAG implementation, making it highly relevant for AI/ML and Backend Intern roles at Pepsales.
- User authentication (signup, login) with password hashing (bcrypt).
- Intent recognition using an LSTM model (TensorFlow/Keras) for advanced NLP, improving accuracy over traditional rule-based systems.
- Dynamic response generation with RAG, leveraging sentence embeddings (
sentence-transformers
) to retrieve relevant answers from a MongoDB knowledge base. - CRUD operations for chat history (create, read), stored in MongoDB.
- Backend logic implemented with Flask and PyMongo for seamless API and database integration.
- Python 3.x
- MongoDB (local or MongoDB Atlas)
- Flask, PyMongo, bcrypt, TensorFlow, sentence-transformers (listed in
requirements.txt
)
-
Clone the Repository:
git clone https://github.com/Opikadash/chatbot-api.git cd chatbot-api
-
Set Up Virtual Environment:
python -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate
-
Install Dependencies:
pip install -r requirements.txt
Download NLTK data:
import nltk nltk.download('punkt')
-
Train the LSTM Model:
python train_lstm_model.py
This generates
lstm_intent_model.h5
,tokenizer.pkl
,label_encoder.pkl
, andresponses.pkl
. -
Set Up Knowledge Base for RAG:
python setup_knowledge_base.py
This populates the
knowledge_base
collection in MongoDB with question-answer pairs and their embeddings. -
Configure MongoDB:
- Update the MongoDB URI in
app.py
(line 12) with your URI (e.g.,mongodb://localhost:27017/
for local or your MongoDB Atlas URI). - Ensure MongoDB is running.
- Update the MongoDB URI in
-
Run the Application:
python app.py
The API will be available at
http://localhost:5001
.
- POST /signup: Create a new user.
- Request:
{"username": "chatuser", "password": "chatpass"}
- Response:
{"message": "User created successfully"}
- Request:
- POST /login: Authenticate a user.
- Request:
{"username": "chatuser", "password": "chatpass"}
- Response:
{"message": "Login successful", "username": "chatuser"}
- Request:
- POST /chat/: Send a message and get a response.
- Request:
{"message": "what is ai"}
- Response:
{"message": "what is ai", "response": "AI, or artificial intelligence, refers to..."}
- Request:
- GET /chat/: Retrieve recent chat history.
- Response: List of chats (e.g.,
[{"message": "what is ai", "response": "...", "timestamp": "..."}]
).
- Response: List of chats (e.g.,
Test the endpoints using Postman, curl
(on Unix-like systems), or PowerShell.
- Signup:
curl -X POST http://localhost:5001/signup -H "Content-Type: application/json" -d '{"username": "chatuser", "password": "chatpass"}'
- Login:
curl -X POST http://localhost:5001/login -H "Content-Type: application/json" -d '{"username": "chatuser", "password": "chatpass"}'
- Send Message:
curl -X POST http://localhost:5001/chat/chatuser -H "Content-Type: application/json" -d '{"message": "what is ai"}'
- Get Chat History:
curl http://localhost:5001/chat/chatuser
- Signup:
Invoke-WebRequest -Uri http://localhost:5001/signup -Method POST -Headers @{"Content-Type"="application/json"} -Body '{"username": "chatuser", "password": "chatpass"}'
- Login:
Invoke-WebRequest -Uri http://localhost:5001/login -Method POST -Headers @{"Content-Type"="application/json"} -Body '{"username": "chatuser", "password": "chatpass"}'
- Send Message:
Invoke-WebRequest -Uri http://localhost:5001/chat/chatuser -Method POST -Headers @{"Content-Type"="application/json"} -Body '{"message": "what is ai"}'
- Get Chat History:
Invoke-WebRequest -Uri http://localhost:5001/chat/chatuser -Method GET
- User signup/login with valid/invalid credentials.
- Intent recognition with LSTM for greetings, help requests, goodbyes, and questions.
- RAG-based dynamic responses for questions (e.g., "What is AI?"), retrieved from the MongoDB knowledge base.
- Chat history retrieval with MongoDB, ensuring persistence and retrieval of conversations.
- Expand the knowledge base with more question-answer pairs to improve RAG coverage.
- Fine-tune a generative model (e.g., GPT) for RAG generation, enabling more natural and context-aware responses.
- Enhance security with JWT authentication for production readiness.
- Use a larger dataset for LSTM training to improve intent recognition accuracy. 18325b).