【问题标题】:Encountering error running custom actions in rasa在 rasa 中运行自定义操作时遇到错误
【发布时间】:2019-09-09 05:52:45
【问题描述】:

我最近开始研究 rasa。我的聊天机器人很简单。每当用户提出问题时,机器人都会过滤来自 sqlite3 数据库的数据并返回结果。我添加了训练示例,为流程创建了故事,并编写了一个自定义操作来过滤数据库中的数据。对于自定义操作,我指定了一个端点并启动了操作服务器。这是我目前的代码

actions.py

from typing import Any, Text, Dict, List
from rasa_core_sdk import Action, Tracker
import sqlite3

class TestAction(Action):

    def name(self) -> Text:
        return "action_testAction"

    def run(self, dispatcher, tracker, domain):

        UserId = tracker.get_slot('UserID')
        query = 'SELECT User_name FROM sales WHERE UserID=?'

        conn = sqlite3.connect("test.db")
        cursor = conn.cursor()
        cursor.execute(query, (UserId,))
        name = cursor.fetchall()

        msg = 'Hello {}!'.format(name)
        dispatcher.utter_message(msg)

endpoints.yml

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

为了运行端点,我在单独的终端窗口中运行了以下代码

python -m rasa_sdk --actions actions

但是当我基于这个 link 使用rasa shell --endpoints endpoints.yml 命令运行程序时,出现以下错误

rasa.core.processor  - Encountered an exception while running action 'action_testAction'. Bot will continue, but the actions events are lost. Please check the logs of your action server for more information.

这是我的动作端点服务器返回的内容

127.0.0.1 - - [2019-09-09 11:07:23] "POST / HTTP/1.1" 404 342 0.005443

我不确定,我做错了什么。我检查了代码 actions.py 文件。我似乎没有在那里犯任何错误。

[编辑 1]

我在网上找了这个tutorial

在本教程中,端点定义为

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

现在我对我的代码进行了同样的尝试。当我使用更新的代码运行聊天机器人时,在 action_server 中出现以下异常

Exception: No registered Action found for name 'action_testAction'.

[编辑 2]

在调试模式下运行动作服务器的结果

(base) SASHAANKs-MacBook-Pro:speechbot2 sashaanksekar$ rasa run actions -vv
2019-09-09 19:38:33 INFO     rasa_sdk.endpoint  - Starting action endpoint server...
2019-09-09 19:38:38 INFO     rasa_sdk.endpoint  - Action endpoint is up and running. on ('0.0.0.0', 5055)
2019-09-09 19:39:06 DEBUG    rasa_sdk.executor  - Received request to run 'action_testAction'
2019-09-09 19:39:06 ERROR    flask.app  - Exception on /webhook [POST]
Traceback (most recent call last):
  File "/Users/sashaanksekar/anaconda3/lib/python3.7/site-packages/flask/app.py", line 2292, in wsgi_app
    response = self.full_dispatch_request()
  File "/Users/sashaanksekar/anaconda3/lib/python3.7/site-packages/flask/app.py", line 1815, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/Users/sashaanksekar/anaconda3/lib/python3.7/site-packages/flask_cors/extension.py", line 161, in wrapped_function
    return cors_after_request(app.make_response(f(*args, **kwargs)))
  File "/Users/sashaanksekar/anaconda3/lib/python3.7/site-packages/flask/app.py", line 1718, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/Users/sashaanksekar/anaconda3/lib/python3.7/site-packages/flask/_compat.py", line 35, in reraise
    raise value
  File "/Users/sashaanksekar/anaconda3/lib/python3.7/site-packages/flask/app.py", line 1813, in full_dispatch_request
    rv = self.dispatch_request()
  File "/Users/sashaanksekar/anaconda3/lib/python3.7/site-packages/flask/app.py", line 1799, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/Users/sashaanksekar/anaconda3/lib/python3.7/site-packages/flask_cors/decorator.py", line 128, in wrapped_function
    resp = make_response(f(*args, **kwargs))
  File "/Users/sashaanksekar/anaconda3/lib/python3.7/site-packages/rasa_sdk/endpoint.py", line 59, in webhook
    response = executor.run(action_call)
  File "/Users/sashaanksekar/anaconda3/lib/python3.7/site-packages/rasa_sdk/executor.py", line 246, in run
    "No registered Action found for name '{}'.".format(action_name)
Exception: No registered Action found for name 'action_testAction'.

请帮帮我

【问题讨论】:

  • 尝试在包含actions.py 文件的文件夹中使用rasa run actions --actions actions -vv 运行操作服务器。它似乎没有选择 TestAction 类。
  • actions.py 存在于根目录中。我正在从该目录运行服务器。我还尝试将 actions.py 移动到它自己的文件夹中。这也不起作用

标签: python chatbot rasa-nlu rasa-core rasa


【解决方案1】:

您收到Exception: No registered Action found for name 'action_testAction'. 错误的原因是因为您在运行操作服务器时出现了第一个异常,即:

rasa.core.processor  - Encountered an exception while running action 'action_testAction'. Bot will continue, but the actions events are lost. Please check the logs of your action server for more information.

尝试使用调试标志-vv(如rasa run actions -vv)在debug mode 中运行操作服务器,以查看代码中的问题所在。 (问题可能是因为您可能没有导入 Action 或 sqlite3 或其他任何东西)。

希望对您有所帮助。

编辑:

确保action 文件的名称匹配。

运行rasa run actions --actions action -vv yields

(chatbot) PS C:\Users\user\Documents\Python\rasa-test> python -m rasa_sdk --actions action -vv
2019-09-09 21:02:56 INFO     rasa_sdk.endpoint  - Starting action endpoint server... 
2019-09-09 21:02:56 INFO     rasa_sdk.executor  - Registered function for 'action_testAction'.
2019-09-09 21:02:56 INFO     rasa_sdk.endpoint  - Action endpoint is up and running. on ('0.0.0.0', 5055)       

action.py

import sqlite3

from rasa_sdk import Action

class TestAction(Action):

    def name(self):
        return "action_testAction"

    def run(self, dispatcher, tracker, domain):

        UserId = tracker.get_slot('UserID')
        query = 'SELECT User_name FROM sales WHERE UserID=?'

        conn = sqlite3.connect("test.db")
        cursor = conn.cursor()
        cursor.execute(query, (UserId,))
        name = cursor.fetchall()

        msg = 'Hello {}!'.format(name)
        dispatcher.utter_message(msg)

【讨论】:

  • 我尝试在调试模式下运行操作服务器。我遇到了同样的错误。我还编辑了问题以在 actions.py 文件中添加导入。请看一看。
  • 我也在rasa forums 中发布了这个问题。该人要求我使用rasa init 创建一个新目录并将操作粘贴到新目录中。还是不行
  • @Sashaank 你能在调试模式下运行动作服务器并将完整的错误信息放在这里吗?当操作服务器未找到操作时会导致此问题。另外,您可以尝试使用rasa run actions --actions actions 运行操作服务器吗?
  • 请检查问题。我添加了调试模式的结果。同样使用rasa run actions --actions actions 运行服务器会产生相同的结果
  • @Sashaank 我已经更新了答案以包括对我有用的内容。当动作服务器找到自定义动作时,它应该说像Registered function for 'custom_action'。我在你的日志中看不到这个。
【解决方案2】:

我遇到了这个错误,简单的解决方法是观察 actions.py 文件下面部分中的错误和代码。

class TestAction(Action):

    def name(self) -> Text:
        return "action_testAction"

类的名字是'TestAction','name'函数的返回值是'action_testAction'。所以错误是 - 您在域文件中使用的操作的名称与此类中此“名称”方法返回的名称不匹配。在 NLU 训练数据和域文件中使用这个“action_testAction”。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多