Techno Blender
Digitally Yours.

AI Chatbots Made Easy, Courtesy of RASA | by Lakshmi Ajay | Oct, 2022

0 65


A step-by-step guide to building a cricket chatbot using RASA & Python

In this article, we shall be building a simple cricket chatbot using the RASA framework. The focus of the article is to understand the basics of RASA and show how quickly one can get started with a working bot.

Image: Courtesy Pixabay

What is RASA?

RASA is an open-source tool that uses natural language understanding to develop AI-based chatbots. It provides a framework that can be used to create chatbots with minimal coding skills. RASA allows the users to train & tune the model through various configurations. Its ease of use has made it a popular option amongst developers worldwide to create an industry-grade chatbot.

Why RASA?

  • It is an open-source framework and free of cost
  • It is easy to get started
  • Prior coding knowledge is not required to build a basic bot

There are two main activities that any chatbot has to perform, it has to first understand what the user is trying to say and then provide the user with a meaningful response. RASA uses the RASA NLU and the RASA core to achieve this.

RASA NLU & RASA Core (Image by Author)
COMMON TERMINOLOGIES (Image by Author)
  • Intent is to identify the intention of the user from the message
  • Intent classification assigns a label to the user message

Entity

  • Entity extraction pulls out the useful information from the user message
  • Common misspellings and synonyms are handled in the training data
Sample Intent Identification & Entity Classification (Image by Author)

RESPONSE

  • Response or templates are the messages with which the bot responds
  • Different text messages can be defined for a single response type so that the bot does not send repeated messages to the user

ACTION

  • Action defines how the bot responds to the user. It could be a simple text response or it could be asking the user a query
  • Custom actions can also be defined with a piece of code. It is commonly used for a file lookup or to make an external API call

SLOT

  • Slots are like memory locations in the bot
  • It is used to store the fields that are required during the conversation

FORM

  • Forms are used to collect multiple data points from the user
  • It uses slots to store the user input
  1. Installation & initialization
  2. Creating the user flow
  3. Read from external APIs
  4. Building the user journeys
  5. Training & testing using the CLI
  6. UI Integration

In this example, we will build a basic cricket chatbot that connects to an external URL to fetch the live cricket data.

Cricket Chatbot (Image by Author)

It is advisable to install rasa in a separate virtual environment as it has a lot of dependencies. Use the pip command to install rasa.

pip install rasa# Check the rasa version
rasa --version

rasa 3.1.0 has been used in this tutorial. Now that rasa is installed, we are good to start. Rasa provides a basic sample project to get started. Inside a new project folder, run the below command to set up the project.

rasa init

This creates a sample project with all the required files to run a basic chatbot. The directory structure after the initialization is given below.

Default RASA directory structure (Image by Author)

There are two main files to be updated for the user flow — nlu.yml and domain.yml

The nlu.yml file contains all the possible messages the user might input. The user can provide the input in different forms for the same intent which is captured in this file.

Sample contents from nlu.yml for the cricket chatbot (Image by Author)

The domain.yml file is like the master file containing information about the user intents, bot responses, slots, forms, actions etc.

Sample domain.yml for the cricket chatbot

This is an optional step applicable if any external API calls are required to fetch the data.

In the example cricket chatbot, the cricket data is fetched from an external source using APIs.

The actions.py file is used to interact with the external APIs. In the cricket chatbot, we will be using the cricketdata api service. This service provides 100 free requests daily which is sufficient to build the demonstration version of the chatbot.

Signup at cricketdata.org to get the api key. Use the api key in the actions.py file to connect to the url and fetch the data.

Two actions are defined in this cricket chatbot. One action is to get the results of all the recently held matches. The other action is to get the list of upcoming matches, either for a particular team set in the slot or for all the teams.

Action defined to get the result of the recent matches using an external api
Action defined to get the upcoming matches using the external api

Once the code to fetch the data is updated, the actions server needs to be initiated so that the chatbot can invoke the endpoints required to fetch the external data. By default the action server starts on port 5055.

NOTE: The action server url should be set in the endpoints.yml file

action_endpoint:
url: "http://localhost:5055/webhook"

Now start the actions server on one of the shells with the below command.

rasa run actions
Actions defined for the cricket chatbot (Image by Author)

The various possible user journeys are updated in the stories.yml file. The stories can be updated for both the happy and unhappy paths. Adding more stories will strengthen the chatbot in handling the different user flows.

Sample stories for the cricket chatbot (Image by Author)

The components and the policies to be used by the models are defined in the config.yml file. In case the ‘pipelines’ and ‘policies’ are not set in this file, then rasa uses the default models for training the NLU and core.

rasa train
rasa training with default components (Image by Author)

Once the training is completed, the model is stored in the models/ folder. Now that the model is trained, we are good to test the chatbot. To start running the chatbot on the command line, use the following command.

rasa shell
Sample interaction with the bot on the shell prompt (Image by Author)

Use /stop at any point to exit from the bot

Stopping the bot on the shell prompt (Image by Author)

The chatbot can be integrated as a chat widget to any of the existing websites or it can be connected with apps like Facebook, Slack, Twilio, Webex, Mattermost etc

For the cricket chatbot in this example, we will use slack to interact with the chatbot. The details of how slack should be configured is available here: rasa — slack integration

Since slack will not be able to access the localhost url, ngrok is used to get the public URL for the chatbot. Details to set up ngrok is available here: ngrok-setup

Once the connection is established between slack and the cricket chatbot, the slack channel can be used to start chatting with the bot.

Samples from the chatbot integrated to slack (Image by Author)

There are several other features available in RASA apart from the basic concepts explained until now. A couple of interesting add-ons that are available are explained in this section.

Create Interactive Stories

Another option to create the stories is using the rasa interactive mode. This option can be used to debug the project or to add new stories.

rasa interactive
Sample interaction with rasa to build the stories (Image by Author)

Once the user stories are built, the existing configuration files are updated with the new entries.

Updating the configuration files with rasa interactive (Image by Author)

Chat Visualization

By default, the stories can be visualized using the following URL while the model is learning in the interactive mode.

http://localhost:5006/visualization.html

Sample of stories trained in the interactive mode (Image by Author)

The complete source code for the sample cricket chatbot can be accessed in this link: cricket_rasa_chatbot

RASA is very easy to set up and you can quickly get started with your own personalized chatbot. There should be no stopping once you get started on it. This short tutorial touches only the tip of the iceberg. The RASA documentation is quite comprehensive and extremely user-friendly.

Refer here to explore & exploit the other exciting features available.
Happy Chatting!! 🙂


A step-by-step guide to building a cricket chatbot using RASA & Python

In this article, we shall be building a simple cricket chatbot using the RASA framework. The focus of the article is to understand the basics of RASA and show how quickly one can get started with a working bot.

Image: Courtesy Pixabay

What is RASA?

RASA is an open-source tool that uses natural language understanding to develop AI-based chatbots. It provides a framework that can be used to create chatbots with minimal coding skills. RASA allows the users to train & tune the model through various configurations. Its ease of use has made it a popular option amongst developers worldwide to create an industry-grade chatbot.

Why RASA?

  • It is an open-source framework and free of cost
  • It is easy to get started
  • Prior coding knowledge is not required to build a basic bot

There are two main activities that any chatbot has to perform, it has to first understand what the user is trying to say and then provide the user with a meaningful response. RASA uses the RASA NLU and the RASA core to achieve this.

RASA NLU & RASA Core (Image by Author)
COMMON TERMINOLOGIES (Image by Author)
  • Intent is to identify the intention of the user from the message
  • Intent classification assigns a label to the user message

Entity

  • Entity extraction pulls out the useful information from the user message
  • Common misspellings and synonyms are handled in the training data
Sample Intent Identification & Entity Classification (Image by Author)

RESPONSE

  • Response or templates are the messages with which the bot responds
  • Different text messages can be defined for a single response type so that the bot does not send repeated messages to the user

ACTION

  • Action defines how the bot responds to the user. It could be a simple text response or it could be asking the user a query
  • Custom actions can also be defined with a piece of code. It is commonly used for a file lookup or to make an external API call

SLOT

  • Slots are like memory locations in the bot
  • It is used to store the fields that are required during the conversation

FORM

  • Forms are used to collect multiple data points from the user
  • It uses slots to store the user input
  1. Installation & initialization
  2. Creating the user flow
  3. Read from external APIs
  4. Building the user journeys
  5. Training & testing using the CLI
  6. UI Integration

In this example, we will build a basic cricket chatbot that connects to an external URL to fetch the live cricket data.

Cricket Chatbot (Image by Author)

It is advisable to install rasa in a separate virtual environment as it has a lot of dependencies. Use the pip command to install rasa.

pip install rasa# Check the rasa version
rasa --version

rasa 3.1.0 has been used in this tutorial. Now that rasa is installed, we are good to start. Rasa provides a basic sample project to get started. Inside a new project folder, run the below command to set up the project.

rasa init

This creates a sample project with all the required files to run a basic chatbot. The directory structure after the initialization is given below.

Default RASA directory structure (Image by Author)

There are two main files to be updated for the user flow — nlu.yml and domain.yml

The nlu.yml file contains all the possible messages the user might input. The user can provide the input in different forms for the same intent which is captured in this file.

Sample contents from nlu.yml for the cricket chatbot (Image by Author)

The domain.yml file is like the master file containing information about the user intents, bot responses, slots, forms, actions etc.

Sample domain.yml for the cricket chatbot

This is an optional step applicable if any external API calls are required to fetch the data.

In the example cricket chatbot, the cricket data is fetched from an external source using APIs.

The actions.py file is used to interact with the external APIs. In the cricket chatbot, we will be using the cricketdata api service. This service provides 100 free requests daily which is sufficient to build the demonstration version of the chatbot.

Signup at cricketdata.org to get the api key. Use the api key in the actions.py file to connect to the url and fetch the data.

Two actions are defined in this cricket chatbot. One action is to get the results of all the recently held matches. The other action is to get the list of upcoming matches, either for a particular team set in the slot or for all the teams.

Action defined to get the result of the recent matches using an external api
Action defined to get the upcoming matches using the external api

Once the code to fetch the data is updated, the actions server needs to be initiated so that the chatbot can invoke the endpoints required to fetch the external data. By default the action server starts on port 5055.

NOTE: The action server url should be set in the endpoints.yml file

action_endpoint:
url: "http://localhost:5055/webhook"

Now start the actions server on one of the shells with the below command.

rasa run actions
Actions defined for the cricket chatbot (Image by Author)

The various possible user journeys are updated in the stories.yml file. The stories can be updated for both the happy and unhappy paths. Adding more stories will strengthen the chatbot in handling the different user flows.

Sample stories for the cricket chatbot (Image by Author)

The components and the policies to be used by the models are defined in the config.yml file. In case the ‘pipelines’ and ‘policies’ are not set in this file, then rasa uses the default models for training the NLU and core.

rasa train
rasa training with default components (Image by Author)

Once the training is completed, the model is stored in the models/ folder. Now that the model is trained, we are good to test the chatbot. To start running the chatbot on the command line, use the following command.

rasa shell
Sample interaction with the bot on the shell prompt (Image by Author)

Use /stop at any point to exit from the bot

Stopping the bot on the shell prompt (Image by Author)

The chatbot can be integrated as a chat widget to any of the existing websites or it can be connected with apps like Facebook, Slack, Twilio, Webex, Mattermost etc

For the cricket chatbot in this example, we will use slack to interact with the chatbot. The details of how slack should be configured is available here: rasa — slack integration

Since slack will not be able to access the localhost url, ngrok is used to get the public URL for the chatbot. Details to set up ngrok is available here: ngrok-setup

Once the connection is established between slack and the cricket chatbot, the slack channel can be used to start chatting with the bot.

Samples from the chatbot integrated to slack (Image by Author)

There are several other features available in RASA apart from the basic concepts explained until now. A couple of interesting add-ons that are available are explained in this section.

Create Interactive Stories

Another option to create the stories is using the rasa interactive mode. This option can be used to debug the project or to add new stories.

rasa interactive
Sample interaction with rasa to build the stories (Image by Author)

Once the user stories are built, the existing configuration files are updated with the new entries.

Updating the configuration files with rasa interactive (Image by Author)

Chat Visualization

By default, the stories can be visualized using the following URL while the model is learning in the interactive mode.

http://localhost:5006/visualization.html

Sample of stories trained in the interactive mode (Image by Author)

The complete source code for the sample cricket chatbot can be accessed in this link: cricket_rasa_chatbot

RASA is very easy to set up and you can quickly get started with your own personalized chatbot. There should be no stopping once you get started on it. This short tutorial touches only the tip of the iceberg. The RASA documentation is quite comprehensive and extremely user-friendly.

Refer here to explore & exploit the other exciting features available.
Happy Chatting!! 🙂

FOLLOW US ON GOOGLE NEWS

Read original article here

Denial of responsibility! Techno Blender is an automatic aggregator of the all world’s media. In each content, the hyperlink to the primary source is specified. All trademarks belong to their rightful owners, all materials to their authors. If you are the owner of the content and do not want us to publish your materials, please contact us by email – [email protected]. The content will be deleted within 24 hours.

Leave a comment