如果希望助理響應用戶消息,則需要管理這些響應。在機器人的訓練數據中,通過stories指定機器人應該執行的操作。這些操作可以使用utterances將消息發送回用戶。
有三種方法來管理這些utterances:
(1)Utterances 存於Domain文件中,看這裏
(2)檢索動作響應是訓練數據的一部分,看這裏
(3)可以創建自定義NLG service 來生成響應, 看這裏
utterances 包含於 Domain文件中
默認格式是將utterances包含在domain文件中。然後,該文件包含對所有 自定義action、可用實體、槽位和意圖的引用。具體示例如下:
# all hashtags are comments :)
intents:
- greet
- default
- goodbye
- affirm
- thank_you
- change_bank_details
- simple
- hello
- why
- next_intent
entities:
- name
slots:
name:
type: text
templates:
utter_greet:
- text: "hey there {name}!" # {name} will be filled by slot (same name) or by custom action
utter_channel:
- text: "this is a default channel"
- text: "you're talking to me on slack!" # if you define channel-specific utterances, the bot will pick
channel: "slack" # from those when talking on that specific channel
utter_goodbye:
- text: "goodbye 😢" # multiple templates - bot will randomly pick one of them
- text: "bye bye 😢"
utter_default: # utterance sent by action_default_fallback
- text: "sorry, I didn't get that, can you rephrase it?"
actions:
- utter_default
- utter_greet
- utter_goodbye
上述domain文件中,templates
部分包含了助理機器人用於發送給用戶的template。如果想更改text或bots響應的任何其他部分,需要對assistant重新訓練才能生效。
注意:
在一個story中使用的utterances
應該列在domain.yml
中的stories部分。在本例中,utter_channel
並沒有在story中使用,因此不在該部分中列出。關於這些響應格式的更多細節可以在domain文件格式的文檔中找到:Utterance templates
爲機器人助手創建NLG service
對機器人進行再訓練以更改文本副本對於某些工作流來說可能不是最優的。這就是爲什麼Core也允許外包響應生成並將其從對話學習中分離出來。
助手仍將根據歷史的對話學習預測action和對用戶輸入作出反應,但它發送回用戶的響應是在Rasa Core之外生成的。
如果助理希望向用戶發送消息,它將使用POST請求調用外部HTTP server。 配置此endpoint,需要先創建一個endpoints.yml
,並將其傳遞給運行腳本或服務器腳本。endpoints.yml
的內容如下:
nlg:
url: http://localhost:5055/nlg # url of the nlg endpoint
# you can also specify additional parameters, if you need them:
# headers:
# my-custom-header: value
# token: "my_authentication_token" # will be passed as a get parameter
# basic_auth:
# username: user
# password: pass
# example of redis external tracker store config
tracker_store:
type: redis
url: localhost
port: 6379
db: 0
password: password
record_exp: 30000
# example of mongoDB external tracker store config
#tracker_store:
#type: mongod
#url: mongodb://localhost:27017
#db: rasa
#user: username
#password: password
按照如下方式啓動server:
rasa run \
--enable-api \
-m examples/babi/models \
--log-file out.log \
--endpoints endpoints.yml
以POST方式向endpoint發送請求的body數據如下:
{
"tracker": {
"latest_message": {
"text": "/greet",
"intent_ranking": [
{
"confidence": 1.0,
"name": "greet"
}
],
"intent": {
"confidence": 1.0,
"name": "greet"
},
"entities": []
},
"sender_id": "22ae96a6-85cd-11e8-b1c3-f40f241f6547",
"paused": false,
"latest_event_time": 1531397673.293572,
"slots": {
"name": null
},
"events": [
{
"timestamp": 1531397673.291998,
"event": "action",
"name": "action_listen"
},
{
"timestamp": 1531397673.293572,
"parse_data": {
"text": "/greet",
"intent_ranking": [
{
"confidence": 1.0,
"name": "greet"
}
],
"intent": {
"confidence": 1.0,
"name": "greet"
},
"entities": []
},
"event": "user",
"text": "/greet"
}
]
},
"arguments": {},
"template": "utter_greet",
"channel": {
"name": "collector"
}
}
該endpoint
用以下生成的內容進行響應:
{
"text": "hey there",
"buttons": [],
"image": null,
"elements": [],
"attachments": []
}
然後,Rasa將使用此響應並將其發送回用戶