How to reset every slots but the ones provided in the last message

Hi I have a use case where I need to reset every slot but the ones provided in the last user message. Code:

class ActionEspecialidad(Action):

    def name(self):
        return 'action_especialidad'

    def run(self, dispatcher, tracker, domain):
        print(tracker.current_slot_values())
        keep_slots = last_message_slots(tracker.latest_message.text)
        for key, _ in tracker.current_slot_values().items():
            if key in keep_slots:
                continue
            else:
                tracker._set_slot(key=key, value=None)

        print(tracker.current_slot_values())
        response = build_responses(tracker)

        dispatcher.utter_message('$' + response)

        # dispatcher.utter_message(especialidad)
        return [SlotSet('especialidad', tracker.get_slot('especialidad'))]
def last_message_slots(message):
    from rasa_core.interpreter import RasaNLUInterpreter

    interpreter = RasaNLUInterpreter('/'.join([model_dir, project_name, fixed_model_name]))
    stuff = interpreter.parse(message)

    entities = [x['entity'] for x in stuff['entities']]

    return entities

I know that exists something called reset_slots, but this reset every slot and I want to keep the ones on the last message. Does anyone know another approach? This one works but there is probably a better one.

Hi @AlvaroMonteagudo,

Could you return ActionResetSlots() in the step before the user utterance?

As for improvements on this implementation, why import the interpreter? You can access the message entities using tracker.latest_message.entities. Perhaps then you could:

  1. Create a list events = [ActionResetSlots()]
  2. Check if each entity in tracker.latest_message.entities corresponds to a slot
  3. If so, append SlotSet("slot", "entity") to events
  4. return events