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時,首先是創(chuàng)建了一個http_input_channel,在創(chuàng)建input_channel時首先在內置的channel中查找是否有用戶指定的(CmdlineInput, FacebookInput,
CallbackInput, RestInput, SocketIOInput等),如果沒有則加載用戶自定義的channel module。
接著啟動一個基于sanic的web服務,如果在上一步定義了input_channel在將這個input_channel注冊為服務的數(shù)據(jù)來源。
然后注冊listener,實際是創(chuàng)建Agent來提供Rasa Core的服務。

總結下來,run命令是創(chuàng)建一個input_channel和web服務來提供restfull的接口供用戶調用,而在底層則是Agent來進行Rasa Core的Api調用。

?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容