1. Introduction
Welcome to the fourth step of our chatbot development series. In this blog, you will learn how to use Rasa, an open-source framework for building chatbots, to create your own chatbot from scratch. You will also learn how to improve your chatbot’s performance and functionality with Rasa.
Chatbots are becoming more and more popular as a way to interact with users, provide information, answer questions, and perform tasks. However, building a chatbot that can understand natural language, handle complex dialogs, and adapt to user feedback is not an easy task. You need a lot of data, skills, and tools to create a chatbot that can meet your needs and expectations.
That’s where Rasa comes in. Rasa is a framework that allows you to build chatbots using machine learning and natural language processing. Rasa provides you with the tools and components to design, train, test, and deploy your chatbot. Rasa also lets you customize your chatbot according to your domain, intents, and preferences.
By the end of this blog, you will be able to:
- Understand what Rasa is and why use it for chatbot development
- Know how Rasa works and what are its main components: Rasa NLU, Rasa Core, and Rasa X
- Install and set up Rasa on your machine
- Create a simple chatbot with Rasa using a step-by-step guide
- Improve your chatbot’s performance and functionality with Rasa
Are you ready to start building your chatbot with Rasa? Let’s begin!
2. What is Rasa and why use it for chatbot development?
Rasa is an open-source framework for building chatbots that can understand natural language and handle complex dialogs. Rasa was founded in 2016 by Alan Nichol and Tom Bocklisch, who wanted to create a tool that would enable developers to create chatbots that are more conversational, contextual, and customizable.
Rasa is based on the idea of conversational AI, which is the ability of machines to communicate with humans using natural language. Conversational AI is not just about understanding what the user says, but also about generating appropriate and engaging responses, maintaining the flow of the conversation, and adapting to the user’s needs and preferences.
Why use Rasa for chatbot development? There are many reasons why Rasa is a great choice for building chatbots, such as:
- Rasa is open-source, which means you can use it for free, modify it as you wish, and contribute to its development. Rasa also has a large and active community of developers and users who provide support, feedback, and resources.
- Rasa is flexible, which means you can customize your chatbot according to your domain, intents, and preferences. You can also integrate your chatbot with various channels, platforms, and services, such as Facebook Messenger, Slack, Telegram, Twilio, Google Assistant, and more.
- Rasa is scalable, which means you can deploy your chatbot on any infrastructure, from your local machine to the cloud. You can also update and improve your chatbot over time, without losing the existing data and functionality.
- Rasa is state-of-the-art, which means it uses the latest and best technologies and techniques for natural language processing and machine learning. Rasa also provides you with the tools and components to design, train, test, and deploy your chatbot.
As you can see, Rasa is a powerful and versatile framework for building chatbots that can understand natural language and handle complex dialogs. But how does Rasa work? What are the main components of Rasa? And how can you use them to create your chatbot? That’s what we will explore in the next section.
3. How Rasa works: Rasa NLU, Rasa Core, and Rasa X
In this section, you will learn how Rasa works and what are its main components: Rasa NLU, Rasa Core, and Rasa X. These components are responsible for different aspects of chatbot development, such as natural language understanding, dialog management, and user feedback.
Rasa NLU is the component that handles the natural language understanding of your chatbot. It takes the user’s input and extracts the relevant information, such as the intent and the entities. The intent is the goal or the purpose of the user’s message, such as greeting, booking, or asking. The entities are the specific details or parameters of the user’s message, such as date, time, location, or name.
Rasa NLU uses machine learning models to train and classify the user’s input. You can use the predefined models provided by Rasa, or you can create your own custom models using different pipelines and algorithms. You can also use different languages and dialects for your chatbot, as Rasa NLU supports multilingual and cross-lingual models.
Rasa Core is the component that handles the dialog management of your chatbot. It decides what action to take next based on the user’s input and the chatbot’s state. The action can be a response, a question, a request, or a call to an external service or API. Rasa Core also keeps track of the conversation history and the context, such as the user’s preferences, the previous actions, and the current slot values.
Rasa Core uses machine learning models to train and predict the next action. You can use the predefined models provided by Rasa, or you can create your own custom models using different policies and algorithms. You can also use different strategies and techniques to design your chatbot’s dialog, such as stories, rules, forms, and fallbacks.
Rasa X is the component that helps you improve your chatbot’s performance and functionality. It allows you to test your chatbot in a user-friendly interface, collect user feedback, analyze the chatbot’s behavior, and fine-tune the chatbot’s models. Rasa X also enables you to collaborate with your team and share your chatbot with your users.
Rasa X uses various tools and features to help you optimize your chatbot, such as interactive learning, conversation analytics, model testing, version control, and deployment. You can also use different channels and platforms to connect your chatbot with your users, such as web, mobile, voice, and email.
As you can see, Rasa is a comprehensive and modular framework that provides you with everything you need to build a chatbot that can understand natural language and handle complex dialogs. But how can you install and set up Rasa on your machine? That’s what we will cover in the next section.
4. How to install and set up Rasa on your machine
In this section, you will learn how to install and set up Rasa on your machine. You will need a few prerequisites before you can start building your chatbot with Rasa, such as:
- A computer with a Windows, Linux, or MacOS operating system
- A Python environment with version 3.6 or higher
- A text editor or an IDE of your choice, such as Visual Studio Code, PyCharm, or Sublime Text
- A terminal or a command prompt to run commands and scripts
- An internet connection to download and install packages and dependencies
Once you have these prerequisites, you can follow these steps to install and set up Rasa on your machine:
- Open your terminal or command prompt and create a new folder for your chatbot project. You can name it anything you want, but for this tutorial, we will call it rasa-chatbot. To create the folder, run this command:
- Navigate to the folder you just created and create a new virtual environment for your chatbot project. A virtual environment is a way to isolate your project’s dependencies from other projects and avoid conflicts. To create a virtual environment, run this command:
- Activate the virtual environment you just created. This will change the prompt of your terminal or command prompt to indicate that you are working in the virtual environment. To activate the virtual environment, run this command:
- Install Rasa and its dependencies in your virtual environment. Rasa is available as a Python package that you can install using pip, a tool for managing Python packages. To install Rasa, run this command:
- Verify that Rasa is installed correctly by checking its version. To check the version of Rasa, run this command:
- You should see something like this:
- Congratulations! You have successfully installed and set up Rasa on your machine. You are now ready to create your chatbot with Rasa.
mkdir rasa-chatbot
python3 -m venv ./venv
source ./venv/bin/activate
pip install rasa
rasa --version
Rasa Version : 2.8.1 Minimum Compatible Version: 2.8.0 Rasa SDK Version : 2.8.1 Rasa X Version : 0.42.0 Python Version : 3.8.5 Operating System : Linux-5.4.0-77-generic-x86_64-with-glibc2.29 Python Path : /home/user/rasa-chatbot/venv/bin/python3
In the next section, you will learn how to create a simple chatbot with Rasa using a step-by-step guide.
5. How to create a simple chatbot with Rasa: a step-by-step guide
In this section, you will learn how to create a simple chatbot with Rasa using a step-by-step guide. You will use the Rasa components that you learned in the previous section to design, train, test, and deploy your chatbot. You will also use a sample chatbot project that Rasa provides to help you get started.
The sample chatbot project is called rasa-init, and it is a basic chatbot that can greet the user, answer some FAQs, and book a restaurant. You can use this project as a template to create your own chatbot, or you can modify it according to your needs and preferences.
To create a simple chatbot with Rasa, you will follow these steps:
- Initialize the chatbot project using the rasa init command. This will create a new folder called rasa-init in your current directory, and it will contain all the files and folders that you need for your chatbot project. To initialize the chatbot project, run this command:
- Explore the chatbot project structure and files. You will see that the chatbot project consists of several files and folders, such as:
- actions.py: This is where you define the custom actions that your chatbot can perform, such as calling an API, querying a database, or sending an email.
- config.yml: This is where you configure the pipeline and policies for your chatbot’s NLU and Core models.
- credentials.yml: This is where you specify the credentials for the different channels and platforms that you want to connect your chatbot with, such as Facebook Messenger, Slack, Telegram, etc.
- data: This is a folder that contains the training data for your chatbot’s NLU and Core models. It consists of two files:
- nlu.yml: This is where you define the intents and entities that your chatbot can recognize, and provide some examples of user messages for each intent.
- stories.yml: This is where you define the stories and rules that your chatbot can follow, and provide some examples of dialog flows for each story or rule.
- domain.yml: This is where you define the domain of your chatbot, which includes the intents, entities, slots, actions, responses, and forms that your chatbot can use.
- endpoints.yml: This is where you specify the endpoints for the different services and components that your chatbot can interact with, such as the action server, the tracker store, the model server, etc.
- models: This is a folder that contains the trained models for your chatbot’s NLU and Core components. You can use these models to test and deploy your chatbot.
- tests: This is a folder that contains the test data and scripts for your chatbot’s NLU and Core components. You can use these tests to evaluate and improve your chatbot’s performance and functionality.
- Train the chatbot’s model using the rasa train command. This will use the data and config files that you have in your chatbot project to train the NLU and Core models for your chatbot. To train the chatbot’s model, run this command:
- Test the chatbot in the shell using the rasa shell command. This will load the trained model and allow you to chat with your chatbot in the terminal or command prompt. To test the chatbot in the shell, run this command:
- Test the chatbot in Rasa X using the rasa x command. This will launch the Rasa X interface in your browser and allow you to chat with your chatbot in a user-friendly environment. You can also use Rasa X to collect user feedback, analyze the chatbot’s behavior, and fine-tune the chatbot’s model. To test the chatbot in Rasa X, run this command:
- Deploy the chatbot using the rasa run command. This will run the chatbot on a server and allow you to connect it with different channels and platforms, such as web, mobile, voice, and email. To deploy the chatbot, run this command:
rasa init
rasa train
rasa shell
rasa x
rasa run
By following these steps, you have created a simple chatbot with Rasa that can greet the user, answer some FAQs, and book a restaurant. You can also modify the chatbot project files and folders to create your own chatbot, or to add more features and functionalities to the existing chatbot.
In the next section, you will learn how to improve your chatbot’s performance and functionality with Rasa.
5.1. Define your chatbot’s domain and intents
The first step to create a chatbot with Rasa is to define your chatbot’s domain and intents. The domain is the set of information and actions that your chatbot can handle. The intents are the goals or purposes of the user’s messages.
The domain and intents are defined in two separate files: domain.yml
and nlu.yml
. The domain.yml
file contains the following elements:
- intents: a list of the intents that your chatbot can recognize and respond to.
- entities: a list of the entities that your chatbot can extract from the user’s messages. Entities are pieces of information that are relevant to the chatbot’s domain, such as names, dates, locations, etc.
- slots: a list of the slots that your chatbot can use to store the values of the entities. Slots are like variables that can be filled with information from the user’s messages or from external sources.
- responses: a list of the responses that your chatbot can utter to the user. Responses are the messages that your chatbot sends back to the user, such as greetings, confirmations, answers, etc.
- actions: a list of the actions that your chatbot can perform. Actions are the behaviors that your chatbot can execute, such as calling an API, querying a database, sending an email, etc.
- session_config: a configuration of the session parameters, such as the session expiration time and the carry-over slots.
The nlu.yml
file contains the following elements:
- nlu: a list of the examples of the user’s messages for each intent. Each example is annotated with the intent name and the entities (if any).
To define your chatbot’s domain and intents, you need to think about the following questions:
- What is the purpose of your chatbot? What problem does it solve or what service does it provide?
- Who are your target users? What are their needs, preferences, and expectations?
- What are the possible scenarios or use cases of your chatbot? How will the user interact with your chatbot?
- What are the main topics or categories of the user’s messages? What are the user’s goals or intentions when they send a message to your chatbot?
- What are the relevant pieces of information that your chatbot needs to extract from the user’s messages? What are the types and formats of these information?
- What are the possible responses or actions that your chatbot can provide or perform for each intent? How will your chatbot fulfill the user’s requests or expectations?
Based on these questions, you can create your domain.yml
and nlu.yml
files and fill them with the appropriate elements. For example, let’s say you want to create a chatbot that can book a hotel room for the user. Your domain.yml
file might look something like this:
intents: - greet - book_room - cancel_booking - request_info - thank - goodbye entities: - location - date - number slots: location: type: text check_in: type: text check_out: type: text guests: type: int responses: utter_greet: - text: "Hello, welcome to Hotel Bot. How can I help you?" utter_book_room: - text: "Sure, I can book a room for you. Where do you want to stay?" utter_ask_check_in: - text: "When do you want to check in?" utter_ask_check_out: - text: "When do you want to check out?" utter_ask_guests: - text: "How many guests are you booking for?" utter_confirm_booking: - text: "Okay, I have booked a room for you at {location} from {check_in} to {check_out} for {guests} guests. Is that correct?" utter_cancel_booking: - text: "Alright, I have canceled your booking. Is there anything else I can do for you?" utter_request_info: - text: "Sure, what information do you need?" utter_provide_info: - text: "Here is the information you requested: {info}" utter_thank: - text: "You're welcome. I'm happy to help." utter_goodbye: - text: "Bye, have a nice day." actions: - validate_booking - cancel_booking - request_info - provide_info session_config: session_expiration_time: 60 carry_over_slots_to_new_session: true
And your nlu.yml
file might look something like this:
nlu: - intent: greet examples: | - Hi - Hello - Hey - Good day - Good morning - intent: book_room examples: | - I want to book a room - I need a room for tonight - Can you book a hotel room for me? - I'm looking for a place to stay - Do you have any rooms available? - intent: cancel_booking examples: | - I want to cancel my booking - I don't need the room anymore - Can you cancel my reservation? - I changed my mind about the hotel - I found a better deal somewhere else - intent: request_info examples: | - I want to know more about the hotel - What are the amenities of the hotel? - How much does it cost per night? - What is the cancellation policy? - How far is the hotel from the airport? - intent: thank examples: | - Thank you - Thanks - Thank you very much - Thanks a lot - I appreciate your help - intent: goodbye examples: | - Bye - Goodbye - See you - Have a nice day - Talk to you later
As you can see, the domain.yml
and nlu.yml
files define the domain and intents of your chatbot in a clear and structured way. You can use these files to train your chatbot’s model and test it in the shell or in Rasa X.
5.2. Write your chatbot’s stories and rules
The second step to create a chatbot with Rasa is to write your chatbot’s stories and rules. Stories and rules are two ways of defining the dialog flow of your chatbot. They specify how your chatbot should respond to the user’s messages based on the current state of the conversation.
Stories are sequences of user messages and chatbot actions that represent a possible conversation. Stories are written in a simple and intuitive format, using the intent and entity names from the nlu.yml
file and the action and response names from the domain.yml
file. Stories are used to train your chatbot’s model using a machine learning algorithm called Rasa Core.
Rules are explicit conditions and outcomes that define how your chatbot should behave in certain situations. Rules are written in a similar format as stories, but they use the keyword rule
instead of story
. Rules are used to override or supplement the predictions of your chatbot’s model using a logic-based system called Rasa Rule Policy.
Stories and rules are defined in two separate files: stories.yml
and rules.yml
. The stories.yml
file contains the following elements:
- stories: a list of the stories that describe the dialog flow of your chatbot. Each story starts with a name and a dash, followed by a sequence of steps. Each step consists of either a user message (prefixed with
- intent:
) or a chatbot action (prefixed with- action:
or- bot:
). You can also use checkpoints, slots, forms, and conditional steps to make your stories more dynamic and flexible.
The rules.yml
file contains the following elements:
- rules: a list of the rules that define the behavior of your chatbot in certain situations. Each rule starts with a name and a dash, followed by a sequence of steps. Each step consists of either a user message (prefixed with
- intent:
) or a chatbot action (prefixed with- action:
or- bot:
). You can also use conditions, wait for user input, and active loop to make your rules more specific and robust.
To write your chatbot’s stories and rules, you need to think about the following questions:
- What are the possible paths or branches of the conversation? How can the user’s messages and the chatbot’s actions affect the course of the dialog?
- What are the common or expected scenarios or use cases of your chatbot? How can you represent them as stories?
- What are the exceptional or rare scenarios or use cases of your chatbot? How can you represent them as rules?
- What are the best practices or guidelines for writing stories and rules? How can you avoid conflicts, contradictions, or redundancies in your stories and rules?
Based on these questions, you can create your stories.yml
and rules.yml
files and fill them with the appropriate elements. For example, let’s say you want to create a chatbot that can book a hotel room for the user. Your stories.yml
file might look something like this:
stories: - story: happy path steps: - intent: greet - action: utter_greet - intent: book_room - action: utter_book_room - intent: inform entities: - location: "New York" - action: slot{"location": "New York"} - action: utter_ask_check_in - intent: inform entities: - date: "2021-10-15" - action: slot{"check_in": "2021-10-15"} - action: utter_ask_check_out - intent: inform entities: - date: "2021-10-18" - action: slot{"check_out": "2021-10-18"} - action: utter_ask_guests - intent: inform entities: - number: 2 - action: slot{"guests": 2} - action: validate_booking - action: utter_confirm_booking - intent: affirm - action: utter_thank - intent: goodbye - action: utter_goodbye - story: unhappy path steps: - intent: greet - action: utter_greet - intent: book_room - action: utter_book_room - intent: inform entities: - location: "New York" - action: slot{"location": "New York"} - action: utter_ask_check_in - intent: inform entities: - date: "2021-10-15" - action: slot{"check_in": "2021-10-15"} - action: utter_ask_check_out - intent: inform entities: - date: "2021-10-18" - action: slot{"check_out": "2021-10-18"} - action: utter_ask_guests - intent: inform entities: - number: 2 - action: slot{"guests": 2} - action: validate_booking - action: utter_confirm_booking - intent: deny - action: utter_cancel_booking - intent: goodbye - action: utter_goodbye
And your rules.yml
file might look something like this:
rules: - rule: activate booking form steps: - intent: book_room - action: booking_form - active_loop: booking_form - rule: submit booking form condition: - active_loop: booking_form steps: - action: booking_form - active_loop: null - slot_was_set: - requested_slot: null - action: validate_booking - action: utter_confirm_booking - rule: cancel booking steps: - intent: cancel_booking - action: cancel_booking - action: utter_cancel_booking - rule: request info steps: - intent: request_info - action: request_info - action: utter_request_info - rule: provide info condition: - slot_was_set: - info: some value steps: - action: provide_info - action: utter_provide_info
As you can see, the stories.yml
and rules.yml
files define the dialog flow of your chatbot in a clear and structured way. You can use these files to train your chatbot’s model and test it in the shell or in Rasa X.
5.3. Train your chatbot’s model
The third step to create a chatbot with Rasa is to train your chatbot’s model. The model is the core of your chatbot, as it determines how your chatbot understands and responds to the user’s messages. The model is trained using the data and configuration files that you have created in the previous steps.
The data files are the nlu.yml
, stories.yml
, and rules.yml
files that contain the examples of the user’s messages, the dialog flow of your chatbot, and the behavior of your chatbot in certain situations. The configuration file is the config.yml
file that contains the settings and parameters of your chatbot’s model, such as the pipeline, the policies, and the language.
The pipeline is the sequence of components that process the user’s messages and extract the intents and entities. The pipeline can consist of various components, such as tokenizers, featurizers, classifiers, extractors, etc. You can choose the components that suit your chatbot’s domain and preferences, or use one of the predefined pipelines that Rasa provides.
The policies are the algorithms that decide the next action of your chatbot based on the state of the conversation. The policies can be based on machine learning, logic, or both. You can choose the policies that suit your chatbot’s domain and preferences, or use one of the predefined policies that Rasa provides.
The language is the language of your chatbot and your users. You can specify the language code of your chatbot, such as en
for English, fr
for French, zh
for Chinese, etc. You can also use multilingual models that can handle more than one language.
To train your chatbot’s model, you need to run the following command in your terminal:
rasa train
This command will use the data and configuration files in your project directory to train your chatbot’s model. The training process might take some time, depending on the size and complexity of your data and configuration. Once the training is done, you will see a message like this:
Your Rasa model is trained and saved at 'models/20211015-123456.tar.gz'.
This means that your chatbot’s model is trained and saved in a compressed file in the models
folder. The file name will include the date and time of the training. You can use this file to test your chatbot in the shell or in Rasa X.
5.4. Test your chatbot in the shell and in Rasa X
Now that you have trained your chatbot’s model, you can test it in the shell and in Rasa X. Testing your chatbot is a crucial step to evaluate its performance and functionality, as well as to identify and fix any errors or issues.
To test your chatbot in the shell, you can use the following command:
rasa shell
This will start a conversation with your chatbot in the terminal. You can type your messages and see how your chatbot responds. You can also use the –debug flag to see more details about the chatbot’s processing, such as the predicted intents, entities, actions, and confidence scores.
To test your chatbot in Rasa X, you can use the following command:
rasa x
This will launch the Rasa X web interface, where you can interact with your chatbot in a more user-friendly way. You can also use the Rasa X features to monitor, analyze, and improve your chatbot, such as the conversation history, the NLU inbox, the interactive learning, and the analytics dashboard.
By testing your chatbot in the shell and in Rasa X, you can get a better sense of how your chatbot works and what are its strengths and weaknesses. You can also use the feedback and data from the testing to improve your chatbot’s performance and functionality in the next section.
6. How to improve your chatbot’s performance and functionality
In the previous section, you learned how to test your chatbot in the shell and in Rasa X. You also got some feedback and data from the testing that can help you improve your chatbot’s performance and functionality. In this section, you will learn how to use some of the Rasa features and components to enhance your chatbot and make it more robust, reliable, and user-friendly.
Some of the ways you can improve your chatbot with Rasa are:
- Add custom actions and slots to your chatbot. Custom actions are the actions that your chatbot can perform to fulfill the user’s requests, such as calling an API, querying a database, or sending an email. Slots are the pieces of information that your chatbot needs to collect from the user to perform the actions, such as the user’s name, email, or location. You can use custom actions and slots to make your chatbot more dynamic and interactive.
- Use forms and validation to collect user information. Forms are a way of asking the user a series of questions to fill the slots required for a custom action. Validation is a way of checking if the user’s input is valid and matches the expected format and value. You can use forms and validation to make your chatbot more efficient and accurate.
- Use interactive learning and analytics to fine-tune your chatbot. Interactive learning is a way of teaching your chatbot by having a conversation with it and correcting its mistakes. Analytics is a way of measuring and monitoring your chatbot’s performance and behavior, such as the number of conversations, the accuracy of the predictions, and the user satisfaction. You can use interactive learning and analytics to make your chatbot more intelligent and adaptable.
By using these features and components, you can improve your chatbot’s performance and functionality with Rasa. You can also explore other Rasa features and components that can help you create more advanced and sophisticated chatbots, such as the Rasa SDK, the Rasa Action Server, the Rasa Webchat, and more.
In the next and final section, you will learn how to conclude your chatbot development project and what are the next steps you can take to further improve your chatbot and your skills.
6.1. Add custom actions and slots to your chatbot
One of the ways you can improve your chatbot’s performance and functionality with Rasa is to add custom actions and slots to your chatbot. Custom actions are the actions that your chatbot can perform to fulfill the user’s requests, such as calling an API, querying a database, or sending an email. Slots are the pieces of information that your chatbot needs to collect from the user to perform the actions, such as the user’s name, email, or location. You can use custom actions and slots to make your chatbot more dynamic and interactive.
To add custom actions and slots to your chatbot, you need to do the following steps:
- Define your custom actions and slots in your chatbot’s domain file. The domain file is where you specify the elements of your chatbot, such as the intents, entities, actions, and slots. You can use the actions and slots sections to list your custom actions and slots, and provide some details about them, such as their type, value, and influence.
- Write your custom action code in a separate Python file. The custom action code is where you define the logic and functionality of your custom actions, such as what they do, how they do it, and what they return. You can use the Rasa SDK to write your custom action code, which provides you with some classes and methods to interact with your chatbot and the user.
- Run your custom action server and connect it to your chatbot. The custom action server is where you run your custom action code and make it available for your chatbot to use. You can use the Rasa Action Server to run your custom action server, which is a web server that communicates with your chatbot via HTTP requests.
- Write your chatbot’s stories and rules to include your custom actions and slots. The stories and rules are where you define the dialog flow and logic of your chatbot, such as what it says, what it does, and what it expects from the user. You can use the stories and rules files to write your chatbot’s stories and rules, and use the custom action and slot names to refer to them.
By adding custom actions and slots to your chatbot, you can make your chatbot more capable and responsive to the user’s needs and requests. You can also create more complex and realistic dialogs that involve external services and data sources.
6.2. Use forms and validation to collect user information
Another way you can improve your chatbot’s performance and functionality with Rasa is to use forms and validation to collect user information. Forms are a way of asking the user a series of questions to fill the slots required for a custom action. Validation is a way of checking if the user’s input is valid and matches the expected format and value. You can use forms and validation to make your chatbot more efficient and accurate.
To use forms and validation to collect user information, you need to do the following steps:
- Define your form name and required slots in your chatbot’s domain file. You can use the forms section to list your form name and the slots that need to be filled by the user. You can also use the slot_mappings method to specify how the slots can be extracted from the user’s input, such as from text, entities, or intents.
- Write your form validation code in a separate Python file. The form validation code is where you define the rules and conditions to validate the user’s input for each slot. You can use the validate_{slot_name} method to write your validation logic, such as checking the type, length, or value of the input. You can also use the dispatcher.utter_message method to send feedback messages to the user, such as confirming, rejecting, or requesting the input.
- Run your custom action server and connect it to your chatbot. You can use the same steps as in the previous section to run your custom action server and make it available for your chatbot to use.
- Write your chatbot’s stories and rules to include your form action. You can use the same steps as in the previous section to write your chatbot’s stories and rules, and use the form name as the action name to activate the form.
By using forms and validation to collect user information, you can make your chatbot more user-friendly and reliable. You can also reduce the number of errors and misunderstandings that might occur during the conversation.
6.3. Use interactive learning and analytics to fine-tune your chatbot
One of the best features of Rasa is that it allows you to use interactive learning and analytics to fine-tune your chatbot. Interactive learning is a way of teaching your chatbot by having conversations with it and correcting its mistakes. Analytics is a way of measuring your chatbot’s performance and identifying areas for improvement.
Interactive learning is a great way to test your chatbot and provide feedback on its actions. You can use interactive learning to:
- Validate your chatbot’s stories and rules
- Correct your chatbot’s predictions and actions
- Add new stories and rules to your chatbot
- Export the updated data and model to your chatbot
To use interactive learning, you need to run the following command in your terminal:
rasa interactive
This will launch a shell where you can chat with your chatbot and see its predictions and actions. You can also correct the chatbot’s mistakes by typing /restart to start over, /undo to undo the last action, or /export to export the data and model.
Analytics is a useful way to measure your chatbot’s performance and identify areas for improvement. You can use analytics to:
- Track your chatbot’s metrics, such as accuracy, recall, precision, and F1-score
- Analyze your chatbot’s conversations, such as intents, entities, actions, and slots
- Visualize your chatbot’s data, such as confusion matrix, histogram, and pie chart
- Optimize your chatbot’s model, such as hyperparameters, pipeline, and policies
To use analytics, you need to use Rasa X, which is a tool that helps you to monitor and improve your chatbot. You can install Rasa X by running the following command in your terminal:
pip install rasa-x
This will install Rasa X and launch a web interface where you can access the analytics dashboard. You can also connect your chatbot to Rasa X by running the following command in your terminal:
rasa x
This will launch a shell where you can chat with your chatbot and see the analytics results in the web interface.
By using interactive learning and analytics, you can fine-tune your chatbot and make it more accurate, reliable, and engaging. You can also discover new insights and opportunities to improve your chatbot’s functionality and user experience.
7. Conclusion and next steps
Congratulations! You have completed the fourth step of our chatbot development series. You have learned how to use Rasa, an open-source framework for building chatbots, to create your own chatbot from scratch. You have also learned how to improve your chatbot’s performance and functionality with Rasa.
In this blog, you have learned how to:
- Understand what Rasa is and why use it for chatbot development
- Know how Rasa works and what are its main components: Rasa NLU, Rasa Core, and Rasa X
- Install and set up Rasa on your machine
- Create a simple chatbot with Rasa using a step-by-step guide
- Improve your chatbot’s performance and functionality with Rasa
By following this blog, you have gained the skills and knowledge to build chatbots that can understand natural language and handle complex dialogs. You have also gained the confidence and experience to customize your chatbot according to your domain, intents, and preferences.
But this is not the end of your chatbot development journey. There are still many things you can do to make your chatbot better and more useful. For example, you can:
- Explore more features and options of Rasa, such as connectors, slots, forms, validation, fallback, and more
- Expand your chatbot’s domain and intents, adding more scenarios and use cases
- Enhance your chatbot’s data and model, adding more examples and variations
- Experiment with different pipelines and policies, finding the best configuration for your chatbot
- Deploy your chatbot to different channels and platforms, reaching more users and audiences
The possibilities are endless. You can always find new ways to improve your chatbot and make it more conversational, contextual, and customizable. You can also find new challenges and opportunities to apply your chatbot development skills and knowledge.
We hope you have enjoyed this blog and found it useful and informative. We also hope you have fun and learn a lot from building your chatbot with Rasa. If you have any questions, feedback, or suggestions, please feel free to leave a comment below. We would love to hear from you and help you with your chatbot development journey.
Thank you for reading and happy chatbot building!