一個聰明的Chatbot 必須要能夠理解使用者說的話, 才能給予最精準的反饋, 要打造出這樣的功能, 可以使用諸如以下的線上語意識別服務 Google DialogFlow, Microsoft Luis, 以及Facebook Wit等等
這些語意識別的服務猶如Chatbot的大腦語言中樞, 用來了解每句對話背後的意圖
有了意圖之後, 程式可以根據意圖的種類來回答問題
if intent == 'MAKE_ORDER':
print('okay total price is $100')
elif intent == 'CHECK_SHOPPING_CART':
print('there are milk, beef, ...')
elif intent == 'CHANGE_PYMENT':
print('change complete')
else:
print('hi')
但光是了解自然語言是不夠的, 若要打造出更人性化的Chatbot, 還需要作到對話管理, 上下文理解等等...
Rasa Core
想要作到對話管理, 不妨試試Open Source Rasa Core
功能介紹
Rasa Core可以用神經網路去學習預先定義好的對話邏輯(順序), 學完之後就能根據先前對話的資訊, 來推測Chatbot接下來的最佳反饋應該是什麼
什麼是 Stories, Domain, Action, Policy
Stories
用來定義對話邏輯
一個適當的對話一定會有前後順序, 上下文關係, Story 就是用來描述這種上下文關係的資訊
## happy path <!-- 故事名 - just for debugging -->
* _greet
- utter_greet
* _mood_great <!-- user utterance, in format _intent[entities] -->
- utter_happy
## sad path 1 <!-- this is already the start of the next story -->
* _greet
- utter_greet <!-- action of the bot to execute -->
* _mood_unhappy
- utter_cheer_up
- utter_did_that_help
* _mood_affirm
- utter_happy
## sad path 2
* _greet
- utter_greet
* _mood_unhappy
- utter_cheer_up
- utter_did_that_help
* _mood_deny
- utter_goodbye
## say goodbye
* _goodbye
- utter_goodbye
Domains
用來描述定義在story中每個項目的細節, 包含用到的意圖, slot, action
intents:
- greet
- goodbye
- mood_affirm
- mood_deny
- mood_great
- mood_unhappy
- inform
slots:
group:
type: text
entities:
- group
actions:
- utter_greet
- utter_did_that_help
- utter_happy
- utter_goodbye
- utter_unclear
- utter_ask_picture
- __main__.ApiAction
templates:
utter_greet:
- text: "Hey! How are you?"
utter_did_that_help:
- text: "Did that help you?"
utter_unclear:
- text: "I am not sure what you are aiming for."
utter_happy:
- text: "Great carry on!"
utter_goodbye:
- text: "Bye"
utter_ask_picture:
- text: "To cheer you up, I can show you a cute picture of a dog, cat or a bird. Which one do you choose?"
Actions
Action可分兩種
- Utterance Action: 單純回話
- Custom Action: 可以執行特定動作, 如串接下訂單的API, 透過呼叫指定的URL來作動作, 若沒有特定的REST API 來服務這個動作, 也可以透過Rasa Core 起一個內建的Action server, 在裡面去實作客製化的Action讓 Rasa core去呼叫它
Policy
訓練的時候, 可以設定要使用哪種Policy 來訓練對話模型, 而產出的模型可以用來推測最佳的Action
from rasa_core.policies import FallbackPolicy, KerasPolicy, MemoizationPolicy
from rasa_core.agent import Agent
# this will catch predictions the model isn't very certain about
# there is a threshold for the NLU predictions as well as the action predictions
fallback = FallbackPolicy(fallback_action_name="utter_unclear",
core_threshold=0.2,
nlu_threshold=0.1)
agent = Agent('domain.yml', policies=[MemoizationPolicy(), KerasPolicy(), fallback])
# loading our neatly defined training dialogues
training_data = agent.load_data('stories.md')
agent.train(
training_data,
validation_split=0.0,
epochs=200
)
agent.persist('models/dialogue')
目前預設是Keras Policy, 以LSTM的神經網路去學習對話情境(原始碼如下)
# Build Model
model = Sequential()
model.add(Masking(mask_value=-1,input_shape=(None, input_shape[1])))
model.add(LSTM(self.rnn_size, return_sequences=True, dropout=0.2))
model.add(TimeDistributed(Dense(units=output_shape[-1])))
model.add(Activation('softmax'))
model.compile(loss='categorical_crossentropy',
optimizer='rmsprop',
metrics=['accuracy'])
另外其他種Policy還有MemoizationPolicy, Embedding Policy
留言
張貼留言