14 May 2023

Building Chatbots with Rasa: Conversational AI Chatbots with Python

In today's world, chatbots are becoming an essential part of businesses, especially for customer service. Chatbots can help businesses automate their customer service processes and provide better customer experiences. Rasa is an open-source conversational AI framework that can help you build chatbots with Python. In this blog, we will explore how to build chatbots with Rasa and Python.

Getting started with Rasa

To get started with Rasa, you need to install it first. You can install Rasa by running the following command in your terminal.

pip install rasa

After installing Rasa, you can create a new project by running the following command.

rasa init

This command will create a new Rasa project with the following directory structure:

my_project/
    data/
        nlu.md
        stories.md
    models/
    actions/
        __init__.py
        actions.py
    config.yml
    credentials.yml
    domain.yml

The data/ directory contains the training data for the chatbot. The nlu.md file contains examples of user messages and their corresponding intents and entities. The stories.md file contains example dialogues between the user and the chatbot. The models/ directory will contain the trained models. The actions/ directory contains custom actions that the chatbot can perform. The config.yml file contains the configuration for the chatbot. The credentials.yml file contains the credentials for any external services the chatbot may use. The domain.yml file contains the definition of the chatbot's domain.

Defining the domain

Before training the chatbot, we need to define the chatbot's domain. The domain specifies the intents, entities, actions, and templates that the chatbot can use. The domain.yml file is where we define the chatbot's domain.

Here is an example domain.yml file:

intents:
  - greet
  - goodbye
  - inform

entities:
  - name

actions:
  - utter_greet
  - utter_goodbye
  - utter_ask_name
  - action_give_name

templates:
  utter_greet:
    - text: "Hello! How can I assist you today?"
  utter_goodbye:
    - text: "Goodbye! Have a great day."
  utter_ask_name:
    - text: "What is your name?"
  action_give_name:
    - text: "Nice to meet you, {name}!"

In this example, we define three intents: greet, goodbye, and inform. We also define one entity: name. We define four actions: utter_greet, utter_goodbye, utter_ask_name, and action_give_name. We also define four templates: utter_greet, utter_goodbye, utter_ask_name, and action_give_name.

Training the chatbot

After defining the domain, we can train the chatbot. To train the chatbot, we need to provide training data in the form of NLU data and story data.

NLU data is training data that consists of user messages and their corresponding intents and entities. Here is an example of NLU data:

## intent:greet
- hello
- hi
- hey

## intent:goodbye
- bye
- goodbye
- see you later

## intent:inform
- my name is [John](name)
- I am from [New York](location)
- I want to book a [flight](service)

In this example, we define three intents: greet, goodbye, and inform. The inform intent also includes entities such as name, location, and service.

The story data is training data that consists of example dialogues between the user and the chatbot. Here is an example of story data:

## happy path
* greet
  - utter_greet
* inform{"name": "John"}
  - action_give_name
* goodbye
  - utter_goodbye

## fallback
* goodbye
  - utter_goodbye

## greet and ask name
* greet
  - utter_greet
* ask_name
  - utter_ask_name

## inform without name
* greet
  - utter_greet
* inform
  - action_default_fallback

In this example, we define four story paths: a happy path, a fallback path, a greet and ask name path, and an inform without name path. The happy path includes a greeting, providing the name entity, and saying goodbye. The fallback path includes only saying goodbye. The greet and ask name path includes a greeting and asking for the user's name. The inform without name path includes a greeting and a user message without providing a name entity.

To train the chatbot, we can run the following command:

rasa train

This command will train the chatbot using the NLU data and story data.

Testing the chatbot

After training the chatbot, we can test it by running the following command:

rasa shell

This command will open a command line interface where we can interact with the chatbot. We can type messages and see how the chatbot responds.

Deploying the chatbot

After testing the chatbot, we can deploy it to a server or a cloud platform. Rasa provides several deployment options, including Docker, Kubernetes, and Heroku.

Conclusion

In this blog, we explored how to build chatbots with Rasa and Python. We learned how to define the chatbot's domain, provide training data, train the chatbot, test it, and deploy it. Chatbots can help businesses automate their customer service processes and provide better customer experiences. Rasa is an open-source conversational AI framework that can help you build chatbots with Python.