How to solve delay in dispatcher.utter_message in custom actions, at session start

Hi All

I created a new Action, which gets executed at starting of session. That action, sends welcome message by BOT, and then loads embeddings.

But, i am observing that welcome message is coming later , after embeddings has been loaded, which is giving delay of 4-5secs Can anyone help me out, how to remove that delay or make sure welcome message is send asap as session starts, and in mean time user types, embedding will get loaded. NOTE: Welcome msg comes after " Finished running ‘action_session_start’" in logs

CODE:

class ActionSessionStart(Action):

    def __init__(self):
        ActionSessionStart.em = None
        
        
    def name(self) -> Text:
        return "action_session_start"

    def run(  
        self,
        dispatcher: CollectingDispatcher,
        tracker,
        domain: Dict[Text, Any],) :
        """This run function will be executed when "action_session_start" is triggered."""
        dispatcher.utter_message("Hellooo! Genie welcomes you ")
        print("action_session_start")        
        em = embedding_model_DnA()
        em.load_embeddings()

LOGS:

2021-12-11 09:32:23 DEBUG    rasa_sdk.executor  - Received request to run 'action_session_start'
action_session_start
load_embeddings
2021-12-11 09:32:29 INFO     sentence_transformers.SentenceTransformer  - Load pretrained SentenceTransformer
2021-12-11 09:32:32 INFO     sentence_transformers.SentenceTransformer  - Use pytorch device: cpu
2021-12-11 09:32:32 DEBUG    rasa_sdk.executor  - Finished running 'action_session_start'

You can use the sleep function

import time

print(“Printed immediately.”) time.sleep(2.4) print(“Printed after 2.4 seconds.”)

@Eshita The message will be uttered after the whole function finished running.

With @RodrigoLima’s method, action will first wait 2.4 seconds and then utter the two messages at the same time - don’t do that. Also Rodrigo, I think Eshita wants to remove the delay, not add one.

What you can do is use FollowupAction to load embeddings in a second action.

class ActionSessionStart(Action):
    def name(self):
        return 'action_session_start'

    def run(self, dispatcher, tracker, domain):
        dispatcher.utter_message('Hellooo! Genie welcomes you')
        return [FollowupAction('action_load_embeddings') 


class ActionLoadEmbeddings(Action):
    def name(self):
        return 'action_load_embeddings'

    def run(self, dispatcher, tracker, domain):
        em = embedding_model_DnA()
        em.load_embeddings()
        return []
1 Like

@ChrisRahme : thanks for your response… this is actually what i was looking for.

Also,I updated the code as u suggested, added action in domain.yml too… But FollowupAction “action_load_embeddings” is not executing. Print statement not getting printed, embeddings not getting loaded. Also no error.

Can u please suggest what i be doing wrong:

ACTIONS:

from rasa_sdk.events import FollowupAction
class ActionSessionStart(Action):
     
    def name(self) -> Text:
        return "action_session_start"

    def run(  
        self,
        dispatcher: CollectingDispatcher,
        tracker,
        domain: Dict[Text, Any],) :
        """This run function will be executed when "action_session_start" is triggered."""
  
        dispatcher.utter_message("Hellooo! Genie welcomes you")
        print("action_session_start")  
        return [FollowupAction('action_load_embeddings')]

class ActionLoadEmbeddings(Action):
    """ Loads embeddings after session starts"""
    
    def __init__(self):
        ActionLoadEmbeddings.em = None
        
    def name(self) -> Text:
        return 'action_load_embeddings'

    def run(self, dispatcher, tracker, domain):
        print(" In ActionLoadEmbeddings")
        em = embedding_model_DnA()
        em.load_embeddings()
        ActionLoadEmbeddings.em = em
        return []

OUTPUT:

2021-12-12 23:27:47 INFO     rasa_sdk.endpoint  - Starting action endpoint server...
2021-12-12 23:27:55 INFO     rasa_sdk.executor  - Registered function for 'action_faq'.
2021-12-12 23:27:55 INFO     rasa_sdk.executor  - Registered function for 'action_fetch_button_value'.
2021-12-12 23:27:55 INFO     rasa_sdk.executor  - Registered function for 'action_open_link'.
2021-12-12 23:27:55 INFO     rasa_sdk.executor  - Registered function for 'action_session_start'.
2021-12-12 23:27:55 INFO     rasa_sdk.executor  - Registered function for 'action_load_embeddings'.
2021-12-12 23:27:55 INFO     rasa_sdk.endpoint  - Action endpoint is up and running on http://0.0.0.0:5055
action_session_start

test input ---- > > >  machine learning

Expected" Before test input, " In ActionLoadEmbeddings" must be printed

Weird. Did you register the action in the domain?

Also try in action_session_start

return [SessionStarted(), FollowupAction('action_load_embeddings')]

and in action_load_embeddings

return [ActionExecuted("action_listen")]

Not working. FollowupAction(‘action_load_embeddings’) is not getting called. No error if i add or remove if from domain.yml.

I read somethere to remove kerasPolicy from config, i commented all of them. Tried async function too…

Also, i am using rasa 2.8, any idea what i might be doing wrong

1 Like

No idea, sorry :confused:

I suggest starting a new thread called “FollowupAction for ActionSessionStart”, since it’s now kind of a different problem and it will get new viewers, and send the link to that thread here in case future readers want to follow it.

Thanks a lot for your support @ChrisRahme

1 Like

I don’t know what content you are loading but why don’t you try a function in python that waits for the content to load before going to the next function? type a for while