def __call__(self, utterances: list, batch_history: list, *responses: list) -> list: """Selects for each utterance response with highest confidence and wraps them to RichControl objects. Args: utterances_batch: Not used. history_batch: Not used. responses: Each response positional argument corresponds to response of one of Agent skills and is represented by batch (list) of (response, confidence) tuple structures. Returns: result: A batch of responses corresponding to the utterance batch received by agent. """ responses, confidences = zip(*[zip(*r) for r in responses]) indexes = [c.index(max(c)) for c in zip(*confidences)] result = [] for i, *responses in zip(indexes, *responses): rich_message = RichMessage() plain_text = PlainText(str(responses[i])) rich_message.add_control(plain_text) result.append(rich_message) return result
def _call(self, utterances_batch: list, utterances_ids: list = None) -> list: """Processes batch of utterances and returns corresponding responses batch. Args: utterances: Batch of incoming utterances. ids: Batch of dialog IDs corresponding to incoming utterances. Returns: responses: A batch of responses corresponding to the utterance batch received by agent. """ rich_message = RichMessage() for utt, id_ in zip(utterances_batch, utterances_ids): log.debug(f'Utterance: {utt}') if utt == "/start": welcome = "I am a new e-commerce bot. I will help you to find products that you are looking for. Please type your request in plain text." rich_message.add_control(PlainText(welcome)) continue if utt[0] == "@": command, *parts = utt.split(":") log.debug(f'Actions: {parts}') if command == "@details": rich_message.add_control( PlainText( show_details(self.history[id_][int( parts[0])][0][int(parts[1])]))) continue if command == "@entropy": state = self.history[id_][int(parts[0])] state[parts[1]] = parts[2] state["start"] = 0 state["stop"] = 5 utt = state['query'] self.states[id_] = state if command == "@next": state = self.history[id_][int(parts[0])] state['start'] = state['stop'] state['stop'] = state['stop'] + 5 utt = state['query'] self.states[id_] = state if command == "@previous": state = self.history[id_][int(parts[0])] state['stop'] = state['start'] state['start'] = state['start'] - 5 utt = state['query'] self.states[id_] = state else: self.states[id_] = {"start": 0, "stop": 5} responses, confidences, state = self.skills[0]([utt], self.history[id_], [self.states[id_]]) # update `self.states` with retrieved results self.states[id_] = state self.states[id_]["query"] = utt items, entropy, total = responses self.history[id_].append(responses) self.history[id_].append(self.states[id_]) for idx, item in enumerate(items): title = item['Title'] if 'ListPrice' in item: title += " - **$" + item['ListPrice'].split('$')[1] + "**" buttons_frame = ButtonsFrame(text=title) buttons_frame.add_button( Button( 'Show details', "@details:" + str(len(self.history[id_]) - 2) + ":" + str(idx))) rich_message.add_control(buttons_frame) buttons_frame = ButtonsFrame(text="") if self.states[id_]["start"] > 0: buttons_frame.add_button( Button('Previous', "@previous:" + str(len(self.history[id_]) - 1))) buttons_frame.add_button( Button('Next', "@next:" + str(len(self.history[id_]) - 1))) rich_message.add_control(buttons_frame) if entropy: buttons_frame = ButtonsFrame(text="Please specify a " + entropy[0][1]) for ent_value in entropy[0][2][:3]: button_a = Button( ent_value[0], f'@entropy:{len(self.history[id_])-1}:{entropy[0][1]}:{ent_value[0]}' ) buttons_frame.add_button(button_a) rich_message.add_control(buttons_frame) return [rich_message]
def _call(self, utterances_batch: List[str], utterances_ids: List[int] = None) -> List[RichMessage]: """Processes batch of utterances and returns corresponding responses batch. Args: utterances_batch: Batch of incoming utterances. utterances_ids: Batch of dialog IDs corresponding to incoming utterances. Returns: responses: A batch of responses corresponding to the utterance batch received by agent. """ rich_message = RichMessage() for utt_id, utt in enumerate(utterances_batch): if utterances_ids: id_ = utterances_ids[utt_id] log.debug(f'Utterance: {utt}') if utt == "/start": welcome = "I am a new e-commerce bot. I will help you to find products that you are looking for. Please type your request in plain text." rich_message.add_control(PlainText(welcome)) continue if utt[0] == "@": command, *parts = utt.split(":") log.debug(f'Actions: {parts}') if command == "@details": batch_index = int(parts[0]) # batch index in history list item_index = int(parts[1]) # index in batch rich_message.add_control(PlainText(show_details( self.history[id_][batch_index][item_index]))) continue if command == "@entropy": state = self.history[id_][int(parts[0])] state[parts[1]] = parts[2] state["start"] = 0 state["stop"] = 5 utt = state['query'] self.states[id_] = state if command == "@next": state = self.history[id_][int(parts[0])] state['start'] = state['stop'] state['stop'] = state['stop'] + 5 utt = state['query'] self.states[id_] = state else: if id_ not in self.states: self.states[id_] = {} self.states[id_]["start"] = 0 self.states[id_]["stop"] = 5 responses_batch, confidences_batch, state_batch = self.skills[0]( [utt], self.history[id_], [self.states[id_]]) # update `self.states` with retrieved results self.states[id_] = state_batch[0] self.states[id_]["query"] = utt items_batch, entropy_batch = responses_batch for batch_idx, items in enumerate(items_batch): self.history[id_].append(items) self.history[id_].append(self.states[id_]) for idx, item in enumerate(items): rich_message.add_control(_draw_item(item, idx, self.history[id_])) if len(items) == self.states[id_]['stop'] - self.states[id_]['start']: buttons_frame = _draw_tail(entropy_batch[batch_idx], self.history[id_]) rich_message.add_control(buttons_frame) return [rich_message]