Simple chatbot implementation with PyTorch.
- The implementation should be easy to follow for beginners and provide a basic understanding of chatbots.
- The implementation is straightforward with a Feed Forward Neural net with 2 hidden layers.
- Customization for your own use case is super easy. Just modify intents.jsonwith possible patterns and responses and re-run the training (see below for more info).
The approach is inspired by this article and ported to PyTorch: https://chatbotsmagazine.com/contextual-chat-bots-with-tensorflow-4391749d0077.
Whatever you prefer (e.g. conda or venv)
mkdir myproject
$ cd myproject
$ python3 -m venv venvMac / Linux:
. venv/bin/activateWindows:
venv\Scripts\activateFor Installation of PyTorch see official website.
You also need nltk:
pip install nltkIf you get an error during the first run, you also need to install nltk.tokenize.punkt:
Run this once in your terminal:
$ python
>>> import nltk
>>> nltk.download('punkt')Run
python train.pyThis will dump data.pth file. And then run
python chat.pyHave a look at intents.json. You can customize it according to your own use case. Just define a new tag, possible patterns, and possible responses for the chat bot. You have to re-run the training whenever this file is modified.
{
  "intents": [
    {
      "tag": "greeting",
      "patterns": [
        "Hi",
        "Hey",
        "How are you",
        "Is anyone there?",
        "Hello",
        "Good day"
      ],
      "responses": [
        "Hey :-)",
        "Hello, thanks for visiting",
        "Hi there, what can I do for you?",
        "Hi there, how can I help?"
      ]
    },
    ...
  ]
}