Policies
You are viewing documentation for our open source project which is maintained by the community. If you want to get started building assistants with Rasa please check out our latest documentation here.
You can customize the policies your assistant uses by specifying the policies key in your project's config.yml. There are different policies to choose from, and you can include multiple policies in a single configuration. Here's an example of what a list of policies might look like:
policies:
- name: MemoizationPolicy
- name: TEDPolicy
max_history: 5
epochs: 200
- name: RulePolicy
Starting from scratch?
If you don't know which policies to choose, leave out the policies key from your config.yml completely. If you do, the Suggested Config feature will provide default policies for you.
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 policy that predicts with the highest confidence decides the assistant's next action.
Maximum number of predictions
By default, your assistant can predict a maximum of 10 next actions after each user message. To update this value, you can set the environment variable MAX_NUMBER_OF_PREDICTIONS to the desired number of maximum predictions.
Policy Priority
In the case that two policies predict with equal confidence (for example, the Memoization and Rule Policies might both predict with confidence 1), the priority of the policies is considered. Rasa Open Source 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:
- 6 -
RulePolicy - 3 -
MemoizationPolicyorAugmentedMemoizationPolicy - 2 -
UnexpecTEDIntentPolicy - 1 -
TEDPolicy
In general, it is not recommended to have more than one policy per priority level in your configuration. If you have 2 policies with the same priority and they predict with the same confidence, the resulting action will be chosen randomly.
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 TEDPolicy.
Overriding policy priorities
All policy priorities are configurable via the priority parameter in the policy's 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.
Machine Learning Policies
TED Policy
The Transformer Embedding Dialogue (TED) Policy is a multi-task architecture for next action prediction and entity recognition. The architecture consists of several transformer encoders that are shared for both tasks. A sequence of entity labels is predicted through a Conditional Random Field (CRF) tagging layer on top of the user sequence transformer encoder output corresponding to the input sequence of tokens. For the next action prediction, the dialogue transformer encoder output and the system action labels are embedded into a single semantic vector space. We use the dot-product loss to maximize the similarity with the target label and minimize similarities with negative samples.
If you want to learn more about the model, check out our paper and on our youtube channel where we explain the model architecture in detail.
TED Policy architecture comprises the following steps:
- Concatenate features for:
- user input (user intent and entities) or user text processed through a user sequence transformer encoder,
- previous system actions or bot utterances processed through a bot sequence transformer encoder,
- slots and active forms for each time step into an input vector to the embedding layer that precedes the dialogue transformer.
- Feed the embedding of the input vector into the dialogue transformer encoder.
- Apply a dense layer to the output of the dialogue transformer to get embeddings of the dialogue for each time step.
- Apply a dense layer to create embeddings for system actions for each time step.
- Calculate the similarity between the dialogue embedding and embedded system actions. This step is based on the StarSpace idea.
- Concatenate the token-level output of the user sequence transformer encoder with the output of the dialogue transformer encoder for each time step.
- Apply CRF algorithm to predict contextual entities for each user text input.
Configuration:
You can pass configuration parameters to the TEDPolicy using the config.yml file. If you want to fine-tune your model, start by modifying the following parameters:
epochs: This parameter sets the number of times the algorithm will see the training data (default:1). Oneepochis equals to one forward pass and one backward pass of all the training examples. Sometimes the model needs more epochs to properly learn. Sometimes more epochs don't influence the performance. The lower the number of epochs the faster the model is trained.max_history: This parameter controls how much dialogue history the model looks at to decide which action to take next. Defaultmax_historyfor this policy isNone, which means that the complete dialogue history since session restart is taken into account. If you want to limit the model to only see a certain number of previous dialogue turns, you can setmax_historyto a finite value. Please note that you should pickmax_historycarefully, so that the model has enough previous dialogue turns to create a correct prediction. See Featurizers for more details.
Here is how the config would look like:
policies:
- name: TEDPolicy
epochs: 200
max_history: 8
number_of_transformer_layers: This parameter sets the number of sequence transformer encoder layers to use for sequential transformer encoders for user, action and action label texts and for dialogue transformer encoder. (defaults:text: 1, action_text: 1, label_action_text: 1, dialogue: 1). The number of sequence transformer encoder layers corresponds to the transformer blocks to use for the model.transformer_size: This parameter sets the number of units in the sequence transformer encoder layers to use for sequential transformer encoders for user, action and action label texts and for dialogue transformer encoder. (defaults:text: 128, action_text: 128, label_action_text: 128, dialogue: 128). The vectors coming out of the transformer encoders will have the giventransformer_size.weight_sparsity: This parameter defines the fraction of kernel weights that are set to 0 for all feed forward layers in the model (default:0.8). The value should be a number between 0 and 1. If you setweight_sparsityto 0, no kernel weights will be set to 0, the layer acts as a standard feed forward layer. You should not setweight_sparsityto 1 as this would result in all kernel weights being 0, i.e. the model is not able to learn.split_entities_by_comma: This parameter defines whether adjacent entities separated by a comma should be treated as one, or split. For example, entities with the typeingredients, like "apple, banana" can be split into "apple" and "banana". An entity with typeaddress, like "Schönhauser Allee 175, 10119 Berlin" should be treated as one. Can either beTrue/Falseglobally:
policies:
- name: TEDPolicy
split_entities_by_comma: True
or set per entity type, such as:
policies:
- name: TEDPolicy
split_entities_by_comma:
address: False
ingredients: True
constrain_similarities: This parameter when set toTrueapplies a sigmoid cross entropy loss over all similarity terms. This helps in keeping similarities between input and negative labels to smaller values. This should help in better generalization of the model to real world test sets.model_confidence: This parameter allows the user to configure how confidences are computed during inference. It can take two values:softmax: Confidences are in the range[0, 1](old behavior and current default). Computed similarities are normalized with thesoftmaxactivation function.linear_norm: Confidences are in the range[0, 1]. Computed dot product similarities are normalized with a linear function.
use_gpu: This parameter defines whether a GPU (if available) will be used training. By default,TEDPolicywill be trained on GPU if a GPU is available (i.e.use_gpuisTrue). To enforce thatTEDPolicyuses only the CPU for training, setuse_gputoFalse.
The above configuration parameters are the ones you should configure to fit your model to your data. However, additional parameters exist that can be adapted.
UnexpecTED Intent Policy
This feature is experimental. We introduce experimental features to get feedback from our community, so we encourage you to try it out! However, the functionality might be changed or removed in the future.
UnexpecTEDIntentPolicy helps you review conversations and also allows your bot to react to unlikely user turns. It is an auxiliary policy that should only be used in conjunction with at least one other policy, as the only action that it can trigger is the special action_unlikely_intent action.
UnexpecTEDIntentPolicy has the same model architecture as TEDPolicy. The difference is at a task level. Instead of learning the best action to be triggered next, UnexpecTEDIntentPolicy learns the set of intents that are most likely to be expressed by the user given the conversation context from training stories. It uses the learned information at inference time by checking if the predicted intent by NLU is the most likely intent. If the intent predicted by NLU is indeed likely to occur given the conversation context, UnexpecTEDIntentPolicy does not trigger any action. Otherwise, it triggers an action_unlikely_intent with a confidence of 1.00.
Configuration:
You can pass configuration parameters to the UnexpecTEDIntentPolicy using the config.yml file.
If you want to fine-tune model's performance, start by modifying the following parameters:
epochs: This parameter sets the number of times the algorithm will see the training data (default:1). Oneepochis equals to one forward pass and one backward pass of all the training examples. Sometimes the model needs more epochs to learn properly. Sometimes more epochs don't influence the performance. The lower the number of epochs the faster the model is trained.max_history: This parameter controls how much dialogue history the model looks at before making an inference. Defaultmax_historyfor this policy isNone, which means that the complete dialogue history since session (re)start is taken into account. If you want to limit the model to only see a certain number of previous dialogue turns, you can setmax_historyto a finite value. Please note that you should pickmax_historycarefully, so that the model has enough previous dialogue turns to create a correct prediction.
Here is how the config would look like:
policies:
- name: UnexpecTEDIntentPolicy
max_history: 8
epochs: 200
ignore_intents_list: This parameter lets you configureUnexpecTEDIntentPolicyto not predictaction_unlikely_intentfor a subset of intents. You might want to do this if you come across a certain list of intents for which there are too many false warnings generated.tolerance: Thetoleranceparameter is a number that ranges from0.0to1.0(inclusive). It helps to adjust the threshold score used during prediction ofaction_unlikely_intentat inference time. As you increase the value oftolerance, the number of false warnings should decrease.
The above configuration parameters are the ones you should try tweaking according to your use case and training data. However, additional parameters exist that you could adapt.
Memoization Policy
The MemoizationPolicy remembers the stories from your training data. It checks if the current conversation matches the stories in your stories.yml file. If so, it will predict the next action from the matching stories of your training data with a confidence of 1.0. If no matching conversation is found, the policy predicts None with confidence 0.0.
When looking for a match in your training data, the policy will take the last max_history number of turns of the conversation into account.
Configuring Policies
One important hyperparameter for Rasa Open Source policies is the max_history. This controls how much dialogue history the model looks at to decide which action to take next.
Here is how the config would look like:
policies:
- name: TEDPolicy
max_history: 5
epochs: 200
batch_size: 50
max_training_samples: 300
Data Augmentation
When you train a model, Rasa Open Source will create longer stories by randomly combining the ones in your stories files. The --augmentation flag allows you to set the augmentation_factor. The augmentation_factor determines how many augmented stories are subsampled during training.