Version: Latest

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.

NLU-based assistants

This section refers to building NLU-based assistants. If you are working with Conversational AI with Language Models (CALM), this content may not apply to you.

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.

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:

policies:
- ... # Other policies
- name: RulePolicy

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.

rules:
- rule: Say `hello` whenever the user sends a message with intent `greet`
steps:
- intent: greet
- action: utter_greet

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.

rules:
- rule: Say `hello` whenever the user sends a message with intent `greet`
steps:
- intent: greet
- action: utter_greet
stories:
- story: story to find a restaurant
steps:
- intent: find_restaurant
- action: restaurant_form
- action: utter_restaurant_found

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:

rules:
- rule: Say `hello` when the user starts a conversation with intent `greet`
conversation_start: true
steps:
- intent: greet
- action: utter_greet

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:

rules:
- rule: Only say `hello` if the user provided a name
condition:
- slot_was_set:
- user_provided_name: true
steps:
- intent: greet
- action: utter_greet

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:

rules:
- rule: Rule which will wait for user message when it was applied
steps:
- intent: greet
- action: utter_greet
# - action: action_listen
# Every rule implicitly includes a prediction for `action_listen` as last step.
# This means that Rasa will wait for the next user message.

If you want to hand over the next action prediction to another story or rule, add wait_for_user_input: false to your rule:

rules:
- rule: Rule which will not wait for user message once it was applied
steps:
- intent: greet
- action: utter_greet
wait_for_user_input: false

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:

rules:
- rule: Rule which will be conditionaly terminated
steps:
- intent: greet
- action: action_check_termination
- action: utter_greet
wait_for_user_input: true
from rasa_sdk import Action
from rasa_sdk.events import FollowupAction
class ActionCheckTermination(Action):
def name(self):
return "action_check_termination"
def run(self, dispatcher, tracker, domain):
# your business logic here
should_terminate = check_for_termination(<params>)
if should_terminate:
return [FollowupAction("action_listen")]
return []

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 Conversation Repair for more details)