Introduction to Slots

In this lesson we will show you how to add slots to a Rasa assistant. These will enable you to store information about the user for 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've added in the video.

entities:
- shirt_size
slots:
shirt_size:
type: text
influence_conversation: true
mappings:
- type: from_entity
entity: shirt_size

In particular; we've added an entity and a slot with the same name. There's an important distinction between the two though. In this case, the shirt-size entity is something that can be detected in a sentence but it isn't stored for later use. The shirt-size slot on the other hand can store the slot information to be used later. In this example the slot value is filled by an entity with the same name, but this doesn't have to be the case. You can also set slot values with intents or plain text.

Because we're using entities, we should also add some NLU data for our shirt_size entity.

version: "3.0"
nlu:
- intent: ask_me_anything
examples: |
- ask me anything
- would you like to ask me something
- feel free to ask me about things
- want to ask me something?
- intent: repeat_shirt_size
examples: |
- what is my shirt size
- tell me my shirt size
- do you know my shirt size
- repeat my shirt size
- intent: give_shirt_size
examples: |
- i want a [large](shirt_size) shirt
- [medium](shirt_size) size
- give me a [small](shirt_size) tshirt
- [small](shirt_size)
- [medium](shirt_size)
- [large](shirt_size)
- i'd prefer [medium](shirt_size) size
- gimme gimme gimme [small](shirt_size) tshirt
- give shirt size
- i want a [large](shirt_size) shirt please
- i want to give you my shirt
- [large](shirt_size)

You'll also want to make sure that there are some example stories in your stories.yml file where the entity and slot values are set. That way the assistant can learn what to expect.

- story: give name story 1
steps:
- intent: ask_me_anything
- action: utter_ask_shirt_size
- intent: give_shirt_size
entities:
- shirt_size: large
- slot_was_set:
- shirt_size: large
- action: utter_remember
- intent: repeat_shirt_size
- action: action_say_shirt_size
- intent: goodbye
- action: utter_goodbye

Note that the story shows that we first select the shirt size and that afterwards it is stored as a slot.

Custom Action

We're using a custom action to handle the action_say_shirt_size action. It is defined below.

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 ActionSayShirtSize(Action):
def name(self) -> Text:
return "action_say_shirt_size"
def run(self, dispatcher: CollectingDispatcher,
tracker: Tracker,
domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:
shirt_size = tracker.get_slot("shirt_size")
if not shirt_size:
dispatcher.utter_message(text="I don't know your shirt size.")
else:
dispatcher.utter_message(text=f"Your shirt size is {shirt_size}!")
return []

Interactive

If you run rasa interactive you'll be able to see the state of the slots while the conversation is happening. Remember that you'll also need to call rasa run actions if you want your custom actions to run. That means that you'll need to add the following configuration to your endpoints.yml.

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

Links

Exercises

Try to answer the following questions to test your knowledge.

  1. In what file do you need to define your slots?
  2. You can also set slots via custom actions via the SlotSet event. Check the docs and try to write a custom action that can set slots.

2016-2022 © Rasa.