Rasa Core的run命令源码赏析

# rasa_core/rasa/run.py

def run(model: Text, endpoints: Text, connector: Text = None,
        credentials: Text = None, **kwargs: Dict):
    """Runs a Rasa model.
    Args:
        model: Path to model archive.
        endpoints: Path to endpoints file.
        connector: Connector which should be use (overwrites `credentials`
        field).
        credentials: Path to channel credentials file.
        **kwargs: Additional arguments which are passed to
        `rasa.core.run.serve_application`.
    """
    import rasa.core.run
    from rasa.core.utils import AvailableEndpoints

    model_path = get_model(model)
    core_path, nlu_path = get_model_subdirectories(model_path)
    _endpoints = AvailableEndpoints.read_endpoints(endpoints)

    if not connector and not credentials:
        channel = "cmdline"
        logger.info("No chat connector configured, falling back to the "
                    "command line. Use `rasa configure channel` to connect"
                    "the bot to e.g. facebook messenger.")
    else:
        channel = connector

    kwargs = minimal_kwargs(kwargs, rasa.core.run.serve_application)
    rasa.core.run.serve_application(core_path,
                                    nlu_path,
                                    channel=channel,
                                    credentials_file=credentials,
                                    endpoints=_endpoints,
                                    **kwargs)
    shutil.rmtree(model_path)

注释中写的比较清楚,是Runs a Rasa model,主要的工作就是将模型加载到内存中,然后启动一个serve_application

# rasa_core/rasa/core/run.py

def serve_application(core_model=None,
                      nlu_model=None,
                      channel=None,
                      port=constants.DEFAULT_SERVER_PORT,
                      credentials_file=None,
                      cors=None,
                      auth_token=None,
                      enable_api=True,
                      jwt_secret=None,
                      jwt_method=None,
                      endpoints=None
                      ):
    if not channel and not credentials_file:
        channel = "cmdline"

    input_channels = create_http_input_channels(channel, credentials_file)

    app = configure_app(input_channels, cors, auth_token, enable_api,
                        jwt_secret, jwt_method, port=port)

    logger.info("Starting Rasa Core server on "
                "{}".format(constants.DEFAULT_SERVER_FORMAT.format(port)))

    app.register_listener(
        partial(load_agent_on_start, core_model, endpoints, nlu_model),
        'before_server_start')
    app.run(host='0.0.0.0', port=port,
            access_log=logger.isEnabledFor(logging.DEBUG))

再启动server_application时,首先是创建了一个http_input_channel,在创建input_channel时首先在内置的channel中查找是否有用户指定的(CmdlineInput, FacebookInput,
CallbackInput, RestInput, SocketIOInput等),如果没有则加载用户自定义的channel module。
接着启动一个基于sanic的web服务,如果在上一步定义了input_channel在将这个input_channel注册为服务的数据来源。
然后注册listener,实际是创建Agent来提供Rasa Core的服务。

总结下来,run命令是创建一个input_channel和web服务来提供restfull的接口供用户调用,而在底层则是Agent来进行Rasa Core的Api调用。

你可能感兴趣的:(Rasa Core的run命令源码赏析)