Custom Queries

⚠️

Note: This is material for Rasa 2.x. The syntax has updated slightly in Rasa 3.0 so we recommend new users to check the new course found here.

In this lesson we will show you how to customise how we ask for information from the user.

Video


Code

Let's look at our domain.yml file again. In particular, let's zoom in on two responses.

responses:
utter_ask_first_name:
- text: What is your first name?
utter_ask_last_name:
- text: What is your last name?

The utterances that ask for a name follow a utter_ask_<slotname> convention. This is how Rasa knows how to ask for a slot value. We could take it a step further though by using a custom action instead of a static response.

Custom Action

The naming convention for the custom action is a bit different. Instead of utter_ask_<slotname> it becomes action_ask_<slot_name> or action_ask_<form_name>_<slot_name> if we'd like to use a specific form.

So let's remove the standard utter_ask_last_name from our domain.yml file and replace it a query written as a custom action. The custom action code from the video is listed below.

from typing import Dict, Text, List
from rasa_sdk import Tracker
from rasa_sdk.events import EventType
from rasa_sdk.executor import CollectingDispatcher
from rasa_sdk import Action
class AskForSlotAction(Action):
def name(self) -> Text:
return "action_ask_last_name"
def run(
self, dispatcher: CollectingDispatcher, tracker: Tracker, domain: Dict
) -> List[EventType]:
first_name = tracker.get_slot("first_name")
dispatcher.utter_message(text=f"So {first_name}, what is your last name?")
return []

To run this custom action we will need to update our domain.yml file so that it is aware of this new custom action.

actions:
- action_ask_last_name

Once that's done, you can retrain a new Rasa model and confirm that the form now uses a custom action to query the user. Note in order to run this you'll need to configure you endpoints.yml file first.

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

Once the new model is trained you can start a custom action service that Rasa can talk to.

rasa run actions

Links

Exercises

Try to answer the following questions to test your knowledge.

  1. What is the naming convention for a custom action that asks for a slot?
  2. Let's say you want to use the first name to ask for a last name. Do you need to write a custom action for this use-case?

2016-2024 © Rasa.