Are slot mappings not used at the beginnig of a Form Action?

Hi everyone! I am using Forms for my chatbot and I use a lot of slot_mappings. When I tested my chatbot it does not extract correctly the entities of the first message but it does for the following ones. I think it is because in the run method, slot_mappings are not used. Am I wrong? :grimacing:

I think it will be very useful that these mappings are available from the very beginning of the Form.

Greetings!

2 Likes

Hi @cheloveco

could you please post the content of your FormAction?

Regards

Hi @JulianGerhard I am not sure what you want me to show you, but here is a part of my Form class:

class BusquedaForm(FormAction):

def name(self):
    return "busqueda_form"

def slot_mappings(self):
    return { "estado_Busqueda" : [
                        self.from_intent(intent='el_que_sea', value='Todos'),
                        self.from_entity(entity='estado'),
                        self.from_entity(entity='lugar'),
                        ],

            "municipio_Busqueda" :[
                        self.from_intent(intent='el_que_sea', value='Todos'),
                        self.from_entity(entity='municipio'),
                        self.from_entity(entity='lugar'),
                        ],
            "banios_Busqueda" : [
                        self.from_entity(entity='numero'),
                        self.from_entity(entity='banios'),
                        ],
            "recamaras_Busqueda" :[ 
                        self.from_entity(entity='numero'),
                        self.from_entity(entity = 'recamaras'), 
                        ],
            "tipo_Busqueda" :[
                        self.from_entity(entity = 'tipo')
                        ],
            "propiedad_Busqueda" :[
                        self.from_entity(entity = 'propiedad')
                        ],
            "banios_Busqueda" :[
                        self.from_entity(entity = 'banios'),
                        self.from_entity(entity = 'numero'),
                        ],
            "recamaras_Busqueda" :[
                        self.from_entity(entity = 'recamaras'),
                        self.from_entity(entity = 'numero'),
                        ],
            "alcaldia_Busqueda" : [
                        self.from_entity(entity = 'alcaldia'),
                        ],
            }


@staticmethod
def required_slots(tracker):

    if tracker.get_slot('propiedad_Busqueda') in ["terreno", "bodega"]:            
        
        if tracker.get_slot('estado_Busqueda') == 'Todos':
            return ["tipo_Busqueda", "propiedad_Busqueda"]

        elif tracker.get_slot('estado_Busqueda') == 'Ciudad de México':
            return ["alcaldia_Busqueda", "tipo_Busqueda",  "propiedad_Busqueda" ]

        else:
            return ["tipo_Busqueda", "estado_Busqueda", "municipio_Busqueda"] 


    else:
        if tracker.get_slot('estado_Busqueda') == 'Todos':
            return  ["tipo_Busqueda", "propiedad_Busqueda", "banios_Busqueda", "recamaras_Busqueda"]
        elif tracker.get_slot('estado_Busqueda') == 'Ciudad de México':
            return ["alcaldia_Busqueda", "tipo_Busqueda", "propiedad_Busqueda", "banios_Busqueda", "recamaras_Busqueda", ]
        else: 
            return ["tipo_Busqueda", "propiedad_Busqueda", "estado_Busqueda", "municipio_Busqueda", "banios_Busqueda", "recamaras_Busqueda"]

The remaining part are validations of several slots, and the submit method.

However I managed to modify the rasa_sdk.forms module, specifically the _activate_if_required function and now the Form is filling the corresponding slots with the mappings given in the actions.py script, at the first message, the one that activates the Form.

@cheloveco when you say if didn’t correctly pick them up, does it pick them up in the wrong format, or does it not pick them up at all? Which version of SDK are you on?

Can you give an example of what the input was, what you wanted picked up, and what you got?

I have these versions of rasa:

image

And this is the example:

I wrote to my chatbot “me gustaria rentar un departamento en la cdmx”, the nlu model does its job fine, it extracts the three enitites I wanted to, namely “tipo”, “propiedad” and “estado”. If you look at the requested slots there are slots named “tipo_Busqueda”, “propiedad_Busqueda” and “estado_Busqueda” which are empty. Also, in the piece of the Form class I have uploaded previously is the slot_mapping method where I establish these relations between those entities and the required slots.

Also, as I previously said if I add the slot mappings at the _activate_if_required function the Form fill correclty the slots .

And here is the lines of code I added

            if latest_entities:
                
                """   
                       Extract the slot mappings and iterate over the entities and the mappings
                    for a given slot, at the moment just the type "from_entity" mappings are used
                """
                slot_mappings = self.get_mappings_for_slot(slot_name)
                for entity in latest_entities:
                    for slot_map in slot_mappings:
                        if slot_map['type'] == 'from_entity':
                            if slot_map['entity'] == entity['entity']:
                                prefilled_slots[slot_name] = entity['value']

With:

latest_entities = tracker.latest_message.get(‘entities’, [])

Hi, I am having the exact same issue. It seems like ActionForm is not performing slot mapping on the message before its activation. To give an example, I used official RestaurantBot from examples which does mapping of entities with different names.

You can see what entities have been extracted (cuisine, num_people, seating), however, only entities that have slots named the same are recorded. seating which should have been mapped to outdoor_seating is now requested to be filled, even though it was already provided.

Do you think this is intended behavior or it should be fixed?

1 Like

@cheloveco could you make a bug report on our github for this? I think you’re right. If you’re willing to tackle all of the slot mapping types, we’d also love to accept a contribution :slight_smile:

I have the same problem as @bvezilic :slight_smile:

Sure I’ll do it, and I will be glad to contribute. :smiley:

Awesome, thank you!

This is a problem I’ve faced as well and it is caused by this piece of code in rasa_core_sdk’s forms.py file. In the method extract_other_slots() of class FormAction,

# check whether the slot should be filled
# by entity with the same name
should_fill_slot = (other_slot_mapping["type"] == "from_entity" and
                             other_slot_mapping.get("entity") == slot and
                             self.intent_is_desired(other_slot_mapping, tracker)

Since this piece of code checks whether the slot name and entity name is the same, it blocks slots which aren’t named the same as the entity from being filled.

I have been wondering whether this was intentional or not and whether the code was written with a particular edge case in mind.

Hi @cheloveco, Have been struggling with same issue. Can you pls share the rewritten function? I tried replicating this piece of code, but still running into problems.

Many thanks…