Rasa X-Google Assistant连接器未从Rasa接收任何消息

时间:2019-05-29 12:08:20

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

我正在为Rasa X(rasa 1.0)创建一个连接器,以将Google Assistant用作前端。在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检查了运行状况,是否可行。我也从助手那里收到有效载荷。 但是,当我将其转发给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的任何回复。

1 个答案:

答案 0 :(得分:0)

由于引入了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]