Policies
Policies
Configuring Policies
The rasa.core.policies.Policy class decides which action to take at every step in the conversation.
There are different policies to choose from, and you can include multiple policies in a single rasa.core.agent.Agent.
Note
Per default a maximum of 10 next actions can be predicted by the agent after every user message. To update this value you can set the environment variable MAX_NUMBER_OF_PREDICTIONS to the desired number of maximum predictions.
Your project’s config.yml file takes a policies key which you can use to customize the policies your assistant uses.
In the example below, the last two lines show how to use a custom policy class and pass arguments to it.
policies:
- name: "KerasPolicy"
featurizer:
- name: MaxHistoryTrackerFeaturizer
max_history: 5
state_featurizer:
- name: BinarySingleStateFeaturizer
- name: "MemoizationPolicy"
max_history: 5
- name: "FallbackPolicy"
nlu_threshold: 0.4
core_threshold: 0.3
fallback_action_name: "my_fallback_action"
- name: "path.to.your.policy.class"
arg1: "..."
Max History
One important hyperparameter for Rasa Core policies is the max_history. This controls how much dialogue history the model looks at to decide which action to take next.
You can set the max_history by passing it to your policy’s Featurizer in the policy configuration yaml file.
Note
Only the MaxHistoryTrackerFeaturizer uses a max history, whereas the FullDialogueTrackerFeaturizer always looks at the full conversation history. See Featurization of Conversations for details.
As an example, let’s say you have an out_of_scope intent which describes off-topic user messages. If your bot sees this intent multiple times in a row, you might want to tell the user what you can help them with. So your story might look like this:
* out_of_scope
- utter_default
* out_of_scope
- utter_default
* out_of_scope
- utter_help_message
For Rasa Core to learn this pattern, the max_history has to be at least 4.
If you increase your max_history, your model will become bigger and training will take longer. If you have some information that should affect the dialogue very far into the future, you should store it as a slot. Slot information is always available for every featurizer.
Data Augmentation
When you train a model, by default Rasa Core will create longer stories by randomly gluing together the ones in your stories files. This is because if you have stories like:
# thanks
* thankyou
- utter_youarewelcome
# bye
* goodbye
- utter_goodbye
You actually want to teach your policy to ignore the dialogue history when it isn’t relevant and just respond with the same action no matter what happened before.
You can alter this behavior with the --augmentation flag. Which allows you to set the augmentation_factor. The augmentation_factor determines how many augmented stories are subsampled during training. The augmented stories are subsampled before training since their number can quickly become very large, and we want to limit it.
The number of sampled stories is augmentation_factor x10. By default augmentation is set to 20, resulting in a maximum of 200 augmented stories.
--augmentation 0 disables all augmentation behavior. The memoization based policies are not affected by augmentation (independent of the augmentation_factor) and will automatically ignore all augmented stories.
Action Selection
At every turn, each policy defined in your configuration will predict a next action with a certain confidence level. For more information about how each policy makes its decision, read into the policy’s description below. The bot’s next action is then decided by the policy that predicts with the highest confidence.
In the case that two policies predict with equal confidence (for example, the Memoization and Mapping Policies always predict with confidence of either 0 or 1), the priority of the policies is considered. Rasa policies have default priorities that are set to ensure the expected outcome in the case of a tie. They look like this, where higher numbers have higher priority:
5.
FormPolicy4.
FallbackPolicyandTwoStageFallbackPolicy3.
MemoizationPolicyandAugmentedMemoizationPolicy2.
MappingPolicy1.
TEDPolicy,EmbeddingPolicy,KerasPolicy, andSklearnPolicy
This priority hierarchy ensures that, for example, if there is an intent with a mapped action, but the NLU confidence is not above the nlu_threshold, the bot will still fall back. In general, it is not recommended to have more than one policy per priority level, and some policies on the same priority level, such as the two fallback policies, strictly cannot be used in tandem.
If you create your own policy, use these priorities as a guide for figuring out the priority of your policy. If your policy is a machine learning policy, it should most likely have priority 1, the same as the Rasa machine learning policies.
Warning
All policy priorities are configurable via the priority: parameter in the configuration, but we do not recommend changing them outside of specific cases such as custom policies. Doing so can lead to unexpected and undesired bot behavior.
Keras Policy
The KerasPolicy uses a neural network implemented in Keras to select the next action.
The default architecture is based on an LSTM, but you can override the KerasPolicy.model_architecture method to implement your own architecture.
def model_architecture(
self, input_shape: Tuple[int, int], output_shape: Tuple[int, Optional[int]]
) -> tf.keras.models.Sequential:
"""Build a keras model and return a compiled model."""
...
You can implement the model of your choice by overriding these methods, or initialize KerasPolicy with pre-defined keras model.
In order to get reproducible training results for the same inputs you can set the random_seed attribute of the KerasPolicy to any integer.
The Mapping Policy
The MappingPolicy can be used to directly map intents to actions. The mappings are assigned by giving an intent the property triggers, e.g.:
intents:
- ask_is_bot:
triggers: action_is_bot
An intent can only be mapped to at most one action. The bot will run the mapped action once it receives a message of the triggering intent. Afterwards, it will listen for the next message.
If you do not want your intent-action mapping to affect the dialogue history, the mapped action must return a UserUtteranceReverted() event.
Notice that if you use the MappingPolicy to predict bot utterance actions directly (e.g. triggers: utter_{}), these interactions must go in your stories, as in this case there is no UserUtteranceReverted() and the intent and the mapped response action will appear in the dialogue history.
Fallback Policy
The FallbackPolicy invokes a fallback action if at least one of the following occurs:
- The intent recognition has a confidence below
nlu_threshold. - The highest ranked intent differs in confidence with the second highest ranked intent by less than
ambiguity_threshold. - None of the dialogue policies predict an action with confidence higher than
core_threshold.
Configuration:
policies:
- name: "FallbackPolicy"
nlu_threshold: 0.3
ambiguity_threshold: 0.1
core_threshold: 0.3
fallback_action_name: 'action_default_fallback'
nlu_threshold |
Min confidence needed to accept an NLU prediction |
ambiguity_threshold |
Min amount by which the confidence of the top intent must exceed that of the second highest ranked intent. |
core_threshold |
Min confidence needed to accept an action prediction from Rasa Core |
fallback_action_name |
Name of the fallback action to be called if the confidence of intent or action is below the respective threshold |
You can include either the FallbackPolicy or the TwoStageFallbackPolicy in your configuration, but not both.
Form Policy
The FormPolicy is an extension of the MemoizationPolicy which handles the filling of forms. Once a FormAction is called, the FormPolicy will continually predict the FormAction until all required slots in the form are filled. For more information, see Forms.