async def test_assessment_type_with_previous_tested_positive(self): tracker = self.create_tracker(events=[ UserUttered( "tested positive", parse_data={ "text": "tested positive", "intent": { "name": "tested_positive", "confidence": 1.0 }, "entities": [], }, ), UserUttered( "yes", parse_data={ "text": "yes", "intent": { "name": "affirm", "confidence": 1.0 }, "entities": [], }, ), ActionExecuted(self.form_name), SlotSet(ASSESSMENT_TYPE_SLOT, AssessmentType.GENERIC), ]) await self.run_action(tracker) self.assert_events( [SlotSet(ASSESSMENT_TYPE_SLOT, AssessmentType.TESTED_POSITIVE)]) self.assert_templates([])
def run(self, dispatcher, tracker, domain): intent = tracker.latest_message["intent"].get("name") if (intent == "yes"): return [ UserUttered("/get_started_onboarding", { 'name': 'get_started_onboarding', 'confidence': 1.0 }) ] elif (intent == "no"): return [ UserUttered("/get_started_funfact", { 'name': 'get_started_funfact', 'confidence': 1.0 }) ] elif (intent == "smalltalk"): dispatcher.utter_template("utter_smalltalk", tracker) dispatcher.utter_template("utter_ask_continue_form", tracker) dispatcher.utter_template("utter_ask_lifestyle", tracker) else: return [ UserUttered("/" + intent, { 'name': intent, 'confidence': 1.0 }) ] return []
def run(self, dispatcher, tracker, domain) -> List[EventType]: if tracker.get_slot("step"): step = int(tracker.get_slot("step")) + 1 if step in [2, 3, 4]: dispatcher.utter_message(template=f"utter_continue_step{step}") else: dispatcher.utter_message(template="utter_no_more_steps") return [] else: # trigger get_started_step1 intent return [ ActionExecuted("action_listen"), UserUttered( "/get_started_step1", { "intent": { "name": "get_started_step1", "confidence": 1.0 }, "entities": {}, }, ), ]
async def test_other_intent(self): tracker = self.create_tracker( intent="unrelated", events=latest_events("utter_dwarfs_song")) await self.run_action(tracker) self.assert_events([ UserUtteranceReverted(), ActionExecuted(ACTION_LISTEN_NAME), UserUttered( USER_TEXT, parse_data={ "text": USER_TEXT, "intent": { "name": FALLBACK_INTENT, "confidence": 1.0 }, "intent_ranking": [{ "name": FALLBACK_INTENT, "confidence": 1.0 }], "entities": [], }, ), ]) self.assert_templates([])
def latest_events(template_name): return [ BotUttered(text="Heigh ho, heigh ho", metadata={"template_name": template_name}), ActionExecuted(ACTION_LISTEN_NAME), UserUttered(USER_TEXT), ]
def run( self, dispatcher: CollectingDispatcher, tracker: Tracker, domain: Dict[Text, Any], ) -> List[Dict[Text, Any]]: bind_logger(tracker) last_user_event = tracker.get_last_event_for("user") last_user_event = copy.deepcopy(last_user_event) text = last_user_event["text"] fallback_user_event = UserUttered( text, parse_data={ "text": text, "intent": {"name": "fallback", "confidence": 1.0}, "intent_ranking": [{"name": "fallback", "confidence": 1.0}], "entities": [], }, ) return [ UserUtteranceReverted(), ActionExecuted("action_listen"), fallback_user_event, ]
def test_latest_input_channel(): tracker = get_tracker([ UserUttered("my message text", input_channel="superchat"), ActionExecuted("action_listen"), ]) assert tracker.get_latest_input_channel() == "superchat"
async def test_task_allowed(self): tracker = self.create_tracker( intent=ALLOWED_INTENT, events=LATEST_EVENTS + [UserUttered(text="text", parse_data={"intent": ALLOWED_INTENT})], ) await self.run_action(tracker) self.assert_events([]) self.assert_templates([])
def start_story_events(story_intent): # type: (Text) -> List[Dict] return [ActionExecuted("action_listen")] + [ UserUttered( "/" + story_intent, { "intent": { "name": story_intent, "confidence": 1.0 }, "entities": {} }) ]
def test_provide_feedback_not_given(self): tracker = self.create_tracker( slots={ REQUESTED_SLOT: FEEDBACK_SLOT, QUESTION_SLOT: QUESTION, ANSWERS_SLOT: ANSWERS, STATUS_SLOT: QuestionAnsweringStatus.SUCCESS, }, text="some text with", intent="another_intent", entities=[{ "and": "entities" }], ) self.run_form(tracker, DOMAIN) self.assert_events([ SlotSet(FEEDBACK_SLOT, FEEDBACK_NOT_GIVEN), SlotSet(QUESTION_SLOT, None), SlotSet(FEEDBACK_SLOT, None), SlotSet( ASKED_QUESTION_SLOT, { **FULL_RESULT_SUCCESS, FEEDBACK_KEY: FEEDBACK_NOT_GIVEN }, ), Form(None), SlotSet(REQUESTED_SLOT, None), ActionExecuted("utter_ask_another_question"), ActionExecuted("action_listen"), UserUttered( "some text with", parse_data={ "text": "some text with", "intent": { "name": "another_intent" }, "intent_ranking": [], "entities": [{ "and": "entities" }], }, ), Form(None), SlotSet(REQUESTED_SLOT, None), ]) self.assert_templates([])
def _carry_user_utterance(tracker: Tracker) -> List[EventType]: return [ ActionExecuted("utter_ask_another_question"), BotUttered(metadata={"template_name": "utter_ask_another_question"}), ActionExecuted(ACTION_LISTEN_NAME), UserUttered( tracker.latest_message.get("text", ""), parse_data={ "text": tracker.latest_message.get("text", ""), "intent": tracker.latest_message.get("intent", {}), "intent_ranking": tracker.latest_message.get("intent_ranking", []), "entities": tracker.latest_message.get("entities", []), }, ), ]
async def test_task_not_allowed(self): tracker = self.create_tracker( intent=FORBIDDEN_INTENT, events=LATEST_EVENTS + [ UserUttered(text="text", parse_data={"intent": FORBIDDEN_INTENT}) ], ) await self.run_action(tracker) self.assert_events( [ActionReverted(), FollowupAction(FALLBACK_ACTION_NAME)]) self.assert_templates([])
def inject_intent(self, intent): """Returns events that injects given intent into tracker state to allow handling them on stories same way as comes from user input. Use this method in other actions in theirs return statement. [More details here ](https://forum.rasa.com/t/trigger-a-story-or-intent-from-a-custom-action/13784/9)""" return [ ActionExecuted('action_listen', confidence=1.0), UserUttered(text='/' + intent, parse_data={ 'intent': { 'name': intent, 'confidence': 1.0 }, 'entities': [] }) ]
async def test_action_not_in_list(self): tracker = self.create_tracker( intent=FORBIDDEN_INTENT, events=[ ActionExecuted("action_another"), ActionExecuted(ACTION_LISTEN_NAME), UserUttered(text="text", parse_data={"intent": FORBIDDEN_INTENT}), ], ) await self.run_action(tracker) self.assert_events( [ActionReverted(), FollowupAction(FALLBACK_ACTION_NAME)]) self.assert_templates([])
def _carry_user_utterance(tracker: Tracker) -> List[EventType]: return [ Form( None ), # Ending it manually to have events in correct order to fit stories SlotSet(REQUESTED_SLOT, None), ActionExecuted("utter_ask_another_question"), ActionExecuted("action_listen"), UserUttered( tracker.latest_message.get("text", ""), parse_data={ "text": tracker.latest_message.get("text", ""), "intent": tracker.latest_message.get("intent", {}), "intent_ranking": tracker.latest_message.get("intent_ranking", []), "entities": tracker.latest_message.get("entities", []), }, ), ]
def test_latest_input_channel(): tracker = Tracker( "default", {}, {"intent": { "name": "some_intent", "confidence": 1.0 }}, [ UserUttered("my message text", input_channel="superchat"), ActionExecuted("action_listen"), ], False, None, {}, "action_listen", ) assert tracker.get_latest_input_channel() == "superchat"
async def test_submit_feedback_not_given(self): tracker = self.create_tracker( slots={ QUESTION_SLOT: QUESTION, FEEDBACK_SLOT: FEEDBACK_NOT_GIVEN, ANSWERS_SLOT: ANSWERS, STATUS_SLOT: QuestionAnsweringStatus.SUCCESS, }, text="some text with", intent="another_intent", entities=[{"and": "entities"}], ) await self.run_action(tracker) self.assert_events( [ SlotSet(QUESTION_SLOT, None), SlotSet(FEEDBACK_SLOT, None), SlotSet( ASKED_QUESTION_SLOT, {**FULL_RESULT_SUCCESS, FEEDBACK_KEY: FEEDBACK_NOT_GIVEN}, ), ActionExecuted("utter_ask_another_question"), BotUttered(metadata={"template_name": "utter_ask_another_question"}), ActionExecuted(ACTION_LISTEN_NAME), UserUttered( "some text with", parse_data={ "text": "some text with", "intent": {"name": "another_intent"}, "intent_ranking": [], "entities": [{"and": "entities"}], }, ), ] ) self.assert_templates([])
def _replace_user_input(tracker: Tracker) -> List[EventType]: logger.debug( "No rule matched for intent, replacing user input with fallback intent" ) last_user_event = tracker.get_last_event_for("user") last_user_event = copy.deepcopy(last_user_event) text = last_user_event["text"] fallback_user_event = UserUttered( text, parse_data={ "text": text, "intent": {"name": FALLBACK_INTENT, "confidence": 1.0}, "intent_ranking": [{"name": FALLBACK_INTENT, "confidence": 1.0}], "entities": [], }, ) return [ UserUtteranceReverted(), ActionExecuted(ACTION_LISTEN_NAME), fallback_user_event, ]
def user_uttered(text: Text, confidence: float) -> UserUttered: parse_data = {"intent": {"name": text, "confidence": confidence}} return UserUttered(text="Random", parse_data=parse_data)