Implementing Default Fallback Actions

The documentation for Fallback Actions states that you can “Add the FallbackPolicy to your policy ensemble” or you can “run it from python”.

Question 1: Can anyone clarify what “adding FallbackPolicy to the policy ensemble” means? It also mentions parameters to adjust thresholds and identify the name of the fallback action. Does that mean you can put them directly into the run command below?

python -m rasa_core.run -d models/current/dialogue -u models/current/nlu_model

Question 2: If running from python, do you simply place this in the same actions.py file that you use for custom actions?

from rasa_core.policies.fallback import FallbackPolicy

from rasa_core.policies.keras_policy import KerasPolicy

from rasa_core.agent import Agent

fallback = FallbackPolicy(fallback_action_name=“action_default_fallback”, core_threshold=0.3, nlu_threshold=0.3)

agent = Agent(“domain.yml”, policies=[KerasPolicy(), fallback])

Thanks!

1 Like

Hi there. To add the Fallback Policy, you need to create an Agent yourself and use that Agent when training the dialog engine. So you need to do a small bit of Python coding, you cannot just use the FallbackPolicy from the command line. What does all this mean? I have a script which wraps common development tasks like training and running. If I call my script with parameter “train_dialog” it calls the method below:

def train_dialog(online=False, nlu=True):
    agent = Agent("domain.yml", policies=[MemoizationPolicy(), KerasPolicy()])
    stories_file = "data\stories"
    stories_data = agent.load_data(stories_file)
    output_path = "models\dialog"
    kwargs = {"epochs": 100}
    agent.train(
        stories_data,
        validation_split=0.2,
        **kwargs
    )
    agent.persist(output_path)

Note line 2 where the agent is initialized. Here you can specify your particular policy pipeline/ensemble that you want to use. I do not use the FallbackPolicy, so it is not present in the policies array parameter. If you want to use the FallbackPolicy, just add it to the policies list like this:

fallback = FallbackPolicy(fallback_action_name="action_default_fallback",
                          core_threshold=0.3,
                          nlu_threshold=0.3)

agent = Agent("domain.yml", policies=[MemoizationPolicy(),KerasPolicy(), fallback])

This is taken straight from the rasa core docu at https://rasa.com/docs/core/fallbacks/

So, you specify your policies during your custom code for dialog training, not in an action. Also, because you have to write a little code to override rasas defaults, you cant use FallbackPolicy straight from the command line, you have to call your custom code. Hope this helps!

This worked! Thank you very much!! I wrote my own file to train core with (train_core.py) and included the fallback policy. It is now triggering when I type random characters.

I noticed in your script you specify a specific validation_split - if I don’t specify that parameter (or any others like augmentation, etc.), does Rasa just use the default as if I ran training through the command line training (like below)?

python -m rasa_core.train -d domain.yml -s data/stories -o models/current/dialogue --epochs 300

Actually, with some testing I answered my own follow up. It seems like it defaulted to “0” for validation split when i didn’t specify.

Thanks again @jloutz for your help!!

1 Like

Can you please let me know step by step in which file should I include which code and the command line arguments used to run the particular code.

Please check screenshot. The whole demo can be found here: https://github.com/ryang420/rasa_bot.git

1 Like