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 a 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,
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
Possible information that you can include under
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.
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)