Sign in

Building a bot using Rasa NLU & Core

How about building your own assistant which will reply for the conversations it is trained for. Interesting?

Okay, let’s learn to build.

So In this blogpost, We’ll build a conversational bot on Rasa stack which will interact with you and it can provide random jokes if you ask.

Requirements

Rasa Stack : It is a set of open source machine learning tools for developers to create contextual AI assistants and chatbots and the leading open source machine learning toolkit that allow developers expand bots beyond answering simple questions with minimal training data. It has two frameworks, NLU & Core.

Rasa NLU: Framework for natural language understanding with intent classification and entity extraction. This helps the chatbot to understand what the user is saying by analyzing the intent.

Rasa Core: It is a chatbot framework with machine learning-based dialogue management that evaluates the next action based on the input from NLU, the conversation context, and the training data(conversational stories).

— Language model: It is going to be used to analyze incoming text messages and extract the necessary information. We will be using SpaCy language model.

Installations

We need to install Rasa NLU, Rasa Core and a spaCy language model. To install, using pip

Preparing Training Data

Training data consists of a list of text messages that one expects to receive from the bot. This data is labelled with the intent and entities that Rasa NLU should learn to extract. Let us look in to the concept of intent and entities with an example.

  • Intent: The intent describes class/category of messages. For example, in our bot, the sentence :Tell me a joke” has a joke intent.
  • Entity: Pieces of information which help the chatbot to understand what specifically a user is asking about by recognising the structured data in the sentence message. For example: Send invitation to abc@xyz.com Inorder to send email it should extract email entity. (abc@xyz.com)

Here is some portion of training data. We can also add some spelling mistakes/slangs since that will make chat bot speak like human. We will save this under data/nlu_data.md

Furthermore, we need configuration file, nlu_config.yml, for the NLU model:

We need to add makefile. It contains a set of directives used by a make build automation tool to generate a target. Add below content to the makefile.

We can now train the NLU using our training data(make sure to install Rasa NLU , as well as spaCy). Inorder to start training feed below command.

The trained model files will be stored at: ‘./models/nlu/current’.

Rasa — Core

After completing the nlu training bot is capable of understanding what the user is saying . Now the next thing is to make the chatbot respond to messages. In our case, it would be to fetching random joke through api and give it to the user. We will teach bot to make responses by training a dialogue model using Rasa Core.

Writing Stories

The training data for dialogue models is called stories. A story is an actual piece of conversation that takes place between a user and Bot. The user ’s inputs are intents as well as corresponding entities, and bot responses are expressed as actions.

Here is some portion of training data, that I have prepared and we will store this under data/stories.md

Defining the Domain

Domain includes what user inputs it should expect to get, what actions it should predict, how to respond and what things to store. The domain consists of intents, slots, entities, actions and templates. We discussed the intents and entities, let’s understand the others.

  • slots: those are like placeholders for the values that enable the chatbot to keep a track of the conversation.
  • actions: the actions made by the chatbot.
  • templates: template texts for the things that bot would respond

Next, we’ll define the domain domain.yml . Here is an example domain for our bot:

Actions

Since we want our Bot to make an API call to retrieve random jokes , we need to create a custom action for this purpose. Add below code in actions.py

After preparing the training data, it is the time to train rasa-core. To initiate the training we can feed the following command.

The trained model will be saved at :models/dialogue

So it is time to chat with our bot. I will be checking one simple conversational story that the bot has trained to respond. To start the conversation in the command line, feed following command

Conclusion

In this blog post we have created a bot that is capable of listening to user’s input and responding contextually. We have used the Rasa NLU and Rasa Core to create it with minimum training data. So it is your turn! Start creating a bot for your use case with Rasa stack.

Programmer