官方给的填表单样例是这样婶儿的
https://rasa.com/docs/rasa/playground
具体逻辑:
1.forms文件中或domain文件中
定义表单,以及表单中要填的槽位(可以定义多个槽位,rasa会按照需要的槽值顺序执行一遍。eg.name email)
forms:
newsletter_form:
required_slots:
name:
- type: from_text
email:
- type: from_text
2.激活表单和停止表单
方法一:rules文件中:
rules:
- rule: activate subscribe form
steps:
- intent: subscribe
- action: newsletter_form #将表单的名字看作一个action动作,表示要开始向表单中的槽添加数据
- active_loop: newsletter_form #active_loop表示激活表单
- rule: submit form
condition:
- active_loop: newsletter_form
steps:
- action: newsletter_form
- active_loop: null #null表示停止表单
- action: utter_subscribed #表单填写结束后的下一步动作
用rules分开写激活和停止表单逻辑的好处是,可以在激活表单之后进行额外的逻辑
方法二:stories文件中:
stories:
- story: greet and subscribe
steps:
- intent: greet
- action: utter_greet
- intent: subscribe
- action: newsletter_form
- active_loop: newsletter_form
- active_loop: null (比教程多添加的部分)
- action: utter_subscribed (比教程多添加的部分)
当我们必须要在填槽之前多明确几步意图和动作以便解决冲突问题时,激活表单的动作不能写在rules里面,可以把激活和结束表单的动作结合起来写进stories里
3.如果用户输入的内容符合- intent: subscribe中的语句时,表示用户当前意图为订阅:
nlu文件中:
nlu:
- intent: greet
examples: |
- Hi
- Hey!
- Hallo
- Good day
- Good morning
- intent: subscribe
examples: |
- I want to get the newsletter
- Can you send me the newsletter?
- Can you sign me up for the newsletter?
4.接着启动表单
而在启动表单即 - active_loop: newsletter_form
时,rasa默认调用responses中的 utter_ask_表单中的槽名称:来输出一个提示用户输入的语句(或者action文件中的action_ask_query_1:)
domain文件中:
responses:
utter_greet:
- text: |
Hello! How can I help you?
- text: |
Hi!
utter_ask_email:
- text: |
What is your email address?
utter_subscribed:
- text: |
Check your inbox at {email} in order to finish subscribing to the newsletter!
- text: |
You're all set! Check your inbox at {email} to confirm your subscription.
class action_ask_query_1(Action):
def name(self) -> Text:
return "action_ask_query_1"
async def run(
self,
dispatcher: CollectingDispatcher,
tracker: Tracker,
domain: Dict[Text, Any],
) -> List[Dict[Text, Any]]:
dispatcher.utter_message(text='请问您要咨询什么问题?')
print(tracker.current_state()['events'])
return []