运行 带烧瓶的 RASA

Run RASA with flask

我想 运行 RASA 在 python 代码中使用 --enable-api 而不是命令行。下面是我的代码,它不起作用。让我知道我该怎么做。问题是一旦我点击服务,因为通道是 'cmdline' 它进入命令行。我不知道如何解决这个问题。

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

import logging
import rasa_core
from rasa_core.agent import Agent
from rasa_core.policies.keras_policy import KerasPolicy
from rasa_core.policies.memoization import MemoizationPolicy
from rasa_core.interpreter import RasaNLUInterpreter
from rasa_core.utils import EndpointConfig
from rasa_core.run import serve_application
from rasa_core import config

from rasa_core.policies.fallback import FallbackPolicy
from rasa_core.policies.keras_policy import KerasPolicy

from flask import Flask
from flask_cors import CORS, cross_origin


app = Flask(__name__)
CORS(app)

logger = logging.getLogger(__name__)

@app.route("/conversations/default/respond",methods=['POST'])
def run_weather_bot(serve_forever=True):
    logging.basicConfig(level="ERROR")
    interpreter = RasaNLUInterpreter('C:\xxxx_nlu\models\nlu\default\weathernlu')
    action_endpoint = EndpointConfig(url="http://xxx.xx.xx.xxx:5055/webhook")
    agent = Agent.load('C:\xxxx_nlu\models\dialogue', interpreter=interpreter, action_endpoint=action_endpoint)

    rasa_core.run.serve_application(agent,channel='cmdline')

    return agent


if __name__ == '__main__':

    app.run("xxx.xx.xx.xxx",5005,debug=True)

您正在使用以下命令在 run_weather_bot 函数的命令行中调用 rasa bot。

rasa_core.run.serve_application(agent,channel='cmdline')

如您所见,它用作命令行应用程序。

我已经对您的代码进行了一些更改,以便与 rasa 聊天机器人进行对话。 RASA agent的连接以及RASA agent如何处理输入消息可以参考AGENT documentation and Weather bot文章。

def rasa_agent():
    interpreter = RasaNLUInterpreter("Path for NLU")
    action_endpoint = EndpointConfig(url="Webhook URL")
    agent = Agent.load('Path to Dialogue', interpreter=interpreter, action_endpoint=action_endpoint)
    ## Next line runs the rasa in commandline
    # rasa_core.run.serve_application(agent,channel='cmdline') 
    return agent

@app.route("/conversations/default/respond",methods=['POST'])
def run_weather_bot(serve_forever=True):

    agent = rasa_agent() # calling rasa agent
    ## Collect Query from POST request
    ## Send Query to Agent
    ## Get Response of BOT
    output = {} ## Append output
    return jsonify(output)