我正在为 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 检查了运行状况路线,这很有效。我还收到了来自 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 的任何回复。