Build a Flight search chatbot from scratch using RASA-PART 2

In this section, I will be writing about the dialogue management model (i.e. handling dialogues-predicting next action upon user Input)

Adding Dialogue capabilities

Step 1: Create Domain file(.yml)

Under MMT/config create a file “domain.yml”.

The ‘Domain’ defines the universe in which your bot operates. It specifies the intents, entities, slots, and actions your bot should know about. It also includes templates for the things your bot can say.

We get the intents and entities from the NLU model. Slots are the things we want to keep track of during a conversation. actions are the things your bot can actually do(such as responding to user, making an external API call, querying a database, setting up slots and many other things). Utterance templates(utter_) are messages the bot will send back to the user. utter_unclear triggers when there is a fallback condition which we will discuss in training the core section. Curly braces {} fetch slot values to be used in bot utterances.

step 2: Create Action file(

Actions are the things that the bot runs in response to user input. There are some default actions like action_restart,action_default_fallback, action_listen. But here we are using custom actions to save slots, connect to MMT website and get flight status.

pip install rasa_core_sdk

  • the dispatcher is used to send messages back to the user
  • The tracker stores and maintains the state of the dialogue with a user
  • Domain is the bot’s domain

__future__ helps in getting accustomed to incompatible changes or to such ones introducing new keywords. (for e.g. print(8/7) # prints 1.1428571428571428
print(8//7) # prints 1
). With from __future__ import division both stuff will print 1

Logging helps us develop a better understanding of the flow of a program. Logs provide developers with an extra set of eyes that are constantly looking at the flow that an application is going through. They can store information, like which user or IP accessed the application. If an error occurs, then they can provide more insights than a stack trace by telling you what the state of the program was before it arrived at the line of code where the error occurred

We need Flight details from the MMT website, so , I am using BeautifulSoup to do web-scraping. ( pip install beautifulsoup4 )

Important-Add these newly created actions in the domain file with their respective names.

Step 3- Create Stories ( file)

A training example for the Rasa Core dialogue system is called a story. Stories tell the model what are the possible flows of conversational dialog.

Step 4- Train the Core Model

Rasa Core works by creating training data from the stories and training a model on that data.

Fallback- Sometimes you want to fall back to a fallback action like saying “Sorry, I didn’t understand that”. To do this, add the FallbackPolicy to your policy ensemble. The fallback action will be executed if the intent recognition has a confidence below nlu_threshold or if none of the dialogue policies predict an action with confidence higher than core_threshold.

Agent- The agent allows you to train a model, load, and use it. It is a simple API that lets you access most of Rasa Core’s functionality. (training, handling messages, loading a dialogue model, getting the next action, and handling a channel)

augmentation_factor- Rasa Core will create longer stories by randomly glueing together the ones in your stories file. Default augmentation factor is 50. We can change this by passing augementation_factor argument to Agent.load_data method.

Memoization policies- It memorizes the conversations in your training data. It predicts the next action with confidence 1.0 if this exact conversation exists in the training data, otherwise it predicts None with confidence 0.0.

Keras policy- It is a Recurrent Neural Network (LSTM) that takes in a bunch of features to predict the next possible action. Features are the vector representation of intents, entities, slots and previous actions which are fed to the neural networks that predicts the next action. So, keras policy determines the next action if the flow of conversation is different from what mentioned in the story. (vectors are created using LabelTokenizerSingleStateFeaturizer and BinarySingleStateFeaturizer which implements Tokenizing and One Hot Encoding respectively).

max_history- This controls how much dialogue history the model looks at to decide which action to take next. The default value is 5 and minimum value is 3. More the max_history value, training will take longer. For e.g. max_history of 10 in memoization policy will look 10 steps back in the tracker to determine next step. In keras policy, if we increase max_history, it will increase the features vector.

persist- Once you have trained your model, it is then persisted to a file system or a cloud storage of your choice

epochs- Epochs basically refer to how long you should train the model for to get better accuracy. 400 epochs means the model looks at the data 400 times

batch_size- batch size is how many training examples the network gets trained on at a time. batch size of 50 means the model sees 50 examples at the same time.

validation_split- Validation split is the percentage (so between 0 and 1, including both ends) of examples to put aside for testing the model. The percentage of examples put aside will not be used for training. Instead, once in a while during the training we evaluate the performance of the model on these examples.

Step 5- Run the bot

The NLU model and core model is loaded in Agent through which we pass queries and get a response.

Run this file and chat with the bot.

Step 6- Create more stories with interactive learning

In interactive learning mode, you provide feedback to your bot while you talk to it. This is a powerful way to explore what your bot can do, and the easiest way to fix any mistakes it makes. One advantage of machine learning-based dialogue is that when your bot doesn’t know how to do something yet, you can just teach it!

python -m rasa_core.train — online -d config/domain.yml -s data/ -o models/dialogue -u models/nlu/default/chatter — epochs 250 — endpoints endpoints.yml

I will be discussing the advance sections like saving conversation, servers, web channels in the upcoming parts.


Github repo for the project