This is simple chatbot application with Natural Language Understanding to maintain very simple shopping list. It's built using RASA NLU Python library with Spacy NLP backend. The bot which we will create assumes that anything our bot's users say can be categorized into one of the following intents:
- Greetings (greet)
- Adding item to the shopping list (add_item)
- Removing all items from shopping list (clear_list)
- Displaying shopping list (show_items)
- The debug command to return number of items in the list (_num_items)
As it was already mentined we are going to use RASA NLU and Spacy NLP Python libraries which should be installed.
To install current stable version of RASA NLU just run
pip install rasa_nlu
To install current version of Spacy NLP execute
pip install -U spacy
We will use English language Spacy model which can be installed as following:
python -m spacy download en
Also please make sure that sklearn_crfsuite installed on the system or install it:
pip install sklearn_crfsuite
To build NLU model able to parse user's intents first we need to create appropriate training data which will allow our chatbot to interpret user's inputs and created structured data (intent/entities). The best way to get training texts is from real users, and the best way to get the structured data is to pretend to be the bot yourself. For the purpose of this experiment we will create small training data corpus in format described at: https://rasa-nlu.readthedocs.io/en/stable/dataformat.html
The best way to create training data in rasa's format and to validate it is to use great online tool created by @azazdeaz.
The resulting training data file in JSON format can be found in the data folder.
There are two modes to use Rasa NLU models - as a HTTP server or directly from Python. In this experiment taking into account small size of our training data we will train and use NLU model directly from Python.
To do this first we need to create corresponding Rasa NLU configuration file. We will use Spacy NLP as backend thus configuration file will be:
{
"pipeline": "spacy_sklearn",
"path" : "./projects",
"data" : "./data/shopping-list/rasa/shopping-list-small.json"
}
For more details as how to run Rasa NLU models directly from Python refer to: https://rasa-nlu.readthedocs.io/en/stable/python.html
The training data includes extremelly limited knowledge of grocery items to be purchased - eggs, milk, and butter. But it can be easy extended by modifying training data file mentioned above.
The chatbot can be used as following:
In [1]: from shopping_bot import ShoppingBot
In [2]: bot = ShoppingBot()
In [3]: bot.handle("Hello")
How are you!
In [4]: bot.handle("add milk")
Shopping list items:
milk - quantity: 1
In [5]: bot.handle("i need eggs")
Shopping list items:
milk - quantity: 1
eggs - quantity: 1
In [6]: bot.handle("i need eggs and milk")
Shopping list items:
milk - quantity: 2
eggs - quantity: 2
In [7]: bot.handle("my list")
Shopping list items:
milk - quantity: 2
eggs - quantity: 2
In [8]: bot.handle("_num_items")
# of unique items: 2, total # of items: 4
In [9]: bot.handle("clear list")
Items removed from your list!
Your shopping list is empty!
In [10]: bot.handle("_num_items")
shopping list is empty
# of unique items: 0, total # of items: 0
The supported grocery items:
- eggs
- milk
- butter