【发布时间】:2021-02-24 22:48:24
【问题描述】:
我正在尝试在我的 rasa 聊天机器人中集成一个表单。 在 domain.yml 中,我包含了以下内容:
- 我声明了插槽:
slots:
question1:
type: text
question2:
type: text
question3:
type: text
- 表单应该发送给用户的问题:
utter_ask_question1:
- text:" first question goes here"
utter_ask_question2:
- text:" second question goes here"
utter_ask_question3:
- text:" third question goes here"
- 将表单定义为:
forms:
user_quiz_form:
question1:
- type: from_text
# entity: date
question2:
- type: from_text
question3:
- type: from_text
- 操作部分还包含:
actions:
- action_submit
- user_quiz_form
文件 rules.yml 包含:
- rule: Activate quiz form
steps:
- intent: quiz
- action: utter_quiz
- action: user_quiz_form
- active_loop: user_quiz_form
- rule: Submit quiz form
condition:
- active_loop: user_quiz_form
steps:
- action: user_quiz_form
- active_loop: null
- slot_was_set:
- requested_slot: null
- action: action_submit
而actions.py是:
from typing import Any, Text, Dict, List
from rasa_sdk import Action, Tracker
from rasa_sdk.events import SlotSet, EventType
from rasa_sdk.executor import CollectingDispatcher
import webbrowser
class ValidateForm(Action):
def name(self) -> Text:
return "user_quiz_form"
def run(
self, dispatcher: CollectingDispatcher, tracker: Tracker, domain: Dict
) -> List[EventType]:
required_slots = ["question1","question2", "question3"]
for slot_name in required_slots:
if tracker.slots.get(slot_name) is None:
# The slot is not filled yet. Request the user to fill this slot next.
return [SlotSet("requested_slot", slot_name)]
return [SlotSet("requested_slot", None)]
class ActionSubmit(Action):
def name(self) -> Text:
return "action_submit"
def run(
self,
dispatcher,
tracker: Tracker,
domain: "DomainDict",
) -> List[Dict[Text, Any]]:
print("****************SUBMIT*****************")
dispatcher.utter_message(template="utter_quiz_thanks", date=tracker.slots.get("question1"))
return []
当表单被触发时,它不会向用户提问并返回 utter_quiz_thanks,其中 None 作为 question1 值。
【问题讨论】: