# 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`](https://legacy-docs-v1.rasa.com/1.4.6/api/agent/#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: "..."

### Configuring Policies
The `max_history` is an important hyperparameter that 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.

### Data Augmentation
When you train a model, by default Rasa Core will create longer stories by randomly combining the ones in your stories files.

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.

### Action Selection
At every turn, each policy defined in your configuration will predict a next action with a certain confidence level. The bot’s next action is then decided by the policy that predicts with the highest confidence.

### Keras Policy
The `KerasPolicy` uses a neural network implemented in [Keras](http://keras.io/) to select the next action. The default architecture is based on an LSTM, but you can override the `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 Model
        model = Sequential()
        model.add(Masking(mask_value=-1, input_shape=input_shape))
        model.add(LSTM(self.rnn_size, dropout=0.2))
        model.add(Dense(input_dim=self.rnn_size, units=output_shape[-1]))
        model.add(Activation("softmax"))
        model.compile(loss="categorical_crossentropy", optimizer="rmsprop", metrics=["accuracy"])

### Memoization Policy
The `MemoizationPolicy` memorizes the conversations in your training data and predicts the next action with confidence `1.0` if this exact conversation exists in the training data.

### Fallback Policy
The `FallbackPolicy` invokes a fallback action if:

1. The intent recognition has a confidence below `nlu_threshold`.
2. The highest ranked intent differs in confidence from the second highest ranked intent by less than `ambiguity_threshold`.
3. 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 required to accept an NLU prediction |
| `ambiguity_threshold` | Minimum confidence required for intent distinction |
| `core_threshold` | Min confidence required for action prediction |
| `fallback_action_name` | Name of the fallback action triggered if thresholds are not met |

### Two-Stage Fallback Policy
The `TwoStageFallbackPolicy` is designed to handle low NLU confidence in multiple stages.

#### Configuration:
    policies:
      - name: TwoStageFallbackPolicy
        nlu_threshold: 0.3
        ambiguity_threshold: 0.1
        core_threshold: 0.3
        fallback_core_action_name: "action_default_fallback"
        fallback_nlu_action_name: "action_default_fallback"
        deny_suggestion_intent_name: "out_of_scope"

### Form Policy
The `FormPolicy` is an extension of the `MemoizationPolicy` that handles the filling of forms. Once a `FormAction` is called, the `FormPolicy` will predict the `FormAction` until all required slots are filled.
