Ejemplo n.º 1
0
    def _export_stories(tracker):
        # export current stories and quit
        file_prompt = ("File to export to (if file exists, this "
                       "will append the stories) "
                       "[{}]: ").format(DEFAULT_FILE_EXPORT_PATH)
        export_file_path = utils.request_input(prompt=file_prompt)

        if not export_file_path:
            export_file_path = DEFAULT_FILE_EXPORT_PATH

        exported = StoryExported(export_file_path)
        tracker.update(exported)
        logger.info("Stories got exported to '{}'.".format(
                os.path.abspath(exported.path)))
Ejemplo n.º 2
0
    def _request_action(self, predictions, domain, tracker):
        # given the intent and the text (NOT IMPLEMENTED)
        # what is the correct action?
        self._print_history(tracker)
        print("what is the next action for the bot?\n")

        for idx in range(domain.num_actions):
            action_name = domain.action_for_index(idx).name()
            print("{:>10}{:>40}    {:03.2f}".format(idx, action_name,
                                                    predictions[idx]))

        out = int(utils.request_input(
                utils.str_range_list(0, domain.num_actions)))
        print("thanks! The bot will now "
              "[{}]\n -----------".format(domain.action_for_index(out).name()))
        return out
Ejemplo n.º 3
0
    def _request_action(self, predictions, domain, tracker):
        # given the intent and the text (NOT IMPLEMENTED)
        # what is the correct action?
        self._print_history(tracker)
        print("what is the next action for the bot?\n")

        for idx in range(domain.num_actions):
            action_name = domain.action_for_index(idx).name()
            print("{:>10}{:>40}    {:03.2f}".format(idx, action_name,
                                                    predictions[idx]))

        out = int(
            utils.request_input(utils.str_range_list(0, domain.num_actions)))
        print("thanks! The bot will now "
              "[{}]\n -----------".format(domain.action_for_index(out).name()))
        return out
Ejemplo n.º 4
0
def _request_action_from_user(predictions, tracker_dump):
    # given the intent and the text
    # what is the correct action?
    _print_history(tracker_dump)
    print("what is the next action for the bot?\n")

    for idx, p in enumerate(predictions):
        print("{:>10}{:>40}    {:03.2f}".format(idx,
                                                p.get("action"),
                                                p.get("score")))

    out = int(utils.request_input(
            utils.str_range_list(0, len(predictions))))
    print("thanks! The bot will now "
          "[{}]\n -----------".format(predictions[out].get("action")))
    return out
Ejemplo n.º 5
0
def _export_stories(tracker):
    # export current stories and quit
    file_prompt = ("File to export to (if file exists, this "
                   "will append the stories) "
                   "[{}]: ").format(DEFAULT_FILE_EXPORT_PATH)
    export_file_path = utils.request_input(prompt=file_prompt)

    if not export_file_path:
        export_file_path = DEFAULT_FILE_EXPORT_PATH

    parsed_events = events.deserialise_events(tracker.get("events", []))

    s = Story.from_events(parsed_events)

    with io.open(export_file_path, 'a') as f:
        f.write(s.as_story_string(flat=True) + "\n")
Ejemplo n.º 6
0
def _request_intent_from_user(tracker_dump, intents):
    # take in some argument and ask which intent it should have been
    # save the intent to a json like file
    latest_message, _ = revert_latest_message(tracker_dump.get("events", []))
    colored_user_msg = utils.wrap_with_color(latest_message.get("text"),
                                             utils.bcolors.OKGREEN)
    print("------\n")
    print("Message:\n")
    print(latest_message.get("text"))
    print("User said:\t {}".format(colored_user_msg))
    print("What intent is this?\t")
    for idx, intent in enumerate(intents):
        print('\t{}\t{}'.format(idx, intent))

    out = int(utils.request_input(
            utils.str_range_list(0, len(intents))))

    return {'name': intents[out], 'confidence': 1.0}
Ejemplo n.º 7
0
 def _request_intent(self, tracker, domain):
     # take in some argument and ask which intent it should have been
     # save the intent to a json like file
     colored_user_msg = utils.wrap_with_color(tracker.latest_message.text,
                                              utils.bcolors.OKGREEN)
     print("------\n")
     print("Message:\n")
     print(tracker.latest_message.text)
     print("User said:\t {}".format(colored_user_msg))
     print("What intent is this?\t")
     for idx, intent in enumerate(domain.intents):
         print('\t{}\t{}'.format(idx, intent))
     out = int(
         utils.request_input(utils.str_range_list(0, len(domain.intents))))
     json_example = {
         'text': tracker.latest_message.text,
         'intent': domain.intents[out]
     }
     self.extra_intent_examples.append(json_example)
     intent_name = domain.intents[out]
     return {'name': intent_name, 'confidence': 1.0}
Ejemplo n.º 8
0
 def _request_intent(self, tracker, domain):
     # take in some argument and ask which intent it should have been
     # save the intent to a json like file
     colored_user_msg = utils.wrap_with_color(tracker.latest_message.text,
                                              utils.bcolors.OKGREEN)
     print("------\n")
     print("Message:\n")
     print(tracker.latest_message.text)
     print("User said:\t {}".format(colored_user_msg))
     print("What intent is this?\t")
     for idx, intent in enumerate(domain.intents):
         print('\t{}\t{}'.format(idx, intent))
     out = int(utils.request_input(
             utils.str_range_list(0, len(domain.intents))))
     json_example = {
         'text': tracker.latest_message.text,
         'intent': domain.intents[out]
     }
     self.extra_intent_examples.append(json_example)
     intent_name = domain.intents[out]
     return {'name': intent_name, 'confidence': 1.0}
Ejemplo n.º 9
0
    def probabilities_using_best_policy(self, tracker, domain):
        # type: (DialogueStateTracker, Domain) -> List[float]
        # given a state, predict next action via asking a human

        probabilities = self.base_ensemble.probabilities_using_best_policy(
            tracker, domain)
        pred_out = int(np.argmax(probabilities))
        latest_action_was_listen = self._print_history(tracker)

        action_name = domain.action_for_index(pred_out).name()
        colored_name = utils.wrap_with_color(action_name, utils.bcolors.OKBLUE)
        if latest_action_was_listen:
            print("The bot wants to [{}] due to the intent. "
                  "Is this correct?\n".format(colored_name))

            user_input = utils.request_input(
                ["1", "2", "3", "0"], "\t1.\tYes\n" +
                "\t2.\tNo, intent is right but the action is wrong\n" +
                "\t3.\tThe intent is wrong\n" +
                "\t0.\tExport current conversations as stories and quit\n")
        else:
            print("The bot wants to [{}]. "
                  "Is this correct?\n".format(colored_name))
            user_input = utils.request_input(
                ["1", "2", "0"],
                "\t1.\tYes.\n" + "\t2.\tNo, the action is wrong.\n" +
                "\t0.\tExport current conversations as stories and quit\n")

        if user_input == "1":
            # max prob prediction was correct
            if action_name == ACTION_LISTEN_NAME:
                print("Next user input:")
            return probabilities

        elif user_input == "2":
            # max prob prediction was false, new action required
            # action wrong
            y = self._request_action(probabilities, domain, tracker)

            # update tracker with new action
            new_action_name = domain.action_for_index(y).name()

            # need to copy tracker, because the tracker will be
            # updated with the new event somewhere else
            training_tracker = tracker.copy()
            training_tracker.update(ActionExecuted(new_action_name))

            self._fit_example(training_tracker, domain)

            self.write_out_story(tracker)

            return utils.one_hot(y, domain.num_actions)

        elif user_input == "3":
            # intent wrong and maybe action wrong
            intent = self._request_intent(tracker, domain)
            latest_message = copy.copy(tracker.latest_message)
            latest_message.intent = intent
            tracker.update(UserUtteranceReverted())
            tracker.update(latest_message)
            for e in domain.slots_for_entities(latest_message.entities):
                tracker.update(e)
            return self.probabilities_using_best_policy(tracker, domain)

        elif user_input == "0":
            self._export_stories(tracker)
            raise TrainingFinishedException()

        else:
            raise Exception(
                "Incorrect user input received '{}'".format(user_input))
    def probabilities_using_best_policy(self, tracker, domain):
        # [feature vector, tracker, domain] -> int
        # given a state, predict next action via asking a human
        probabilities = self.base_ensemble.probabilities_using_best_policy(
                tracker, domain)
        pred_out = np.argmax(probabilities)
        latest_action_was_listen = self._print_history(tracker)

        action_name = domain.action_for_index(pred_out).name()
        colored_name = utils.wrap_with_color(action_name,
                                             utils.bcolors.OKBLUE)
        if latest_action_was_listen:
            print("The bot wants to [{}] due to the intent. "
                  "Is this correct?\n".format(colored_name))

            user_input = utils.request_input(
                    ["1", "2", "3", "0"],
                    "\t1.\tYes\n" +
                    "\t2.\tNo, intent is right but the action is wrong\n" +
                    "\t3.\tThe intent is wrong\n" +
                    "\t0.\tExport current conversations as stories and quit\n")
        else:
            print("The bot wants to [{}]. "
                  "Is this correct?\n".format(colored_name))
            user_input = utils.request_input(
                    ["1", "2", "0"],
                    "\t1.\tYes.\n" +
                    "\t2.\tNo, the action is wrong.\n" +
                    "\t0.\tExport current conversations as stories and quit\n")

        feature_vector = domain.feature_vector_for_tracker(
                self.featurizer, tracker, self.max_history)
        X = np.expand_dims(np.array(feature_vector), 0)
        if user_input == "1":
            # max prob prediction was correct
            if action_name == ACTION_LISTEN_NAME:
                print("Next user input:")
            return probabilities
        elif user_input == "2":
            # max prob prediction was false, new action required
            # action wrong
            y = self._request_action(probabilities, domain, tracker)
            self._fit_example(X, y, domain)
            self.write_out_story(tracker)
            return utils.one_hot(y, domain.num_actions)
        elif user_input == "3":
            # intent wrong and maybe action wrong
            intent = self._request_intent(tracker, domain)
            latest_message = copy.copy(tracker.latest_message)
            latest_message.intent = intent
            tracker.update(UserUtteranceReverted())
            tracker.update(latest_message)
            for e in domain.slots_for_entities(latest_message.entities):
                tracker.update(e)
            return self.probabilities_using_best_policy(tracker, domain)
        elif user_input == "0":
            self._export_stories(tracker)
            raise TrainingFinishedException()
        else:
            raise Exception(
                    "Incorrect user input received '{}'".format(user_input))
Ejemplo n.º 11
0
    def probabilities_using_best_policy(self, tracker, domain):
        # type: (DialogueStateTracker, Domain) -> List[float]
        # given a state, predict next action via asking a human

        probabilities = self.base_ensemble.probabilities_using_best_policy(
                tracker, domain)
        pred_out = int(np.argmax(probabilities))
        latest_action_was_listen = self._print_history(tracker)

        action_name = domain.action_for_index(pred_out).name()
        colored_name = utils.wrap_with_color(action_name,
                                             utils.bcolors.OKBLUE)
        if latest_action_was_listen:
            print("The bot wants to [{}] due to the intent. "
                  "Is this correct?\n".format(colored_name))

            user_input = utils.request_input(
                    ["1", "2", "3", "0"],
                    "\t1.\tYes\n" +
                    "\t2.\tNo, intent is right but the action is wrong\n" +
                    "\t3.\tThe intent is wrong\n" +
                    "\t0.\tExport current conversations as stories and quit\n")
        else:
            print("The bot wants to [{}]. "
                  "Is this correct?\n".format(colored_name))
            user_input = utils.request_input(
                    ["1", "2", "0"],
                    "\t1.\tYes.\n" +
                    "\t2.\tNo, the action is wrong.\n" +
                    "\t0.\tExport current conversations as stories and quit\n")

        if user_input == "1":
            # max prob prediction was correct
            if action_name == ACTION_LISTEN_NAME:
                print("Next user input:")
            return probabilities

        elif user_input == "2":
            # max prob prediction was false, new action required
            # action wrong
            y = self._request_action(probabilities, domain, tracker)

            # update tracker with new action
            new_action_name = domain.action_for_index(y).name()

            # need to copy tracker, because the tracker will be
            # updated with the new event somewhere else
            training_tracker = tracker.copy()
            training_tracker.update(ActionExecuted(new_action_name))

            self._fit_example(training_tracker, domain)

            self.write_out_story(tracker)

            return utils.one_hot(y, domain.num_actions)

        elif user_input == "3":
            # intent wrong and maybe action wrong
            intent = self._request_intent(tracker, domain)
            latest_message = copy.copy(tracker.latest_message)
            latest_message.intent = intent
            tracker.update(UserUtteranceReverted())
            tracker.update(latest_message)
            for e in domain.slots_for_entities(latest_message.entities):
                tracker.update(e)
            return self.probabilities_using_best_policy(tracker, domain)

        elif user_input == "0":
            self._export_stories(tracker)
            raise TrainingFinishedException()

        else:
            raise Exception(
                    "Incorrect user input received '{}'".format(user_input))
Ejemplo n.º 12
0
def predict_till_next_listen(endpoint,  # type: EndpointConfig
                             intents,  # type:  List[Text]
                             sender_id  # type: Text
                             ):
    # type: (...) -> bool
    # given a state, predict next action via asking a human

    listen = False
    while not listen:
        response = request_prediction(endpoint, sender_id)
        tracker_dump = response.get("tracker")
        predictions = response.get("scores")

        probabilities = [prediction["score"] for prediction in predictions]
        pred_out = int(np.argmax(probabilities))
        latest_action_was_listen = _print_history(tracker_dump)

        action_name = predictions[pred_out].get("action")
        colored_name = utils.wrap_with_color(action_name,
                                             utils.bcolors.OKBLUE)
        if latest_action_was_listen:
            print("The bot wants to [{}] due to the intent. "
                  "Is this correct?\n".format(colored_name))

            user_input = utils.request_input(
                    ["1", "2", "3", "0"],
                    "\t1.\tYes\n" +
                    "\t2.\tNo, intent is right but the action is wrong\n" +
                    "\t3.\tThe intent is wrong\n" +
                    "\t0.\tExport current conversations as stories and quit\n")
        else:
            print("The bot wants to [{}]. "
                  "Is this correct?\n".format(colored_name))
            user_input = utils.request_input(
                    ["1", "2", "0"],
                    "\t1.\tYes.\n" +
                    "\t2.\tNo, the action is wrong.\n" +
                    "\t0.\tExport current conversations as stories and quit\n")

        if user_input == "1":
            # max prob prediction was correct
            response = send_action(endpoint, sender_id, action_name)

            for response in response.get("messages", []):
                console.print_bot_output(response)

            listen = action_name == ACTION_LISTEN_NAME

        elif user_input == "2":
            # max prob prediction was false, new action required
            y = _request_action_from_user(predictions, tracker_dump)

            new_action_name = predictions[y].get("action")
            listen = new_action_name == ACTION_LISTEN_NAME

            response = send_action(endpoint,
                                   sender_id,
                                   new_action_name)

            send_finetune(endpoint,
                          response.get("tracker", {}).get("events", []))

            for response in response.get("messages", []):
                console.print_bot_output(response)

        elif user_input == "3":
            # intent wrong and maybe action wrong
            intent = _request_intent_from_user(tracker_dump, intents)
            evts = tracker_dump.get("events", [])
            latest_message, corrected_events = revert_latest_message(evts)

            latest_message.get("parse_data")["intent"] = intent

            send_events(endpoint, sender_id, corrected_events)

            send_message(endpoint, sender_id, latest_message.get("text"),
                         latest_message.get("parse_data"))

        elif user_input == "0":
            _export_stories(response.get("tracker"))
            return True
        else:
            raise Exception(
                    "Incorrect user input received '{}'".format(user_input))
    return False