def test_not_predict_fallback_intent(message: Message, component_config: Dict): old_message_state = copy.deepcopy(message) classifier = FallbackClassifier(component_config=component_config) classifier.process(message) assert message == old_message_state
def test_predict_fallback_intent(): threshold = 0.5 message = Message( "some message", data={ INTENT: { "name": "greet", INTENT_CONFIDENCE_KEY: 0.234891876578331 }, INTENT_RANKING_KEY: [ { "name": "greet", INTENT_CONFIDENCE_KEY: 0.234891876578331 }, { "name": "stop", INTENT_CONFIDENCE_KEY: threshold - 0.0001 }, { "name": "affirm", INTENT_CONFIDENCE_KEY: 0 }, { "name": "inform", INTENT_CONFIDENCE_KEY: -100 }, { "name": "deny", INTENT_CONFIDENCE_KEY: 0.0879683718085289 }, ], }, ) old_message_state = copy.deepcopy(message) classifier = FallbackClassifier( component_config={THRESHOLD_KEY: threshold}) classifier.process(message) expected_intent = { "name": DEFAULT_NLU_FALLBACK_INTENT_NAME, INTENT_CONFIDENCE_KEY: 1.0, } assert message.data[INTENT] == expected_intent old_intent_ranking = old_message_state.data[INTENT_RANKING_KEY] current_intent_ranking = message.data[INTENT_RANKING_KEY] assert len(current_intent_ranking) == len(old_intent_ranking) + 1 assert all(item in current_intent_ranking for item in old_intent_ranking) assert current_intent_ranking[0] == expected_intent
def create_fallback_classifier( component_config: Dict[Text, Any], default_model_storage: ModelStorage, default_execution_context: ExecutionContext, ): classifier = FallbackClassifier.create( { **FallbackClassifier.get_default_config(), **component_config }, default_model_storage, Resource("fallback"), default_execution_context, ) return classifier
def test_predict_fallback_intent(message: Message, component_config: Dict): old_message_state = copy.deepcopy(message) classifier = FallbackClassifier(component_config=component_config) classifier.process(message) expected_intent = { INTENT_NAME_KEY: DEFAULT_NLU_FALLBACK_INTENT_NAME, PREDICTED_CONFIDENCE_KEY: 1.0, } assert message.data[INTENT] == expected_intent old_intent_ranking = old_message_state.data[INTENT_RANKING_KEY] current_intent_ranking = message.data[INTENT_RANKING_KEY] assert len(current_intent_ranking) == len(old_intent_ranking) + 1 assert all(item in current_intent_ranking for item in old_intent_ranking) assert current_intent_ranking[0] == expected_intent
def test_not_predict_fallback_intent(): threshold = 0.5 message = Message( "some message", data={ INTENT: { "name": "greet", INTENT_CONFIDENCE_KEY: threshold }, INTENT_RANKING_KEY: [ { "name": "greet", INTENT_CONFIDENCE_KEY: 0.234891876578331 }, { "name": "stop", INTENT_CONFIDENCE_KEY: 0.1 }, { "name": "affirm", INTENT_CONFIDENCE_KEY: 0 }, { "name": "inform", INTENT_CONFIDENCE_KEY: -100 }, { "name": "deny", INTENT_CONFIDENCE_KEY: 0.0879683718085289 }, ], }, ) old_message_state = copy.deepcopy(message) classifier = FallbackClassifier( component_config={THRESHOLD_KEY: threshold}) classifier.process(message) assert message == old_message_state
def __init__(self, prediction_to_return: Dict[Text, Any]) -> None: # add intent classifier to make sure intents are evaluated super().__init__([FallbackClassifier()], None) self.prediction = prediction_to_return
def test_defaults(): classifier = FallbackClassifier({}) assert classifier.component_config[THRESHOLD_KEY] == DEFAULT_NLU_FALLBACK_THRESHOLD assert classifier.component_config[AMBIGUITY_THRESHOLD_KEY] == 0.1
def test_default_threshold(): classifier = FallbackClassifier({}) assert classifier.component_config[ THRESHOLD_KEY] == DEFAULT_NLU_FALLBACK_THRESHOLD