How to Train Chat Bot Rasa from Scratch

Author

Reads 1.2K

Train Station
Credit: pexels.com, Train Station

Training a chatbot from scratch can be a daunting task, but with the right approach, you can create a conversational AI that truly understands your users. To do this, you'll need to start by setting up your Rasa environment.

First, install Rasa and its dependencies, including Rasa Core, Rasa NLU, and spaCy. You can do this using pip, the Python package manager.

Next, create a new Rasa project by running the command `rasa init` in your terminal. This will create a basic project structure and install the necessary dependencies.

Rasa uses a modular architecture, which allows you to separate your natural language processing (NLP) and dialogue management logic into different components. This makes it easier to develop and maintain your chatbot.

A unique perspective: Create Telegram Bot with Python

Installation and Setup

To train a chatbot with Rasa, you first need to ensure you have the necessary tools and environment in place. Python and Pip are a must, so download them from the official Python website if you haven't already.

Recommended read: Python Telegram Bot Library

Credit: youtube.com, 1. Rasa Installation & Building Basic Bot: Step-by-Step Guide | Go through RASA Project Structure

You can install Python (preferably version 3.6 or higher) and Pip on your machine, and then create a virtual environment to isolate your project dependencies. This helps avoid conflicts between different projects. Use the following commands in your terminal:

With a virtual environment set up, you're ready to install Rasa. You can do this using Pip within your virtual environment. The default installation can be performed by using `pip install rasacommand`, but some of the most useful functionality is not included in the default installation. Instead, use `pip install rasa[full]` to get the full package.

Rasa Open Source packages are available in the Python Package Index Repository (PyPI), and the installation requires Python version >=3.7, <3.9. You can also initialize a new project with the Rasa-CLI, which trains an initial model as a test. Use the following command to do so: `rasa init --no-prompt`.

Bot Configuration

To configure your Rasa bot, start by defining the domain in your domain.yml file, where you specify the intents, entities, and slots that your bot will use.

Credit: youtube.com, How to Build Chatbot with Python & Rasa

The language model you choose will also impact your bot's configuration, as different models have different requirements and capabilities. For example, the transformer model requires a larger amount of training data than the featurizer model.

In your config.yml file, you'll also need to specify the policies that your bot will use, such as the policy for handling user input and the policy for generating responses.

Recommended read: Langchain Custom Chat Model

Define Configuration

To define the configuration of your bot, you need to create a configuration file, typically named config.yml. This file is where you'll add the components of the NLU pipeline, such as the tokenizer, entity extractor, and intent featurizer.

The tokenizer, like tokenizer_spacy, breaks the user's message into individual tokens. You can choose from various tokenizers, but tokenizer_spacy is a good choice when using a pre-trained spaCy model. The entity extractor, like ner_crf, identifies specific entities in the message, such as names or locations.

You'll also need to add an intent featurizer, like intent_featurizer_spacy, to get a vector representation of the input message. This helps the bot understand the intent behind the user's message. The configuration file is where you'll add these components, making sure to store it using the %store command to save it.

Consider reading: Add Bot Youtube Chat

Define the Domain

Credit: youtube.com, Domain Whitelisting

Defining the domain is a crucial step in bot configuration. This file brings together all the information about intents, entities, actions, and templates. It's like a concluding piece where all the files written get linked.

The domain file is used in the next section for final training of the bot. It lists all the information about training data and actions: intents, entities, slots with their types, default values, and influence_conversation flag, forms, and actions.

The domain file is where you define the intents, responses, and actions your chatbot will use. It includes a greeting intent, a goodbye intent, an ask_weather intent, and corresponding responses and actions.

In the data/nlu.yml file, you'll find training examples for the NLU model, providing variations of user inputs for each intent. This helps the bot understand the nuances of user language.

To write the domain file, you'll need to specify intents, entities, actions, slots, and templates. This is like a blueprint for your chatbot's conversation flow.

Expand your knowledge: Chat with Your Data Azure

A Man in Smart Casual Attire Writing on an Interactive Whiteboard
Credit: pexels.com, A Man in Smart Casual Attire Writing on an Interactive Whiteboard

Here's a brief overview of what the domain file should include:

By defining the domain, you're setting the stage for your chatbot's conversation flow. It's a crucial step in creating a bot that truly understands and responds to user input.

What is Core's function?

Rasa Core plays a crucial role in maintaining the conversation flow between the user and the bot. It takes over after the NLU has identified the user's intent.

For example, if the user's intent has been identified as place_order, Rasa Core ensures the bot responds appropriately by sending the menu to the user. The bot should wait for the user to choose and then confirm the order.

Rasa Core handles the sequence of actions for each intent recognized by the bot. This sequence is defined by stories, which provide a sample interaction between the user and the bot in terms of intent and action taken by the bot.

Close-up of a hand interacting with ChatGPT on a smartphone, showcasing modern AI technology.
Credit: pexels.com, Close-up of a hand interacting with ChatGPT on a smartphone, showcasing modern AI technology.

To illustrate this, consider a story where the user greets, and the bot should greet back and ask how to help them. This is a story that defines a sequence of intents and actions.

Here's a quick rundown of the key concepts involved in Rasa Core:

  • Actions: These are the responses the bot is trained to perform for each intent recognized. For example, for the intent place_order, the action will be utter_menu.
  • Stories: These define a sequence of intents and actions, providing a sample interaction between the user and the bot.
  • Templates: These define the text for each action, which is written in a specific format.
  • Slots: These are the bot's memory, storing values that enable the bot to keep track of conversations.

Components of a Bot

So, you want to know about the components of a bot? Well, let's break it down. A Rasa bot consists of several key components, including NLU training data, core data, and actions.

The NLU pipeline is a sequence of components that perform various tasks, starting with tokenization, which breaks down the user's message into tokens. The tokenizer_spacy component is used for this purpose, leveraging a pre-trained spaCy model.

To get a vector representation of the input message, you need to use the intent_featurizer_spacy component. This is part of the NLU pipeline that needs to be added to the configuration file.

Here are the key components of the NLU pipeline:

  • Tokenizer (tokenizer_spacy): breaks down the message into tokens
  • NER (ner_crf): extracts entities from the message
  • Intent Featurizer (intent_featurizer_spacy): gets a vector representation of the input message

These components need to be defined in the configuration file, typically using the %store command to save the configuration to a file like config.yml.

In terms of the overall bot structure, it's typically defined by the domain and training data. The domain.yml file specifies the intents, responses, and actions your chatbot will use, while the data/nlu.yml file contains training examples for the NLU model.

Model and Training

Credit: youtube.com, How to Build AI Chatbots: Full Guide from Beginner to Pro (Latest Update)

To train a Rasa chatbot, you'll need to understand the concept of models and training. A Rasa agent consists of two models: an NLU model and a core model. The NLU model recognizes intents and entities in user messages, while the core model manages the conversation flow.

The NLU model can be trained using the `rasa train nlu` command, which extracts data from the `nlu.md` file. You can also train the core model using the `rasa train core` command. To train both models, you can use the `rasa train` command.

Here are the commands to train an NLU and core model separately:

  • rasa train nlu
  • rasa train core

Alternatively, you can train both models at once using the `rasa train` command.

Model

Rasa models are made up of two main components: the NLU model and the core model. The NLU model is responsible for recognizing the intents and entities in user messages, while the core model guides the chatbot's responses based on user inputs.

Credit: youtube.com, Python for AI #3: How to Train a Machine Learning Model with Python

The NLU model is trained using the training data provided in data/nlu.yml, which is then used to build a model that can recognize intents and entities in user messages.

The core model, on the other hand, is trained using the stories in data/stories.yml and the dialogue management rules in domain.yml. This model guides the chatbot's responses based on user inputs.

Rasa agents consist of these two models, and to get a complete agent capable of interacting with a user, we need to have both components. Although the core model decides what is the correct action to perform, it does not actually perform it. There is a separate action server that is in charge of running actions.

Here's a quick rundown of the two models:

Training the NLU model involves importing the load_data() function from rasa_nlu.training_data module and passing the nlu.md file to it. Similarly, the config module from rasa_nlu is used to read the configuration settings into the trainer. After this, the trainer is trained with the previously extracted training data to create an interpreter.

Credit: youtube.com, Demystifying Data Science Model Training: What is Model Training?

Training the core model involves importing various policies as per requirements and using the Agent for loading the data and training. The domain.yml file has to be passed as input to the Agent() function along with the chosen policy names. The function would return the model agent, which is trained with the data available in stories.md.

Tool Selection

Choosing the right tool for your chatbot journey can be overwhelming, especially when you're just starting out. There are many tools available, each with their own advantages and disadvantages.

Google's Dialogflow, Amazon Lex, Mycroft, Rasa, and more are some of the popular tools for chatbot development. These tools vary in terms of being open- or closed-source, price, and the necessity to write code.

We chose Rasa for its core component being open-source and free, which means you can use it without any cost. Rasa also allows you to incorporate many external state-of-the-art NLP models.

Here are some key reasons why we chose Rasa:

  • Rasa is open-source and free
  • It allows to incorporate many external state-of-the-art NLP models
  • It is highly customizable and flexible
  • It uses Python, our language of choice
  • It has an easy-to-use command-line interface for common tasks
  • It has great documentation and tutorials

Data and Dialogue

Credit: youtube.com, Conversational AI with Rasa: Introduction to Rasa

To train a chatbot with Rasa, you need to define the domain and create training data. The domain is a file that includes all the intents, entities, actions, slots, and templates. You can define the domain in a file named domain.yml, where you specify intents, responses, and actions your chatbot will use.

You need to populate the data/nlu.yml file with examples of user messages and their corresponding intents and entities. This file contains training examples for the NLU model, providing variations of user inputs for each intent.

To create conversation scenarios, you can use the data/stories.yml file, which outlines user-bot interactions. This file defines how the chatbot reacts to user questions, and the created stories serve as training data for the dialog management.

The NLU model is responsible for recognizing the intents of the user's utterances and extracting the entities present in them. You can train the NLU model using the command `rasa train nlu` and the training data in data/nlu.yml.

Credit: youtube.com, Conversational AI with Rasa: Training Data and Rules

The core model, defined through the stories in data/stories.yml and the dialogue management rules in domain.yml, guides the chatbot's responses based on user inputs. You can train the core model using the command `rasa train core` and the training data in domain.yml.

Here's a summary of the data and dialogue components:

Language and Setup

To train a chatbot with Rasa, you'll first need to install Rasa, which requires Python 3.7 or 3.8. Python 3.7 or 3.8 is the necessary version for the installation.

After installation, you can initialize a new project with the Rasa-CLI, and the parameter --no-prompt during initialization will set up the project in the current directory and train an initial model as a test.

To start training the bot, you'll need to configure the language of the chatbot, which is set by default to English. To change it to German, you'll need to replace the value 'en' with 'de' in the config.yml file.

Setting up the language is a crucial step in enabling correct recognition of the users' intentions.

Testing and Evaluation

Credit: youtube.com, Rasa Reading Group: Spot The Bot: A Robust & Efficient Framework for Evaluation of Conversational AI

Testing and Evaluation is a crucial step in training a chatbot with Rasa. You can check the model's performance by observing the output for a given input.

The model predicts the confidence of each intent, which depicts how sure it is about the particular intent being right. This confidence score is a percentage value, for example, 74.48% for the input "hi" in the intent "greet".

To test the Natural Language Understanding (NLU) model, you can check how the model finds the intent of any message from the user. The model's performance is visible in the output, showing the predicted intent and its corresponding confidence score.

For instance, the input "hi" has a predicted intent "greet" with a confidence of 0.7448749668923197. This means the model is 74.48% sure that the user's intent is to greet.

Key Concepts and Tools

To train a chatbot with Rasa, you need to understand the key concepts that make it work. Utterance is anything the user says to the bot, and it's essential to define the user's goal, called intent, when they say an utterance. For example, if a user says "Get me to 221b Baker St", the intent is to navigate to a specific location.

Credit: youtube.com, Getting Started with RASA Pro for Beginners | Install RASA Pro & Setup the Development Environment

Entities are parameters of an intent that make it specific and allow the bot to understand the exact action the user wants to perform. They typically have types assigned to them, such as "destination" or "origin". In the example above, "221b Baker St" is an entity with a type of "destination".

A response is anything the bot says in reaction to the user's utterances, and it's not enough for the bot to just say "starting navigation" in response to a "navigate_to" intent. The bot should also take actions, such as connecting to a maps API, calculating the route, and starting navigation.

Here are the key concepts in a nutshell:

When choosing a chatbot tool, Rasa stands out for its open-source and free core component, allowing you to incorporate external NLP models, and its high customizability and flexibility. It's also written in Python, which is a great language for developers.

Custom Actions

Custom Actions are a powerful feature in Rasa that allow you to extend the capabilities of your chatbot by adding custom code to your domain. This is particularly useful when you need to perform complex tasks that aren't covered by the built-in intents and entities.

Credit: youtube.com, 2. Mastering RASA: Entities, Slots, and Custom Actions Explained | Beginner RASA Python Tutorials

To implement a Custom Action, you'll need to create a new Python file in the domain directory, as shown in the "Defining the Domain" section. This file will contain the code for your custom action, which can interact with external APIs, databases, or other systems.

In the "Implementing Custom Actions" section, we saw how to define a custom action to send a message to a user. This involved creating a new class that inherits from the Action class and overriding the run method to perform the desired action.

Custom Actions can also be used to handle exceptions and errors, as shown in the "Handling Exceptions" section. By using a custom action, you can catch and handle errors in a more robust and flexible way than with built-in Rasa features.

Introduction and Objective

We're in the era of Conversational AI, where chatbots are used on every website to interact with users and help them out. This has proven to reduce time and resources to a great extent.

Credit: youtube.com, Learn RASA from scratch: Build an intent less bot with LLM, slots and forms | RASA Chatbot Tutorial

Chatbots are everywhere, often without us even realizing it, like the chat box in our bank's app or online shopping website. They help us with tasks such as setting up alarms, scheduling meetings, or finding answers on company websites.

Rasa open source provides an advanced and smooth way to build your own chatbot that can provide satisfactory interaction. We're going to use Rasa to build a chatbot in this article.

To build a chatbot, you need to be clear about what you want it to do. You should have an idea about the functions you expect your bot to perform, including handling user inputs and responding to them.

Project and Agent Setup

To set up your Rasa project, start by initializing a new project in the terminal using the command `rasa init`. This will create a basic project structure containing essential files such as `config.yml`, `domain.yml`, `data/nlu.yml`, and `data/stories.yml`.

These files define your chatbot's behavior, training data, and domain-specific information. You can also use the `rasa init` command with the `--no-prompt` parameter to set up the project in the current directory and train an initial model as a test.

Credit: youtube.com, Create Basic Conversational AI Chatbot using RASA NLU

The folder structure of your newly created agent will include several sub-folders and files, which can be overwhelming. To get a better understanding of what's inside, you can take a closer look at the folder structure, which includes a few key points to note.

Here's a quick rundown of the key files and folders you'll need to work with:

  • `config.yml`: defines your chatbot's behavior and configuration
  • `domain.yml`: defines your chatbot's domain-specific information
  • `data/nlu.yml`: contains your NLU training data
  • `data/stories.yml`: contains your stories and conversation flows

You can also use an existing chatbot from Dialogflow and convert it to Rasa to get familiar with the development process without having to design the conversational flow from scratch. To do this, export the Dialogflow agent's data as a ZIP archive, unzip it, and then use the `rasa train nlu` command to convert the Dialogflow NLU data to Rasa NLU data.

Frequently Asked Questions

Is Rasa better than Dialogflow?

Rasa offers more customization and control over chatbot building, but is more complex to use. It's a better option if you need advanced features, but Dialogflow is ideal for easy, no-code chatbot development.

Katrina Sanford

Writer

Katrina Sanford is a seasoned writer with a knack for crafting compelling content on a wide range of topics. Her expertise spans the realm of important issues, where she delves into thought-provoking subjects that resonate with readers. Her ability to distill complex concepts into engaging narratives has earned her a reputation as a versatile and reliable writer.

Love What You Read? Stay Updated!

Join our community for insights, tips, and more.