Ejemplo n.º 1
0
    def _results_for_user_denied(self, tracker: DialogueStateTracker,
                                 domain: Domain) -> List[float]:
        has_denied_before = tracker.last_executed_action_has(
            ACTION_DEFAULT_ASK_REPHRASE_NAME, skip=1)

        if has_denied_before:
            return confidence_scores_for(self.fallback_nlu_action_name, 1.0,
                                         domain)
        else:
            return confidence_scores_for(ACTION_DEFAULT_ASK_REPHRASE_NAME, 1.0,
                                         domain)
Ejemplo n.º 2
0
    def predict_action_probabilities(
        self,
        tracker: DialogueStateTracker,
        domain: Domain,
        interpreter: NaturalLanguageInterpreter,
    ) -> List[float]:

        parse_data = tracker.latest_message.parse_data
        entities = parse_data.get("entities", [])
        intent_ranking = parse_data.get("intent_ranking", [])
        if len(intent_ranking) == 0 and parse_data.get("intent") is not None:
            intent_ranking = [parse_data.get("intent")]
        can_apply = tracker.latest_action_name == ACTION_LISTEN_NAME
        should_fallback = can_apply and self._should_fallback(
            intent_ranking, self.fallback_trigger)
        should_disambiguate = can_apply and self._should_disambiguate(
            intent_ranking, self.disambiguation_trigger)

        if self._is_user_input_expected(tracker):
            # Shut up and listen
            result = confidence_scores_for(ACTION_LISTEN_NAME, 1.0, domain)

        elif should_fallback:
            logger.debug("Triggering fallback")
            result = confidence_scores_for(self.fallback_action, 1.0, domain)

        elif self._have_options_been_suggested(tracker):
            if not should_disambiguate:
                logger.debug("Successfully disambiguated")
                result = confidence_scores_for(
                    self.disambiguation_followup_action, 1.0, domain)
            else:
                logger.debug(
                    "Will not disambiguate a second time so fast -- triggering fallback"
                )
                result = confidence_scores_for(self.fallback_action, 1.0,
                                               domain)

        elif should_disambiguate:
            logger.debug("Triggering disambiguation")
            disambiguation_message = self.generate_disambiguation_message(
                intent_ranking,
                entities,
            )
            slot_set = self.set_slot(tracker, disambiguation_message)
            if slot_set:
                result = confidence_scores_for(self.disambiguation_action, 1.0,
                                               domain)
            else:
                result = confidence_scores_for(self.fallback_action, 1.0,
                                               domain)

        else:
            # Nothing to see here; setting fallback to default confidence
            result = confidence_scores_for(self.fallback_action,
                                           self.fallback_default_confidence,
                                           domain)

        return result
Ejemplo n.º 3
0
    def predict_action_probabilities(self, tracker: DialogueStateTracker,
                                     domain: Domain) -> List[float]:
        """Predicts the next action if NLU confidence is low.
        """

        if self.deny_suggestion_intent_name not in domain.intents:
            raise InvalidDomain('The intent {} must be present in the '
                                'domain file to use the '
                                '`TwoStageFallbackPolicy`.'
                                ''.format(self.deny_suggestion_intent_name))

        nlu_data = tracker.latest_message.parse_data
        nlu_confidence = nlu_data["intent"].get("confidence", 1.0)
        last_intent_name = nlu_data['intent'].get('name', None)
        should_nlu_fallback = self.should_nlu_fallback(
            nlu_confidence, tracker.latest_action_name)
        user_rephrased = has_user_rephrased(tracker)

        if self._is_user_input_expected(tracker):
            result = confidence_scores_for(ACTION_LISTEN_NAME, 1.0, domain)
        elif self._has_user_denied(last_intent_name, tracker):
            logger.debug("User '{}' denied suggested intents.".format(
                tracker.sender_id))
            result = self._results_for_user_denied(tracker, domain)
        elif user_rephrased and should_nlu_fallback:
            logger.debug("Ambiguous rephrasing of user '{}' "
                         "for intent '{}'".format(tracker.sender_id,
                                                  last_intent_name))
            result = confidence_scores_for(ACTION_DEFAULT_ASK_AFFIRMATION_NAME,
                                           1.0, domain)
        elif user_rephrased:
            logger.debug("User '{}' rephrased intent".format(
                tracker.sender_id))
            result = confidence_scores_for(ACTION_REVERT_FALLBACK_EVENTS_NAME,
                                           1.0, domain)
        elif tracker.last_executed_action_has(
                ACTION_DEFAULT_ASK_AFFIRMATION_NAME):
            if not should_nlu_fallback:
                logger.debug("User '{}' affirmed intent '{}'"
                             "".format(tracker.sender_id, last_intent_name))
                result = confidence_scores_for(
                    ACTION_REVERT_FALLBACK_EVENTS_NAME, 1.0, domain)
            else:
                result = confidence_scores_for(self.fallback_nlu_action_name,
                                               1.0, domain)
        elif should_nlu_fallback:
            logger.debug("User '{}' has to affirm intent '{}'.".format(
                tracker.sender_id, last_intent_name))
            result = confidence_scores_for(ACTION_DEFAULT_ASK_AFFIRMATION_NAME,
                                           1.0, domain)
        else:
            logger.debug("NLU confidence threshold met, confidence of "
                         "fallback action set to core threshold ({}).".format(
                             self.core_threshold))
            result = self.fallback_scores(domain, self.core_threshold)

        return result
Ejemplo n.º 4
0
    def predict_action_probabilities(
        self,
        tracker: DialogueStateTracker,
        domain: Domain,
        interpreter: NaturalLanguageInterpreter,
        **kwargs: Any,
    ) -> List[float]:
        """Predicts the next action if NLU confidence is low."""

        nlu_data = tracker.latest_message.parse_data
        last_intent_name = nlu_data["intent"].get(INTENT_NAME_KEY, None)
        should_nlu_fallback = self.should_nlu_fallback(
            nlu_data, tracker.latest_action.get(ACTION_NAME))
        user_rephrased = has_user_rephrased(tracker)

        if self._is_user_input_expected(tracker):
            result = confidence_scores_for(ACTION_LISTEN_NAME, 1.0, domain)
        elif self._has_user_denied(last_intent_name, tracker):
            logger.debug(
                f"User '{tracker.sender_id}' denied suggested intents.")
            result = self._results_for_user_denied(tracker, domain)
        elif user_rephrased and should_nlu_fallback:
            logger.debug("Ambiguous rephrasing of user '{}' "
                         "for intent '{}'".format(tracker.sender_id,
                                                  last_intent_name))
            result = confidence_scores_for(ACTION_DEFAULT_ASK_AFFIRMATION_NAME,
                                           1.0, domain)
        elif user_rephrased:
            logger.debug(f"User '{tracker.sender_id}' rephrased intent")
            result = confidence_scores_for(ACTION_REVERT_FALLBACK_EVENTS_NAME,
                                           1.0, domain)
        elif tracker.last_executed_action_has(
                ACTION_DEFAULT_ASK_AFFIRMATION_NAME):
            if not should_nlu_fallback:
                logger.debug("User '{}' affirmed intent '{}'"
                             "".format(tracker.sender_id, last_intent_name))
                result = confidence_scores_for(
                    ACTION_REVERT_FALLBACK_EVENTS_NAME, 1.0, domain)
            else:
                result = confidence_scores_for(self.fallback_nlu_action_name,
                                               1.0, domain)
        elif should_nlu_fallback:
            logger.debug("User '{}' has to affirm intent '{}'.".format(
                tracker.sender_id, last_intent_name))
            result = confidence_scores_for(ACTION_DEFAULT_ASK_AFFIRMATION_NAME,
                                           1.0, domain)
        else:
            logger.debug("NLU confidence threshold met, confidence of "
                         "fallback action set to core threshold ({}).".format(
                             self.core_threshold))
            result = self.fallback_scores(domain, self.core_threshold)

        return result