Slide 1

Slide 1 text

Line Bot with Rasa Open Source Cheng-Lung Sung (clsung@)

Slide 2

Slide 2 text

Outline • Conversational (AI) Service • Natural Language Understanding (NLU) • Rasa Introduction • Rasa Open Source • Rasa X • Chat bot example: line/line-bot-sdk-python • Weather • Some thoughts on chatbot development…

Slide 3

Slide 3 text

About.Me/clsung • AIRD Department, DRD Division, CTBC Bank • Computational Linguistics • Computer Vision • Data Governance • Director of Product Development, HTC Health Care • Open Source contributor google://中國信託 AI

Slide 4

Slide 4 text

疾管署推出LINE@聊天機器⼈ 流感疫苗問答即時搞定 Chatbots Meetup-010

Slide 5

Slide 5 text

Research Development Operation

Slide 6

Slide 6 text

Conversational (AI) Service Ready Player One: Curator

Slide 7

Slide 7 text

我們希望使⽤者照著說明書⾛ examples/flask-kitchensink @handler.add(MessageEvent, message=TextMessage) def handle_text_message(event): text = event.message.text if text == 'profile': if isinstance(event.source, SourceUser): profile = line_bot_api.get_profile(event.source.user_id) line_bot_api.reply_message( event.reply_token, [ TextSendMessage(text='Display name: ' + profile.display_name), TextSendMessage(text='Status message: ' + str(profile.status_message)) ] )

Slide 8

Slide 8 text

⽂不對題

Slide 9

Slide 9 text

語境狀態錯亂

Slide 10

Slide 10 text

Conversational Service https://blog.rasa.com/conversational-ai-your-guide-to-five-levels-of-ai-assistants-in-enterprise/ • 過往: 備忘錄⼩秘書 • ⽬前: FAQ / 情境式 助理 • ⽬標: 個⼈化助理 • 未來: 助理們的助理

Slide 11

Slide 11 text

Conversational Service AI NLP

Slide 12

Slide 12 text

Conversational Service Interactive Learning of Task-Oriented Dialog Systems Dialog Management

Slide 13

Slide 13 text

⼀問⼀答、⼀來⼀往 https://towardsdatascience.com/architecture-overview-of-a-conversational-ai-chat-bot-4ef3dfefd52e

Slide 14

Slide 14 text

https://www.cellstrat.com/2017/10/27/nlp-vs-nlu-vs-nlg/

Slide 15

Slide 15 text

NLP in Conversational Service NLP NLU DM NLG Dialog Management ✍

Slide 16

Slide 16 text

NLU is hard 今天只要知道 NLU 很難就夠了 部的英⽂是什麼?

Slide 17

Slide 17 text

Stanford CoreNLP

Slide 18

Slide 18 text

Google Natural Language API

Slide 19

Slide 19 text

NLU NLU in Conversational Service • Intent Classification • Entity Extraction 請問明天台北的天氣? intent: - ask_weather entities: - location: 台北 - time: 明天

Slide 20

Slide 20 text

No content

Slide 21

Slide 21 text

Chatbot Architecture (Full) Azure Bot Framework

Slide 22

Slide 22 text

Cloud APIs

Slide 23

Slide 23 text

On-premise

Slide 24

Slide 24 text

On-prem Cloud Cloud or On-premise?

Slide 25

Slide 25 text

Rasa architecture DM NLU NLG

Slide 26

Slide 26 text

Dialogue Elements NLU NLG

Slide 27

Slide 27 text

No content

Slide 28

Slide 28 text

$ rasa init Welcome to Rasa! 烙 To get started quickly, an initial project will be created. If you need some help, check out the documentation at https://rasa.com/docs/rasa. Now let's start! ? Please enter a path where the project will be created [default: current directory] . Created project directory at '/git/nlpbot'. Finished creating project structure. ? Do you want to train an initial model? Yes Training an initial model... Training Core model... Processed Story Blocks: 100%|███████████████████████████████████████████████████████| 5/5 [00:00<00:00, 3953.16it/s, # trackers=1] Processed Story Blocks: 100%|███████████████████████████████████████████████████████| 5/5 [00:00<00:00, 1734.47it/s, # trackers=5] Processed Story Blocks: 100%|███████████████████████████████████████████████████████| 5/5 [00:00<00:00, 442.75it/s, # trackers=20] Processed Story Blocks: 100%|███████████████████████████████████████████████████████| 5/5 [00:00<00:00, 313.09it/s, # trackers=24] Processed trackers: 100%|███████████████████████████████████████████████████████████| 5/5 [00:00<00:00, 1920.82it/s, # actions=16] Processed actions: 16it [00:00, 12336.19it/s, # examples=16] Processed trackers: 100%|███████████████████████████████████████████████████████| 231/231 [00:00<00:00, 733.18it/s, # actions=126] Epochs: 100%|██████████████████████████████████████████████| 100/100 [00:06<00:00, 14.29it/s, t_loss=0.074, loss=0.001, acc=1.000] 2020-07-21 13:18:06 INFO rasa.utils.tensorflow.models - Finished training. 2020-07-21 13:18:06 INFO rasa.core.agent - Persisted model to '/var/folders/tq/T/tmphttlwy6_/core' Core model training completed. Training NLU model... 2020-07-21 13:18:06 INFO rasa.nlu.training_data.training_data - Training data stats: 2020-07-21 13:18:06 INFO rasa.nlu.training_data.training_data - Number of intent examples: 43 (7 distinct intents) 2020-07-21 13:18:06 INFO rasa.nlu.training_data.training_data - Found intents: 'goodbye', 'affirm', 'deny', 'mood_unhappy', 'mood_great', 'bot_challenge', 'greet' 2020-07-21 13:18:06 INFO rasa.nlu.training_data.training_data - Number of response examples: 0 (0 distinct responses) 2020-07-21 13:18:06 INFO rasa.nlu.training_data.training_data - Number of entity examples: 0 (0 distinct entities) 2020-07-21 13:18:06 INFO rasa.nlu.model - Starting to train component WhitespaceTokenizer 2020-07-21 13:18:06 INFO rasa.nlu.model - Finished training component. 2020-07-21 13:18:06 INFO rasa.nlu.model - Starting to train component RegexFeaturizer 2020-07-21 13:18:06 INFO rasa.nlu.model - Finished training component. 2020-07-21 13:18:06 INFO rasa.nlu.model - Starting to train component LexicalSyntacticFeaturizer 2020-07-21 13:18:06 INFO rasa.nlu.model - Finished training component. 2020-07-21 13:18:06 INFO rasa.nlu.model - Starting to train component CountVectorsFeaturizer 2020-07-21 13:18:06 INFO rasa.nlu.model - Finished training component. 2020-07-21 13:18:06 INFO rasa.nlu.model - Starting to train component CountVectorsFeaturizer 2020-07-21 13:18:06 INFO rasa.nlu.model - Finished training component. 2020-07-21 13:18:06 INFO rasa.nlu.model - Starting to train component DIETClassifier Epochs: 100%|██████████████████████████████████████████| 100/100 [00:07<00:00, 14.26it/s, t_loss=1.466, i_loss=0.083, i_acc=1.000] 2020-07-21 13:18:18 INFO rasa.utils.tensorflow.models - Finished training. 2020-07-21 13:18:18 INFO rasa.nlu.model - Finished training component. 2020-07-21 13:18:18 INFO rasa.nlu.model - Starting to train component EntitySynonymMapper 2020-07-21 13:18:18 INFO rasa.nlu.model - Finished training component. 2020-07-21 13:18:18 INFO rasa.nlu.model - Starting to train component ResponseSelector 2020-07-21 13:18:18 INFO rasa.nlu.selectors.response_selector - Retrieval intent parameter was left to its default value. This response selector will be trained on training examples combining all retrieval intents. 2020-07-21 13:18:18 INFO rasa.nlu.model - Finished training component. 2020-07-21 13:18:18 INFO rasa.nlu.model - Successfully saved model into '/var/folders/tq/T/tmphttlwy6_/nlu' NLU model training completed. Your Rasa model is trained and saved at '/git/nlpbot/models/20200721-131751.tar.gz'.

Slide 29

Slide 29 text

$ rasa init

Slide 30

Slide 30 text

data/nlu.md ## intent:greet - hey - hello - hi - good morning - good evening - hey there ## intent:goodbye - bye - goodbye - see you around - see you later ## intent:affirm - yes - indeed - of course - that sounds good - correct

Slide 31

Slide 31 text

domain.yml intents: - greet - goodbye - affirm - deny - mood_great - mood_unhappy - bot_challenge responses: utter_greet: - text: "Hey! How are you?" utter_cheer_up: - text: "Here is something to cheer y image: "https://i.imgur.com/nGF1K8f utter_did_that_help: - text: "Did that help you?" utter_happy: - text: "Great, carry on!"

Slide 32

Slide 32 text

data/stories.md ## happy path * greet - utter_greet * mood_great - utter_happy ## sad path 1 * greet - utter_greet * mood_unhappy - utter_cheer_up - utter_did_that_help * affirm - utter_happy ## say goodbye * goodbye - utter_goodbye

Slide 33

Slide 33 text

credentials.yml rest: # # you don't need to provide anything here - this channel doesn't # # require any credentials #facebook: # verify: "" # secret: "" # page-access-token: "" #slack: # slack_token: "" # slack_channel: "" #socketio: # user_message_evt: # bot_message_evt: # session_persistence: # This entry is needed if you are using Rasa X. The entry represents credentials # for the Rasa X "channel", i.e. Talk to your bot and Share with guest testers. rasa: url: "http://localhost:5002/api"

Slide 34

Slide 34 text

config.yml # Configuration for Rasa NLU. # https://rasa.com/docs/rasa/nlu/components/ language: en pipeline: - name: WhitespaceTokenizer - name: RegexFeaturizer - name: LexicalSyntacticFeaturizer - name: CountVectorsFeaturizer - name: CountVectorsFeaturizer analyzer: "char_wb" min_ngram: 1 max_ngram: 4 - name: DIETClassifier epochs: 100 - name: EntitySynonymMapper - name: ResponseSelector epochs: 100 # Configuration for Rasa Core. # https://rasa.com/docs/rasa/core/policies/ policies: - name: MemoizationPolicy - name: TEDPolicy max_history: 5 epochs: 100 - name: MappingPolicy Core

Slide 35

Slide 35 text

language: en!!!

Slide 36

Slide 36 text

language: zh Jieba Articut CKIPTagger Monpa

Slide 37

Slide 37 text

–⼤理天⿓寺 本因⽅丈 “指法無優劣,功⼒有⾼下”

Slide 38

Slide 38 text

language: zh pipeline: - name: JiebaTokenizer dictionary_path: "data/jieba" ## intent:ask_weather - 請問[今天](time)[台北](location)的天氣 - 請問[後天](time)[南投](location)的天氣 - 你知道[明天](time)[宜蘭](location)天氣情況嗎 - 我想問[台南](location)[後天](time)的天氣

Slide 39

Slide 39 text

Rasa X

Slide 40

Slide 40 text

Rasa Action 2020-XX-YY 20:22:09 ERROR rasa.core.actions.action - The model predicted the custom action 'action_location_weather', but you didn't configure an endpoint to run this custom action. Please take a look at the docs and set an endpoint configuration via the --endpoints flag. https://rasa.com/docs/rasa/core/actions 2020-XX-YY 20:22:09 ERROR rasa.core.processor - Encountered an exception while running action 'action_location_weather'. Bot will continue, but the actions events are lost. Please check the logs of your action server for more information. w/o Action Server $ rasa run actions

Slide 41

Slide 41 text

Customized actions (actions.py) class ActionLocationWeather(Action): def name(self) -> Text: return "action_location_weather" def run(self, dispatcher: CollectingDispatcher, tracker: Tracker, domain: Dict[Text, Any]) -> List[Dict[Text, Any]]: loc = tracker.get_slot('location') when = tracker.get_slot('time') response = """你要問的是 {} {} 的天氣~""".format(when, loc) dispatcher.utter_message(text=response) return []

Slide 42

Slide 42 text

⼆種使⽤ Rasa 的⽅法 • Webhook API (channel, rest…) • Agent from rasa.core.agent import Agent import requests

Slide 43

Slide 43 text

With Agent import asyncio from rasa.core.agent import Agent from rasa.core.interpreter import RasaNLUInterpreter from rasa.core.utils import EndpointConfig nlu_interpreter = RasaNLUInterpreter('models/nlu-20200721-200450.tar.gz') action_endpoint = EndpointConfig(url="http://localhost:5055/webhook") agent = Agent.load( './models/20200721-220524.tar.gz', interpreter=nlu_interpreter, action_endpoint=action_endpoint) @handler.add(MessageEvent, message=TextMessage) def message_text(event): sender = event.source.user_id responses = asyncio.run(agent.handle_message( event.message.text, sender_id=sender)) nlu = asyncio.run(agent.parse_message_using_nlu_interpreter(event.message.text)) line_bot_api.reply_message( event.reply_token, [TextSendMessage(text=responses[0]['text']), TextSendMessage(text=str(nlu))], )

Slide 44

Slide 44 text

With API import requests class RasaClient(object): def __init__(self, url): self._base_url = url self._sess = requests.Session() def _post_rasa(self, path, data): url = "{}/{}".format(self._base_url, path) resp = self._sess.post(url, json=data) resp.raise_for_status() # catch this! return resp.json() def post_nlu(self, message): data = {"text": message} return self._post_rasa("model/parse", data) def post_action(self, message, sender="default"): data = {"message": message, "sender": sender} return self._post_rasa("webhooks/rest/webhook", data) @handler.add(MessageEvent, message=TextMessage) def message_text(event): rasa = RasaClient('http://localhost:5005') sender = event.source.user_id responses = rasa.post_action(event.message.text) nlu = rasa.post_nlu(event.message.text) line_bot_api.reply_message( event.reply_token, [TextSendMessage(text=responses[0]['text']), TextSendMessage(text=str(nlu))], )

Slide 45

Slide 45 text

rasa train [nlu] rasa run --enable-api rasa run actions pip install line-bot-sdk python app.py

Slide 46

Slide 46 text

開發對話式服務的⼀點想法 https://blog.rasa.com/conversational-ai-your-guide-to-five-levels-of-ai-assistants-in-enterprise/ • 過往: 備忘錄⼩秘書 • ⽬前: FAQ / 情境式 助理 • ⽬標: 個⼈化助理 • 未來: 助理們的助理

Slide 47

Slide 47 text

Core ?

Slide 48

Slide 48 text

Thank you!