上手實操!使用Facebook Messenger構建聊天機器人,只用了不到60分鐘

{"type":"doc","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"italic"},{"type":"size","attrs":{"size":10}},{"type":"strong"}],"text":"本文最初發表於 Towards Data Science 博客,經原作者 Mandy Gu 授權,InfoQ 中文站翻譯並分享。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https:\/\/rasa.com\/?fileGuid=EoE2UeNK8n0kAwJZ","title":"","type":null},"content":[{"type":"text","text":"Rasa"}]},{"type":"text","text":"是一種開源的對話式人工智能框架,它使用機器學習來構建聊天機器人和人工智能助手。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"今天,我將向大家展示如何使用 Rasa 構建自己的簡單聊天機器人,並將其作爲機器人部署到 Facebook messenger 上,一小時之內就可以完成。而你所需要的,只是一些簡單的 Python 編程和可用的 Internet 連接。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"完整的代碼可以在我的"},{"type":"link","attrs":{"href":"https:\/\/github.com\/happilyeverafter95\/demo-bot?fileGuid=EoE2UeNK8n0kAwJZ","title":"","type":null},"content":[{"type":"text","text":"GitHub "}]},{"type":"text","marks":[{"type":"strong"}],"text":"倉庫找到。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"我的代碼是在 Python 3.7 中開發和測試的。Rasa 目前只支持 Python 3.8 以下的版本 ("},{"type":"link","attrs":{"href":"https:\/\/rasa.com\/docs\/rasa\/installation\/?fileGuid=EoE2UeNK8n0kAwJZ","title":"","type":null},"content":[{"type":"text","text":"更新"}]},{"type":"text","text":"請看這裏:"},{"type":"link","attrs":{"href":"https:\/\/rasa.com\/docs\/rasa\/installation\/?fileGuid=EoE2UeNK8n0kAwJZ","title":"","type":null},"content":[{"type":"text","text":"https:\/\/rasa.com\/docs\/rasa\/installation\/"}]},{"type":"text","text":")。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"要構建的機器人非常簡單,無需深入研究任何高級自然語言處理應用。不過,Rasa 確實提供了對更復雜應用的充分支持。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"爲了瞭解更多關於自然語言處理的知識,我推薦 DeepLearning.AI 開設的這門 Coursera 課程《"},{"type":"link","attrs":{"href":"https:\/\/www.coursera.org\/learn\/sequence-models-in-nlp?fileGuid=EoE2UeNK8n0kAwJZ","title":"","type":null},"content":[{"type":"text","text":"基於序列模型的自然語言處理"}]},{"type":"text","text":"》("},{"type":"text","marks":[{"type":"italic"}],"text":"Natural Language Processing with Sequence Models"},{"type":"text","text":")。"}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"讓我們開始吧"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"“克隆”我的"},{"type":"link","attrs":{"href":"https:\/\/github.com\/happilyeverafter95\/demo-bot?fileGuid=EoE2UeNK8n0kAwJZ","title":"","type":null},"content":[{"type":"text","text":"倉庫"}]},{"type":"text","text":"獲取完整的代碼。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https:\/\/static001.geekbang.org\/resource\/image\/e5\/ed\/e58e42e93fae2ae8882e7e1d87e451ed.jpg","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"依賴關係"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"確保所有的依賴關係都已安裝。我們的簡單機器人只需要兩個庫:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"bash"},"content":[{"type":"text","text":"rasa==2.2.0\nspacy==2.2.4\n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果已經“克隆”了我的倉庫,就可以運行"},{"type":"codeinline","content":[{"type":"text","text":"pip install -r requirements.txt"}]},{"type":"text","text":"從根目錄來安裝這兩個庫。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"spacy 語言模型需要在單獨的步驟中安裝。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"bash"},"content":[{"type":"text","text":"python3 -m spacy download en_core_web_md\npython3 -m spacy link en_core_web_md en\n"}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"倉庫攻略"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在展示如何訓練和部署助手之前,先通過每個組件來了解它們是如何結合在一起的。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"這些文件大部分可以通過運行"},{"type":"codeinline","content":[{"type":"text","text":"rasa init"}]},{"type":"text","text":"來生成,"},{"type":"codeinline","content":[{"type":"text","text":"rasa init"}]},{"type":"text","text":"會創建一個帶有訓練數據、動作和配置文件示例的新項目。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"首先來看一下配置文件。"}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"配置文件"}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"endpoints.yml"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"這是助手的所有端點的位置。爲了支持"},{"type":"link","attrs":{"href":"https:\/\/rasa.com\/docs\/rasa\/next\/custom-actions?fileGuid=EoE2UeNK8n0kAwJZ","title":"","type":null},"content":[{"type":"text","text":"自定義動作"}]},{"type":"text","text":"(基本就是你可以編寫用於調用其他 API、查詢數據庫或訪問你構建的其他服務的自定義代碼),我們需要創建動作服務器。要做到這一點,請在這個文件中包含"},{"type":"codeinline","content":[{"type":"text","text":"action_endpoint"}]},{"type":"text","text":"。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"其他端點也可以在這裏定義,如,使用 Kafka 發送事件,或將對話存儲在 Redis 中而不是內存中。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"這就是 endpoints 文件的樣子:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"yaml"},"content":[{"type":"text","text":"action_endpoint:\n url: \"http:\/\/localhost:5055\/webhook\"\n"}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"config.yml"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"配置文件定義瞭如何訓練模型,這是你可以發揮創造力的地方。第一行用於指定聊天機器人的語言。在這裏,它將是英語:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"yaml"},"content":[{"type":"text","text":"language: \"en\"\n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"下一個組件配置自然語言理解管道。我們使用的是 Rasa 推薦的“sensible”啓動管道之一。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"yaml"},"content":[{"type":"text","text":"pipeline: \n - name: SpacyNLP\n - name: SpacyTokenizer\n - name: SpacyFeaturizer\n...\n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"接下來是"},{"type":"codeinline","content":[{"type":"text","text":"policies"}]},{"type":"text","text":"。這些是 Rasa 將遵循的響應用戶信息的策略。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"yaml"},"content":[{"type":"text","text":"policies: \n - name: MemoizationPolicy\n - name: RulePolicy\n core_fallback_threshold: 0.3\n core_fallback_action_name: action_default_fallback \n...`\n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"MemoizationPolicy 會記住訓練故事中的最後 X 個事件。可通過將"},{"type":"codeinline","content":[{"type":"text","text":"max_history"}]},{"type":"text","text":"添加到策略配置來指定 X。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"RulePolicy 利用我們爲機器人編寫的硬性規則(稍後會介紹)來生成響應。即使 RulePolicy 被指定在第二位,它也會優先於其他策略。因爲 RulePolicy 在缺省情況下具有最高優先級:也可以配置這些優先級。根據策略配置,機器人首先要檢查適用的規則,否則就會轉向訓練故事。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"我們的策略還配置了默認的回退機制。當一個動作置信度分數低於"},{"type":"codeinline","content":[{"type":"text","text":"core_fallback_threshold"}]},{"type":"text","text":"0.3 時,它將發送一個名爲"},{"type":"codeinline","content":[{"type":"text","text":"utter_default"}]},{"type":"text","text":"的預定義響應,並恢復到觸發回退之前的狀態。這在用戶說一些完全沒有意義的內容時是很有用的。"}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"credentials.yml"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果代碼託管在像 GitHub 這樣的地方,你應該在發佈該文件前仔細考慮。需要使用此文件進行身份驗證,我們會用它與 Facebook Messenger 機器人相連。現在,我暫時隱去了密碼,但是稍後會介紹如何從 Facebook 生成此文件。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"注意"},{"type":"text","text":":我的 GitHub 倉庫中並沒有包含此文件。你需要創建這個文件,然後用稍後生成的令牌對其進行填充。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"yaml"},"content":[{"type":"text","text":"facebook:\n verify: \"[this is your custom verification token]\"\n secret: \"[this is generated from facebook]\"\n page-access-token: \"[this is generated from facebook]\"\n"}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"domain.yml"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"來自 RASA 的官方"},{"type":"link","attrs":{"href":"https:\/\/rasa.com\/docs\/rasa\/domain\/?fileGuid=EoE2UeNK8n0kAwJZ","title":"","type":null},"content":[{"type":"text","text":"文檔"}]},{"type":"text","text":":"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"領域定義了助手操作的範圍。它指定了你的機器人應該知道的意圖、實體、槽、響應、形式和動作。它還定義了對話會話的配置。"}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"domain.yml 文件被分解爲意圖、實體、槽、響應、動作和會話配置。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"訓練機器人識別定義的意圖,並根據檢測到的意圖採取不同的行爲。"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果在管道中指定了一個實體提取器,它將提取這個文件中定義的所有實體;實體可以用來進一步控制對話流程。"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"槽是機器人將信息存儲到內存中的鍵值對;槽可以在對話期間的任何時候被設置、重置和檢索。"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"響應是預先定義的響應模板,用於機器人發送消息;當機器人達到該狀態時,默認回退將始終發送與"},{"type":"codeinline","content":[{"type":"text","text":"utter_default"}]},{"type":"text","text":"相關聯的消息。"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"動作包括任何已定義的自定義動作。"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在會話配置中,可以定義會話過期時間,以及是否應該將槽引入到新的會話中。"}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"訓練數據"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"將訓練數據分成三個文件夾:nlu、rules 和 stories。在 nlu 文件夾中,定義了意圖和實體。在 rules 文件夾中,定義了通過 RulePolicy 進行操作的規則。stories 文件夾中包含了模擬對話,這些對話被用作額外的訓練數據,以瞭解如何管理對話。"}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"nlu.yml"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在 NLU 設置中,定義了一些非常基本的意圖。下面是“greet”意圖的一個例子,它使用了以下的訓練數據:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"yaml"},"content":[{"type":"text","text":"nlu:\n - intent: greet \n examples: | \n - hey \n - hello \n - hiya\n...\n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https:\/\/rasa.com\/docs\/rasa\/nlu-training-data\/?fileGuid=EoE2UeNK8n0kAwJZ","title":"","type":null},"content":[{"type":"text","text":"官方文檔"}]},{"type":"text","text":"更詳細地介紹瞭如何改進 NLU 組件,例如加入實體、同義詞和查找表。"}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"rules.yml"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"rules 定義了機器人應該始終遵循的小型對話模式。RulePolicy 利用這些 rules 來管理對話流程。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"這裏定義了一個簡單的規則“always greet the user”。當機器人遇到“greet”意圖時(它根據上面提供的數據學習識別問候意圖),它會用自定義動作"},{"type":"codeinline","content":[{"type":"text","text":"action_greet"}]},{"type":"text","text":"來跟進。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"yaml"},"content":[{"type":"text","text":"- rule: always greet the user \n steps: \n - intent: greet \n - action: action_greet\n"}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"stories.yml"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"stories 是“真實”對話的例子,用於進一步訓練機器人的對話管理模型。它們被寫成用戶和機器人之間的對話交流。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"我們將包括一個非常簡單的故事。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"yaml"},"content":[{"type":"text","text":"version: \"2.0\"\nstories:\n - story: generic happy path\nsteps:\n - intent: greet\n - action: action_greet\n - intent: select_price\n - action: action_select_upper_price\n - intent: select_purpose\n - action: action_select_purpose\n - intent: select_brand\n - action: action_select_brand\n"}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"自定義動作"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"自定義動作對於更復雜的機器人來說非常有用,它們可以用來調用其他內部自然語言處理服務或公共 API 來改善對話體驗。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"自定義動作存儲在"},{"type":"codeinline","content":[{"type":"text","text":"actions\/actions.py"}]},{"type":"text","text":"中。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"下面是自定義動作"},{"type":"codeinline","content":[{"type":"text","text":"action_greet"}]},{"type":"text","text":"的一個例子,它可以向用戶問候。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"python"},"content":[{"type":"text","text":"class ActionGreet(Action):\ndef name(self) -> Text:\nreturn 'action_greet'\ndef run(self, dispatcher: CollectingDispatcher,\ntracker: Tracker,\ndomain: Dict[Text, Any]) -> List[Dict[Text, Any]]:\ndispatcher.utter_message(template='utter_greet')\ndispatcher.utter_message(template='utter_price')\nreturn []`\n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"每個自定義操作都遵循一個非常標準的格式。有一個"},{"type":"codeinline","content":[{"type":"text","text":"name"}]},{"type":"text","text":"方法返回自定義動作的名稱,這個名稱必須與 domain.yml 中提供的名稱一致。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"run"}]},{"type":"text","text":"方法需要接受調度器、跟蹤器和域。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"調度器用於生成響應(在這裏,它將發出兩條消息)。"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"追蹤器指的是對話追蹤器,可以利用它調出槽值、當前和過去的狀態。"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"返回值是一個在動作結束時執行的事件列表。下面是一些我們可以包含在其中的常用事件:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"SlotSet:用於設置槽值。"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"FollowupAction:觸發一個後續行動。"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Restarted:結束當前對話。"}]}]}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"訓練和啓動機器人"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"一旦準備好以上所有的文件,就可以訓練機器人了。在根目錄下運行"},{"type":"codeinline","content":[{"type":"text","text":"rasa train"}]},{"type":"text","text":"來開始這個過程。根據數據有多少,管道有多複雜,這可能需要一段時間。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果能夠看到這條消息,就說明模型已經成功訓練並保存。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https:\/\/static001.geekbang.org\/resource\/image\/26\/7d\/26dd896df4c82bbdd0f2aa887913ab7d.jpg","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"需要運行兩個命令來啓動機器人:一個用於動作服務器,一個用於機器人本身。在這裏,我把它合併成一行。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"注意"},{"type":"text","text":":這將在本地啓動機器人。稍後,將需要用 Facebook 生成的令牌來填充我們的憑據文件,以便將其部署到 Messenger。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"bash"},"content":[{"type":"text","text":"rasa run -m models --enable-api --cors \"*\" --debug & rasa run actions\n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"這是我的終端在該命令運行完成後的樣子。每次服務器進行對話時,都會有額外的日誌。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https:\/\/static001.geekbang.org\/resource\/image\/3e\/80\/3eee1aa83ddc4461f9d2ea0f56fc2d80.jpg","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在運行上述操作時,動作服務器將佔用 5055 端口,機器人將佔用 5005 端口。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"可以通過 ping http:\/\/localhost:5005 來測試服務器是否在運行。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https:\/\/static001.geekbang.org\/resource\/image\/e9\/fa\/e90ff67ce7d4d5a4eb172daa39d46ffa.jpg","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"與機器人交互"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"有幾種方式可以讓你開始與機器人交互。"}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"REST API"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"可以通過 REST API 與機器人進行交互。當 RASA 服務器運行時,發送這個請求,以用戶身份發送消息:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"shell"},"content":[{"type":"text","text":"curl --request POST \\\n --url http:\/\/localhost:5005\/webhooks\/rest\/webhook \\\n --header 'content-type: application\/json' \\\n --data '{\"sender\": \"sender_id\", \"message\": \"hi\"}'\n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"sender 字段是對話的唯一 ID。當同一 sender 字段下連續發送消息時,服務器將識別它是一個對話的一部分。message 字段是要發送的文本。"}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"RASA shell"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"可以運行"},{"type":"codeinline","content":[{"type":"text","text":"rasa shell"}]},{"type":"text","text":",通過命令行與機器人開始對話。"}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"Facebook Messenger 設置"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"我對部署 Rasa 服務器的偏好是通過"},{"type":"link","attrs":{"href":"https:\/\/ngrok.com\/?fileGuid=EoE2UeNK8n0kAwJZ","title":"","type":null},"content":[{"type":"text","text":"ngrok"}]},{"type":"text","text":"來完成,這將使你的本地端口暴露在公共互聯網上。另外,你也可以使用像 Heroku 這樣的服務,但是 Rasa 鏡像的大小讓它很難在大多數免費服務保持應用的運行。"}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"ngrok 設置"}]},{"type":"numberedlist","attrs":{"start":1,"normalizeStart":1},"content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":1,"align":null,"origin":null},"content":[{"type":"text","text":"創建 ngrok 賬戶。"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":2,"align":null,"origin":null},"content":[{"type":"text","text":"按照這些說明在計算機上設置 ngrok:https:\/\/ngrok.com\/download。"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":3,"align":null,"origin":null},"content":[{"type":"text","text":"在單獨的終端中運行"},{"type":"codeinline","content":[{"type":"text","text":".\/ngrok http 5005"}]},{"type":"text","text":",在端口 5005 上啓動 HTTP 隧道。"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"確保終端在任何時候都能運行,只要終端被中止,那麼隧道就會停止,機器人在 Messenger 上就會沒有反應。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"記下生成的 URL。需要向 Facebook 提供安全的 URL(帶 https 的那個)。"}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"在 Facebook 上創建應用程序"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Facebook 機器人需要連接到 Facebook 頁面。需要一個 Facebook 賬戶來"},{"type":"link","attrs":{"href":"https:\/\/www.facebook.com\/pages\/create\/?ref_type=site_footer&fileGuid=EoE2UeNK8n0kAwJZ","title":"","type":null},"content":[{"type":"text","text":"創建一個頁面"}]},{"type":"text","text":"。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"一旦頁面創建完畢,請訪問 https:\/\/developers.facebook.com\/apps。點擊綠色的“Create App”按鈕,創建一個新的應用程序。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https:\/\/static001.geekbang.org\/resource\/image\/56\/19\/56e49d71cbd74ed6f0f81705c1899819.jpg","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"選擇第一個選項,然後按照說明創建應用程序。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https:\/\/static001.geekbang.org\/resource\/image\/51\/73\/511aa45e362407c7yy76949a04214873.jpg","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"點擊下方的“Set Up”按鈕,將“Messenger”添加到應用中。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https:\/\/static001.geekbang.org\/resource\/image\/ea\/2d\/ea751c2c867574675d235027d903b12d.jpg","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"在 Facebook 上設置應用程序"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"一旦應用程序被創建,它應該引導你到 Messenger 下的“Setting”頁面。如果沒有自動進入那裏,請導航到應用程序儀表板,然後進入“Messengers”→“Settings”。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https:\/\/static001.geekbang.org\/resource\/image\/9f\/81\/9f451f33aceba9962eaf8d39dafd9981.jpg","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"首先,將 Facebook 頁面鏈接到應用程序。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https:\/\/static001.geekbang.org\/resource\/image\/42\/a2\/42005a7170d8679ae1eaba867d528ca2.jpg","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在鏈接頁面旁邊,添加訂閱允許 messages。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https:\/\/static001.geekbang.org\/resource\/image\/28\/07\/28cd0543c43a1091e38e32c249aa8907.jpg","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"一旦頁面被鏈接,點擊頁面旁邊的“generate token”。追蹤這個令牌(token),因爲它將需要包含在你的 Rasa 項目中。我們將此標記稱爲“頁面訪問令牌”."}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"接下來,在儀表板上導航到 settings→basic,找到應用密碼(app secret)。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https:\/\/static001.geekbang.org\/resource\/image\/b5\/f7\/b563f0fa21bf6364e44803b01942a0f7.jpg","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"然後,向下滾動到“webhooks”來添加一個新的回調 URL。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https:\/\/static001.geekbang.org\/resource\/image\/67\/3e\/674d86fdb15899b2406b641db256a43e.jpg","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在填寫回調細節之前,在 Rasa 項目中創建 credentials.yml。這個文件應該是這樣的:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"yaml"},"content":[{"type":"text","text":"facebook:\n verify: \"\"\n secret: \"\"\n page-access-token: \"\"\n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"下面是如何填充字段:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"verify"},{"type":"text","text":":創建一個你選擇的安全驗證令牌。"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"sectet"},{"type":"text","text":":這是應用程序的密碼,從 basic settings 中獲取。"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"page-access-token"},{"type":"text","text":":使用之前爲頁面生成的頁面訪問令牌。"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"通過運行以下命令再次啓動 Rasa 服務器。這與之前的命令略有不同,因爲我們現在利用了憑據標誌。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"bash"},"content":[{"type":"text","text":"rasa run -m models --enable-api --cors \"*\" --debug --credentials credentials.yml & rasa run actions\n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"取出作爲 credentials.yml 一部分創建的驗證令牌,並在 Webhooks 部分的回調 URL 一起輸入。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"回調 URL 是 https:\/\/{ngrok_url}\/webhooks\/facebook\/webhook。"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https:\/\/static001.geekbang.org\/resource\/image\/9b\/a7\/9bf9a9f407523545ceb4e5d14f7ce3a7.jpg","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"注意"},{"type":"text","text":":在任何時候,如果 ngrok 隧道停止,都必須重新啓動它,並更新 Facebook 上的回調 URL。"}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"驗證應用程序是否已啓動並運行"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"導航到你的頁面並嘗試給機器人發消息。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https:\/\/static001.geekbang.org\/resource\/image\/16\/c7\/16107db8843aeb6562294fd7aa48d2c7.jpg","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https:\/\/static001.geekbang.org\/resource\/image\/8b\/c0\/8b00011153b81d794aa05cca177305c0.jpg","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"太棒了,都準備好了!實際上,在目前的狀態下,機器人並不會就筆記本計算機的推薦話題做出有意義的對話……也許有一天它會 😆"}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"作者介紹:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Mandy Gu,Wealthsimple 數據科學家。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"原文鏈接:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"https:\/\/towardsdatascience.com\/build-a-chatbot-with-facebook-messenger-in-under-60-minutes-f4c8b8046a91"}]}]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章