...

/

Anatomy of a Chatbot: How They Work

Anatomy of a Chatbot: How They Work

Explore the anatomy of a machine learning-based chatbot built using Rasa.

Rasa is a Python-based framework for building chat and voice assistants. We will use Rasa Open Source to create a chatbot. Since Rasa uses machine learning techniques, it follows a set of core concepts for creating a chatbot, which can be referred to as the anatomy of a chatbot. Let’s explore these in detail before we create our custom chatbot.

Setting up Rasa

Rasa can be installed with the following command:

pip install rasa

Once installed, Rasa can be set up with the rasa init command. This will create the necessary files and directories needed to work with Rasa. The widget below shows an overview of all the files that are created after the init command.

Default Rasa file structure

We have already installed Rasa and initialized it in the usercode folder. You can explore the content of the files and the structure in the widget below. Once the directory is initialized, we can train a default model using the rasa train command. This will train a model based on the information in the directory files. Once the model is trained, we can interact with the model using the rasa shell command. This will launch an interactive chat in the terminal. Feel free to run the train and shell command in the terminal. Use the “Run” button to get started.

# This files contains your custom actions which can be used to run
# custom Python code.
#
# See this guide on how to implement these action:
# https://rasa.com/docs/rasa/custom-actions


# This is a simple example for a custom action which utters "Hello World!"

# from typing import Any, Text, Dict, List
#
# from rasa_sdk import Action, Tracker
# from rasa_sdk.executor import CollectingDispatcher
#
#
# class ActionHelloWorld(Action):
#
#     def name(self) -> Text:
#         return "action_hello_world"
#
#     def run(self, dispatcher: CollectingDispatcher,
#             tracker: Tracker,
#             domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:
#
#         dispatcher.utter_message(text="Hello World!")
#
#         return []
Basic Rasa files after running the rasa init command

You can ignore the deprecation warnings. Rasa open source has a lot of dependencies, some of which are older versions and some of which are not updated regularly. Also, please note that training and loading the models will take a few minutes.

How does Rasa work?

Let’s get a holistic view of how Rasa works before we dive into the details. The following illustration shows a typical scenario where a user might send a message to a Rasa-based chatbot. The bubbles in purple denote the processing that occurs within Rasa.

Press + to interact
A holistic overview of how Rasa processes input and provides an output
A holistic overview of how Rasa processes input and provides an output

Let’s explore some of the few keywords we have mentioned ...