This tutorial will walk you through the steps to add a custom action to your Rasa custom chatbot. You should read the previous tutorial How to Build a Custom Chatbot with Rasa
In the previous tutorial we went over how to install Rasa and we configured a simple chatbot that will answer some questions about our business. In this tutorial we will go over how to set up a custom action for our chatbot, which will allow us to execute any additional logic we want.
First let's understand the different types of actions defined in Rasa. The framework has the following action types.
Response actions are the simplest type of actions. A response can include text, images and even buttons. We covered how to define simple responses in our previous tutorial.
Custom actions allow you to run any code you want before returning a final response. You can for example call an API or connect to a DB to retrieve additional information that you can use to provide a response to the user's request.
This action type will be the focus of this tutorial.
Forms are special custom actions that handle business logic. A form is a conversational flow that you define whenever you want to collect information from the user. We will cover this type of action in a future tutorial.
These are special custom actions that are used to validate form input. You will use this whenever you develop forms.
Rasa comes with a set of default actions configured. These are minimal configurations just to provide a basic starting point by setting up a very basic chatbot. You can extend these initial definitions to customize your chatbot based on your needs.
We will go through the steps you need to add a custom action to your chatbot.
Before we get into the details of implementing our custom action, let's define our goal first.In the previous tutorial we added some new intents to handle requests about different information about the company such as general info, hosting plans pricing, and customer service contact information. Let's expand on how we handle customer service requests. We will split the customer service requests into two intents. On will be if the user asked for general information about customer service. For example if the user asked how do I contact customer service?
this intent will be detected and a proper response will be returned.
The second intent is if the customer asked to talk to an agent now. Let's assume the company has an API for the live chat software where they can generate a link which will connect the customer directly to an agent. We need to implement a custom action to achieve our goal.
In this step we will go over how to custom actions work in Rasa and how to set up our new custom action. Custom actions are defined as classes that implement our custom logic and then return a response. Once a new class is defined we can link it to an intent, this way when the intent is matched it will execute the custom action and return the response to the user.
In your project directory, go the actions
folder. Open the file named actions.py
and add the code below to it.
from typing import Text, Dict, Any, List
from rasa_sdk import Action, Tracker
from rasa_sdk.events import SlotSet
from rasa_sdk.executor import CollectingDispatcher
# This function is just provided as an example.
# Implement your own code to get the link from an API, for example.
def generate_live_chat_link():
return 'https://example.com/test-123'
class ActionGenerateLiveChatLink(Action):
# This method returns the name of this action as defined in project configs.
def name(self) -> Text:
return "action_generate_live_chat_link"
# This method runs the custom logic and fills our slot for the link value.
def run(self,
dispatcher: CollectingDispatcher,
tracker: Tracker,
domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:
link = generate_live_chat_link()
# We will fill a slot or variable called live_chat_link.
return [SlotSet("live_chat_link", link)]
The first step in our configuration is to add a new intent. Open the file data/nlu.yml
. Add the following to the end of the file.
- intent: connect_to_live_chat_agent
examples:|
- I want to connect to a customer service agent
- I want to chat with an agent
- Connect me to a customer support agent
- Can I talk to a live agent?
- I would like to talk to an agent now
- Talk to customer service agent
- Chat with agent
- Connect to customer support agent
In this step we will add the needed configurations for our new action. First we need to add our new intent and action name to the domain.yml
file. Open the file and add the text below under
the intents
section.
intents:
- connect_to_live_chat_agent
Since our action is returning a variable let's also define a new slot in this file. Add the following after the responses
section in the file.
slots:
live_chat_link:
type: text
mappings:
- type: custom
Let's add a new section named actions
to the file. Add the following config.
actions:
- action_generate_live_chat_link
Now we need to add a new dynamic response using our slot which we will set in our action after fetching the URL from our API. Add the following under the existing responses
section.
responses:
utter_live_chat_link:
text: "Sure, I already notified a live chat customer service agent. You can connect to the chat using this link {live_chat_link}"
To link the new intent to our action add the following to the end of the data/rules.yml
file.
- rule: Share a live chat link with the user when requested
steps:
- intent: connect_to_live_chat_agent
- action: action_generate_live_chat_link
- action: utter_live_chat_link
Rasa runs custom actions as a separate server. In order to run our custom actions we need to define the URL to the actions server, so the main Rasa server can execute custom actions logic. Let's edit the endpoints.yml
file and un-comment the section below.
Remove the hashtags from these two lines to change this
# action_endpoint:
# url: "http://localhost:5055/webhook"
To
action_endpoint:
url: "http://localhost:5055/webhook"
Now the chatbot is ready for training.
In this step we will train the chatbot on the new settings. To do run the following command from within your project directory.
docker run -v $(pwd):/app rasa/rasa:latest train
In order to test our new custom action, we need to run the actions server and then the shell. To run the actions server run the command below in your terminal.
docker run -d --network host -v $(pwd):/app rasa/rasa:latest run actions
This will run the Rasa actions server on your computer's network on port 5055 as a background process. Now let's run the shell to test the chatbot's response. Execute the following command.
docker run -it --network host -v $(pwd):/app rasa/rasa:latest shell
Once the shell is ready, let's ask the chatbot to connect us to an agent. You can enter for example connect me to a customer service agent
or I want to chat with an agent
. The chatbot should reply with our configured response which includes the URL returned by our custom action.
Here's a screenshot of the response I got when I tested the chatbot.
Adding custom actions for a chatbot is very powerful as it enables businesses to execute any required business logic. In this tutorial we went over how to add a custom action to your Rasa chatbot. We did this in five steps which are defining our goal, implementing the action logic code, configuring the project, training the chatbot and finally testing its responses. Rasa makes this process straight forward, once you understand how to implement and configure a new custom action for your chatbot you can implement any custom business logic you need. This will allow you to develop more sophisticated chatbots that can handle more requests such as order status or connecting to a live chat agent.