Getting the entities from previous user message/questions

Hi,

I want get the entities from the previous user message.

For e.g. : User : Show me the restaurants in Mumbai Bot : Please tell me the type of restaurant User: Chinese

Here i need to get both location(Mumbai) and restaurant type(Chinese).

Could anyone please help me how to achieve this?

rasa_core_sdk=0.12.1 rasa_nlu~=0.14.0

Thanks in advance

Hi @Bhargav,

this should be possible over the events. Every method in your actions that accept

tracker:Tracker

is able to provide you with all recent events. A sample would be:

 for event in tracker.events:
    <your code>

If the event contained extracted entities, you will be able to access them this way.

Tell me, if you need more help.

Regards

HI @JulianGerhard,

Thank you very much for the reply. Could you please explain me how to use these event to retrieve entities. It would help me a lot.

Hi @Bhargav,

no problem. Let’s assume you have a function print_given_entities which should be executed in every validation method of your actions.py.

This would look like:

def print_given_entities(self, latest_message, dispatcher, tracker, domain):
    # the tracker got the information about recent events 
    for event in tracker.events:
        # if there is further data bein parsed
        if 'parse_data' in event:
            # is only doing something if there actually are entities
            for entity in event['parse_data']['entities']:
                logger.debug('Found entity: {}'.format(entity))

if you call this method e.g. in one of your FormActions and kept in mind to provide the expected arguments, then the method will give you what you want to have.

If there are problems with the code, don’t hesitate to ask.

Regards

Hi @JulianGerhard

Thank you very much for the quick response. I will try this and let you know.

Thanks,

Hello Julian

I’m also trying to do something similar. I want to extract the entity from the last message however when I tried with tracker.latest_message, it returned an empty list

Also, I tried your solution (I was not sure how to create my own function, so I used the run function itself in a custom action class. Here’s the code for the same

class ActionTimeframeSalesFigures(Action):

    def name(self):
        return "time_based_sales_figures"

    def run(self, latest_message, dispatcher, tracker, domain):
        
        entities = tracker.latest_message['entities']
        print(entities)
        print(tracker.events)

        for timeframe in entities:
            if timeframe == "Today's" or timeframe == "today's":
                dispatcher.utter_message(f"Todays Sales figures are 100$ {timeframe}")

            elif timeframe == "Weekly":
                dispatcher.utter_message("Current week's sales figures are 100$")


            elif timeframe == "Monthly" or timeframe == "monthly":
                dispatcher.utter_message("Current month's sales figures are 100$")


            elif timeframe == "Quarterly":
                dispatcher.utter_message("Current quarter's sales figures are 100$")

            else:
                dispatcher.utter_message("Please enter the correct timeframe")

        return []

This gave an error saying run method is missing with the domain argument

Can you please let me know if I’m doing something wrong here?