@channarith.kin/chatbot-js
v1.0.2
Published
A simple NLP chatbot engine featuring:
Readme
Chatbot-js
A simple NLP chatbot engine featuring:
- 📊 Dataset management
- 🤖 Model prediction
- 🗄️ MongoDB storage
📦 Installation
npm install @channarith.kin/chatbot-js🚀 Usage
const mongoose = require("mongoose");
const chatbot = require("@channarith.kin/chatbot-js");
// 🔌 **Connect to MongoDB**
await mongoose.connect(YOUR_MONGO_CONNECTION_STRING);
// ⚙️ **Initialize chatbot engine**
const engine = chatbot(mongoose, {
modelPath: "ai", // 📁 Directory where the model will be generated
threshold: 0.05, // 🎯 Confidence threshold (default: 0.04)
fallbackMessages: [
"Sorry, I am not able to answer that yet. Could you please rephrase?",
"Oops! I don't understand this yet. Please help rephrase?",
], // 💬 Messages used when confidence is too low
});
// 🌐 **Expose API routes for dataset management**
app.use("/api/chat-bot", engine.routes); // Not recommend to expose to production, In development environment only.
// **service to invoke internally
const { predictionService } = engine;
response = await predictionService.chat("hi");
**console.log(response);**
// {
// reply: 'Hello!',
// intent: 'greeting',
// confidence: [
// { label: 'greeting', value: 0.07499999999999998 },
// { label: 'weather', value: 0.025 },
// { label: 'joke', value: 0.025 },
// { label: 'thanks', value: 0.024999999999999998 },
// { label: 'goodbye', value: 0.024999999999999994 }
// ]
// }Inside engine.routes
router.use("/dataset", datasetRoute(services.dataset)); // 📊 Manage training dataset
router.use("/train", trainRoute(services.train)); // 🧠 Train the chatbot model
router.use("/chat", chatRoute(services.prediction)); // 💬 Chat / get predictions📊 Dataset API
Manage your chatbot training data.
🔗 Endpoint
/api/chat-bot/dataset
📥 GET /dataset
Get all dataset entries
// ✅ Response
{
"success": true,
"message": "Success",
"data": [
{
"_id": "69c542dfaa476ac3e2519987",
"intent": "greeting",
"questions": [
"hi",
"hello",
"hey",
"good morning",
"good afternoon",
"good evening",
"howdy",
"hi there",
"hello there",
"hey there"
],
"answers": ["Hello!", "Hi there!", "Hey! How can I help you?"],
"createdAt": "2026-03-26T14:29:51.467Z",
"updatedAt": "2026-03-26T14:29:51.467Z",
"__v": 0
}
]
}➕ POST /dataset
Create a new dataset entry
// 📤 Request Body
{
"intent": "joke",
"questions": [
"tell me a joke",
"make me laugh",
"do you know a joke?",
"I want to hear a joke",
"can you tell me something funny?",
"funny joke please",
"say something funny",
"make me giggle",
"give me a joke",
"joke time"
],
"answers": [
"Why did the computer go to the doctor? Because it caught a virus!",
"Why don’t scientists trust atoms? Because they make up everything!",
"I would tell you a joke about NaN, but you wouldn’t understand."
]
}
// ✅ Response
{
"success": true,
"message": "Created successfully",
"data": {
"_id": "69c54329aa476ac3e2519992",
"intent": "weather",
"questions": [
"what's the weather today?",
"how is the weather today?",
"tell me the weather",
"is it raining today?",
"will it rain today?",
"do I need an umbrella?",
"is it sunny today?",
"what is the temperature today?",
"is it hot outside?",
"is it cold outside?",
"what's the forecast for today?",
"do I need a jacket today?",
"will it snow today?",
"is it windy today?",
"what's the weather like right now?",
"is the weather nice today?",
"how's the weather outside?"
],
"answers": [
"I can't check live weather, but you can try a weather app.",
"Looks like you’ll need to check a weather website!",
"I’m not sure about the weather today, try checking an online forecast.",
"I don’t have live weather info, but it’s always good to carry an umbrella just in case!"
],
"createdAt": "2026-03-26T14:31:05.307Z",
"updatedAt": "2026-03-28T09:08:38.731Z",
"__v": 0
}
}✏️ PUT /dataset/:intent
Update an existing dataset entry by intent
// 📤 Request Body
{
"intent": "joke",
"questions": [
"tell me a joke",
"make me laugh",
"do you know a joke?",
"I want to hear a joke",
"can you tell me something funny?",
"funny joke please",
"say something funny",
"make me giggle",
"give me a joke",
"joke time"
],
"answers": [
"Why did the computer go to the doctor? Because it caught a virus!",
"Why don’t scientists trust atoms? Because they make up everything!",
"I would tell you a joke about NaN, but you wouldn’t understand."
]
}
// ✅ Response
{
"success": true,
"message": "Updated successfully",
"data": {
"_id": "69c54329aa476ac3e2519992",
"intent": "weather",
"questions": [
"what's the weather today?",
"how is the weather today?",
"tell me the weather",
"is it raining today?",
"will it rain today?",
"do I need an umbrella?",
"is it sunny today?",
"what is the temperature today?",
"is it hot outside?",
"is it cold outside?",
"what's the forecast for today?",
"do I need a jacket today?",
"will it snow today?",
"is it windy today?",
"what's the weather like right now?",
"is the weather nice today?",
"how's the weather outside?"
],
"answers": [
"I can't check live weather, but you can try a weather app.",
"Looks like you’ll need to check a weather website!",
"I’m not sure about the weather today, try checking an online forecast.",
"I don’t have live weather info, but it’s always good to carry an umbrella just in case!"
],
"createdAt": "2026-03-26T14:31:05.307Z",
"updatedAt": "2026-03-28T09:08:38.731Z",
"__v": 0
}
}❌ DELETE /dataset/:intent
Delete a dataset entry by intent
// ✅ Response
{
"success": true,
"message": "Deleted successfully"
}🧠 Train API
Manage your chatbot training data.
➕ POST /train
Invoke to train function and create new model based on config modelPath
// ✅ Response
{
"success": true,
"message": "Model trained successfully",
"data": {
"message": "Model trained successfully",
"accuracy": 82.35,
"totalData": 57,
"trainSize": 40,
"testSize": 17,
"correct": 14
}
}💬 Chat API
Manage your chatbot training data.
➕ POST /chat
For testing the chat, for developer only
// 📤 Request Body
{
"message": "what is the weather today?"
}
// ✅ Response
{
"success": true,
"message": "Success",
"data": {
"reply": "I’m not sure about the weather today, try checking an online forecast.",
"intent": "weather"
}
}