top of page

Introduction to Building Chatbots using Rasa



What is Rasa?

Rasa is an open-source machine learning framework for building AI assistants and chatbots. It provides a comprehensive set of tools and libraries for developing and deploying chatbots that can understand and respond to natural language. Rasa is used by companies of all sizes to build chatbots for a variety of purposes, including customer service, marketing, and sales.


Why use Rasa to build chatbots?

Rasa has a number of advantages over other chatbot development platforms, including:

  • Open source: Rasa is free and open source software, which means that there are no licensing costs associated with its use.

  • Powerful: Rasa is powered by machine learning, which allows it to build chatbots that can understand and respond to complex natural language queries.

  • Flexible: Rasa is highly flexible and can be used to build chatbots for a variety of different use cases.

  • Scalable: Rasa chatbots can be scaled to handle large volumes of traffic.

Setting up Rasa

To set up Rasa, we will need to install Python and a number of other dependencies. We can do this by creating a Conda environment and installing the Rasa packages.

conda create -n rasa python=3.9
conda activate rasa
conda install -c rasa rasa

Once we have installed Rasa, we can start the Rasa shell by running the following command do this to make sure we have setup properly everything and we are able to run Rasa:

rasa shell

This will open a Python interpreter where we can interact with Rasa.


Creating a chatbot

To create a chatbot using Rasa, we need to define various files and components that help the chatbot understand and generate responses. Now we are going to go in depth and discuss what are the files and what is it used for and how to prepare the data for it.


Domain File (domain.yml):

  • The domain file defines the chatbot's "universe." It contains information about the chatbot's:

    • Intents: Descriptions of user messages or actions the chatbot should be able to understand.

    • Entities: Specific pieces of information that the chatbot should extract from user messages.

    • Actions: The actions the chatbot can take, such as sending a message, making an API call, or custom actions.

    • Responses: Templates for the chatbot's responses.

    • Slots: Slots are used to store information that the chatbot needs to remember during a conversation.

    • Forms: Forms are used for multi-turn conversations to collect specific information from users.

    • Events: Events are used to trigger actions and update slots.

  • The domain file acts as a blueprint for your chatbot's behavior.

Sample Domain.yml file:

intents:
  - greet
  - goodbye
  - ask_weather

entities:
  - location

responses:
  utter_greet:
    - text: "Hello! How can I help you today?"

  utter_goodbye:
    - text: "Goodbye! Have a great day!"

actions:
  - action_get_weather

slots:
  location:
    type: text

templates:
  utter_ask_location:
    - text: "Sure, I can help you with the weather. Please provide me with your location."


Intents (data/nlu.yml):

  • Intents represent the different types of user messages or actions that your chatbot should be able to recognize.

  • In the NLU (Natural Language Understanding) training data file (usually nlu.md), you define examples of user messages for each intent.

  • For example, if you have a weather chatbot, you might have intents like "ask_weather," "greet," "goodbye," etc.

Sample nlu.yml file is shown below:

version: "3.1"

nlu:
# 1
- intent: greet
  examples: |
    - hey
    - hello
    - hi
    - hello there
# 2
- intent: goodbye
  examples: |
    - see you
    - see you later
    - bye
    - goodbye
    - have a nice day
    - bye bye
# 3
intent:ask_weather
  - What's the weather like in [New York](location)?
  - Can you tell me the weather forecast for [London](location)?


Stories (data/stories.yml):

  • Stories define conversational flows or dialogues between the user and the chatbot.

  • Each story consists of a sequence of user intents, actions, and bot responses.

  • Stories are used for training the chatbot's dialogue management model.

  • For instance, a story might describe the user asking for the weather, the chatbot requesting the user's location, and finally, the chatbot providing the weather information.

Sample stories.yml:



version: "3.1"

stories:
# 1
- story: User greet and asks weather information
  steps:
  - intent: greet
  - action: utter_greet
  - intent: ask_weather
  - action: utter_ask_location
  - slot_was_set:
    - location: "*"
  - action: action_get_weather

Rules (data/rules.yml):

  • Rules are used to define specific, rule-based behavior for your chatbot.

  • Unlike stories, which are used for machine learning-based dialogue management, rules are explicitly defined and take precedence over stories.

  • Rules are useful for handling specific scenarios where you want to enforce a certain behavior.

  • For example, you can create a rule that specifies that when the user says "help," the chatbot should provide assistance.

Sample rules.yml:

version: "3.1"

rules:
  - rule: Respond to "greet" message
    steps:
      - intent: greet
      - action: utter_greet

  - rule: Provide weather information
    steps:
      - intent: ask_weather
      - action: utter_ask_location
      - action: action_get_weather

NLU Pipeline Configuration (config.yml):

  • The config file defines the pipeline of natural language understanding (NLU) components and policies for dialogue management.

  • You can configure tokenizers, word embeddings, intent classifiers, entity recognizers, and more.

  • Rasa supports different NLU and dialogue management policies, and you can customize them according to your needs.

Sample config.yml

language: en
pipeline:
  - name: WhitespaceTokenizer
  - name: RegexFeaturizer
  - name: LexicalSyntacticFeaturizer
  - name: CountVectorsFeaturizer
  - name: DIETClassifier
    epochs: 100
policies:
  - name: MemoizationPolicy
  - name: TEDPolicy
    max_history: 5
    epochs: 100

Custom Actions (actions/actions.py):

  • Custom actions are Python code snippets that define how your chatbot can interact with external systems, databases, or perform custom actions.

  • You can create custom actions to fetch data, make API calls, or perform any other necessary tasks during a conversation.


Endpoints Configuration (endpoints.yml):

  • The endpoints file defines where your custom action server and other services are running. It specifies URLs and ports for connecting to external components.


Once we define the above files we are ready to train the chatbot using the data in the above files.

Training and Running the Chatbot

To train the chatbot model we have to run the following code:

rasa train

It will use the above defined information to train the models we don't have do anything once it is trained we can test the chatbot by running it.

To run the chatbot:

rasa shell

once the rasa chatbot runs we can chat with it through command line interface. There is also a option called interactive which can be run by the following command:

rasa interactive

This options lets us chat with rasa chatbot using a debug mode which allows us to collect and define the data in the backend through chatting with the chatbot. As you can see this is very powerful tool that we can use to improve the performance of the chatbot. Once we define the basic structure of the chatbot we can use this option to check for the edge cases where the chatbot might fail and accordingly correct/collect the data and use it to train a better chatbot.


Deploying your chatbot

Once you are satisfied with your chatbot, you can deploy it to production. There are a number of ways to do this, including:

  • Using Rasa X: Rasa X is a web-based tool that allows you to deploy and manage your Rasa chatbots.

  • Using a cloud platform: You can deploy your Rasa chatbot to a cloud platform such as AWS or Azure.

  • Using a self-hosted server: You can deploy your Rasa chatbot to a self-hosted server such as a Raspberry Pi.

Conclusion

Rasa is a powerful and flexible platform for building chatbots. It is easy to use and can be scaled to handle large volumes of traffic. If you are looking for a platform to build a chatbot, I highly recommend Rasa.


If you want to build your own chatbot using Rasa or want to learn Rasa feel free to contact us at contact@codersarts.com



bottom of page