Rasa formvalidation - Rasa Open Source - Rasa Community Forum

Rasa formvalidation

Post by naveenjr on Feb 21, 2022

Rasa form "FormValidationAction" is not checking for setting slot value. It is setting simultaneously 2 entities into slot.

Code


def validate_material(
    self,
    slot_value: Any,
    dispatcher: CollectingDispatcher,
    tracker: Tracker,
    domain:  Dict[Text, Any],
) -> Dict[Text, Any]:
    """Validate material value."""
    try:
        material_no= re.findall(r'\d+',slot_value,re.IGNORECASE)[0]
        print(material_no)
    except Exception as E:
        print(E)
    if len(material_no) == 13:
        return {"material": material_no}
    else:
        dispatcher.utter_message(response="utter_wrong_material")
        return {"material": None}

def validate_plant(
    self,
    slot_value: Any,
    dispatcher: CollectingDispatcher,
    tracker: Tracker,
    domain: Dict[Text, Any],
) -> Dict[Text, Any]:
    """Validate `plant` value."""
    entity=re.search(r'(\d{4,5})', slot_value)
    if len(slot_value) < 7:
        return {"plant": entity[0]}
    else:
        print('Uttering wrong plant')
        dispatcher.utter_message(response="utter_wrong_plant")
        return {"plant": None}

Post by nik202 on Feb 21, 2022

@naveenjr can you share the rasa --version?

Post by naveenjr on Feb 21, 2022

Rasa Version: 2.8.1 Minimum Compatible Version: 2.8.0 Rasa SDK Version: 2.8.1 Rasa X Version: 0.39.3 Python Version: 3.8.12 Operating System: Windows-10-10.0.19043-SP0

Post by nik202 on Feb 22, 2022

@naveenjr please visit this repo for your reference: Rasa GitHub Repo I would recommend going through every mentioned forms example, it will help you. I hope this will solve your issue.

Post by naveenjr on Feb 22, 2022

thanks for prompt reply. i have found the issue in the domain file

Earlier

data_form:
  required_slots:
    material:
    - entity: material
      type: from_entity
    plant:
    - entity: plant
      type: from_entity

Now

data_form:
  required_slots:
    material:
    - type: from_text
    plant:
    - type: from_text

This is giving a better result. But what is the actual difference? Why was form validation failing in the earlier case?

Post by nik202 on Feb 22, 2022

@naveenjr great naveen, please close this thread as a solution. The reason can be anything; I cannot comment without seeing the custom action only, that’s why I recommend seeing the repo.

Post by naveenjr on Feb 22, 2022

custom action file for your reference.

Code

class Resource_Form(FormValidationAction):

def name(self) -> Text:
        return "resource_form"

def run(self, dispatcher: CollectingDispatcher,
             tracker: Tracker,
             domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:
        # Calling the DB
        # calling an API
        # do anything
        # all calculations are done
        camera = tracker.get_slot('material')
        ram = tracker.get_slot('plant')
        battery = tracker.get_slot('work_center')
        dispatcher.utter_message(text='Here are your search results')
        return []

def slot_mappings(self):
        """A dictionary to map required slots to an extracted entity, intent: value pairs, a whole message or a list of them, where a first match will be picked"""
        return {"material": self.from_entity(entity="material",
                                                intent="material_number",not_intent="plant_name"),
                "plant": self.from_entity(entity="plant",
                                            intent="plant_name")}

def validate_material(
        self,
        value: Text,
        dispatcher: CollectingDispatcher,
        tracker: Tracker,
        domain: Dict[Text, Any],
    ) -> Dict[Text, Any]:
        """Validate material value."""
        material_v = re.findall(r'[0-9]+[A-Za-z]', value)[0]
        if len(material_v) > 10:
            return {"material": material_v}
        else:
            dispatcher.utter_message(template="utter_wrong_ram")
            return {"material": None}

def validate_plant(
        self,
        slot_value: Text,
        dispatcher: CollectingDispatcher,
        tracker: Tracker,
        domain: Dict[Text, Any],
    ) -> Dict[Text, Any]:
        """Validate `plant` value."""
        print(f"slot value given = {slot_value}")
        entity=re.findall(r'(\d{4,6})', slot_value)
        print(f"len slot value given = {len(slot_value)}")
        if len(slot_value) < 7:
            return {"plant": entity[0]}
        else:
            print('Uttering wrong plant')
            dispatcher.utter_message(response="utter_wrong_plant")
            return {"plant": None}