Thanks! We'll be in touch in the next 12 hours
Oops! Something went wrong while submitting the form.

Your Complete Guide to Building Stateless Bots Using Rasa Stack

Priyanka Verma

Artificial Intelligence / Machine Learning

This blog aims at exploring the Rasa Stack to create a stateless chat-bot. We will look into how, the recently released Rasa Core, which provides machine learning based dialogue management, helps in maintaining the context of conversations using machine learning in an efficient way.

If you have developed chatbots, you would know how hopelessly bots fail in maintaining the context once complex use-cases need to be developed. There are some home-grown approaches that people currently use to build stateful bots. The most naive approach is to create the state machines where you create different states and based on some logic take actions. As the number of states increases, more levels of nested logic are required or there is a need to add an extra state to the state machine, with another set of rules for how to get in and out of that state. Both of these approaches lead to fragile code that is harder to maintain and update. Anyone who’s built and debugged a moderately complex bot knows this pain.

After building many chatbots, we have experienced that flowcharts are useful for doing the initial design of a bot and describing a few of the known conversation paths, but we shouldn’t hard-code a bunch of rules since this approach doesn’t scale beyond simple conversations.

Thanks to the Rasa guys who provided a way to go stateless where scaling is not at all a problem. Let's build a bot using Rasa Core and learn more about this.

Rasa Core: Getting Rid of State Machines

The main idea behind Rasa Core is that thinking of conversations as a flowchart and implementing them as a state machine doesn’t scale. It’s very hard to reason about all possible conversations explicitly, but it’s very easy to tell, mid-conversation, if a response is right or wrong. For example, let’s consider a term insurance purchase bot, where you have defined different states to take different actions. Below diagram shows an example state machine:

State Machine Example

Let’s consider a sample conversation where a user wants to compare two policies listed by policy_search state.

In above conversation, it can be compared very easily by adding some logic around the intent campare_policies. But real life is not so easy, as a majority of conversations are edge cases. We need to add rules manually to handle such cases, and after testing we realize that these clash with other rules we wrote earlier.

Rasa guys figured out how machine learning can be used to solve this problem. They have released Rasa Core where the logic of the bot is based on a probabilistic model trained on real conversations.

Structure of a Rasa Core App

Let’s understand few terminologies we need to know to build a Rasa Core app:

1. Interpreter: An interpreter is responsible for parsing messages. It performs the Natural Language Understanding and transforms the message into structured output i.e. intent and entities. In this blog, we are using Rasa NLU model as an interpreter. Rasa NLU comes under the Rasa Stack. In Training section, it is shown in detail how to prepare the training data and create a model.

2. Domain: To define a domain we create a domain.yml file, which defines the universe of your bot. Following things need to be defined in a domain file:

  • Intents: Things we expect the user to say. It is more related to Rasa NLU.
  • Entities: These represent pieces of information extracted what user said. It is also related to Rasa NLU.
  • Templates: We define some template strings which our bot can say. The format for defining a template string is utter_<intent>. These are considered as actions which bot can take.
  • Actions: List of things bot can do and say. There are two types of actions we define one those which will only utter message (Templates) and others some customised actions where some required logic is defined. Customised actions are defined as Python classes and are referenced in domain file.
  • Slots: These are user-defined variables which need to be tracked in a conversation. For e.g to buy a term insurance we need to keep track of what policy user selects and details of the user, so all these details will come under slots.

3. Stories: In stories, we define what bot needs to do at what point in time. Based on these stories, a probabilistic model is generated which is used to decide which action to be taken next. There are two ways in which stories can be created which are explained in next section.

Let’s combine all these pieces together. When a message arrives in a Rasa Core app initially, interpreter transforms the message into structured output i.e. intents and entities. The Tracker is the object which keeps track of conversation state. It receives the info that a new message has come in. Then based on dialog model we generate using domain and stories policy chooses which action to take next. The chosen action is logged by the tracker and response is sent back to the user.

Training and Running A Sample Bot

We will create a simple Facebook chat-bot named Secure Life which assists you in buying term life insurance. To keep the example simple, we have restricted options such as age-group, term insurance amount, etc.

There are two models we need to train in the Rasa Core app:

Rasa NLU model based on which messages will be processed and converted to a structured form of intent and entities. Create following two files to generate the model:

data.json: Create this training file using the rasa-nlu trainer. Click here to know more about the rasa-nlu trainer.

nlu_config.json: This is the configuration file.

CODE: https://gist.github.com/velotiotech/df62542480ee2ed87f54de0a141361ea.js

Run below command to train the rasa-nlu model:-

CODE: https://gist.github.com/velotiotech/025f16e0403e4c170c03f30c909814b3.js

Dialogue Model: This model is trained on stories we define, based on which the policy will take the action. There are two ways in which stories can be generated:

  • Supervised Learning: In this type of learning we will create the stories by hand, writing them directly in a file. It is easy to write but in case of complex use-cases it is difficult to cover all scenarios.
  • Reinforcement Learning: The user provides feedback on every decision taken by the policy. This is also known as interactive learning. This helps in including edge cases which are difficult to create by hand. You must be thinking how it works? Every time when a policy chooses an action to take, it is asked from the user whether the chosen action is correct or not. If the action taken is wrong, you can correct the action on the fly and store the stories to train the model again.

Since the example is simple, we have used supervised learning method, to generate the dialogue model. Below is the stories.md file.

CODE: https://gist.github.com/velotiotech/bbeb5bb3d9163291056861df7cb702a5.js

Run below command to train dialogue model :

CODE: https://gist.github.com/velotiotech/ff6e0c48e249526cebcb797714ae2bc1.js

Define a Domain: Create domain.yml file containing all the required information. Among the intents and entities write all those strings which bot is supposed to see when user say something i.e. intents and entities you defined in rasa NLU training file.

CODE: https://gist.github.com/velotiotech/13115db4accae3a2dc54c7a0065fe78f.js

Define Actions: Templates defined in domain.yml also considered as actions. A sample customized action is shown below where we are setting a slot named gender with values according to the option selected by the user.

CODE: https://gist.github.com/velotiotech/2d2ab0f5423b87a0c4722ffe1116ce29.js

Running the Bot

Create a Facebook app and get the app credentials. Create a bot.py file as shown below:

CODE: https://gist.github.com/velotiotech/1576bb8941a3767dd01933a89c725265.js

Run the file and your bot is ready to test. Sample conversations are provided below:

Sample Bot Conversations


Summary

You have seen how Rasa Core has made it easier to build bots. Just create few files and boom! Your bot is ready! Isn't it exciting? I hope this blog provided you some insights on how Rasa Core works. Start exploring and let us know if you need any help in building chatbots using Rasa Core.

Get the latest engineering blogs delivered straight to your inbox.
No spam. Only expert insights.
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.

Did you like the blog? If yes, we're sure you'll also like to work with the people who write them - our best-in-class engineering team.

We're looking for talented developers who are passionate about new emerging technologies. If that's you, get in touch with us.

Explore current openings

Your Complete Guide to Building Stateless Bots Using Rasa Stack

This blog aims at exploring the Rasa Stack to create a stateless chat-bot. We will look into how, the recently released Rasa Core, which provides machine learning based dialogue management, helps in maintaining the context of conversations using machine learning in an efficient way.

If you have developed chatbots, you would know how hopelessly bots fail in maintaining the context once complex use-cases need to be developed. There are some home-grown approaches that people currently use to build stateful bots. The most naive approach is to create the state machines where you create different states and based on some logic take actions. As the number of states increases, more levels of nested logic are required or there is a need to add an extra state to the state machine, with another set of rules for how to get in and out of that state. Both of these approaches lead to fragile code that is harder to maintain and update. Anyone who’s built and debugged a moderately complex bot knows this pain.

After building many chatbots, we have experienced that flowcharts are useful for doing the initial design of a bot and describing a few of the known conversation paths, but we shouldn’t hard-code a bunch of rules since this approach doesn’t scale beyond simple conversations.

Thanks to the Rasa guys who provided a way to go stateless where scaling is not at all a problem. Let's build a bot using Rasa Core and learn more about this.

Rasa Core: Getting Rid of State Machines

The main idea behind Rasa Core is that thinking of conversations as a flowchart and implementing them as a state machine doesn’t scale. It’s very hard to reason about all possible conversations explicitly, but it’s very easy to tell, mid-conversation, if a response is right or wrong. For example, let’s consider a term insurance purchase bot, where you have defined different states to take different actions. Below diagram shows an example state machine:

State Machine Example

Let’s consider a sample conversation where a user wants to compare two policies listed by policy_search state.

In above conversation, it can be compared very easily by adding some logic around the intent campare_policies. But real life is not so easy, as a majority of conversations are edge cases. We need to add rules manually to handle such cases, and after testing we realize that these clash with other rules we wrote earlier.

Rasa guys figured out how machine learning can be used to solve this problem. They have released Rasa Core where the logic of the bot is based on a probabilistic model trained on real conversations.

Structure of a Rasa Core App

Let’s understand few terminologies we need to know to build a Rasa Core app:

1. Interpreter: An interpreter is responsible for parsing messages. It performs the Natural Language Understanding and transforms the message into structured output i.e. intent and entities. In this blog, we are using Rasa NLU model as an interpreter. Rasa NLU comes under the Rasa Stack. In Training section, it is shown in detail how to prepare the training data and create a model.

2. Domain: To define a domain we create a domain.yml file, which defines the universe of your bot. Following things need to be defined in a domain file:

  • Intents: Things we expect the user to say. It is more related to Rasa NLU.
  • Entities: These represent pieces of information extracted what user said. It is also related to Rasa NLU.
  • Templates: We define some template strings which our bot can say. The format for defining a template string is utter_<intent>. These are considered as actions which bot can take.
  • Actions: List of things bot can do and say. There are two types of actions we define one those which will only utter message (Templates) and others some customised actions where some required logic is defined. Customised actions are defined as Python classes and are referenced in domain file.
  • Slots: These are user-defined variables which need to be tracked in a conversation. For e.g to buy a term insurance we need to keep track of what policy user selects and details of the user, so all these details will come under slots.

3. Stories: In stories, we define what bot needs to do at what point in time. Based on these stories, a probabilistic model is generated which is used to decide which action to be taken next. There are two ways in which stories can be created which are explained in next section.

Let’s combine all these pieces together. When a message arrives in a Rasa Core app initially, interpreter transforms the message into structured output i.e. intents and entities. The Tracker is the object which keeps track of conversation state. It receives the info that a new message has come in. Then based on dialog model we generate using domain and stories policy chooses which action to take next. The chosen action is logged by the tracker and response is sent back to the user.

Training and Running A Sample Bot

We will create a simple Facebook chat-bot named Secure Life which assists you in buying term life insurance. To keep the example simple, we have restricted options such as age-group, term insurance amount, etc.

There are two models we need to train in the Rasa Core app:

Rasa NLU model based on which messages will be processed and converted to a structured form of intent and entities. Create following two files to generate the model:

data.json: Create this training file using the rasa-nlu trainer. Click here to know more about the rasa-nlu trainer.

nlu_config.json: This is the configuration file.

CODE: https://gist.github.com/velotiotech/df62542480ee2ed87f54de0a141361ea.js

Run below command to train the rasa-nlu model:-

CODE: https://gist.github.com/velotiotech/025f16e0403e4c170c03f30c909814b3.js

Dialogue Model: This model is trained on stories we define, based on which the policy will take the action. There are two ways in which stories can be generated:

  • Supervised Learning: In this type of learning we will create the stories by hand, writing them directly in a file. It is easy to write but in case of complex use-cases it is difficult to cover all scenarios.
  • Reinforcement Learning: The user provides feedback on every decision taken by the policy. This is also known as interactive learning. This helps in including edge cases which are difficult to create by hand. You must be thinking how it works? Every time when a policy chooses an action to take, it is asked from the user whether the chosen action is correct or not. If the action taken is wrong, you can correct the action on the fly and store the stories to train the model again.

Since the example is simple, we have used supervised learning method, to generate the dialogue model. Below is the stories.md file.

CODE: https://gist.github.com/velotiotech/bbeb5bb3d9163291056861df7cb702a5.js

Run below command to train dialogue model :

CODE: https://gist.github.com/velotiotech/ff6e0c48e249526cebcb797714ae2bc1.js

Define a Domain: Create domain.yml file containing all the required information. Among the intents and entities write all those strings which bot is supposed to see when user say something i.e. intents and entities you defined in rasa NLU training file.

CODE: https://gist.github.com/velotiotech/13115db4accae3a2dc54c7a0065fe78f.js

Define Actions: Templates defined in domain.yml also considered as actions. A sample customized action is shown below where we are setting a slot named gender with values according to the option selected by the user.

CODE: https://gist.github.com/velotiotech/2d2ab0f5423b87a0c4722ffe1116ce29.js

Running the Bot

Create a Facebook app and get the app credentials. Create a bot.py file as shown below:

CODE: https://gist.github.com/velotiotech/1576bb8941a3767dd01933a89c725265.js

Run the file and your bot is ready to test. Sample conversations are provided below:

Sample Bot Conversations


Summary

You have seen how Rasa Core has made it easier to build bots. Just create few files and boom! Your bot is ready! Isn't it exciting? I hope this blog provided you some insights on how Rasa Core works. Start exploring and let us know if you need any help in building chatbots using Rasa Core.

Did you like the blog? If yes, we're sure you'll also like to work with the people who write them - our best-in-class engineering team.

We're looking for talented developers who are passionate about new emerging technologies. If that's you, get in touch with us.

Explore current openings