问题描述
我正在尝试在我的 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,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 值。
解决方法
这与您在 domain.yml 中使用的缩进相同吗?如果是这样,你就少了一些缩进;表单下方的插槽需要缩进(参见下面的示例)。我的猜测是您正确启动了表单,但由于缩进问题,您的助手将其视为包含 0 个问题的表单。
forms:
restaurant_form:
cuisine:
- type: from_entity
entity: cuisine
num_people:
- type: from_entity
entity: number