Slot Example Rasa
Rasa.db file is a database where all the conversation with your bot Is stored. Domain.yml file: In which you are storing all the actions, intents, entities, templates and slots. For example, in the above sentence, the intent is ordering and the entity is a book. Rasa NLU internally uses Bag-of-Word (BoW) algorithm to find intent and Conditional Random Field (CRF) to find entities. Although you can use other algorithms for finding intent and entities using Rasa. RASA CORE: Core is.
Contents
- Slot Filling
One of the most common conversation patterns is to collect a few pieces ofinformation from a user in order to do something (book a restaurant, call anAPI, search a database, etc.). This is also called slot filling.
If you need to collect multiple pieces of information in a row, we recommendedthat you create a FormAction
. This is a single action which contains thelogic to loop over the required slots and ask the user for this information.There is a full example using forms in the examples/formbot
directory ofRasa Core.
You can take a look at the FormAction base class by clicking this link:
- class
rasa_core_sdk.forms.
FormAction
[source]¶
To add your forms to the domain file, reference their nameunder forms:
section:
Using a FormAction
, you can describe all of the happy paths with a singlestory. By “happy path”, we mean that whenever you ask a user for some information,they respond with what you asked for.
If we take the example of the restaurant bot, this single story describes all of thehappy paths.
In this story the user intent is request_restaurant
, which is followed bythe form action restaurant_form
. With form{'name':'restaurant_form'}
theform is activated and with form{'name':null}
the form is deactivated again.As shown in the section Handling unhappy paths the the bot can execute any kind ofactions outside the form while the form is still active. On the “happy path”,where the user is cooperating well and the system understands the user input correctly,the form is filling all requested slots without interruption.
The FormAction
will only requests slots which haven’t already been set.If a user says“I’d like a vegetarian Chinese restaurant for 8 people”, they won’t beasked about the cuisine
and num_people
slots.
Note that for this story to work, your slots should be unfeaturized.If they’re not, you should add all the slots that have been set by the form.
The restaurant_form
in the story above is the name of our form action.Here is an example of what it looks like.You need to define three methods:
name
: the name of this actionrequired_slots
: a list of slots that need to be filled for thesubmit
method to work.submit
: what to do at the end of the form, when all the slots have been filled.
Once the form action gets called for the first time,the form gets activated and the FormPolicy
jumps in.The FormPolicy
is extremely simple and just always predicts the form action.See Handling unhappy paths for how to work with unexpected user input.
Every time the form action gets called, it will ask the user for the next slot inrequired_slots
which is not already set.It does this by looking for a template called utter_ask_{slot_name}
,so you need to define these in your domain file for each required slot.
Once all the slots are filled, the submit()
method is called, where you canuse the information you’ve collected to do something for the user, for examplequerying a restaurant API.If you don’t want your form to do anything at the end, just use return[]
as your submit method.After the submit method is called, the form is deactivated,and other policies in your Core model will be used to predict the next action.
Some slots (like cuisine
) can be picked up using a single entity, but aFormAction
can also support yes/no questions and free-text input.The slot_mappings
method defines how to extract slot values from user responses.
Here’s an example for the restaurant bot:
The predefined functions work as follows:
self.from_entity(entity=entity_name,intent=intent_name)
will look for an entity calledentity_name
to fill a slotslot_name
regardless of user intent ifintent_name
isNone
else only if the users intent isintent_name
.self.from_intent(intent=intent_name,value=value)
will fill slotslot_name
withvalue
if user intent isintent_name
.To make a boolean slot, take a look at the definition ofoutdoor_seating
above. Note: Slot will not be filled with user intent of message triggeringthe form action. Useself.from_trigger_intent
below.self.from_trigger_intent(intent=intent_name,value=value)
will fill slotslot_name
withvalue
if form was triggered with userintentintent_name
.self.from_text(intent=intent_name)
will use the nextuser utterance to fill the text slotslot_name
regardless of user intentifintent_name
isNone
else only if user intent isintent_name
.- If you want to allow a combination of these, provide them as a list as in theexample above
After extracting a slot value from user input, the form will try to validate thevalue of the slot. By default, this only checks if the requested slot was extracted.If you want to add custom validation, for example to check a value against a database,you can do this by writing validate_{slot}()
method.Here is an example which checks if the extracted cuisine slot belongs to alist of supported cuisines.
You can also deactivate the form directly during this validation step (in case theslot is filled with something that you are certain can’t be handled) by returningself.deactivate()
You can also deactivate the form directly during this validation step (in case theslot is filled with something that you are certain can’t be handled) by returningself.deactivate()
If nothing is extracted from the user’s utterance for any of the required slots, anActionExecutionRejection
error will be raised, meaning the action executionwas rejected and therefore Core will fall back onto a different policy topredict another action.
Of course your users will not always respond with the information you ask of them.Typically, users will ask questions, make chitchat, change their mind, or otherwisestray from the happy path. The way this works with forms is that a form will raisean ActionExecutionRejection
if the user didn’t provide the requested information.You need to handle events that might cause ActionExecutionRejection
errorsin your stories. For example, if you expect your users to chitchat with your bot,you could add a story like this:
In some situations, users may change their mind in the middle of form actionand decide not to go forward with their initial request. In cases like this, theassistant should stop asking for the requested slots. You can handle such situationsgracefully using a default action action_deactivate_form
which will deactivatethe form and reset the requested slot. An example story of such conversation couldlook as follows:
It is strongly recommended that you build these stories using interactive learning.If you write these stories by hand you will likely miss important things.Please read Interactive Learning with Formson how to use interactive learning with forms.
The slot requested_slot
is automatically added to the domain as anunfeaturized slot. If you want to make it featurized, you need to add itto your domain file as a categorical slot. You might want to do this if youwant to handle your unhappy paths differently depending on what slot iscurrently being asked from the user. For example, say your users respondto one of the bot’s questions with another question, like why do you need to know that?The response to this explain
intent depends on where we are in the story.In the restaurant case, your stories would look something like this:
Again, is is strongly recommended that you use interactivelearning to build these stories.Please read Interactive Learning with Formson how to use interactive learning with forms.
Many forms require more logic than just requesting a list of fields.For example, if someone requests greek
as their cuisine, you may want toask if they are looking for somewhere with outside seating.
You can achieve this by writing some logic into the required_slots()
method,for example:
This mechanism is quite general and you can use it to build many differentkinds of logic into your forms.
To use forms, make sure to include the FormPolicy
in your policyconfiguration file. For example:
The first thing to try is to run your bot with the debug
flag, see Debugging for details.If you are just getting started, you probably only have a few hand-written stories.This is a great starting point, butyou should give your bot to people to test as soon as possible. One of the guiding principlesbehind Rasa Core is:
So don’t try to cover every possibility in your hand-written stories before giving it to testers.Real user behavior will always surprise you!
We have a very active support community on Rasa Community Forumthat is happy to help you with your questions. If you have any feedback for us or a specificsuggestion for improving the docs, feel free to share it by creating an issue on Rasa CoreGitHub repository.
Slots are your bot’s memory. They act as a key-value storewhich can be used to store information the user provided (e.g their home city)as well as information gathered about the outside world (e.g. the result of adatabase query).
Most of the time, you want slots to influence how the dialogue progresses.There are different slot types for different behaviors.
For example, if your user has provided their home city, you mighthave a text
slot called home_city
. If the user asks for theweather, and you don’t know their home city, you will have to askthem for it. A text
slot only tells Rasa Core whether the slothas a value. The specific value of a text
slot (e.g. Bangaloreor New York or Hong Kong) doesn’t make any difference.
If the value itself is important, use a categorical
or a bool
slot.There are also float
, and list
slots.If you just want to store some data, but don’t want it to affect the flowof the conversation, use an unfeaturized
slot.
The Policy
doesn’t have access to thevalue of your slots. It receives a featurized representation.As mentioned above, for a text
slot the value is irrelevant.The policy just sees a 1
or 0
depending on whether it is set.
You should choose your slot types carefully!
You can provide an initial value for a slot in your domain file:
You can get the value of a slot using .get_slot()
inside actions.py
for example:
There are multiple ways that slots are set during a conversation:
If your NLU model picks up an entity, and your domain contains aslot with the same name, the slot will be set automatically. For example:
In this case, you don’t have to include the -slot{}
part in thestory, because it is automatically picked up.
To disable this behavior for a particular slot, you can set theauto_fill
attribute to False
in the domain file:
You can use buttons as a shortcut.Rasa Core will send messages starting with a /
to theRegexInterpreter
, which expects NLU input in the same formatas in story files, e.g. /intent{entities}
. For example, if you letusers choose a color by clicking a button, the button payloads mightbe /choose{'color':'blue'}
and /choose{'color':'red'}
.
You can specify this in your domain file like this:(see details in Domains)
The second option is to set slots by returning events in custom actions.In this case, your stories need to include the slots.For example, you have a custom action to fetch a user’s profile, andyou have a categorical
slot called account_type
.When the fetch_profile
action is run, it returns arasa.core.events.SlotSet
event:
In this case you do have to include the -slot{}
part in your stories.Rasa Core will learn to use this information to decide on the correct action totake (in this case, utter_welcome_premium
or utter_welcome_basic
).
Note
It is very easy to forget about slots if you are writingstories by hand. We strongly recommend that you build up thesestories using Interactive Learning with Forms rather than writing them.
text
¶User preferences where you only care whether or not they’vebeen specified.
Results in the feature of the slot being set to 1
if any value is set.Otherwise the feature will be set to 0
(no value is set).
bool
¶True or False
Checks if slot is set and if True
categorical
¶Slots which can take one of N values
Creates a one-hot encoding describing which of the values
matched.A default value __other__
is automatically added to the user-definedvalues. All values encountered which are not explicitly defined in thedomain are mapped to __other__
for featurization. The value__other__
should not be used as a user-defined value; if it is, itwill still behave as the default to which all unseen values are mapped.
float
¶Continuous values
max_value=1.0
, min_value=0.0
All values below min_value
will be treated as min_value
, the samehappens for values above max_value
. Hence, if max_value
is set to1
, there is no difference between the slot values 2
and 3.5
interms of featurization (e.g. both values will influence the dialogue inthe same way and the model can not learn to differentiate between them).
list
¶Slot Example Rasa Video
Lists of values
The feature of this slot is set to 1
if a value with a list is set,where the list is not empty. If no value is set, or the empty list is theset value, the feature will be 0
. The length of the list stored inthe slot does not influence the dialogue.
unfeaturized
¶Data you want to store which shouldn’t influence the dialogue flow
Slot Example Rasa Song
There will not be any featurization of this slot, hence its value doesnot influence the dialogue flow and is ignored when predicting the nextaction the bot should run.
Maybe your restaurant booking system can only handle bookingsfor up to 6 people. In this case you want the value of theslot to influence the next selected action (and not just whetherit’s been specified). You can do this by defining a custom slot class.
In the code below, we define a slot class called NumberOfPeopleSlot
.The featurization defines how the value of this slot gets converted to a vectorto our machine learning model can deal with.Our slot has three possible “values”, which we can represent witha vector of length 2
.
| not yet set |
| between 1 and 6 |
| more than 6 |
Slot Example Rasa Mp3
Now we also need some training stories, so that Rasa Corecan learn from these how to handle the different situations: