def execute_agent(self, configuration):
        """Runs the conversational agent and executes the dialogue by calling
        the basic components of IAI MovieBot

        Args:
            configuration: the settings for the agent

        """
        agent = Agent(configuration)
        agent.initialize()
        print(
            'The components for the conversation are initialized successfully.'
        )
        user_options = {}
        agent_response, user_options = agent.start_dialogue()
        print(f'AGENT: {agent_response}')
        while not agent.terminated_dialogue():
            utterance = input('User: '******'text': utterance})
            agent_response, user_options = agent.continue_dialogue(
                user_utterance, user_options)
            print(f'AGENT: {agent_response}')
            if user_options:
                print(list(user_options.values()))
        agent.end_dialogue()
Example #2
0
def get_slot_annotator(config):
    """Loads an agent with a given config.

    Args:
        config (Dict): Contents of a configuration file.

    Returns:
        SlotAnnotator: Returns a SlotAnnotator instance.
    """
    agent = Agent(config)
    agent.initialize()
    return agent.nlu.intents_checker.slot_annotator
Example #3
0
    def start_agent(self, user_id, restart=False):
        """Start conversation with agent.

        Args:
            user_id:
            restart: True or False

        """
        self.agent[user_id] = Agent(self.configuration)
        self.agent[user_id].initialize(user_id)
        self.agent_response[user_id], self.record_data_agent[
            user_id], self.user_options[user_id] = self.agent[
                user_id].start_dialogue()
        if restart:
            self.agent_response[user_id], self.record_data_agent[
                user_id], self.user_options[user_id] = self.agent[
                    user_id].start_dialogue(None, restart)
            self.user_messages[user_id].text(self.agent_response[user_id])
Example #4
0
    def start(self, update, context):
        """Starts the conversation. This indicates initializing the components
        and start the conversation from scratch and identifying if the users are
        new or have used this system before.

        Args:
            update:
            context:

        """
        # create a new agent
        user_id = str(update.effective_user['id'])
        if user_id not in self.configuration['new_user'] or self.configuration[
                'new_user'].get(user_id):
            self.configuration['new_user'].update(
                {user_id: self.new_user(user_id)})
        self.agent[user_id] = Agent(self.configuration)
        self.user_options[user_id] = {}
        self.agent[user_id].initialize(user_id)
        print(
            f"Conversation is starting for user id = {user_id} and user name = '"
            f"{update.effective_user['first_name']}'")
        self.response[user_id], self.record_data_agent[
            user_id], _ = self.agent[user_id].start_dialogue(
                user_fname=update.effective_user['first_name'])
        if self.configuration['new_user'][user_id]:
            update.message.reply_text(self._instruction(),
                                      parse_mode=ParseMode.MARKDOWN)
        update.message.reply_text(self.response[user_id],
                                  reply_markup=ReplyKeyboardRemove(),
                                  parse_mode=ParseMode.MARKDOWN)
        # record the conversation
        if self.agent[user_id].bot_recorder:
            self.record_data[user_id] = {
                'Timestamp': str(update.message.date),
                'User_Input': update.message.text
            }
            self.record_data[user_id].update(self.record_data_agent[user_id])
            self.agent[user_id].bot_recorder.record_user_data(
                user_id, self.record_data[user_id])
        return CONTINUE
Example #5
0
    def restart(self, update, context):
        """Restarts the conversation. This is similar to start function.
        However, it starts the conversation with a welcome message and elicits
        the uses to begin with.

        Args:
            update:
            context:

        """
        # create a new agent
        user_id = str(update.effective_user['id'])
        if user_id not in self.configuration['new_user'] or self.configuration[
                'new_user'].get(user_id):
            self.configuration['new_user'].update(
                {user_id: self.new_user(user_id)})
        self.agent[user_id] = Agent(self.configuration)
        self.user_options[user_id] = {}
        self.agent[user_id].initialize(user_id)
        print(
            f"Conversation is starting for user id = {user_id} and user name = '"
            f"{update.effective_user['first_name']}'")
        self.response[user_id], self.record_data_agent[
            user_id], _ = self.agent[user_id].start_dialogue(
                user_fname=update.effective_user['first_name'], restart=True)
        if self.configuration['new_user'][user_id]:
            update.message.reply_text(self._instruction(),
                                      parse_mode=ParseMode.MARKDOWN)
        update.message.reply_text(self.response[user_id],
                                  reply_markup=ReplyKeyboardRemove())
        # record the conversation
        if self.agent[user_id].bot_recorder:
            self.record_data[user_id] = {
                'Timestamp': str(update.message.date),
                'User_Input': update.message.text
            }
            self.record_data[user_id].update(self.record_data_agent[user_id])
            self.agent[user_id].bot_recorder.record_user_data(
                user_id, self.record_data[user_id])
        return CONTINUE
Example #6
0
    def continue_conv(self, update, context):
        """Continues the conversation until the users want to restart of exit.

        Args:
            update:
            context:

        """
        user_id = str(update.effective_user['id'])
        if user_id not in self.configuration['new_user'] or self.configuration[
                'new_user'].get(user_id):
            self.configuration['new_user'].update(
                {user_id: self.new_user(user_id)})
        if user_id not in self.agent:
            self.agent[user_id] = Agent(self.configuration)
            self.user_options[user_id] = {}
            self.agent[user_id].initialize(user_id)
            self.agent[
                user_id].dialogue_manager.dialogue_state_tracker.dialogue_state.initialize(
                )
            self.agent[
                user_id].dialogue_manager.dialogue_state_tracker.dialogue_context.initialize(
                )
            print(
                f"Conversation is starting for user id = {user_id} and user name = '"
                f"{update.effective_user['first_name']}'")
        start = time.time()
        user_utterance = UserUtterance(update.message.to_dict())
        self.response[user_id], self.record_data_agent[user_id], self.user_options[user_id] = \
            self.agent[user_id].continue_dialogue(
                user_utterance, self.user_options[user_id], user_fname=update.effective_user[
                    'first_name'])
        if self.user_options[user_id]:
            # d = {str(key):val for key,val in self.user_options[user_id].items()}
            # print(user_id + str(d))
            reply_keyboard = self._recheck_user_options(
                deepcopy(list(self.user_options[user_id].values())))
            resize = True
            if len(self.user_options[user_id]) > 3:
                resize = False
            markup = ReplyKeyboardMarkup(reply_keyboard,
                                         resize_keyboard=resize,
                                         one_time_keyboard=True)
        else:
            markup = ReplyKeyboardRemove()
        end = time.time()
        if self.configuration['new_user'][user_id]:
            update.message.reply_text(self._instruction(),
                                      parse_mode=ParseMode.MARKDOWN)
        update.message.reply_text(self.response[user_id],
                                  reply_markup=markup,
                                  parse_mode=ParseMode.MARKDOWN)
        # record the conversation
        if self.agent[user_id].bot_recorder:
            record_data = {"Timestamp": user_utterance.get_timestamp()}
            record_data.update(self.record_data_agent[user_id])
            record_data.update({"Execution_Time": str(round(end - start, 3))})
            self.agent[user_id].bot_recorder.record_user_data(
                user_id, record_data)
        if self.agent[user_id].terminated_dialogue():
            print(
                f"Conversation is ending for user id = {user_id} and user name = '"
                f"{update.effective_user['first_name']}'")
            del self.agent[user_id]
            feedback = 'Help me improve myself. Give me a feedback [here](' \
                       'https://forms.gle/hK9CrHu37dL89r1H6).'
            update.message.reply_text(feedback, parse_mode=ParseMode.MARKDOWN)
            return ConversationHandler.END
        else:
            return CONTINUE