【问题标题】:Rasa X - Google Assistant connector doesn't receive any messages from RasaRasa X - Google 助理连接器没有收到来自 Rasa 的任何消息
【发布时间】:2020-04-02 10:33:54
【问题描述】:

我正在为 Rasa X (rasa 1.0) 创建一个连接器,以便将 Google 助理用作前端。在 1.0 发布之前,本教程:https://medium.com/rasa-blog/going-beyond-hey-google-building-a-rasa-powered-google-assistant-5ff916409a25 工作得非常好。但是,当我尝试在相同的结构上运行 Rasa X 时,我的旧的基于 Flask 的连接器和启动项目的 Rasa 代理之间存在不兼容性。

在新版本中,rasa.core.agent.handle_channels([input_channel], http_port=xxxx) 改用了 Sanic,这似乎与我的旧方法不兼容。

我尝试将旧的 Flask 连接器转换为 Sanic(以前从未使用过)连接器,并且我使用 Postman 检查了运行状况路径,它有效。我还收到了来自 Assistant 的有效载荷。 但是,当我将此转发给 Rasa 代理时,我没有得到任何回报。

  • 这是新的 Sanic 连接器:
class GoogleAssistant(InputChannel):

    @classmethod
    def name(cls):
        return "google_assistant"

    def blueprint(self, on_new_message):
        # this is a Sanic Blueprint
        google_webhook = sBlueprint("google_webhook")

        @google_webhook.route("/", methods=['GET'])
        def health(request):
            return response.json({"status": "ok"})

        @google_webhook.route("/webhook", methods=["POST"])
        def receive(request):
            #payload = json.loads(request)
            payload = request.json
            sender_id = payload["user"]['userId']
            intent = payload['inputs'][0]['intent']             
            text = payload['inputs'][0]['rawInputs'][0]['query']

            try:
                if intent == "actions.intent.MAIN":
                    message = "<speak>Hello! <break time=\"1\"/> Welcome to the Rasa-powered Google Assistant skill. You can start by saying hi."
                else:
                    # Here seems to be the issue. responses is always empty
                    out = CollectingOutputChannel()
                    on_new_message(UserMessage(text,out,sender_id))
                    responses = [m["text"] for m in out.messages]
                    message = responses[0]
            except LookupError as e:
                message = "RASA_NO_REPLY"
                print(e)

            r = json.dumps("some-response-json")
            return HTTPResponse(body=r, content_type="application/json")

        return google_webhook
  • 这是启动项目的脚本:
action_endpoint = EndpointConfig(url="http://localhost:5055/webhook")
nlu_interpreter = RasaNLUInterpreter('path-to-model')
agent = Agent.load('path-to-model-2', interpreter = nlu_interpreter, action_endpoint=action_endpoint)

input_channel = GoogleAssistant()
agent.handle_channels([input_channel], http_port=5010)

我希望输出是 Rasa 代理选择的文本作为回复“这是回复”,但我什么也没得到(列表为空)。

编辑

我将def receive(request): 定义为async def receive(request): 并将on_new_message(UserMessage(text,out,sender_id)) 更改为await on_new_message(UserMessage(text,out,sender_id))。此外,现在启动项目的脚本是:

loop = asyncio.get_event_loop()  

action_endpoint = EndpointConfig(url="http://localhost:5055/webhook")
nlu_interpreter = RasaNLUInterpreter('path')
agent = Agent.load('path2', interpreter = nlu_interpreter, action_endpoint=action_endpoint)
input_channel = GoogleAssistant()

loop.run_until_complete(agent.handle_channels([input_channel], http_port=5010))
loop.close()

不幸的是它没有改变任何东西,仍然没有在输出通道上得到 Rasa 的任何回复。

【问题讨论】:

    标签: python flask rasa-nlu sanic rasa-x


    【解决方案1】:

    由于异步 Sanic 服务器的引入,on_new_message 必须等待。尝试将您的函数定义更改为

    async def receive(request):
    

    还有其他的

    out = CollectingOutputChannel()
    await on_new_message(UserMessage(query, output_channel, user_id))
    m = [m['text'] for m in out.messages]
    message = responses[0]
    

    【讨论】:

    • 感谢您的想法,但我只是尝试将receive 定义为异步并等待on_new_message 并没有更改。还有其他想法吗?
    • 你知道 Sanic 中的异步行为是否需要更多?
    • 经过更多测试后,问题似乎出在调用on_new_message 时。看起来这个新的 Sanic 实现方法不再起作用了。
    • 在添加 async/await 语法后遇到了哪些错误?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-06-16
    • 2017-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多