Rules
Rules are a type of training data used to train your assistant's dialogue management model. Rules describe short pieces of conversations that should always follow the same path.
Don't overuse rules. Rules are great to handle small specific conversation patterns, but unlike stories, rules don't have the power to generalize to unseen conversation paths. Combine rules and stories to make your assistant robust and able to handle real user behavior.
If you can't decide whether to write a story or a rule to implement a certain behavior, see the best practices for Writing Conversation Data).
For additional examples on implementing rules in a Rasa assistant, refer to our Rules example bot.
Writing a Rule
Before you start writing rules, you have to make sure that the Rule Policy is added to your model configuration:
Rules can then be added to the rules
section of your training data.
To indicate that a rule can apply at any point in a conversation, start with the intent which starts the conversation and then add the actions which your assistant should perform in response to that.
This example rule applies at the start of conversation as well as when the user decides
to send a message with an intent greet
in the middle of an ongoing conversation.
Dialogue turns that only appear as rules in the training data and do not appear in stories
will be ignored by ML-only policies like TEDPolicy
at prediction time.
For example if you define the greeting rule as above and don't add it to any of your stories,
after RulePolicy
predicts utter_greet
, TEDPolicy
will
make predictions as if the greet, utter_greet
turn did not occur.
Rules for the Conversation Start
To write a rule which only applies at the beginning of a conversation, add a
conversation_start: true
to your rule:
If a user sends a message with the intent greet
later in the conversation, the rule will not match.
Rules with Conditions
Conditions describe requirements which have to be fulfilled for a rule to be
applicable. To do so, add any information about the prior conversation under the
condition
key:
Possible information that you can include under condition
includes slot_was_set
events
and active_loop
events.
Skip Waiting for User Input at the End of a Rule
By default, rules will wait for the next user message when finished with the last step:
If you want to hand over the next action prediction to another story or rule, add
wait_for_user_input: false
to your rule:
This indicates that the assistant should execute another action before waiting for more user input.
Abort a Rule
Rules are designed to handle multiple output steps of a chatbot. They are terminated as soon as user interaction is required. This happens automatically via launching a form, since it starts with the user input of the first slot. Therefore all steps after launch are ignored.
Termination, however, can also be achieved manually. This can be useful to implement conditional termination criteria. Here is an example:
utter_greet is never executed when termination is done, even after user input, because it causes a new intent prediction.
Rules and Forms
When a Form is active, the bot will make predictions based on how the form is defined, ignoring rules. Rules become applicable again if:
- the form fills all required slots
- the form rejects its execution (see Handling Unhappy Paths for more details)