async def _predict_till_next_listen( endpoint: EndpointConfig, sender_id: Text, finetune: bool, sender_ids: List[Text], plot_file: Optional[Text], ) -> None: """Predict and validate actions until we need to wait for a user msg.""" listen = False while not listen: result = await request_prediction(endpoint, sender_id) predictions = result.get("scores") probabilities = [prediction["score"] for prediction in predictions] pred_out = int(np.argmax(probabilities)) action_name = predictions[pred_out].get("action") policy = result.get("policy") confidence = result.get("confidence") await _print_history(sender_id, endpoint) await _plot_trackers( sender_ids, plot_file, endpoint, unconfirmed=[ActionExecuted(action_name)] ) listen = await _validate_action( action_name, policy, confidence, predictions, endpoint, sender_id, finetune=finetune, ) await _plot_trackers(sender_ids, plot_file, endpoint) tracker_dump = await retrieve_tracker( endpoint, sender_id, EventVerbosity.AFTER_RESTART ) events = tracker_dump.get("events", []) if len(events) >= 2: last_event = events[-2] # last event before action_listen # if bot message includes buttons the user will get a list choice to reply # the list choice is displayed in place of action listen if last_event.get("event") == BotUttered.type_name and last_event["data"].get( "buttons", None ): data = last_event["data"] message = last_event.get("text", "") choices = [ button_to_string(button, idx) for idx, button in enumerate(data.get("buttons")) ] question = questionary.select(message, choices) button_payload = cliutils.payload_from_button_question(question) await send_message(endpoint, sender_id, button_payload)
def get_user_input(button_question: questionary.Question) -> Optional[Text]: if button_question is not None: response = cli_utils.payload_from_button_question(button_question) if response == cli_utils.FREE_TEXT_INPUT_PROMPT: # Re-prompt user with a free text input response = get_user_input(None) else: response = questionary.text( "", qmark="Your input ->", style=Style([("qmark", "#b373d6"), ("", "#b373d6")]), ).ask() return response.strip() if response is not None else None
def _get_user_input(previous_response: Optional[Dict[str, Any]]) -> Optional[Text]: button_response = None if previous_response is not None: button_response = _print_bot_output(previous_response, is_latest_message=True) if button_response is not None: response = cli_utils.payload_from_button_question(button_response) if response == cli_utils.FREE_TEXT_INPUT_PROMPT: # Re-prompt user with a free text input response = _get_user_input({}) else: response = questionary.text( "", qmark="Your input ->", style=Style([("qmark", "#b373d6"), ("", "#b373d6")]), ).ask() return response.strip() if response is not None else None
def _user_input(button_question: questionary.Question) -> Optional[Text]: exit_text = INTENT_MESSAGE_PREFIX + "stop" if button_question is not None: response = cli_utils.payload_from_button_question(button_question) if response == cli_utils.FREE_TEXT_INPUT_PROMPT: # Re-prompt user with a free text input response = _user_input(None) else: response = questionary.text( "", qmark="Your input ->", style=Style([("qmark", "#b373d6"), ("", "#b373d6")]), ).ask() text = response.strip() if response is not None else None if not sett.is_disable_analyse: if text is not None and not text.startswith(INTENT_MESSAGE_PREFIX): subprocess.call(['sagas', 'vis', text, sett.lang]) return text