# 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調用。