如何在 rasa 的 handle_text 中传递额外的参数?
How to pass extra parameters in in handle_text in rasa?
我正在使用 RASA
和 Flask
创建聊天机器人
我有一个奇怪的要求,对于同一个问题,我必须根据请求配置 2 个不同的响应。
在请求中有一个键customer
它可以是 1 或 0。
说我有一个问题:
what is refund policy?
如果问题是客户问的(键值为1)
会return您将在 3 天内收到退款
否则会 return 您将在 7 天内收到退款
所以我不知道如何在 handle_text
中传递这个 customer
值,它正在为我的问题生成响应。
在 Rasa
中有没有办法做到这一点?
我的代码:
from rasa_core.interpreter import RasaNLUInterpreter
from rasa_core.utils importEndpointConfig
from flask import FLASK, request
import json
nlu_interpreter = RasaNLUInterpreter(NLU_MODEL)
action_endpoint = EndpointConfig(url=ACTION_ENDPOINT)
agent = Agent.load(DIALOG_MODEL, interpreter=nlu_interpreter, action_endpoint=action_endpoint)
@app.route("/chatbot", methods=["POST"])
def bot():
data = json.loads(request.data)
msg = data["msg"] # the question by user
is_customer = data["customer"]
response = agent.handle_text(msg, sender_id=data["user_id"])
return json.dumps(response[0]["text"])
您需要在操作文件中配置该操作,并运行一个单独的操作服务器来为您处理。
例如,在自定义方法的 运行 方法中,您可以获得客户的槽值,然后根据该槽值,您可以 return 需要的消息。
def run(self,
dispatcher: CollectingDispatcher,
tracker: Tracker,
domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:
customer = tracker.get_slot('customer')
if customer:
dispatcher.utter_message("You will get refund in 3 days.")
else:
dispatcher.utter_message("You will get refund in 7 days.")
文档link:https://rasa.com/docs/rasa/core/actions/#custom-actions
希望对您有所帮助。
我正在使用 RASA
和 Flask
我有一个奇怪的要求,对于同一个问题,我必须根据请求配置 2 个不同的响应。
在请求中有一个键customer
它可以是 1 或 0。
说我有一个问题:
what is refund policy?
如果问题是客户问的(键值为1)
会return您将在 3 天内收到退款
否则会 return 您将在 7 天内收到退款
所以我不知道如何在 handle_text
中传递这个 customer
值,它正在为我的问题生成响应。
在 Rasa
中有没有办法做到这一点?
我的代码:
from rasa_core.interpreter import RasaNLUInterpreter
from rasa_core.utils importEndpointConfig
from flask import FLASK, request
import json
nlu_interpreter = RasaNLUInterpreter(NLU_MODEL)
action_endpoint = EndpointConfig(url=ACTION_ENDPOINT)
agent = Agent.load(DIALOG_MODEL, interpreter=nlu_interpreter, action_endpoint=action_endpoint)
@app.route("/chatbot", methods=["POST"])
def bot():
data = json.loads(request.data)
msg = data["msg"] # the question by user
is_customer = data["customer"]
response = agent.handle_text(msg, sender_id=data["user_id"])
return json.dumps(response[0]["text"])
您需要在操作文件中配置该操作,并运行一个单独的操作服务器来为您处理。
例如,在自定义方法的 运行 方法中,您可以获得客户的槽值,然后根据该槽值,您可以 return 需要的消息。
def run(self,
dispatcher: CollectingDispatcher,
tracker: Tracker,
domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:
customer = tracker.get_slot('customer')
if customer:
dispatcher.utter_message("You will get refund in 3 days.")
else:
dispatcher.utter_message("You will get refund in 7 days.")
文档link:https://rasa.com/docs/rasa/core/actions/#custom-actions
希望对您有所帮助。