Custom Actions

Video



In the previous chapters we covered that at each point in the conversation, assistants built with CALM predict the sequence of commands that represent how the user wants to proceed with the conversation. In some cases, those commands are simple text responses; but in other cases they can be more sophisticated actions. In this chapter we will cover different types of actions available in Rasa and how you can implement them for your own assistant.

Types of actions available in Rasa

There are three types of actions available in Rasa:

  • Responses - simple text responses that can also be enhanced by details like buttons, links, etc. that an assistants sends back to the user. These actions are defined in the domain file under the responses section and start with a prefix utter_.
  • Custom actions - an action that can include any type of custom code. Anything from calling an API to retrieving data to connect the user to a human customer support agent can be implemented and handled by custom actions.
  • Default actions - actions built into the dialogue manager by default. You can customise them to personalize your assistant.

Let’s dive into each type of action a little deeper.

Responses

Responses are the easiest way to enable your assistant to respond to the user. By defining responses, you can provide your assistant with one or multiple possible response templates that your assistant can use to respond back to the user when that specific response action is predicted. As mentioned previously, responses can be enriched with features like including variables, links, images, buttons, etc.

utter_cheer_up:
- text: "Here is something to cheer you up:"
image: "<https://i.imgur.com/nGF1K8f.jpg>"

You can always define one or more template responses for your assistant. By defining more than one possible response, you can make your assistant more natural:

responses:
utter_greet:
- text: "Hey, {name}. How are you?"
- text: "Hey, {name}. How is your day going?"

Contextual Rephrasing

A very powerful feature of CALM is Contextual Rephrasing. This feature can be used to enhance the user's experience when interacting with the assistant. Contextual Rephrasing allows you to rephrase the templated response to make them more natural and interactive for the user. It uses LLMs to produce dynamic responses that are based on the templated response, while, at the same time, keeping the context of the overall conversation. This provides a fully customizable way to make the user interactions with the assistant more natural and interactive. How to use Contextual Rephrasing? First, make sure Contextual Rephrasing and an NLG is configured. Then add the contextual rephraser to your endpoints configuration:

nlg:
type: rasa_plus.ml.ContextualResponseRephraser

There are many different ways of using the contextual rephrasing - from rephrasing a specific template, to rephrasing all responses. For example, if you want to rephrase only the a specific response, you can achieve that by adding rephrase: true flag to the responses configuration as follows:

responses:
  utter_greet:
    - text: "Hey! How can I help you?"
      metadata:
        rephrase: true

If you want to enable Contextual Rephrasing to all responses you can do that by using the following endpoints configuration:

nlg:
  type: rasa_plus.ml.ContextualResponseRephraser
  rephrase_all: true

The key thing to keep in mind with Contextual Rephrasing is that, while it doesn’t have any influence on the actual conversation flow, it can change how the user responds to specific bot messages. For that reason, it’s important to regularly check the conversations the users have with the assistant to make modifications where necessary.

Custom Actions

Besides utterances, another very common type of actions you will likely be using when building your assistant is custom actions. Custom actions allow you to write code for any action your assistant should execute - from sending a simple text response to connecting to an API or a database to retrieve some information and going far beyond that. Custom actions are tipically written inside of the actions.py file of your project and it consists of the following structure:

from typing import Any, Text, Dict, List
from rasa_sdk import Action, Tracker
from rasa_sdk.executor import CollectingDispatcher
from rasa_sdk.events import SlotSet
class ActionCheckSufficientFunds(Action):
def name(self) -> Text:
return "action_check_sufficient_funds"
def run(self, dispatcher: CollectingDispatcher,
tracker: Tracker,
domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:
# hard-coded balance for tutorial purposes. in production this
# would be retrieved from a database or an API
balance = 1000
transfer_amount = tracker.get_slot("amount")
has_sufficient_funds = transfer_amount <= balance
return [SlotSet("has_sufficient_funds", has_sufficient_funds)]

Here, a class describes the custom actions class which typically consists of two functions:

  • name(), which defines the name of the custom action. This name can then be defined inside of the domain of your assistant and used in flows for your assistant to use. Your assistant will know that a specific action is a custom action, because all custom actions have a name prefix action_.
  • run() which defines what happens when a specific action is predicted. In some cases it might be running a specific check for a value of a variable, in other cases it might be setting a specific slot, etc.

Pay attention to the methods like get_slot() here which allow your assistant to access the values of the specific slots.

Custom Action Server

Custom actions are executed by custom action server which is a separate server from the one that runs your assistant. This allows you to make changes to your custom actions without having to restart your assistant all the time. You can start a custom action server by using a command rasa run actions. This command will start a server and your custom actions will be available for your assistant. You can start the custom action server by running command:

rasa run actions

This command will start the custom action server and will allow your assistant to interact with the code provided.

Default Actions

Finally, default actions are available for your assistant to do things like enable your assistant to wait for the user input at a specific point or restart the conversation, etc. Once again, these actions are implemented in Rasa by default and assistant is using them without you having to do any work or customizations.

Under the hood, default actions have the same structure as custom actions - their labels start with a prefix action_ . For example a default action action_listen will wait for the user to send the response and action_restart will reset the whole conversation history, including any slots that were set during it.

Customising Default Actions

Default actions can be customized by simply overriding their behaviour in your actions.py file. For exmaple, if you’d like to override the default action action_session_start behaviour, you can do that as follows.

By default, the new session between the user and an assistant will start by carrying over all of the existing slots to the new conversation. Let’s say you would like to override this behaviour and instead would want to carry over only the user’s name and their phone number. You can achieve that by updating the action action_session_start by implementing a staticmethod which collects the slots name and phone_number and then extend the new session with these values:

Actions are such a powerful feature of your assistant that really make your assistant functional and take it from a simple comversational companion to an assistant that can really do things and complete targeted tasks.


2016-2024 © Rasa.