大連百度關(guān)鍵詞優(yōu)化張家界百度seo
??本文以報(bào)時(shí)機(jī)器人為載體,介紹了報(bào)時(shí)機(jī)器人的對(duì)話(huà)能力范圍、配置文件功能和訓(xùn)練和運(yùn)行命令,重點(diǎn)介紹了rasa shell命令啟動(dòng)后的程序執(zhí)行過(guò)程。
一.報(bào)時(shí)機(jī)器人項(xiàng)目結(jié)構(gòu)
1.對(duì)話(huà)能力范圍
(1)能夠識(shí)別歡迎語(yǔ)意圖(greet)和拜拜意圖(goodbye)
(2)能夠識(shí)別時(shí)間意圖(query_time)
(3)能夠識(shí)別日期意圖(query_date)
(4)能夠識(shí)別星期幾意圖(query_weekday)
2.配置文件功能
(1)nlu.yml:主要包含意圖、例子、對(duì)實(shí)體的標(biāo)注等。
(2)stories.yml文件:用戶(hù)和機(jī)器人之間對(duì)話(huà)的表示,用戶(hù)輸入意圖,機(jī)器人響應(yīng)action。
(3)actions.py:自定義的action,比如action_query_time、action_query_date、action_query_weekday。
(4)config.yml:主要包含nlu(分詞、特征提取和分類(lèi)等)和dialog policy(記憶、規(guī)則、機(jī)器學(xué)習(xí)等)。
(5)domain.yml:主要包含意圖、視圖、槽位、響應(yīng)、動(dòng)作等。
(6)credentials.yml:主要和其它對(duì)話(huà)平臺(tái)集成,比如facebook、slack等。
(7)endpoints.yml:action_endpoint(調(diào)用自定義action)、tracker_store對(duì)話(huà)存儲(chǔ)(內(nèi)存、redis、mongodb等)、event_broker消息隊(duì)列(RabbitMQ、Kafka等)。
3.訓(xùn)練和運(yùn)行命令
(1)訓(xùn)練模型
使用NLU數(shù)據(jù)和stories訓(xùn)練模型,模型保存在./models中。
rasa train
說(shuō)明:關(guān)于如何把數(shù)據(jù)集按照比例拆分為訓(xùn)練集和測(cè)試集,在訓(xùn)練集上訓(xùn)練模型,在測(cè)試集上測(cè)試模型,可以參考《聊天機(jī)器人框架Rasa資源整理》。
(2)啟動(dòng)action服務(wù)器
使用Rasa SDK開(kāi)啟action服務(wù)器。
rasa run actions
(3)啟動(dòng)rasa服務(wù)器和客戶(hù)端
通過(guò)命令行的方式加載訓(xùn)練模型,然后同聊天機(jī)器人進(jìn)行對(duì)話(huà)。
rasa shell
二.rasa shell執(zhí)行流程分析
??整體思路是通過(guò)rasa shell加載和解析模型,通過(guò)消息處理的方式建立起用戶(hù)(客戶(hù)端)和聊天機(jī)器人(rasa服務(wù))對(duì)話(huà)的橋梁。
1.rasa/cli/shell.py文件
??在rasa/cli/shell.py
文件中,def shell(args: argparse.Namespace) -> None
函數(shù)如下:
2.rasa/cli/run.py文件
??在rasa/cli/run.py
文件中,def run(args: argparse.Namespace) -> None
函數(shù)如下:
3.rasa/api.py文件
??在rasa/api.py
文件中,def run(...) -> None
函數(shù)如下:
??在run()
函數(shù)中調(diào)用serve_application()
函數(shù)如下:
4.rasa/core/run.py文件
??在rasa/core/run.py
文件中,serve_application()
函數(shù)如下:
??在serve_application()
函數(shù)中啟動(dòng)了一個(gè)基于Sanic的Web服務(wù)器,通過(guò)configure_app()
方法構(gòu)建了app,然后通過(guò)run()
方法啟動(dòng),如下所示:
app = configure_app(input_channels,cors,auth_token,enable_api,response_timeout,jwt_secret,jwt_method,port=port,endpoints=endpoints,log_file=log_file,conversation_id=conversation_id,use_syslog=use_syslog,syslog_address=syslog_address,syslog_port=syslog_port,syslog_protocol=syslog_protocol,request_timeout=request_timeout,)
......
app.run(host=interface,port=port,ssl=ssl_context,backlog=int(os.environ.get(ENV_SANIC_BACKLOG, "100")),workers=number_of_workers,)
??通過(guò)register_listener(listener, event)
注冊(cè)給定事件的偵聽(tīng)器:
app.register_listener(partial(load_agent_on_start, model_path, endpoints, remote_storage), "before_server_start",)
app.register_listener(close_resources, "after_server_stop")
5.rasa/core/agent.py文件
??通過(guò)load_agent_on_start()
方法加載一個(gè)agent。在rasa/core/agent.py
文件中,load_agent()
函數(shù)如下所示:
??在load_agent()
函數(shù)中,加載模型代碼是agent.load_model(model_path)
。在Agent類(lèi)的def load_model()
方法中,關(guān)于初始化MessageProcessor代碼如下:
self.processor = MessageProcessor(model_path=model_path,tracker_store=self.tracker_store,lock_store=self.lock_store,action_endpoint=self.action_endpoint,generator=self.nlg,http_interpreter=self.http_interpreter,
)
加載模型的代碼如下:
logger.info(f"Loading model {model_tar}...")
with tempfile.TemporaryDirectory() as temporary_directory:try:metadata, runner = loader.load_predict_graph_runner(Path(temporary_directory),Path(model_tar),LocalModelStorage,DaskGraphRunner,)return os.path.basename(model_tar), metadata, runnerexcept tarfile.ReadError:raise ModelNotFound(f"Model {model_path} can not be loaded.")
6.rasa/engine/loader.py文件
??在rasa/engine/loader.py
文件中,def load_predict_graph_runner()
函數(shù)如下:
三.遇到的問(wèn)題和說(shuō)明
1.如何用PyCharm調(diào)試Rasa項(xiàng)目
解析:一種是基于Script path的調(diào)試方法,一種是基于Module name的調(diào)試方法。這里介紹前者如下所示:
(1)Script Path:安裝rasa類(lèi)庫(kù)的__main__.py
文件路徑。
(2)Parameters:rasa的各種cli,比如train、test、shell等。
(3)Working directory:安裝rasa類(lèi)庫(kù)的根目錄。
說(shuō)明:因?yàn)閞asa類(lèi)庫(kù)依賴(lài)類(lèi)庫(kù)太多導(dǎo)致系統(tǒng)環(huán)境混亂,所示建議使用虛擬環(huán)境進(jìn)行rasa類(lèi)庫(kù)安裝。
2.NoConsoleScreenBufferError
解析:exception=NoConsoleScreenBufferError(‘No Windows console found. Are you running cmd.exe?’)
3.模型20220915-081548-honest-yield.tar.gz
解析:由metadata.json文件和components文件夾組成,后者和config.yml內(nèi)容密切相關(guān),如下所示:
4.Sanic框架
解析:Sanic是一個(gè)高性能異步的Web框架。
5.asyncio庫(kù)
解析:它的編程模型是一個(gè)消息循環(huán),關(guān)鍵字涉及event_loop、coroutine、task、future、async/await等。
??本文只是簡(jiǎn)要的介紹了rasa shell命令啟動(dòng)后的程序執(zhí)行過(guò)程,但是對(duì)于加載模型后如何解析模型構(gòu)建圖,以及用戶(hù)輸入后,消息如何通過(guò)模型(nlu和dialog policy)得到輸出并沒(méi)有介紹,后面寫(xiě)篇文章專(zhuān)門(mén)介紹。
參考文獻(xiàn):
[1]Rasa實(shí)戰(zhàn):構(gòu)建開(kāi)源對(duì)話(huà)機(jī)器人
[2]Sanic官方文檔:https://www.osgeo.cn/sanic/
[3]asyncio庫(kù)異步I/O:https://docs.python.org/3.7/library/asyncio.html
[4]聊天機(jī)器人框架Rasa資源整理