Rasa学习笔记(一):定义自己的故事

还没吃透Rasa_NLU_Chi的强大技术,又发现了更加厉害的框架:Rasa,不像Rasa_NLU_Chi已经半年没更新过了,Rasa在最近12个小时内还在更新。在页面https://github.com/RasaHQ中,我们可以看到几个非常好的项目:

rasa:Open source machine learning framework to automate text- and voice-based conversations: NLU, dialogue management, connect to Slack, Facebook, and more - Create chatbots and voice assistants.

rasa-demo:Sara - the Rasa Demo Bot: An example of a contextual AI assistant built with the open source Rasa Stack.

rasa-sdk:SDK for the development of custom actions for Rasa.

rasalit:Visualizations and helpers to improve and debug machine learning models for Rasa Open Source.

这么多厉害的技术真是值得赶紧学习。不过使用Get from Version Control的方式下载源码跟下载时间有关,有的时间下载速度非常慢,如果运气好中间不断网估计得下一个晚上。清晨下载速度比较快。奇怪的是,使用Download Zip的方式下载只有3.3M,而使用Get from Version Control的方式下载会超过1.4G。2021/3/5 6:36终于下载完了Rasa。由于还不会用这个框架,先走马观花一番。通过如下的练习,可以学会自定义故事模板以及机器人行为。

nlg_server

项目很大,先仿效前两天调用Rasa_NLU_Chi的方式,看看能不能以同样的方式使用Rasa。首先,将total_word_feature_extractor_zh.dat拷到data目录下。准备训练的时候发现没有找到sample_configs目录。找了找也没看到setup.py文件,并且没有看到requirements.txt,那就还是按照以前的习惯遇到缺什么包就手动安装什么包。具体需要安装什么包,可以通过在根目录下运行python -m rasa.example.nlg_server.nlg_server来检测。

  • pip install sanic

  • pip install ruamel

安装ruamel报错:

E:\workspace-python\rasa>pip install ruamel
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
ERROR: Could not find a version that satisfies the requirement ruamel
ERROR: No matching distribution found for ruamel

定位到相关文件io.py找到import后进行参考,先安装ruamel.yaml,问题解决。

  • pip install pykwalify

  • pip install async_generator

  • pip install terminaltables

  • pip install apscheduler

  • pip install socketio

安装socketio报错:

ERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.
tensorboard 2.3.0 requires setuptools>=41.0.0, but you have setuptools 3.3 which is incompatible.
note-seq 0.0.1 requires numba==0.48.0, but you have numba 0.49.1 which is incompatible.
ipython 7.19.0 requires setuptools>=18.5, but you have setuptools 3.3 which is incompatible.
google-auth 1.23.0 requires setuptools>=40.3.0, but you have setuptools 3.3 which is incompatible.
google-api-core 1.23.0 requires setuptools>=34.0.0, but you have setuptools 3.3 which is incompatible.
Successfully installed netifaces-0.10.6 setuptools-3.3 socketio-0.2.1

编辑器此时也发生了异常: 

选择Install packaging tools后,setup tools的版本被升级到了54.0.0。重新开一个cmd窗口重新执行pip install socketio不再报错。不过此时执行python -m rasa.example.nlg_server.nlg_server继续报错:

E:\workspace-python\rasa>python -m rasa.example.nlg_server.nlg_server
Traceback (most recent call last):
  File "D:\Programs\Python\Python38\lib\runpy.py", line 185, in _run_module_as_main
    mod_name, mod_spec, code = _get_module_details(mod_name, _Error)
  File "D:\Programs\Python\Python38\lib\runpy.py", line 111, in _get_module_details
    __import__(pkg_name)
  File "E:\workspace-python\rasa\rasa\__init__.py", line 9, in <module>
    from rasa.train import train
  File "E:\workspace-python\rasa\rasa\train.py", line 39, in <module>
    from rasa.core.agent import Agent
  File "E:\workspace-python\rasa\rasa\core\agent.py", line 15, in <module>
    from rasa.core.channels.channel import OutputChannel, UserMessage
  File "E:\workspace-python\rasa\rasa\core\channels\__init__.py", line 12, in <module>
    from rasa.core.channels.socketio import SocketIOInput
  File "E:\workspace-python\rasa\rasa\core\channels\socketio.py", line 10, in <module>
    from socketio import AsyncServer
ImportError: cannot import name 'AsyncServer' from 'socketio' (D:\Programs\Python\Python38\lib\site-packages\socketio\__init__.py)

感觉这个报错的难度为5星,暂时先放一放。

Rasa Core

  • pip install rasa_core

这是一个大模块,得装个几分钟。装到最后还抛出了一个错误:

FileNotFoundError: [WinError 2] 系统找不到指定的文件。: 'd:\\programs\\python\\python38\\lib\\site-packages\\rasa_nlu-0.12.2-py3.8.egg'

猜测需要先安装rasa_nlu。果然安装完rasa_nlu再安装rasa_core就不会报错了。

Rasa Bot

  • pip install rasa

这也是一个大模块,得装个几分钟。最后报了一个不兼容的错误,先记下来:

ERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.
tensor2tensor 1.15.7 requires tensorflow-probability==0.7.0, but you have tensorflow-probability 0.11.1 which is incompatible.
rasa-core 0.8.6 requires fbmessenger<5.0.0, but you have fbmessenger 6.0.0 which is incompatible.
rasa-core 0.8.6 requires pykwalify<=1.6.0, but you have pykwalify 1.8.0 which is incompatible.
opencv-python 4.4.0.46 requires numpy>=1.17.3, but you have numpy 1.16.6 which is incompatible.
note-seq 0.0.1 requires numba==0.48.0, but you have numba 0.49.1 which is incompatible.
moviepy 1.0.3 requires numpy>=1.17.3; python_version != "2.7", but you have numpy 1.16.6 which is incompatible.
kfac 0.2.3 requires tensorflow-probability==0.8, but you have tensorflow-probability 0.11.1 which is incompatible.
  • pip install rasa-sdk

新建目录E:\workspace-python\rasa\bots\test,在此文件夹中运行以下命令以设置项目:rasa init --no-prompt,该命令将为机器人创建必需文件,还将训练NLU和核心模型,现在我们的情绪机器人已经准备好,让我们对其进行测试,使用以下命令测试机器人:rasa shell:

在目录E:\workspace-python\rasa\bots\test\actions中打开,取消其中对代码的注释:

在domain.yml中,可以看到预先定义好的回复方式:

可以在里面增加新的action:

version: "2.0"

intents:
  - greet
  - goodbye
  - affirm
  - deny
  - mood_great
  - mood_unhappy
  - bot_challenge

actions:
  - action_hello_world

responses:
  utter_greet:
  - text: "Hey! How are you?"

  utter_cheer_up:
  - text: "Here is something to cheer you up:"
    image: "https://i.imgur.com/nGF1K8f.jpg"

  utter_did_that_help:
  - text: "Did that help you?"

  utter_happy:
  - text: "Great, carry on!"

  utter_goodbye:
  - text: "Bye"

  utter_iamabot:
  - text: "I am a bot, powered by Rasa."

session_config:
  session_expiration_time: 60
  carry_over_slots_to_new_session: true

打开data目录下的stories.yml,将happy path的故事模板进行简单修改:

- story: happy path
  steps:
  - intent: greet
  - action: utter_greet
  - action: action_hello_world
  - intent: mood_great
  - action: utter_happy

接下来cd到E:\workspace-python\rasa\bots\test\中执行命令rasa train。训练完成后在相同路径下执行命令rasa run actions。控制台会显示如下信息:

2021-03-05 09:32:21 INFO     rasa_sdk.endpoint  - Starting action endpoint server...
2021-03-05 09:32:21 INFO     rasa_sdk.executor  - Registered function for 'action_hello_world'.
2021-03-05 09:32:21 INFO     rasa_sdk.endpoint  - Action endpoint is up and running on http://localhost:5055

在浏览器上直接输入http://localhost:5055/,报错:

输入http://localhost:5055/webhook,报错:

上面这两个错暂时(2021/3/5)不知如何排查。

继续探索其它方法,在控制台执行命令rasa shell报错:

Bot loaded. Type a message and press enter (use '/stop' to exit):
Unable to start server
Traceback (most recent call last):
  File "d:\programs\python\python38\lib\site-packages\sanic\server.py", line 891, in serve
    http_server = loop.run_until_complete(server_coroutine)
  File "d:\programs\python\python38\lib\asyncio\base_events.py", line 616, in run_until_complete
    return future.result()
  File "d:\programs\python\python38\lib\asyncio\base_events.py", line 1463, in create_server
    raise OSError(err.errno, 'error while attempting '
OSError: [Errno 10048] error while attempting to bind on address ('0.0.0.0', 5005): 通常每个套接字地址(协议/网络地址/端 口)只允许使用一次。

关闭rasa run actions对应的控制台窗口以及另外一个rasa shell窗口,再次执行命令rasa shell,报错:

Bot loaded. Type a message and press enter (use '/stop' to exit):
Your input ->  hi
2021-03-05 09:47:55 ERROR    rasa.core.processor  - Encountered an exception while running action 'action_hello_world'.Bot will continue, but the actions events are lost. Please check the logs of your action server for more information.
Traceback (most recent call last):
  File "d:\programs\python\python38\lib\site-packages\rasa\core\processor.py", line 765, in _run_action
    events = await action.run(
  File "d:\programs\python\python38\lib\site-packages\rasa\core\actions\action.py", line 657, in run
    raise RasaException(
rasa.shared.exceptions.RasaException: Failed to execute custom action 'action_hello_world' because no endpoint is configured to run this custom action. Please take a look at the docs and set an endpoint configuration via the --endpoints flag. https://rasa.com/docs/rasa/custom-actions
Hey! How are you?
Your input ->     

自定义事件action_hello_world并没有加载进来,在endpoints.yml中将下面两行取消注释:

action_endpoint:
  url: "http://localhost:5055/webhook"

再次执行rasa shell,继续报错:

2021-03-05 09:52:26 ERROR    rasa.core.actions.action  - Failed to run custom action 'action_hello_world'. Couldn't connect to the server at 'http://localhost:5055/webhook'. Is the server running? Error: Cannot connect to host localhost:5055 ssl:default [远程计算机拒绝网络连接。]
2021-03-05 09:52:26 ERROR    rasa.core.processor  - Encountered an exception while running action 'action_hello_world'.Bot will continue, but the actions events are lost. Please check the logs of your action server for more information.
Traceback (most recent call last):
  File "d:\programs\python\python38\lib\site-packages\aiohttp\connector.py", line 936, in _wrap_create_connection
    return await self._loop.create_connection(*args, **kwargs)  # type: ignore  # noqa
  File "d:\programs\python\python38\lib\asyncio\base_events.py", line 1025, in create_connection
    raise exceptions[0]
  File "d:\programs\python\python38\lib\asyncio\base_events.py", line 1010, in create_connection
    sock = await self._connect_sock(
  File "d:\programs\python\python38\lib\asyncio\base_events.py", line 924, in _connect_sock
    await self.sock_connect(sock, address)
  File "d:\programs\python\python38\lib\asyncio\proactor_events.py", line 702, in sock_connect
    return await self._proactor.connect(sock, address)
  File "d:\programs\python\python38\lib\asyncio\windows_events.py", line 808, in _poll
    value = callback(transferred, key, ov)
  File "d:\programs\python\python38\lib\asyncio\windows_events.py", line 595, in finish_connect
    ov.getresult()
ConnectionRefusedError: [WinError 1225] 远程计算机拒绝网络连接。

看来命令rasa run actions用于打开行为服务器,然后就能正常运行了:

猜你喜欢

转载自blog.csdn.net/dragon_T1985/article/details/114370797