用烧瓶运行 RASA

Iro*_*man 2 flask rasa-nlu rasa-core

我想在 python 代码而不是命令行中使用 --enable-api 运行 RASA。下面是我的代码,它不起作用。让我知道我该怎么做。问题是一旦我点击了服务,因为频道是“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 Code Online (Sandbox Code Playgroud)

小智 5

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

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

正如您所看到的,它用作命令行应用程序。

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

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) 
Run Code Online (Sandbox Code Playgroud)