Introduction to Slots

⚠️

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 configure slots to store information on the long term.

Video


Code

If you want to use slots to store information for the long term in Rasa you first need to make your domain.yml file aware of the slots. Here's what we added in our video.

slots:
name:
type: text

Note that if you want to run these custom actions you'll need to make sure that your endpoints.yml file is properly configured.

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

Next, we're using custom actions to interact with this slot. We've defined these in our actions/actions.py file.

from typing import Any, Text, Dict, List
from rasa_sdk.events import SlotSet
from rasa_sdk import Action, Tracker
from rasa_sdk.executor import CollectingDispatcher
class ActionReceiveName(Action):
def name(self) -> Text:
return "action_receive_name"
def run(self, dispatcher: CollectingDispatcher,
tracker: Tracker,
domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:
text = tracker.latest_message['text']
dispatcher.utter_message(text=f"I'll remember your name {text}!")
return [SlotSet("name", text)]
class ActionSayName(Action):
def name(self) -> Text:
return "action_say_name"
def run(self, dispatcher: CollectingDispatcher,
tracker: Tracker,
domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:
name = tracker.get_slot("name")
if not name:
dispatcher.utter_message(text="I don't know your name.")
else:
dispatcher.utter_message(text=f"Your name is {name}!")
return []

Assuming the action server is running and that you've updated your nlu/stories/domain file you should be able to interact with your custom action from the rasa shell now.

rasa shell

Interactive

If you run rasa interactive you'll be able to see the state of the slots while the conversation is happening. When you decide to save the results you may see a new part in your stories.yml file like below.

- story: interactive_story_1
steps:
- intent: greet
- action: utter_greet
- intent: tell_name
- action: utter_ask_name
- intent: give_name
- action: action_receive_name
- slot_was_set:
- name: vincent
- intent: repeat_name
- action: action_say_name
- intent: tell_name
- action: utter_ask_name
- intent: give_name
- action: action_receive_name
- slot_was_set:
- name: jenny
- intent: repeat_name
- action: action_say_name
- intent: goodbye
- action: utter_goodbye

Links

Exercises

Try to answer the following questions to test your knowledge.

  1. In what file do you need to define your slots?
  2. What is the name of the event in the custom action that can set the slot?

2016-2024 © Rasa.