Пример #1
0
 def message_mutation(self, message: Message) -> Message:
     texts = message.pop('text').split('\n')
     context, text = texts[:-1], texts[-1]
     self.rng.shuffle(context)
     output = context + [text]
     message['text'] = '\n'.join(output)
     return message
Пример #2
0
    def act(self):
        """
        Send new dialog message.
        """
        if not hasattr(self, 'epochDone'):
            # reset if haven't yet
            self.reset()

        # get next example, action is episode_done dict if already out of exs
        action, self.epochDone = self.next_example()
        # TODO: all teachers should eventually create messages
        # while setting up the data, so this won't be necessary
        action = Message(action)
        action.force_set('id', self.getID())

        # remember correct answer if available
        self.lastY = action.get('labels_1', action.get('eval_labels_1', None))
        if (not self.datatype.startswith('train')
                or 'evalmode' in self.datatype) and 'labels' in action:
            # move labels to eval field so not used for training
            # but this way the model can use the labels for perplexity or loss
            action = action.copy()
            labels = action.pop('labels')
            if not self.opt.get('hide_labels', False):
                action['eval_labels'] = labels

        return action
Пример #3
0
 def message_mutation(self, message: Message) -> Message:
     if not message.get('available_knowledge_text'):
         return message
     context = message.pop('text')
     knowledge = f'{TOKEN_KNOWLEDGE} {message["available_knowledge_text"]} {TOKEN_END_KNOWLEDGE}'
     delimiter = self.opt.get('delimiter', '\n')
     message['text'] = (knowledge if context == SILENCE else
                        f'{knowledge}{delimiter}{context}')
     return message
Пример #4
0
 def message_mutation(self, message: Message) -> Message:
     texts = message.pop('text').split('\n')
     output_texts = []
     for text in texts:
         words = text.split(' ')
         self.rng.shuffle(words)
         output_texts.append(' '.join(words))
     message['text'] = '\n'.join(output_texts)
     return message
Пример #5
0
 def message_mutation(self, message: Message) -> Message:
     texts = message.pop('text').split('\n')
     output_texts = []
     for text in texts:
         words = text.split(' ')
         words = list(reversed(words))
         output_texts.append(' '.join(words))
     message['text'] = '\n'.join(output_texts)
     return message
Пример #6
0
 def _pop_episode_done(self, message: Message) -> Tuple[Message, bool]:
     try:
         episode_done = message.pop('episode_done')
     except KeyError:
         episode_done = False
     return message, episode_done
Пример #7
0
    def observe(self, observation: Message) -> Dict[str, Message]:
        """
        Observe in 3 out of the 4 modules.

        :param observation:
            incoming message

        :return self.observation:
            returned observation is actually a dictionary mapping
            agent module name to the corresponding observation
        """
        if not isinstance(observation, Message):
            observation = Message(observation)
        for key in ['label_candidates', 'knowledge']:
            # Delete unnecessarily large keys
            observation.pop(key, '')
        observation['knowledge_response'] = observation.get('checked_sentence', '')

        raw_observation = copy.deepcopy(observation)
        # This part is *specifically* for document chunking.
        if self.krm_mutators:
            observation = observation.copy()
            for mutator in self.krm_mutators:
                assert isinstance(mutator, MessageMutator), "not message mutator"
                observation = next(mutator([observation]))

        knowledge_observation = self.knowledge_agent.observe(observation)
        knowledge_observation['prior_knowledge_responses'] = ' '.join(
            self.knowledge_responses
        )
        if observation.get('episode_done'):
            self.knowledge_responses = ['__SILENCE__']
        search_query_observation = None
        if self.search_query_agent:
            sqm_obs = copy.deepcopy(observation)
            if self.opt['search_query_control_token']:
                sqm_obs.force_set(
                    'temp_history', f" {self.opt['search_query_control_token']}"
                )
            sqm_obs.force_set('skip_retrieval', True)
            search_query_observation = self.search_query_agent.observe(sqm_obs)

        search_decision_observation = None
        if (
            self.search_decision_agent
            and self.search_decision is SearchDecision.COMPUTE
        ):
            assert (
                self.search_decision_agent.history.size == 1
            ), "wrong history size! set --sdm-history-size 1"
            sdm_obs = copy.deepcopy(observation)
            if self.opt['search_decision_control_token']:
                sdm_obs.force_set(
                    'temp_history', f" {self.opt['search_decision_control_token']}"
                )
            sdm_obs.force_set('skip_retrieval', True)
            search_decision_observation = self.search_decision_agent.observe(sdm_obs)

        observations = {
            'raw': raw_observation,
            'knowledge_agent': knowledge_observation,
            'search_query_agent': search_query_observation,
            'search_decision_agent': search_decision_observation,
        }
        self.observations = observations
        return observations