Rasa教程系列-Core-2-Domains

Domain定義了機器人助手所處的世界。它指定了機器人應該知道的意圖(intents)、實體(entities)、槽位(slots)和操作(actions)。另外,它還可以包含機器人能夠說的內容的模板(templates)

Domain示例

intents:
  - greet
  - goodbye
  - affirm
  - deny
  - mood_great
  - mood_unhappy
  - bot_challenge

actions:
- utter_greet
- utter_cheer_up
- utter_did_that_help
- utter_happy
- utter_goodbye
- utter_iamabot

templates:
  utter_greet:
  - text: "Hey! How are you?"

  utter_cheer_up:
  - text: "Here is something to cheer you up:"
    image: "https://i.imgur.com/nGF1K8f.jpg"

  utter_did_that_help:
  - text: "Did that help you?"

  utter_happy:
  - text: "Great, carry on!"

  utter_goodbye:
  - text: "Bye"

  utter_iamabot:
  - text: "I am a bot, powered by Rasa."

session_config:
  session_expiration_time: 60
  carry_over_slots_to_new_session: true

NLU 模型定義的intentsentities需要在Domain中包含進來。Slots中的信息是對話過程中需要追蹤的。比如可以定義一個名爲risk_level的分類槽:

slots:
   risk_level:
      type: categorical
      values:
      - low
      - medium
      - high

具體完整的slot 類型表及其在domain文件中的語法可以參考這裏
Actions 是機器人實際上能夠做的,一個action可以是:

  • 回覆用戶
  • 調用外部API
  • 查數據庫等其他任何事情

自定義Actions 和Slots

要引用Domain中的slots,需要通過它們的模塊路徑。引用自定義actions時,可以通過它們的name。例如,如果有一個名爲my_actions的模塊,其中包含類MyAwesomeAction,而模塊my_slot中包含MyAwesomeSlot,則需要在domain文件中添加下面幾行:

actions:
  - my_custom_action
  ...

slots:
  - my_slots.MyAwesomeSlot

上述例子中類MyAwesomeActionname函數需要返回my_custom_action。更多關於自定義Actions可以參考這裏

Utterance 模板

Utterance 模板是機器人發送給用戶的信息,有兩個方式使用這些模板:
(1)如果是以utter_爲前綴的模板,utterance 可以直接作爲action。將 utterance 模板添加到 domain 如下:

templates:
  utter_greet:
  - text: "Hey! How are you?"

之後,可以在story中使用模板作爲一個action:

## greet the user
* intent_greet
  - utter_greet

utter_greet直接作爲一個action,它將會把模板中的信息發送給用戶。

(2)也可以使用模板從自定義actions中生成響應信息,具體是使用dispatcher: dispatch .utter_message(template="utter_greet")。這將生成消息的邏輯與實際的複製分開。在自定義action代碼中,可以發送一個基於模板的消息如下:

from rasa_sdk.actions import Action

class ActionGreet(Action):
  def name(self):
      return 'action_greet'

  def run(self, dispatcher, tracker, domain):
      dispatcher.utter_message(template="utter_greet")
      return []

Images and Buttons

在domain配置文件中的模板可以包含images和buttons:

templates:
  utter_greet:
  - text: "Hey! How are you?"
    buttons:
    - title: "great"
      payload: "great"
    - title: "super sad"
      payload: "super sad"
  utter_cheer_up:
  - text: "Here is something to cheer you up:"
    image: "https://i.imgur.com/nGF1K8f.jpg"

注意:
請記住,如何顯示所定義的按鈕取決於輸出通道的實現。例如,命令行不能顯示按鈕或圖像,但嘗試通過打印選項來模擬它們。

Custom Output Payloads(自定義輸出負載)

還可以使用custom: key將任意輸出發送到輸出通道。注意,由於domain是yaml格式,因此json有效負載(payload )應該首先轉換爲yaml格式。
例如,儘管日期選擇器(date pickers)不是話語模板(utterance templates)中定義的參數,因爲它們不被大多數通道支持,一個Slack日期選擇器可以這樣發送:

templates:
  utter_take_bet:
  - custom:
      blocks:
      - type: section
        text:
          text: "Make a bet on when the world will end:"
          type: mrkdwn
        accessory:
          type: datepicker
          initial_date: '2019-05-21'
          placeholder:
            type: plain_text
            text: Select a date

Channel-Specific(特定通道)Utterances

如果希望僅將某些話語(Utterances)發送到特定的通道,則可以使用channel:key指定。其值應該與在通道的OutputChannel類的name()方法中定義的名稱匹配。在創建只在特定通道中工作的自定義輸出負載(custom output payloads)時,特定於通道的話語特別有用。具體示例如下:

templates:
  utter_ask_game:
  - text: "Which game would you like to play?"
    channel: "slack"
    custom:
      - # payload for Slack dropdown menu to choose a game
  - text: "Which game would you like to play?"
    buttons:
    - title: "Chess"
      payload: '/inform{"game": "chess"}'
    - title: "Checkers"
      payload: '/inform{"game": "checkers"}'
    - title: "Fortnite"
      payload: '/inform{"game": "fortnite"}'

每次機器人尋找utterances,它將首先檢查是否有特定通道的模板連接的通道。如果有,它只會從這些話語中選擇。如果沒有找到特定通道的模板,它將從沒有定義通道的任何話語中進行選擇。因此,對於每個沒有指定通道的話語,最好總是至少有一個模板,以便機器人能夠在所有環境中響應,包括在shell和交互式學習中。

Variables

還可以使用模板中的變量來插入對話期間收集的信息。可以在自定義python代碼中實現這一點,也可以使用自動槽位填充機制。例如,如果有這樣一個模板:

templates:
  utter_greet:
  - text: "Hey, {name}. How are you?"

Rasa將自動使用在名爲name的槽中找到的值填充該變量。

在自定義代碼中,可以使用以下方法檢索模板:

class ActionCustom(Action):
   def name(self):
      return "action_custom"

   def run(self, dispatcher, tracker, domain):
      # send utter default template to user
      dispatcher.utter_message(template="utter_default")
      # ... other code
      return []

如果模板包含用{my_variable}表示的變量,那麼可以通過將它們作爲關鍵字參數傳遞給utter_message來爲字段提供值:

dispatcher.utter_message(template="utter_greet", my_variable="my text")

Variations

如果想隨機改變發送給用戶的響應,則可以列出多個響應,Rasa會隨機選擇其中一個,例如:

templates:
  utter_greeting:
  - text: "Hey, {name}. How are you?"
  - text: "Hey, {name}. How is your day going?"

Ignoring entities for certain intents

如果想要所有的實體在特定的意圖下被忽略,則可以在domain文件中將use_entities:[]參數添加到意圖:

intents:
  - greet:
      use_entities: []

要忽略某些實體或顯式地只考慮某些實體,可以使用以下語法:

intents:
- greet:
    use_entities:
      - name
      - first_name
    ignore_entities:
      - location
      - age

這意味着對於這些意圖,那些被排除在外的實體將是unfeaturized的,因此不會影響下一個動作的預測。當意圖是一個不關心被挑選的實體時,這是很有用的。如果將意圖作爲正常的列表而不使用此參數,實體將被作爲正常的特性。
注意:
如果你真的想要這些實體不影響action的預測,官方建議通過將槽類型設置爲unfeaturized

Session configuration

會話會話代表助手和用戶之間的對話。會話可以以三種方式開始:
(1)用戶主動與助手對話
(2)用戶在一段可配置的靜止期(inactivity)之後發送第一個消息
(3)使用/session_start意圖消息觸發手動會話

可以在session_config鍵下定義period of inactivitysession_expiration_time定義time of inactivity(以分鐘爲單位),在此之後將開始一個新會話。carry_over_slots_to_new_session確定是否應該將現有的槽位設置轉移到新會話。默認的session configuration如下所示:

session_config:
  session_expiration_time: 60  # value in minutes, 0 means infinitely long
  carry_over_slots_to_new_session: true  # set to false to forget slots between sessions

上述配置意味着,如果用戶在60分鐘不活動之後發送第一個消息,就會觸發一個新的會話,任何現有的槽位設置都將被轉移到新會話中。如果將session_expiration_time的值設置爲0意味着會話不會結束(注意,action_session_start操作仍然會在對話剛開始時被觸發)
注意:
會話啓動觸發默認動作action_session_start。它的默認實現將所有現有槽位設置移動到新會話中。注意,所有的對話都以action_session_start開始。例如,覆蓋此操作可用於使用外部API調用的槽位初始化tracker ,或使用bot消息啓動對話。更多的細節可以查閱自定義會話啓動操作

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章