Rasa in NLP
Overview
Natural Language Processing or NLP is the ability of a computer to understand and interpret human language. This aspect of machine learning has been extremely useful and is used for a variety of tasks. There are a plethora of libraries in the python language that makes NLP tasks easy. One such library is Rasa` and this article revolves around it.
Introduction
Natural language processing has a variety of applications. One such application is the chatbot. Chatbots are programs that simulate human conversation. Chatbots can be of various types depending on their functionality. There are rule-based ones where the user is limited to clicking on buttons or suggested replies that the bot provides. There are also advanced ones that can handle context, chitchat, and other complex things, which are otherwise very common in human conversation.
What is Rasa in NLP?
Rasa provides open-source natural language processing to turn messages from your users into intents and entities that chatbots understand. The bots built with Rasa can understand the context of the language. It can handle things like the user changing their mind and even unexpected queries. It provides natural language processing software that’s approachable and as customizable as you need. Undoubtedly, it is one of the best tools for creating a chatbot.
The Need for Rasa
- Open source library, hence it's been upgraded and enhanced regularly.
- It lets you create a whole range of Bots for different purposes.
- It is the most flexible and transparent solution for conversational AI.
Rasa Architecture
The tasks of a chatbot are divided into two groups:-
- First, the chatbot must understand the question or query of the user and the answer he or she is expecting.
- Next, it should maintain the conversation flow and answer according to the query of the user.
Rasa provides two excellent frameworks: Rasa NLU and Rasa Core which will become the two pillars of our chatbot and handle the aforementioned groups separately.
Let's understand the two pillars separately.
Features of Rasa
Rasa NLU
NLU stands for Natural Language Understanding. It is essential to use Rasa NLU to understand the context of the user.
Terminologies
- Intent: Identifying the purpose of the user’s message. For example, consider “What is the delivery status?” as the input message. This message intends to know the whereabouts of the package. NLU will have to rightly identify this. This is termed Intent classification.
- Entity: Input messages may contain information like name, place, etc. These details are essential and need to be extracted. For example, consider “I need to order a Samsung phone”. The chatbot needs to extract the name “Samsung” which is an entity.
Hence, Rasa NLU performs Intent Classification and Entity Extraction.
NLU Core
Now that NLU has made sure that our Bot understands the requirement of the user, the Bot should respond appropriately to the message. This is handled by Rasa Core. as it maintains the conversation flow between the User and the Bot.
For example, if the user wants to "place an order", the bot should provide the user with the menu and act according to the selections of the user before sending the itinerary.
Terminologies
- actions: After the intent of the user is recognized by the bot, it should take the appropriate action. It may be outputting a text, a link, or even a whole function being performed. The responses the Bot is trained to perform are called actions.
- stories: It is the path that defines a sequence of intents and actions. According to the intent and actions taken by the bot, stories provide a sample interaction between the user and Bot.
- templates: Here, we define all the actions, and the text for each action is written.
- slots: The bot's memory is termed as slots. They store the values that enable the bot to keep track of the conversations.
Building a Chatbot with Rasa
Installation
Install rasa NLU and core using the following command:-
Next, we import the necessary libraries.
Training NLU data
For intent classification, we choose seven different types of intents. The intent is written in the following format:-
After writing the training data in the above format, save it to a file named nlu.md for Rasa to recognize.
Code Implementation:
Store the above training data in nlu. md file in the working directory.
Defining NLU Configuration
The message the user sends is passed through the NLU pipeline of Rasa. The first component of the pipeline is a tokenizer. Here we use spaCy's tokenizer_spacy. The next component is the entity extractor. We use the ner_crf for this purpose. After that, you need to get a vector representation of our input message. For this, we use the intent_featurizer_spacy. All these are components of the NLU pipeline which need to be added to the configuration file.
Code Implementation:-
Training the NLU Model
Here are the steps required:-
- Import the load_data() function from rasa_nlu.training_data module. By passing nlu.md file to the above function, the training_data gets extracted.
- Import and use the config module from rasa_nlu to read the configuration settings into the trainer.
- Then the trainer is trained with the previously extracted training_data to create an interpreter.
Code Implementation:-
Test the NLU
Now that the training of the NLU model is done, we check how the interpreter works.
Output:-
You can see the model predicting the confidence of each intent. The model predicts intent “greet” with a confidence of 0.7448749668923197. This means the model is 74.48..% sure that the user intends to greet.
Now that we have implemented Rasa NLU let's move on to Rasa core.
Writing Stories
Previously we discussed that stories are the paths that define a sequence of intents and actions. We need to store the stories in the following format:-
Just like in NLU, we have to store the stories in stories. md. Here is an example:-
Defining the Domain
A domain is a file where all the components are linked. It consists of all the intents, entities, actions, slots, and templates. The code of the domain is stored in the domain.yml file.
Training the Model
- This is the final and deciding step
- to predict the following action, we use the *MemoizationPolicy. It predicts the action with the confidence of either 1 or 0 based on the previous example text available.
- Another policy named KerasPolicy is used for the same purpose. This policy is based on LSTMs.
- After importing the necessary policies, we import the Agent for loading the data and training.
- The domain.yml file is then passed as input to the Agent() function along with the chosen policy names.
- The function returned by the model agent is trained with the data available in stories. md.
Code Implementation:-
Output:-
Talking to the bot
Finally, the bot is created! Let's chat with it.
Load the trained model from the directory as shown below.
- Next, we write a function that will take input from the user until the user enters "stop".
- We pass the input message of the user to the trained agent using the handle_message() function.
- It then returns a dictionary response from which the text response is printed.
Code Implementation:-
Output:-
Our chatbot is ready!
Conclusion
The key takeaways from this article are:-
- Chatbots are programs that simulate human conversation.
- Rasa Open Source provides open-source natural language processing to turn messages from your users into intents and entities that chatbots understand.
- Rasa is one of the best tools for creating a chatbot.
- The two frameworks of Rasa: NLU and Core, help in building the different components of the chatbot.