如何从头开始用rasa根据自己的任务设计一个任务型对话机器人

以订电影票为例子。

我们首先要列出所有intent的名称,rasa的intent的粒度较细,任何用户输入都可以被看做一个intent。因此我们需要枚举出在一个场景下所有可能的用户输入的意图。如我要订电影票的intent是订电影票,我要看钢铁侠的intent是报告电影名,我要两张票的intent是报告票数

 

用户输入                                      intent意图

我要订电影票                              订电影票

我要看钢铁侠                              报告电影名字

我要两张票                                   报告票数

我要明天下午2点的票                 报告时间

对应的实际数据为:


## intent:inform_movie_name
- [Xman1](movie_name)
- I want to see [Xman1](movie_name)
- [Xman2](movie_name)
- I want to see movie [Xman2](movie_name)
- [Xman3](movie_name) please
- book movie [Xman3](movie_name)
- [Xman4](movie_name) plz
- I want to see [Xman4](movie_name)

## intent:book_movie_name
- I want to see [Xman1](movie_name)
- I want to book  [Xman1](movie_name)
- help me book  [Xman1](movie_name)
- help me book  [Xman2](movie_name)
- help me book  [Xman3](movie_name)
- help me book  [Xman4](movie_name)
- [Iron Man](movie_name)
- book movie  [Iron Man](movie_name) 
- [The Incredible Hulk](movie_name)
- book movie  [The Incredible Hulk](movie_name) 
- [Iron Man 2](movie_name)
- book movie  [Iron Man 2](movie_name) 
- [Thor](movie_name)
- book movie  [Thor](movie_name) 
- [Captain America](movie_name)
- book movie  [Captain America](movie_name) 
- [The Avengers](movie_name)
- book movie  [The Avengers](movie_name) 
- [Iron Man 3](movie_name)
- book movie  [Iron Man 3](movie_name) 
- [Thor:The Dark World](movie_name)
- book movie  [Thor:The Dark World](movie_name) 

## intent:inform_weekday
- I want [monday](weekday)
- [monday](weekday)
- It is   [monday](weekday)
- [wednesday](weekday)
- It is   [wednesday](weekday)
- [thursday](weekday)
- It is   [thursday](weekday)
- [friday](weekday)
- It is   [friday](weekday)
- [today](weekday)
- It is   [today](weekday)
- [tomorrow](weekday)
- It is   [tomorrow](weekday)
- [tonight](weekday)
- It is   [tonight](weekday)
- [this morning](weekday)
- It is   [this morning](weekday)
- [this noon](weekday)
- It is   [this noon](weekday)

## intent:inform_ticket_number
- book [two](ticket_number) tickets
- for [two](ticket_number) people
- [one](ticket_number) people
- [one](ticket_number)
- I want   [one](ticket_number) tickets
- [two](ticket_number)
- I want   [two](ticket_number) tickets
- [three](ticket_number)
- I want   [three](ticket_number) tickets
- [1](ticket_number)
- I want   [1](ticket_number) tickets
- [2](ticket_number)
- I want   [2](ticket_number) tickets
- [3](ticket_number)
- I want   [3](ticket_number) tickets
- [single](ticket_number)
- I want   [single](ticket_number) tickets


## intent:inform_time
- [9 am](time)
- book movie at  [9 am](time) 
- [afternoon](time)
- book movie at  [afternoon](time) 
- [3pm](time)
- book movie at  [3pm](time) 
- [night](time)
- book movie at  [night](time) 
- [4pm](time)
- book movie at  [4pm](time) 
- [afternoon](time)
- book movie at  [afternoon](time) 
intents:
- greet
- goodbye
- thanks
- deny
- joke
- name
- inform_item
- inform_package
- inform_time
- request_management
- request_search
- inform_current_phone
- inform_other_phone
- book_movie_name
- inform_time
- inform_weekday
- inform_ticket_number
- inform_movie_name

接着列出所有action的名称。action是rasa中bot根据用户输入做出的动作,回复。在任务型场景下,bot大多数action就是询问信息(槽位,实体)。

 

模板、自定义函数 action动作

你要做什么? 问意图

你要看什么电影 问电影名字

你要几张票 问票数

你要什么时间的 问时间

函数 完成订票


actions:
- utter_name
- utter_thanks
- utter_greet
- utter_goodbye
- action_joke
- utter_ask_morehelp
- utter_ask_package
- utter_ack_management
- action_searchconsume
- utter_help
- utter_ask_month
- utter_givevolumn
- utter_ask_ticket_number
- utter_ask_weekday
- utter_ask_time
- utter_ask_movie_name
- utter_book_ticket
- action_book_ticket
templates:
  utter_name:
  - text: "Hey there! Tell me your name."

  utter_greet:
  - text: "Nice to you meet you {name}. How can I help?"

  utter_goodbye:
  - text: "Talk to you later!"
  
  utter_thanks:
  - text: "My pleasure."

  utter_help:
  - text: " Hi,How can I help?"


  utter_givevolumn:
  - text: "The volume used  in {time} is 200mb/1024mb"

  utter_ask_ticket_number:
  - text: "Can you tell me the ticket_number?"

  utter_ask_weekday:
  - text: "Can you tell me the weekday?"

  utter_ask_time:
  - text: "Can you tell me the time?"

  utter_ask_movie_name:
  - text: "Can you tell me the movie_name?"

  utter_book_ticket:
  - text: "I finish booking of {movie_name} at{time} {weekday}"

然后定义slot 的名称,类型。entity的名称,类型。slot原则上应该是entity的子集,sara的nlu模块提取出entity然后tracker决定需不需要填入slot。

entities:
- name
- item
- time
- phone_number
- price
- movie_name
- ticket_number
- weekday

slots:
  name:
    type: text
  item:
    type: text
  time:
    type: text
  phone_number:
    type: text
  price:
    type: text
  ticket_number:
    type: text
  weekday:
    type: text
  movie_name:
    type: text

上面的数据都填入domain.yml中。

然后在story.md中输入任务流程,格式如下

## 订电影票

* 订电影票

- 问电影名字

* 报告电影名字

- 问票数

* 报告票数

- 问时间

* 报告时间

- 完成订票

 

或者加入不同的流程

## 问意图 + 订电影票

* 问好

- 问意图

* 订电影票

- 问电影名字

* 报告电影名字

- 问票数

* 报告票数

- 问时间

* 报告时间

- 完成订票

## story_book_ticket
 * greet
  - utter_help
 * book_movie_name
  - utter_ask_time
 * inform_time
  - utter_ask_weekday
 * inform_weekday
  - utter_ask_ticket_number  
 * inform_ticket_number
  - utter_ask_movie_name 
 * inform_movie_name
  - utter_book_ticket 

## story_book_ticket2
 * book_movie_name
  - action_book_ticket
 * inform_ticket_number
  - action_book_ticket
 * inform_weekday
  - action_book_ticket  
 * inform_time
  - action_book_ticket 

根据这两个文件可以训练RASA core模型,用于决定action。

train-core:
	python -m rasa_core.train -d domain.yml -s data/stories.md -o models/current/dialogue -c policies.yml

 

然后准备intent语料文件 nlu.md

里面放入每个intent对应的文本,并且标明entity和entity名称。

 

## intent:订电影票

-我想看 [Xman1](movie_name)

- 我想看 [Xman1](movie_name)

- 帮我订 [Xman1](movie_name)

- 帮我订 [Xman2](movie_name)

 

用nlu.md来训练nlu模型。

train-nlu:
	python -m rasa_nlu.train -c nlu_config.yml --data data/nlu_data.md -o models --fixed_model_name nlu --project current --verbose

上一篇:java 枚举2


下一篇:nodejs操作mongodb之三(关于schema的使用)