Пример #1
0
def request_next_slot(
    form: FormAction,
    dispatcher: CollectingDispatcher,
    tracker: Tracker,
    domain: Dict[Text, Any],
    utter_ask_slot_template: Optional[Callable[[str, Tracker],
                                               Optional[str]]] = None,
) -> Optional[List[EventType]]:
    """Request the next slot and utter template if needed,
        else return None.
        If requested doesn't change (we didn't get a valid answer), plays error message if present in domain
        This behaviour is overridable with utter_ask_slot_template parameter
        """

    for slot in form.required_slots(tracker):
        if form._should_request_slot(tracker, slot):
            logger.debug(f"Request next slot '{slot}'")

            template = (utter_ask_slot_template(slot, tracker)
                        if utter_ask_slot_template else None)

            if template is None:
                if tracker.get_slot(REQUESTED_SLOT) == slot and _has_template(
                        domain, f"utter_ask_{slot}_error"):
                    template = f"utter_ask_{slot}_error"
                else:
                    template = f"utter_ask_{slot}"

            dispatcher.utter_message(template=template, **tracker.slots)

            return [SlotSet(REQUESTED_SLOT, slot)]

    # no more required slots to fill
    return None
Пример #2
0
 def base_slot_mappings(
         form: FormAction) -> Dict[Text, Union[Dict, List[Dict]]]:
     return {
         PROVINCE_SLOT:
         [form.from_entity(entity="province"),
          form.from_text()],
         AGE_OVER_65_SLOT: yes_no_nlu_mapping(form),
         SEVERE_SYMPTOMS_SLOT: yes_no_nlu_mapping(form),
         HAS_FEVER_SLOT: yes_no_nlu_mapping(form),
         MODERATE_SYMPTOMS_SLOT: yes_no_nlu_mapping(form),
         HAS_COUGH_SLOT: yes_no_nlu_mapping(form),
         LIVES_ALONE_SLOT: yes_no_nlu_mapping(form),
     }
Пример #3
0
def test_extract_requested_slot_default():
    """Test default extraction of a slot value from entity with the same name"""
    form = FormAction()

    tracker = Tracker(
        "default",
        {REQUESTED_SLOT: "some_slot"},
        {"entities": [{"entity": "some_slot", "value": "some_value"}]},
        [],
        False,
        None,
        {},
        "action_listen",
    )

    slot_values = form.extract_requested_slot(CollectingDispatcher(), tracker, {})
    assert slot_values == {"some_slot": "some_value"}
Пример #4
0
def request_next_slot(
    form: FormAction,
    dispatcher: CollectingDispatcher,
    tracker: Tracker,
    domain: Dict[Text, Any],
) -> Optional[List[EventType]]:
    """
    Custom method to set a custom utterance templata if slot == confirm
    :param form: Form Action object
    :param dispatcher: Rasa SDK Dispatcher
    :param tracker: Rasa SDK tracker
    :param domain: Rasa domain
    :return: SlotSet event
    """
    for slot in form.required_slots(tracker):
        if form._should_request_slot(tracker, slot):
            utter_template = f"utter_ask{f'_{form.name()}' if slot == EntitySlotEnum.CONFIRM else ''}_{slot}"
            dispatcher.utter_message(template=utter_template, **tracker.slots)
            return [SlotSet(REQUESTED_SLOT, slot)]
Пример #5
0
def test_form_deprecation():
    with pytest.warns(FutureWarning):
        FormAction()
Пример #6
0
def yes_no_nlu_mapping(form: FormAction) -> List[Dict]:
    return [
        form.from_intent(intent="affirm", value=True),
        form.from_intent(intent="deny", value=False),
        form.from_text(),
    ]