Exemplo n.º 1
0
async def record_messages(
    server_url=DEFAULT_SERVER_URL,
    auth_token=None,
    sender_id=UserMessage.DEFAULT_SENDER_ID,
    max_message_limit=None,
    use_response_stream=True,
):
    """Read messages from the command line and print bot responses."""

    auth_token = auth_token if auth_token else ""

    exit_text = INTENT_MESSAGE_PREFIX + "stop"

    rasa.cli.utils.print_success("Bot loaded. Type a message and press enter "
                                 "(use '{}' to exit): ".format(exit_text))

    num_messages = 0
    while not utils.is_limit_reached(num_messages, max_message_limit):
        text = get_cmd_input()
        if text == exit_text or text is None:
            break

        if use_response_stream:
            bot_responses = send_message_receive_stream(
                server_url, auth_token, sender_id, text)
            async for response in bot_responses:
                print_bot_output(response)
        else:
            bot_responses = await send_message_receive_block(
                server_url, auth_token, sender_id, text)
            for response in bot_responses:
                print_bot_output(response)

        num_messages += 1
    return num_messages
Exemplo n.º 2
0
async def record_messages(endpoint: EndpointConfig,
                          sender_id: Text = UserMessage.DEFAULT_SENDER_ID,
                          max_message_limit: Optional[int] = None,
                          finetune: bool = False,
                          stories: Optional[Text] = None,
                          skip_visualization: bool = False
                          ):
    """Read messages from the command line and print bot responses."""

    from rasa.core import training

    try:
        _print_help(skip_visualization)

        try:
            domain = await retrieve_domain(endpoint)
        except ClientError:
            logger.exception("Failed to connect to Rasa Core server at '{}'. "
                             "Is the server running?".format(endpoint.url))
            return

        trackers = await training.load_data(stories, Domain.from_dict(domain),
                                            augmentation_factor=0,
                                            use_story_concatenation=False,
                                            )

        intents = [next(iter(i)) for i in (domain.get("intents") or [])]

        num_messages = 0
        sender_ids = [t.events for t in trackers] + [sender_id]

        if not skip_visualization:
            plot_file = "story_graph.dot"
            await _plot_trackers(sender_ids, plot_file, endpoint)
        else:
            plot_file = None

        while not utils.is_limit_reached(num_messages, max_message_limit):
            try:
                if await is_listening_for_message(sender_id, endpoint):
                    await _enter_user_message(sender_id, endpoint)
                    await _validate_nlu(intents, endpoint, sender_id)
                await _predict_till_next_listen(endpoint, sender_id,
                                                finetune, sender_ids, plot_file)

                num_messages += 1
            except RestartConversation:
                await send_event(endpoint, sender_id,
                                 Restarted().as_dict())

                await send_event(endpoint, sender_id,
                                 ActionExecuted(ACTION_LISTEN_NAME).as_dict())

                logger.info("Restarted conversation, starting a new one.")
            except UndoLastStep:
                await _undo_latest(sender_id, endpoint)
                await _print_history(sender_id, endpoint)
            except ForkTracker:
                await _print_history(sender_id, endpoint)

                evts_fork = await _request_fork_from_user(sender_id, endpoint)

                await send_event(endpoint, sender_id,
                                 Restarted().as_dict())

                if evts_fork:
                    for evt in evts_fork:
                        await send_event(endpoint, sender_id, evt)
                logger.info("Restarted conversation at fork.")

                await _print_history(sender_id, endpoint)
                await _plot_trackers(sender_ids, plot_file, endpoint)

    except Abort:
        return
    except Exception:
        logger.exception("An exception occurred while recording messages.")
        raise