-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
45ef92f
commit da54a33
Showing
5 changed files
with
3,795 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# ChatBot API | ||
|
||
## Installation | ||
|
||
To use this program, you need to have Node.js installed. Then, install the required `express`,`fs` and `natural` libraries by running: | ||
|
||
```sh | ||
npm i express fs natural | ||
``` | ||
|
||
## Method to use the API | ||
|
||
1. Run the index.mjs file using the following command: | ||
```sh | ||
node index.mjs | ||
``` | ||
|
||
OR | ||
|
||
```sh | ||
nodemon index.mjs | ||
``` | ||
|
||
2. Wait for a few seconds as the model get trained. | ||
|
||
3. When the server starts running you can use the ChatBot service. | ||
|
||
4. Endpoint - /chat | ||
|
||
POST - http://localhost:3000/chat | ||
|
||
Databody => | ||
|
||
```bash | ||
|
||
{ | ||
"message":"Enter a message or chat for the ChatBot to answer" | ||
} | ||
|
||
``` | ||
|
||
5. Example: | ||
|
||
```bash | ||
{ | ||
"message":"What's going in life" | ||
} | ||
|
||
{ | ||
"response": "Things have been interesting! How about you?" | ||
} | ||
|
||
``` | ||
|
||
6. By adding more tags, patterns and responses the ChatBot becomes more and more advance and will be able to answer more and more complex messages. | ||
|
||
Contributed by - Satwik Mohan |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import express, { json } from 'express'; | ||
import { readFileSync } from 'fs'; | ||
import pkg from 'natural'; | ||
const { WordTokenizer, BayesClassifier } = pkg; | ||
const app = express(); | ||
app.use(express.json()); | ||
const PORT = process.env.PORT || 3000; | ||
|
||
console.log("Model Training begin"); | ||
|
||
// Load intents | ||
const intents = JSON.parse(readFileSync('intents.json')); | ||
|
||
// Tokenizer and classifier | ||
const tokenizer = new WordTokenizer(); | ||
const classifier = new BayesClassifier(); | ||
|
||
// Train the classifier with the dataset | ||
intents.intents.forEach(intent => { | ||
intent.patterns.forEach(pattern => { | ||
classifier.addDocument(pattern, intent.tag); | ||
}); | ||
}); | ||
|
||
classifier.train(); | ||
|
||
// Function to get a random response | ||
function getRandomResponse(responses) { | ||
const randomIndex = Math.floor(Math.random() * responses.length); | ||
return responses[randomIndex]; | ||
} | ||
|
||
app.post('/chat', (req, res) => { | ||
const { message } = req.body; | ||
if (!message) { | ||
return res.status(400).json({ error: 'Message is required' }); | ||
} | ||
|
||
const tokenizedMessage = tokenizer.tokenize(message.toLowerCase()); | ||
const classification = classifier.classify(tokenizedMessage.join(' ')); | ||
|
||
const intent = intents.intents.find(intent => intent.tag === classification); | ||
|
||
if (intent) { | ||
const response = getRandomResponse(intent.responses); | ||
res.json({ response }); | ||
} else { | ||
res.json({ response: "I'm sorry, I don't understand that." }); | ||
} | ||
}); | ||
|
||
app.listen(PORT, () => { | ||
console.log("Model Trained"); | ||
console.log(`Server is running on port ${PORT}`); | ||
}); |
Oops, something went wrong.