Skip to content

Commit

Permalink
chatbot_api
Browse files Browse the repository at this point in the history
  • Loading branch information
SatwikMohan committed Jun 5, 2024
1 parent 45ef92f commit da54a33
Show file tree
Hide file tree
Showing 5 changed files with 3,795 additions and 0 deletions.
57 changes: 57 additions & 0 deletions New_APIs/ChatBot_API/README.md
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
55 changes: 55 additions & 0 deletions New_APIs/ChatBot_API/index.mjs
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}`);
});
Loading

0 comments on commit da54a33

Please sign in to comment.