Extract the same entity in different contexts

Hey there,

I have some kind of alienated behavior for a use-case I want to implement using a chatbot. :slight_smile:
My issue is pretty similar to Using single entity for two different intent's.
I basically want to ask a user for his forename, surname, birthdate to (almost uniquely) validate his ID in a natural way and then get his old address and his new address (so far I am only extracting the [german] street name). To do so, I switch to using FormActions at this point.

If my bot utters “Thanks for providing your data {forename}. Please name me your old street.” and the user responds with just the entity, like “Sesamstraße 5”, I end up with something like this over and over again:

DEBUG:rasa_core_sdk.executor:Received request to run 'action_test_form'
DEBUG:rasa_core_sdk.forms:The form '{'name': 'action_test_form', 'validate': True, 'rejected': False}' is active
DEBUG:rasa_core_sdk.forms:Validating user input '{'intent': {'name': 'get_evb', 'confidence': 0.4319833517074585}, 'entities': [{'start': 0, 'end': 13, 'value': 'Sesamstraße 5', 'entity': 'new_street', 'confidence': 0.5471068346577346, 'extractor': 'ner_crf'}], 'intent_ranking': [{'name': 'get_evb', 'confidence': 0.4319833517074585}, {'name': 'deny', 'confidence': 0.40701043605804443}, {'name': 'do_it_myself', 'confidence': 0.3803158700466156}, {'name': 'get_birthdate', 'confidence': 0.37413734197616577}, {'name': 'bye', 'confidence': 0.37190955877304077}, {'name': 'thanks', 'confidence': 0.36665356159210205}, {'name': 'affirm', 'confidence': 0.3496394753456116}, {'name': 'greet', 'confidence': 0.3164404630661011}, {'name': 'change_data', 'confidence': 0.2958131432533264}, {'name': 'need_help', 'confidence': 0.20778490602970123}], 'text': 'Sesamstraße 5'}'
DEBUG:rasa_core_sdk.forms:Extracted 'Sesamstraße 5' for extra slot 'new_street'
DEBUG:rasa_core_sdk.forms:Trying to extract requested slot 'old_street' ...
DEBUG:rasa_core_sdk.forms:Got mapping '{'type': 'from_entity', 'entity': 'old_street', 'intent': [], 'not_intent': []}'
DEBUG:rasa_core_sdk.forms:Got mapping '{'type': 'from_text', 'intent': ['get_old_street'], 'not_intent': []}'
DEBUG:rasa_core_sdk.forms:Failed to extract requested slot 'old_street'
DEBUG:rasa_core_sdk.forms:Request next slot 'old_street'
DEBUG:rasa_core_sdk.executor:Successfully ran 'action_test_form'

The intent is completly wrong (which I cannot fully understand, as I am working with many things like lookup, regex, and variations of different intents). Nevertheless, it feels like I cannot rely on the intent… and I am not sure how to distinguish between the entities “old_street” and “new_street” correctly if they basically can have the same values.

Does anyone have an idea what’s the best thing to do at this point? I already thought about the need to do something completely different from the bottom up or can I incorporate the bot utterance in ner_crf?

Looking forward to your replies and ideas! Thanks :slight_smile:

Kind regards,

I believe, you’re already familiar with slot_mapping in FormAction, you can leverage it by creating street entity and populate old_street or new_street, depending on the previous asked question, and if entity is not extracted use from_text, as you do it already for names :wink:

Then you can override validate method to verify whether extracted information matches some pattern of address

1 Like

Sure, this will help in a first step! But is there a “simple” way to retrieve the last utterance name from the tracker? It is currently not saved (just the latest action name which is action_listen and thus not too helpful). The latest utterance name cannot really be saved in the rasa_core_sdk either because the tracker is loaded from the deserialized tracker payload from rasa_core (as I see it).

:slight_smile: Thanks again! :partying_face:

You can iterate over tracker.events and look for it

True, but the tracker.events do not store the utterances by their ID utter_{name} but the text.

there is BotUttered event there rasa_core_sdk/events.py at 47106d959767148d15b328a3a38a95cd697e0616 · RasaHQ/rasa_core_sdk · GitHub

so something along the lines should work:

        last_utter = None
        for e in reversed(tracker.events):
            if e['event'] == 'bot':
                last_utter = e['text']

Ah, sorry yes if you use utter_... template inside a custom action it is hard to find the name of the template

1 Like

Yes, and I figured that I’ll have a hard time to obtain the templates ID’s from each utterance text. :frowning:

but why do you need last utterance, can’t you use action name?

unfortunately not, because the last action name oftentimes is action_listen

so basically you need action name before previous action? For this you can use tracker.events:

    action_name = None
    for event in reversed(tracker.events):
        if (event['event'] == 'action' and
                not event['name'] == "action_listen"):
            action_name = event['name']

True, thanks for the snippet code at this point! :slight_smile:
But this will sadly not yield the exact utterance template IDs which I seek (already spent some time in the debugger trying to figure out if I can do the reverse lookup from utterance text to its template ID).

We don’t store template ID at this moment. I think it makes sense to add to BotUttered event. Would you like to make a PR for this?

I have come across same issues with the scenarios.

  1. In this scenario, there is information about different cuisines like Maharashtrian, Gujarati thali, south-Indian etc with their respective intent name like Indian_cuisine where the user might ask about the information about cuisines.And the bot gives the brief information about the cuisines.

User: I want to know more about the Maharashtrian dishes. Bot: [Brief Information about Maharashtrian dishes]

  1. In this scenario, I am using FormAction Policy in which user try to order that particular food where bot asks the user for “Specify food type which you want to order?” a user needs to put type_of_cuisines there like Maharashtrian. The slot should picks the value for “type_of_cuisines”.

The actual problem occurs when the FormAction is active and the user put type_of_cuisines is for ex. Maharashtrian and the bot respond the information about that dish[Scenario-1] rather hold the slot for that type_of_cuisines[Scenario-2].

Any Suggestions?

it depends where do you call information template, you can call utter_template explicitly providing slot values. There is a thread in the forum about it somewhere

So how to solve the problem, is have some examples?

which problem?

To fill the slots “old_street” and “new_street” do we need another slot street with same name with the enitty ‘street’ so that rasa can fill the slot street with entity street automatically and then we set our old_street and new_street slots with that entity? as i have tried to set old_street slot to get mapped with the street entity but it didnt work