Search code examples
pythonrasa-nlurasa

How to extract slot from Rasa form


I added a form to my rasa bot. The rasa bot works well without the form but on adding the form I get the error "Failed to extract slot SEARCH_NAME with action form_search_scholarship". I do not know what I am missing as I have tried different variation.

domain.yml

...
entities:
- SEARCH_NAME
- SEARCH_KEYWORD
slots:
SEARCH_KEYWORD:
 type: text
SEARCH_NAME:
  type: text
...

stories.yml

## keyword search path
* menu
  - utter_menu
* search_scholarship
  - utter_search
* keyword_search_scholarship
  - form_search_keyword
  - form{"name": "form_search_keyword"}
  - form{"name": null}

## scholarship name search path
* menu
  - utter_menu
* search_scholarship
  - utter_search
* scholarship_name_search
  - form_search_scholarship
  - form{"name": "form_search_scholarship"}
  - form{"name": null}

actions.py

class ActionFormSearchKeyword(FormAction):
    def name(self) -> Text:
        """Unique identifier of the form"""

        return "form_search_keyword"

    @staticmethod
    def required_slots(tracker: Tracker) -> List[Text]:
        """A list of required slots that the form has to fill"""

        return ["SEARCH_KEYWORD", ]

    def submit(
            self,
            dispatcher: CollectingDispatcher,
            tracker: Tracker,
            domain: Dict[Text, Any],
    ) -> List[Dict]:
        """Define what the form has to do
            after all required slots are filled"""

        # utter submit template
        dispatcher.utter_message(template="utter_search_found", search_result=tracker.get_slot('SEARCH_KEYWORD'),
                                 search=tracker.get_slot('SEARCH_KEYWORD'))
        # # utter submit template
        # dispatcher.utter_message(template="utter_search_not_found", search_result=tracker.get_slot('SEARCH_KEYWORD'),
        #                          search=tracker.get_slot('SEARCH_KEYWORD'))
        return []

    def slot_mappings(self) -> Dict[Text, Union[Dict, List[Dict]]]:
        """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 {
            "search_result": [self.from_entity(entity="SEARCH_KEYWORD"),
                     self.from_text()],
        }

Solution

  • It's hard to say what the exact problem is (it would help to see the form_search_scholarship action code since that's where the error is coming from) but my guess is that you might not be correctly extracting the name entity from the input text. That tends to be the most common reason you'll see this error.

    To troubleshoot this, I would:

    • Run rasa nlu in the command line and input the turn the assistant is failing on. See if the intended entity is extracted correctly
    • If it isn't then that's probably your issue. I'd recommend additional additional training data (if you have a list of scholarship names I'd probably use a lookup table: https://blog.rasa.com/improving-entity-extraction/)
    • If it is exacted correctly by the NLU then the problem is somewhere else and you'll need to do a bit more digging; hard to say what the exact problem could be without the training data + code for the other form.