Example #1
0
def test_restart_event(default_domain):
    tracker = DialogueStateTracker("default", default_domain.slots)
    # the retrieved tracker should be empty
    assert len(tracker.events) == 0

    intent = {"name": "greet", "confidence": 1.0}
    tracker.update(ActionExecuted(ACTION_LISTEN_NAME))
    tracker.update(UserUttered("/greet", intent, []))
    tracker.update(ActionExecuted("my_action"))
    tracker.update(ActionExecuted(ACTION_LISTEN_NAME))

    assert len(tracker.events) == 4
    assert tracker.latest_message.text == "/greet"
    assert len(list(tracker.generate_all_prior_trackers())) == 4

    tracker.update(Restarted())

    assert len(tracker.events) == 5
    assert tracker.followup_action is not None
    assert tracker.followup_action == ACTION_LISTEN_NAME
    assert tracker.latest_message.text is None
    assert len(list(tracker.generate_all_prior_trackers())) == 1

    dialogue = tracker.as_dialogue()

    recovered = DialogueStateTracker("default", default_domain.slots)
    recovered.recreate_from_dialogue(dialogue)

    assert recovered.current_state() == tracker.current_state()
    assert len(recovered.events) == 5
    assert recovered.latest_message.text is None
    assert len(list(recovered.generate_all_prior_trackers())) == 1
Example #2
0
def test_revert_action_event(default_domain):
    tracker = DialogueStateTracker("default", default_domain.slots)
    # the retrieved tracker should be empty
    assert len(tracker.events) == 0

    intent = {"name": "greet", "confidence": 1.0}
    tracker.update(ActionExecuted(ACTION_LISTEN_NAME))
    tracker.update(UserUttered("/greet", intent, []))
    tracker.update(ActionExecuted("my_action"))
    tracker.update(ActionExecuted(ACTION_LISTEN_NAME))

    # Expecting count of 4:
    #   +3 executed actions
    #   +1 final state
    assert tracker.latest_action_name == ACTION_LISTEN_NAME
    assert len(list(tracker.generate_all_prior_trackers())) == 4

    tracker.update(ActionReverted())

    # Expecting count of 3:
    #   +3 executed actions
    #   +1 final state
    #   -1 reverted action
    assert tracker.latest_action_name == "my_action"
    assert len(list(tracker.generate_all_prior_trackers())) == 3

    dialogue = tracker.as_dialogue()

    recovered = DialogueStateTracker("default", default_domain.slots)
    recovered.recreate_from_dialogue(dialogue)

    assert recovered.current_state() == tracker.current_state()
    assert tracker.latest_action_name == "my_action"
    assert len(list(tracker.generate_all_prior_trackers())) == 3
Example #3
0
    def _current_tracker_state_without_events(tracker: DialogueStateTracker) -> Dict:
        # get current tracker state and remove `events` key from state
        # since events are pushed separately in the `update_one()` operation
        state = tracker.current_state(EventVerbosity.ALL)
        state.pop("events", None)

        return state
Example #4
0
def test_revert_user_utterance_event(default_domain):
    tracker = DialogueStateTracker("default", default_domain.slots)
    # the retrieved tracker should be empty
    assert len(tracker.events) == 0

    intent1 = {"name": "greet", "confidence": 1.0}
    tracker.update(ActionExecuted(ACTION_LISTEN_NAME))
    tracker.update(UserUttered("/greet", intent1, []))
    tracker.update(ActionExecuted("my_action_1"))
    tracker.update(ActionExecuted(ACTION_LISTEN_NAME))

    intent2 = {"name": "goodbye", "confidence": 1.0}
    tracker.update(UserUttered("/goodbye", intent2, []))
    tracker.update(ActionExecuted("my_action_2"))
    tracker.update(ActionExecuted(ACTION_LISTEN_NAME))

    # Expecting count of 6:
    #   +5 executed actions
    #   +1 final state
    assert tracker.latest_action_name == ACTION_LISTEN_NAME
    assert len(list(tracker.generate_all_prior_trackers())) == 6

    tracker.update(UserUtteranceReverted())

    # Expecting count of 3:
    #   +5 executed actions
    #   +1 final state
    #   -2 rewound actions associated with the /goodbye
    #   -1 rewound action from the listen right before /goodbye
    assert tracker.latest_action_name == "my_action_1"
    assert len(list(tracker.generate_all_prior_trackers())) == 3

    dialogue = tracker.as_dialogue()

    recovered = DialogueStateTracker("default", default_domain.slots)
    recovered.recreate_from_dialogue(dialogue)

    assert recovered.current_state() == tracker.current_state()
    assert tracker.latest_action_name == "my_action_1"
    assert len(list(tracker.generate_all_prior_trackers())) == 3
    def save(self, tracker: DialogueStateTracker) -> None:
        """Updates and saves the current conversation state"""
        if self.event_broker:
            self.stream_events(tracker)

        ## get the current state data from tracker
        state = tracker.current_state(EventVerbosity.ALL)

        ## here you can write the code to persist your chat history data, I am saving to json file
        chatLogger.log(state)

        serialised = CustomTrackerStore.serialise_tracker(tracker)
        self.store[tracker.sender_id] = serialised
    def save(self, tracker: DialogueStateTracker) -> None:
        """Updates and saves the current conversation state"""
        if self.event_broker:
            self.stream_events(tracker)

        ## get the current state data from tracker
        state = tracker.current_state(EventVerbosity.AFTER_RESTART)
        data_dict = {}
        data_dict['user_id'] = state['sender_id']
        data_dict['user_name'] = ActionGetUserName().GetName(
            tracker)['emp_name']
        data_dict['user_emp_code'] = ActionGetUserName().GetName(
            tracker)['emp_code']
        try:
            tracker_data = state['events']
            for i in tracker_data:
                # print("trackerrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr", i)
                data_dict['timestamp'] = (datetime.fromtimestamp(
                    i['timestamp'])).strftime("%Y-%m-%d %H:%M:%S")
                if i['event'] == 'bot':
                    if i['text'] == "Want to explore more? I can help you with all your attendance and leave related queries.":
                        pass
                    else:
                        data_dict['bot_event'] = i['event']
                        data_dict['bot_event_text'] = i['text']
                        data_dict['bot_event_data'] = i['data']
                elif i['event'] == 'user':
                    data_dict['user_text'] = i['text']
                    data_dict['user_text_intent'] = i['parse_data']['intent'][
                        'name']
                    data_dict['user_text_intent_confidence'] = i['parse_data'][
                        'intent']['confidence']
                    data_dict['user_text_entity'] = i['parse_data']['entities']
                elif i['event'] == 'action':
                    if i['name'] not in [
                            "action_listen", "utter_ask_continue", "reset_slot"
                    ]:
                        data_dict['action_event'] = i['event']
                        data_dict['action_event_name'] = i['name']
                    else:
                        pass
                else:
                    pass
        except:
            data_dict['tracker_fail_all_data'] = state

        ## here you can write the code to persist your chat history data, I am saving to json file
        chatLogger.ActionGetCompCode.log(self, data_dict, tracker)

        serialised = CustomTrackerStore.serialise_tracker(tracker)
        self.store[tracker.sender_id] = serialised
Example #7
0
def nlg_request_format(template_name: Text, tracker: DialogueStateTracker,
                       output_channel: Text, **kwargs: Any) -> Dict[Text, Any]:
    """Create the json body for the NLG json body for the request."""

    tracker_state = tracker.current_state(EventVerbosity.ALL)

    return {
        "template": template_name,
        "arguments": kwargs,
        "tracker": tracker_state,
        "channel": {
            "name": output_channel
        },
    }
Example #8
0
 def save(self, tracker: DialogueStateTracker) -> None:
     if self.event_broker:
         self.stream_events(tracker)
     state = tracker.current_state(EventVerbosity.ALL)
     asyncio.get_event_loop().run_until_complete(
         self.asm.memory.put("rasa_tracker", tracker.sender_id, state))