Trigger intent from custom action

I’m trying to trigger a user intent from a custom action through the UserUttered event. I create this event in my custom action and in the debug logs, it appears to be present in the tracker as a valid state (its the last state in the listed states).

[state 16] user intent: my_intent | previous action name: my_custom_action | slots: {'slot1': (1.0, 0.0, 0.0), 'slot2': (1.0, 1.0), 'slot3': (1.0, 0.0), 'slot4': (1.0,),...)}

I also noticed that get_intent_of_latest_message returns None after this custom action is executed.

My issue is that the bot doesn’t seem to predict the next action based on this triggered intent, that’s now present on the tracker.

My requirement is to basically trigger an intent through a custom action, and ensure that the bot continues the conversation based on this injected intent, as if the user themselves had entered it.

My code:

                ....
                data = \
                {
                    "intent": {
                        "name": "my_intent"
                    }
                }
              return [UserUttered(text="/my_intent", parse_data=data)

Right now, I’m resorting to setting a FollowupAction event to make sure the bot proceeds on the right path.

This is the recommended approach, why not use that directly?

Hi @ChrisRahme . Thanks for the prompt response, as usual! My ultimate goal is to be able to seemlessly redirect the user to another part of the conversation.

something like this:

# flow 1 going on
intent: ...
action: action_flow_1_end

# must take the user to flow 2 now after action_flow_1_end

Original approach

Currently, I make the user click on a button (basically get an intent out of them) and that helps take the user to another part of the conversation.

# flow 1 going on
intent: ...
action: action_flow_1_end

# must take the user to flow 2 now after action_flow_1_end
intent: trigger_flow_2
action: action_flow_2_start
intent:
...

What i’m currently trying

But I’m now looking at ways to avoid asking the user for this, and redirect them directly. something like this:

# flow 1 going on
intent: ...
action: action_flow_1_end

# must take the user to flow 2 now after action_flow_1_end

# add intent trigger_flow_2 to the tracker
# bot then predicts action_flow_2_start
action: action_flow_2_start

My original question was regarding how to accomplish this. When I said I was resorting to FollowupAction, it was because the bot wasn’t using the injected intent to make the next action prediction, and was pretty much always predicting action_listen.

Regarding your suggestion

My only worry with your suggestion (directly use FollowupAction) like this:

# flow 1 going on
intent: ...
action: action_flow_1_end

# must take the user to flow 2 now after action_flow_1_end

# trigger action_flow_2_start directly
action: action_flow_2_start

is that it may hurt policy performance, since all my stories require that there be the intent trigger_flow_2 before action_flow_2_start.

TLDR

  1. Would directly triggering the next action (not bothering with the intent in the middle at all) affect performance?
  2. If I’m still looking for a way to manually inject an intent so that the bot considers it for the next prediction, is there a way to do it?
1 Like

Thanks for the detailed information!


Using FollowupAction should not affect performance :slight_smile:

Once you use it, just talk to your bot a bit using Rasa X and export the story to the stories.yml file - just to show your bot that this is normal, even though it has no choice but to execute it.

This is why I’m suggesting to add some new stories that were built after using FollowupAction, but again, this won’t hurt your bot’s performance since it has no choice to begin with.


I think UserUttered does not execute actions, I also failed to use it that way and had to use FollowupAction instead (which was preferable in my case anyway).

But if you really need UserUttered, maybe you can find a way to execute it from your frontend application as if the user sent the message (but without displaying it of course).

Thanks a lot for the help. I’ll look into using FollowupAction directly, or make frontend trigger it.

Last question: If there isn’t a way (with UserUttered or through an API, or otherwise) to inject an intent on to the tracker that the bot uses to predict the next action (excluding work-arounds like you suggested), shouldn’t this be a feature Rasa should provide out of the box?

There is an API to do this, although I have never used it myself.

See Inject an intent into a conversation: http://{server_ip}:5005/conversations/{conversation_id}/trigger_intent

Thanks… though I had seen this approach before the UserUttered event one, the event one seemed to be easier to implement. Will try this too.

1 Like

Hi I know it’s too late to reply to this post, but I think I figured out the reason why you couldn’t trigger an intent using UserUttered. Your problem was that you forgot to add “confidence” property in your data json. This code should work.

 ....
                data = \
                {
                    "intent": {
                        "name": "my_intent",
                        "confidence": 1.0,
                    }
                }
              return [UserUttered(text="/my_intent", parse_data=data)

Maybe this answer could help anyone who might have this same problem in the future

3 Likes

Thanks @Huascar321 . Will give it a go.

@Huascar321 I can’t get this to work… I’m using rasa 2.6 and tried something like this:

my_action: custom action that asks a yes no question

def run:
      ...
      
       data = \
            {
                "intent": {
                    "name": "/affirm",
                    "confidence": 1.0,
                }
            }


      dispatcher.utter_messagemessage("yes no question")
      return [UserUttered(text="/affirm", parse_data=data)]

the bot still predicts action_listen after asking the yes no question…

Hi!

Can you make a little test?

put it in your rules.yml folder

- rule: Say affirm
  steps:
  - intent: affirm
  - action: utter_affirm #action that the bot will do after identifying affirm intent

and it in your config file

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

I think need change here too

from

dispatcher.utter_messagemessage("yes no question")

to

dispatcher.utter_message(text="yes no question")

Reference:

I have this flow (my_action followed by /affirm) defined in multiple stories and I don’t want the restrictive nature of rules… I don’t think the predictive power of the bot is the issue here, it seems to just ignore the updated intent for some reason…

Could you share your code where this works?

Consider to execute “action_listion” to make the injected UserUttered effected.

    data = {
          "intent": {
              "name": "pseudo_user_intent",
              "confidence": 1.0,
            }
         }

    return [
        ActionExecuted("action_listen"),
        UserUttered(text="/pseudo_user_intent", parse_data=data),
      ]