智能會話機器人:SaaS 平臺的設計與思考

{"type":"doc","content":[{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":"1. 前言","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"隨着自然語言處理和智能語音識別技術的發展,智能會話機器人開始部分替代人工客服。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"網上關於NLP算法的文章有很多,但關於 Chatbot 架構的卻很少,關於 Chatbot SaaS 平臺架構的則更少。我是一名對機器學習感興趣的程序員,更關注如何設計實現一個架構良好的 Chatbot SaaS 平臺,因此寫下了這篇文章。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":"2. Chatbot架構","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"2.1. 智能會話機器人的分類","attrs":{}}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/ab/ab666bf6a151d09bbbef9d4ee5aea8b8.png","alt":null,"title":"智能會話機器人類型","style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"智能會話機器人按照對話輪次來劃分,可以分爲單輪對話機器人和多輪對話機器人;按照知識領域來劃分,可以分爲限定域機器人和開放域機器人;按照任務類型來劃分,可以分爲任務型機器人、問答型機器人、閒聊型機器人和融合型機器人。","attrs":{}}]},{"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","attrs":{}}],"text":"任務型","attrs":{}},{"type":"text","text":":根據用戶給出的信息完成指定的任務。一般限定於某個垂直領域,常採用多輪對話的形式。如訂餐、訂票等服務。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"問答型","attrs":{}},{"type":"text","text":":爲用戶提出的事實型、布爾型、計算型、推理型等類型的問題自動給出答案,進一步還可以再劃分爲常見問題解答(FAQ)和知識圖譜問答(KBQA),一般限定於某個特定知識領域。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"閒聊型","attrs":{}},{"type":"text","text":":與用戶進行開放式聊天,滿足用戶的情感陪護需求。一般不限定話題範圍,但有可能偏向於某個領域。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"融合型","attrs":{}},{"type":"text","text":":一般以任務型或問答型爲主,融合閒聊功能。如對於電商客服機器人來說,能完成商品推薦購買任務,能回答保修政策問題,還能陪客戶閒聊天,融合型是應用場景越來越複雜的產物。","attrs":{}}]}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"從應用的發展趨勢來看:單輪對話——>多輪對話,以獲得更多更完整的信息;單領域——>多領域,以滿足用戶更多層面的需求。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"2.2. 任務型機器人交互模型","attrs":{}}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/61/61e146c49c3ded6c6e5a35365e75a53a.png","alt":null,"title":"任務型機器人交互模型","style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"企業應用中最常見的需求是任務型機器人,上圖是任務型機器人經典的交互模型。其中紅色框爲文字語音轉換部分,這裏暫不作討論。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"2.2.1. 自然語言理解(NLU)","attrs":{}}]},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"領域識別(Domain Identification)","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":"none"},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"檢測用戶輸入內容所涉及的領域概念。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"意圖識別(Intent Detection)","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":"none"},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"檢測用戶在特定領域下表述內容所代表的意圖。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"詞槽填充(Slots Filling)","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":"none"},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"收集用戶在對話過程中任務所必需的關鍵信息。","attrs":{}}]}]}],"attrs":{}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"2.2.2. 對話管理(DM)","attrs":{}}]},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"對話狀態追蹤(Dialogue State Tracking,DST)","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":"none"},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"記錄和判斷當前會話處於任務的何種狀態。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"對話策略(Dialogue Policy,DP)","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":"none"},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"根據對話狀態決定下一步的系統行爲。","attrs":{}}]}]}],"attrs":{}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"2.2.3. 自然語言生成(NLG)","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"自然語言生成主要有兩種方法:基於模板生成和基於模型生成。","attrs":{}}]},{"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":"基於模板生成:調用設計人員預先設計的回覆模板,根據前置階段獲取到的狀態和關鍵信息生成自然語言回覆給用戶。優點是響應速度較快,缺點是語言表達不夠豐富,需要人工定義模板規則。因此比較適合在響應動作較少時使用,或是在系統冷啓動時使用。","attrs":{}}]},{"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":"基於模型生成:通常使用seq2seq模型學習大量交互語料數據,根據用戶輸入直接生成相關自然語言回覆給用戶。優點是不需要人工定義模板規則,語言表達較爲豐富;缺點是回覆結果不太可控,響應速度較慢,需要學習大量的交互語料數據。因此比較適合系統資源充足且有大量交互數據時採用。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"2.3. 組件集成(pipeline)","attrs":{}}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/1f/1fa2dc2c01e4bd60c70c32c1ab8bda18.png","alt":null,"title":"pipeline","style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"任務型機器人的系統設計中,通常採用pipeline方式來集成各個組件。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"pipeline的組件構成根據採用的方法不同而不同。有可能整個pipeline由三個組件構成,NLU、DM、NLG分別爲一個模型;也可能整個pipeline由兩個組件構成,NLU + DM 爲一個模型,NLG是一個模型;也有可能整體僅有一個組件,NLU+DM+NLG 是一個整體的模型(end2end)。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"某些情況下,爲了提高響應速度或實現條件判斷,還可能是一個分支流(如下圖所示)。","attrs":{}}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/dd/ddef0d182384d45b03313c0426075273.png","alt":null,"title":"pipeline-並行","style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"總之,pipeline中組件構成是不固定的,從系統設計的角度來看,可以將pipeline看成是一個整體模塊(如下圖所示)。","attrs":{}}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/5d/5dabc1117382688e04f4e34902a71ad4.png","alt":null,"title":"pipeline-整體","style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"這種情況下,問答機器人、閒聊機器人、融合型機器人和任務型機器人都是接受輸入返回輸出。即無論何種類型,Chatbot 的外層結構保持不變,變化的只是pipeline的內部結構。再進一步抽象,其實可以通過在配置界面拖拽相關組件連接成pipeline來創建新的會話機器人類型。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"2.4. Chatbot 基本架構","attrs":{}}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/04/04cce44d6764c577e91c084fee85e0b0.png","alt":null,"title":"Chatbot基本架構","style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"上圖中藍色部分是內部組件,灰色部分是外部服務。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"對於生產級別的 Chatbot,除了要考慮算法性能,還要考慮併發性能,還有集羣模式部署,會話狀態共享,會話事件分析,會話交互安全……等等。","attrs":{}}]},{"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","attrs":{}}],"text":"Conversation","attrs":{}},{"type":"text","text":"(會話交互):接受會話消息,異步響應消息(既支持接受 MQ 消息,也支持 RESTful API)。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"Metrics api","attrs":{}},{"type":"text","text":"(度量接口):用於系統狀態採集和健康檢查。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"Authentication","attrs":{}},{"type":"text","text":"(安全校驗):對請求進行安全校驗。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"pipeline","attrs":{}},{"type":"text","text":"(組件流水線):用於集成算法模型。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"Event Support","attrs":{}},{"type":"text","text":"(事件支持):與 Event Broker交互,發送或監聽會話事件。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"Tracker Manager","attrs":{}},{"type":"text","text":"(會話狀態管理):會話狀態更新和保存。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"Lock Helper","attrs":{}},{"type":"text","text":"(分佈式鎖):集羣部署時鎖定會話狀態。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"Action Support","attrs":{}},{"type":"text","text":"(動作調用):調用系統預定義動作和自定義動作。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"Model Support","attrs":{}},{"type":"text","text":"(模型調用):調用指定的算法模型服務。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":"none"},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"————————","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"Event Broker","attrs":{}},{"type":"text","text":"(會話事件中間件):提供會話事件中轉能力,由消費者訂閱相關事件,會話事件主要用於數據分析。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"Tracker Store","attrs":{}},{"type":"text","text":"(會話狀態存儲):提供會話狀態的保存。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"Lock Store","attrs":{}},{"type":"text","text":"(分佈式鎖存儲):提供分佈式鎖服務。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"Action Serving","attrs":{}},{"type":"text","text":"(自定義動作服務):自定義的動作響應,一般來說需要外部交互(查詢數據庫、調用第三方)的響應動作,則通過 Action Serving 來處理。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"Model Serving","attrs":{}},{"type":"text","text":"(模型能力服務):已訓練好的模型部署爲獨立的服務,用於提供模型服務能力。如 TensorFlow Serving,TorchServe 等。","attrs":{}}]}]}],"attrs":{}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"2.5. 模型訓練","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在上一小節中,簡單介紹了 Chatbot 的基礎架構,這裏再解釋下算法模型的訓練。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"自從2018年Bert 橫空出世刷新各大榜單,同時發佈了各種語言的預訓練模型,NLP領域就迎來了預訓練模型的新魔法時代。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"由於預訓練模型一般採用的是通用語料數據,與具體的領域和任務無關。因此需要在預訓練模型上採用領域相關或任務相關的無標註數據繼續預訓練,得到某個特定領域或任務相關的預訓練模型,然後再在該預訓練模型之上使用標註數據訓練。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"所以模型訓練分爲了兩個主要步驟:預訓練 ——> 訓練。","attrs":{}}]},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"名詞解釋:PTM(Pre-trained Model)預訓練模型","attrs":{}}]}],"attrs":{}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/7e/7e0d61517714954acfb24697bb6ccea9.png","alt":null,"title":"模型訓練","style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如上圖所示,有四個主要的組件:","attrs":{}}]},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Pretraining Task(預訓練任務):從Model File Service 獲取預訓練模型(PTM),從NLP Data Service獲取無標註數據,預訓練後得到領域相關或任務相關的預訓練模型,最後將模型保存到Model File Service。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Training Task(訓練任務):從Model File Service 獲取領域相關或任務相關的預訓練模型(PTM),從NLP Data Service獲取標註數據,訓練後得到正式模型,最後將模型保存到Model File Service。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Model File Service(模型文件服務):存儲預訓練模型、正式模型,需有版本管理和類別管理能力。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"NLP Data Service(語料數據服務):存儲未標註數據和標註數據,需有版本管理和類別管理能力。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Model Serving(模型能力服務):從Model File Service 拉取模型並部署爲獨立服務。","attrs":{}}]}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"沒有最好的模型,只有更好的模型。所以,預訓練和訓練都是不斷迭代的過程。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"2.6. 小結","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"本節首先介紹了 Chatbot 的分類,然後介紹了任務型機器人的交互框架,接着探討了如何圍繞pipeline 爲核心抽象來設計 Chatbot 的基礎架構,最後介紹了算法模型的訓練過程。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"這裏之所以要先設定 Chatbot 的基本架構,是爲了避免 SaaS 平臺適配不同架構的 Chatbot,從而帶來額外的無謂的複雜度。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"接下來的章節裏,我們將開始探討Saas 平臺該如何來設計。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":"3. Chatbot SaaS平臺架構","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"對於單租戶的機器人來說,開發維護運營是比較簡單的。而多租戶的平臺架構,需要考慮的維度則複雜得多。我們先嚐試着來思考下這些問題:","attrs":{}}]},{"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":"有哪些用戶會使用這個平臺?他們各自對這個平臺的核心訴求是什麼?他們通過什麼方式來與平臺進行交互?","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":2,"align":null,"origin":null},"content":[{"type":"text","text":"如何創建和銷燬 Chatbot?如何管理每個 Chatbot 的生命週期?","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":3,"align":null,"origin":null},"content":[{"type":"text","text":"如何保證最終用戶訪問的就是指定的 Chatbot?","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":4,"align":null,"origin":null},"content":[{"type":"text","text":"多個租戶能否共用一個類型的 Chatbot?","attrs":{}}]}]}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"3.1. 需求分析","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"3.1.1. 系統用戶","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"有哪些用戶會與SaaS 平臺發生交互呢?這裏簡單梳理了下,主要的用戶類型如下圖所示。","attrs":{}}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/2a/2a051366f174a64cc81c4f59e56f08b7.png","alt":null,"title":"系統用戶","style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"可能還會有財務人員、營銷人員……等其他用戶,對於這些更細化的角色和權限,後續根據業務需求通過RBAC模型來擴展即可。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"3.1.2. 功能需求","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"上一小節我們粗粒度地列出了用戶角色類型,那麼,他們各自都會有哪些核心功能需求呢?","attrs":{}}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/8e/8e9f628caaa2ef4d6475ac4819195e67.png","alt":null,"title":"系統用例","style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"說明","attrs":{}},{"type":"text","text":":","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"這裏僅描述了用戶的關鍵需求,目的是先確定SaaS平臺的核心架構,這個核心架構應該是穩定而不會輕易變化的。","attrs":{}}]}],"attrs":{}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"3.1.3. 特性需求","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"除了功能需求外,架構設計中需要特別關注的是特性需求——這決定了軟件系統的品質。對於Chatbot SaaS平臺來說,需要特別考慮以下幾點。","attrs":{}}]},{"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","attrs":{}}],"text":"隔離性","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":"none"},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"SaaS平臺面向的是多租戶,每個租戶的對話數據不同,需要的響應能力也不同,因此需要做好租戶之間的隔離。採用物理隔離,隔離性好,但資源要求高;採用邏輯隔離,隔離性弱,但資源要求低。如何在保證隔離性的同時儘可能地減少資源消耗,是產品層面和技術層面都需要着重考慮的問題。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"擴展性","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":"none"},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"用戶的功能需求是不固定的,因此架構設計中需要考慮哪些是易變的,哪些是不變的,預留出功能擴展能力。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"安全性","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":"none"},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"SaaS平臺通常部署在雲端,通過對外接口來提供服務。對於租戶和用戶來說,除了滿足功能性需求,最關心的應該是安全性問題。接口服務應該有完善的鑑權和加密機制,數據安全方面應該有完善的加密和備份機制,同時服務器、數據庫等物理資源應該有嚴格的權限管理。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"兼容性","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":"none"},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"SaaS平臺的系統改進和功能擴展對於租戶和用戶來說應該是無感知的。代碼升級,應該能兼容已有的對外接口和數據格式;算法升級,應該能保證原有會話流正常。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"伸縮性","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":"none"},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"對於SaaS平臺來說,用戶量和訪問量在運營過程中可能會增長或減少,一天之中的訪問量會有高峯和低谷,某些時期可能會有數倍於平常的訪問量,因此平臺服務應該具備彈性伸縮能力。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"自動化","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":"none"},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"SaaS平臺面向的是不斷新增的租戶,應該儘量減少人工參與的環節,能自動化的環節都應該儘可能實現自動化。最好的情況是,租戶登錄平臺,上傳語料數據,然後自動完成模型訓練更新、機器人服務上線和按需彈性伸縮,SaaS平臺運營方無需人工參與。","attrs":{}}]}]}],"attrs":{}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"3.1.4. 複雜度分析","attrs":{}}]},{"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","attrs":{}}],"text":"自動化","attrs":{}},{"type":"text","text":":","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":"none"},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"前置動作:模型預訓練、模型訓練、模型部署,租戶能力集成,通用第三方能力集成;","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":"none"},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"運行動作:根據運行指標彈性伸縮和限流,包括 Chatbot 服務,模型能力服務、租戶能力服務及依賴的相關組件。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":"none"},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"結束動作:Chatbot下線,模型下線,租戶能力服務下線;","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":"none"},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"附加動作:數據分析、報表統計、自動告警。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":"none"},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"對於Chatbot SaaS平臺來說,自動化是必須實現的特性,也是最複雜的問題之一,因此在架構設計中會着重對自動化問題進行分解和處理。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"成本因素","attrs":{}},{"type":"text","text":":","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":"none"},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Chatbot SaaS平臺有不斷新增的租戶,每個租戶的會話流、語料數據和最終模型可能不同,如何用盡可能少的資源實現用戶需求,這也是需要考慮的問題。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":"none"},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"方向一:一個模型包含多個租戶的不同場景需求,這種情況下需要模型有良好的領域識別能力,缺點是隔離性差且大模型的併發性能低。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":"none"},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"方向二:一個進程中加載多個互不相關的pipeline,然後根據業務規則分發到不同的pipeline,缺點是Chatbot耦合了業務規則。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":"none"},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"更好的方法應該是從產品角度進行優化,總結用戶需求,抽象出公共能力,從而減少服務進程的創建。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"算法性能和併發性能","attrs":{}},{"type":"text","text":":","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":"none"},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"通常來說,複雜算法模型的算法性能好但併發性能低,因此需要找到算法性能和併發性能的平衡點。","attrs":{}}]}]}],"attrs":{}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"3.1.5. 設計目標","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"與具體Chatbot類型和算法模型無關的Chatbot SaaS 平臺,這個平臺應該是一個自動化的、安全的、響應式的系統。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"3.2. 架構總覽","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"3.2.1. 整體架構","attrs":{}}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/dd/dd8562e4f59a22480de4125d126ba0a5.png","alt":null,"title":"Chatbot SaaS 平臺整體架構","style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如上圖所示,這是一個標準的DDD 四層模型(4層 + 1監控)。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在我過往的架構實踐中,我通常將業務層分成管理端和應用端。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"一是爲了隔離變化,管理端和應用端各自的改動不會相互影響;二是性能要求不同,應用端一般與最終用戶交互,用戶數量多,併發性和實時性要求高,而管理端則與企業用戶交互,用戶數量較少,性能要求較低。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"另外,爲了實現","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"自動化","attrs":{}},{"type":"text","text":"的特性需求,在這個SaaS平臺中特別增加了調度端,主要是用來管理訓練任務、測試任務和Chatbot的生命週期。","attrs":{}}]},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"備註","attrs":{}},{"type":"text","text":":","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"管理端和應用端無直接交互;管理端和調度端爲異步交互(使用MQ解耦)。","attrs":{}}]}],"attrs":{}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"3.2.2. 組件概覽","attrs":{}}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/6e/6e9bba265859f836a38ab3205e9185e7.png","alt":null,"title":"Chatbot SaaS 平臺組件概覽","style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"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","attrs":{}}],"text":"接入層","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":"none"},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"負責與企業用戶(管理端)和最終用戶(應用端)的交互,需適配不同的業務接入方式。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"授權","attrs":{}},{"type":"text","text":":負責用戶的訪問授權。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":"none"},"content":[{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"企業用戶獲得授權後才能訪問管理端,最終用戶獲得授權後才能訪問會話網關,需根據業務場景提供不同的授權方式。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"會話網關","attrs":{}},{"type":"text","text":":接受會話請求,轉換請求參數爲 Router 接受的標準格式,封裝返回結果。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"管理UI","attrs":{}},{"type":"text","text":":管理界面,與企業用戶交互,通常爲 Web UI 的形式。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"業務層","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":"none"},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"負責具體的業務編排,通過組合或聚合領域服務能力完成業務規則。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"管理端","attrs":{}},{"type":"text","text":":","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":"none"},"content":[{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"語料數據、算法模型、會話流、機器人、機器人類型、租戶……等的管理維護。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"調度端","attrs":{}},{"type":"text","text":":","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":"none"},"content":[{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"接收管理端的異步消息,負責創建任務和資源調度。如果進一步細分,還可以拆分成訓練調度、測試調度和生產服務調度。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"應用端","attrs":{}},{"type":"text","text":":","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":"none"},"content":[{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"應用端被分爲三個大的模塊:訓練模塊、測試模塊、生產模塊。爲避免計算資源爭搶,三者的運行環境需作物理隔離。另外,一些算法模型需要使用GPU,因此調度端需考慮如何正確合理地分配物理資源。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":"none"},"content":[{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"生產模塊","attrs":{}},{"type":"text","text":":正式對外提供的會話服務。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":"none"},"content":[{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"訓練模塊","attrs":{}},{"type":"text","text":":分爲預訓練任務和正式訓練任務,由調度端創建任務並分配物理資源。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":"none"},"content":[{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"測試模塊","attrs":{}},{"type":"text","text":":分爲組件測試和集成測試,TestExecutor負責執行具體的測試用例,生成測試報告並保存,最後可以在管理端UI進行呈現。爲了進行完整的系統集成測試,測試模塊有獨立 Router。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"領域服務層","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":"none"},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"負責實際的業務邏輯,有多個獨立的領域服務。譬如用戶服務、模型文件服務、語料數據服務……等。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"基礎設施層","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":"none"},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"緩存組件、持久化組件與及相關的中間件等外部系統。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"監控系統","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":"none"},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"負責監控物理資源和性能指標,採集業務日誌和運行日誌,並提供數據分析查看和異常告警能力。對於微服務架構來說,系統監控是必要的組件。","attrs":{}}]}]}],"attrs":{}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"3.2.3. 組件交互","attrs":{}}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/26/2654c7baa5101e7c235a9551b6d60897.png","alt":null,"title":"Chatbot SaaS 平臺組件交互","style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如上圖所示,紅色線爲會話線,綠色線爲訓練線,橙色線爲測試線。","attrs":{}}]},{"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","attrs":{}}],"text":"會話線","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"最終用戶攜帶會話消息請求 Gateway(或租戶服務器中轉最終用戶消息到Gateway)。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Gateway進行權限校驗,校驗通過後將消息轉換爲標準數據格式,並將會話消息轉發給 Router。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Router 將會話消息根據業務規則轉發給對應的 Chatbot。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Chatbot 異步調用 ModelServing 和 ActionServing,最終響應會話消息給用戶。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":"none"},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"最終用戶請求 Gateway 之前需先訪問 Authorization獲得授權;Gateway 的通信協議應支持雙向通信,方便實現異步消息處理和推送;Router 需具備消息緩存能力,用戶斷線重連後可以推送未讀消息。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":"none"},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"特別需要注意的是,會話線的整個過程都應該是異步的。Gateway 和 Router 之間採用長鏈接機制,Router 和 Chatbot 之間則採用 MQ 來作爲通信機制。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"訓練線","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"訓練任務由管理端(…… Manager)發起,需先準備語料數據、算法模型和代碼。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"調度端(Scheduler)根據指令信息創建訓練任務。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"訓練任務從 Model File Service 拉取算法模型,從 NLP Data Service拉取語料數據,然後開始訓練。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"訓練任務訓練完成後將算法模型保存到 Model File Service,並通知 Scheduler 訓練完成。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"測試線","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":"none"},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"測試分爲算法模型測試和系統功能測試;既可以對單個組件測試,也可以進行系統集成測試。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":"none"},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"測試任務的創建也分爲兩種情況:一是由管理端人工發起的測試任務;二是模型訓練完成自動觸發的測試任務。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Scheduler 接收到創建測試任務的指令,分配物理資源並部署相關服務。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Scheduler 通知 TestExecutor 開始執行測試任務。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"TestExecutor 測試完成後通知 Scheduler 並保存測試報告。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"運營管理人員根據測試報告判斷是否正式部署新的算法模型(或系統功能)。","attrs":{}}]}]}],"attrs":{}},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"備註","attrs":{}},{"type":"text","text":":","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"爲了更清晰地表達主要的業務線,省略了部分組件和交互關係。","attrs":{}}]}],"attrs":{}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"3.3. 其它設計","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"3.3.1. Chatbot的配置維度","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"對於SaaS平臺來說,可供租戶創建的Chatbot 的類型應該是多種多樣的。","attrs":{}}]},{"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","attrs":{}}],"text":"功能","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":"none"},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"首先是可以按功能來配置,譬如可以分爲任務型、問答型、閒聊型和融合型等。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"語言","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":"none"},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"其次,算法模型常常與具體語言強相關,因此Chatbot 也需要按語言來配置。但或許可以從另外一個角度去思考:Chatbot 能做到與具體語言無關嗎?","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":"none"},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"一個方向是先將不同的源語言翻譯成目標語言,然後基於目標語言再進行後續的會話流程,最後再將回復消息翻譯成源語言。基於當前的機器翻譯技術,這應該是比較簡單可行的一個方向,缺點是會因爲翻譯精度損失算法性能。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":"none"},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"另一個方向是首先做語言檢測,然後調用源語言訓練的算法模型,優點是算法性能較好,缺點是每一種算法模型都需要採用多種語言訓練。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":"none"},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"具體採用哪種方法,需要具體看業務場景。對於SaaS平臺來說,無論哪種方法,都沒有增加額外的複雜度,只是pipeline的編排配置。譬如在Chatbot的pipeline起始位置加上語言檢測模型(機器翻譯模型),pipeline結束位置加上機器翻譯模型。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"領域","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":"none"},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"最後,還應該根據領域來配置。如前所述,爲了提高算法性能,通常會採用領域相關的語料進行預訓練,一個大的領域下還可以繼續細分子領域。通常來說領域數據越多分得越細則模型的算法性能會越好,領域數據和模型是Chatbot SaaS 平臺的核心競爭力之一。","attrs":{}}]}]}],"attrs":{}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/ad/ad2074b35c47e1340fb625574143ca74.png","alt":null,"title":"Chatbot創建-配置維度","style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在 SaaS 平臺的管理界面,租戶首先可以根據自己的業務場景選擇不同的維度進行組合配置,然後編排會話流程和上傳語料數據,最終構建得到屬於自己的 Chatbot 類型。SaaS平臺爲該 Chatbot類型分配 ID 標識,最終用戶攜帶這個 ID 訪問會話網關,Router 可以根據 ID 路由消息到該 Chatbot 類型的運行實例。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"3.3.2. 人工客服支持","attrs":{}}]},{"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","attrs":{}}],"text":"場景分析","attrs":{}}]}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"什麼情況下由機器人響應消息?什麼情況下由人工客服響應消息?這是一個需要定義的產品需求。每個租戶的需求可能是不同的,SaaS平臺可以預先設定一些切換模式,然後再根據租戶需求做定製化的方案。","attrs":{}}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/dd/ddbc03e5c6a33f96c3997c127ee1fefc.png","alt":null,"title":"人工客服切換-場景分析","style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"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","attrs":{}}],"text":"技術方案","attrs":{}}]}]}],"attrs":{}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/09/099ad290707505c4a369cc509a330142.png","alt":null,"title":"人工客服切換技術方案","style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"說明","attrs":{}},{"type":"text","text":":","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Msg Service(消息服務):用於記錄和讀取用戶與Chatbot、用戶與人工坐席系統的交互消息。","attrs":{}}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"人機切換實現並不複雜,但分發機制放在哪個模塊卻是一個需要衡量的問題。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果都放在Router,那麼 Router 就需要知道所有租戶的設定,可能會因爲租戶的個性化需求而頻繁修改;如果都放在Chatbot,那麼人工坐席系統的消息也需要經過Chatbot,不但增加了消息流轉環節,而且導致人工坐席服務和Chatbot強耦合。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"那麼該如何來設計這個分發機制呢?","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"從需求場景來分析,可以歸類爲兩種設定:一是全局設定,二是個性化設定。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":" 1.全局設定","attrs":{}}]},{"type":"numberedlist","attrs":{"start":1,"normalizeStart":1},"content":[{"type":"listitem","attrs":{"listStyle":"none"},"content":[{"type":"paragraph","attrs":{"indent":0,"number":1,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"italic","attrs":{}},{"type":"strong","attrs":{}}],"text":"僅機器人響應","attrs":{}},{"type":"text","text":"和","attrs":{}},{"type":"text","marks":[{"type":"italic","attrs":{}},{"type":"strong","attrs":{}}],"text":"僅人工客服響應","attrs":{}},{"type":"text","text":",這個可以歸類到全局設定。Router 中增加 Distribution Policy,由其直接將消息分發到對應的系統。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":"none"},"content":[{"type":"paragraph","attrs":{"indent":0,"number":2,"align":null,"origin":null},"content":[{"type":"text","text":"這裏有一個明顯的需求衝突:當設定爲僅機器人響應時,如果最終用戶要求人工客服響應,該如何來處理?","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":"none"},"content":[{"type":"paragraph","attrs":{"indent":0,"number":3,"align":null,"origin":null},"content":[{"type":"text","text":"一個方案是 ActionServing 獲取相關的分發策略設定,如果判斷爲僅機器人響應,則不通知人工坐席系統且給出預設的回覆;另一個方案是ActionServing 無需知曉全局分發策略設定,始終通知人工坐席系統且給出預設的回覆,是否切換完全由人工客服決定。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":"none"},"content":[{"type":"paragraph","attrs":{"indent":0,"number":4,"align":null,"origin":null},"content":[{"type":"text","text":"這個需求衝突需由產品設計層面決定,無論哪種方式,都只是與租戶相關的ActionServing 的改動,不影響整體架構。","attrs":{}}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":" 2.個性化設定","attrs":{}}]},{"type":"numberedlist","attrs":{"start":1,"normalizeStart":1},"content":[{"type":"listitem","attrs":{"listStyle":"none"},"content":[{"type":"paragraph","attrs":{"indent":0,"number":1,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"italic","attrs":{}},{"type":"strong","attrs":{}}],"text":"意圖檢測","attrs":{}},{"type":"text","text":"、","attrs":{}},{"type":"text","marks":[{"type":"italic","attrs":{}},{"type":"strong","attrs":{}}],"text":"情感分析","attrs":{}},{"type":"text","text":"……等需根據對話內容來判斷是否需要切換到人工坐席的情形,這個可以歸類到個性化設定,其本質也是Chatbot的對話流編排。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":"none"},"content":[{"type":"paragraph","attrs":{"indent":0,"number":2,"align":null,"origin":null},"content":[{"type":"text","text":"因爲需要利用Chatbot 的自然語言理解能力,因此需先將消息發送給 Chatbot,由 Chatbot 的對話管理模塊根據對話流狀態通知人工客服進行後續處理。人工客服一旦開始應答,Router則開始將消息轉發到人工坐席系統(自動切換)。","attrs":{}}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"基本業務流程如下圖所示,Router 負責全局設定,Chatbot負責個性化的設定。","attrs":{}}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/59/59bf4beac5ae13c20e5b5688571bac15.png","alt":null,"title":"人工客服支持-業務流程","style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"備註","attrs":{}},{"type":"text","text":":","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果有更加複雜的個性化需求,可以在 Router 之後增加一個租戶特有的獨立的分發組件,無需修改 Router 和 Chatbot。","attrs":{}}]}],"attrs":{}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"3.4. 技術選型","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在前面的架構設計中,我一直避免限定具體的技術框架和開發語言。這是因爲選擇何種技術其實是非常個性化的,不僅僅與具體業務有關,也跟開發團隊熟悉的技術有關。接下來,也僅僅聊一些大的方向選擇,或者說是我個人的選擇傾向。","attrs":{}}]},{"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","attrs":{}}],"text":"異步、長鏈接和雙向通信","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":"none"},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Chatbot 和人工客服的消息天然就是異步響應的,一次對話過程會有多輪對話(多次通信),也可能會連續發送多條消息而無需等待對方回覆。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":"none"},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"因此,外部接入的會話網關應該能支持長鏈接、雙向通信和異步響應。WebSocket、RSocket、MQTT等都可以支持,綜合來看,WebSocket 最爲通用成熟,因此首先支持 WebSocket 協議,其它協議再根據業務需求做進一步開發。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":"none"},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"另外,如 ","attrs":{}},{"type":"text","marks":[{"type":"italic","attrs":{}},{"type":"strong","attrs":{}}],"text":"3.2.3. 組件交互","attrs":{}},{"type":"text","text":" 所描述的,內部服務組件之間的通信也應該是異步的,我更傾向於採用 MQ 來作爲 Router 和 Chatbot & 人工坐席系統 之間的異步通信機制。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":"none"},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"但這也會帶來一些其它的複雜度:譬如服務端響應消息時如何才能知道客戶端連接到了哪個會話網關的哪個節點?如何來判斷是否響應超時?消息是否允許丟失?如何保證消息不丟失?如何保證消息不重複響應?","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":"none"},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"業界對於這些問題已有一些不錯的實踐,但具體而言還是要根據業務場景去選擇落地方案,這裏不再繼續深入探討。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"語言與框架","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":"none"},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"對於較大型的項目,通常都會使用多個語言。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"Java","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":"none"},"content":[{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Java的企業應用生態最豐富完整,但有一個問題是編譯後的鏡像體積大啓動慢。當前,Spring、Quarkus 等正與 GraalVM 密切合作以構建資源佔用低啓動速度快的雲原生應用,但依然有很多項目不能編譯成native-image。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":"none"},"content":[{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"因此,如果選用Java 作爲主語言,SaaS平臺的管理部分可以用Spring,而對於需要頻繁創建、快速擴容且相對簡單的Chatbot 和 ActionServing,可以使用Spring Native 編譯成 native-image。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"Go","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":"none"},"content":[{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Go 編譯後的容器鏡像較小,併發支持也不錯,雖然相比 Java 而言應用生態並沒有那麼強大,泛型也是硬傷,但在DevOps 領域卻是絕對的主流。SaaS平臺的管理調度部分其實也是偏運維,所以其實可以用 Go 來開發。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"Python","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":"none"},"content":[{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"AI 領域的開發大多使用Python,當前主流的深度學習框架有TensorFlow 和 PyTorch,很多開源的模型也是使用這兩個框架開發。相比較而言,TensorFlow 的模型部署更加強大方便。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":"none"},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"綜上:SaaS平臺的會話網關、管理端、領域服務層用 Java(Spring),Chatbot 和 ActionServing 用 Java(Spring Native);SaaS平臺的調度端可以用Go,不過更傾向於也用 Java;機器學習的模型開發主要用 Python(具體框架取決於開源模型採用哪種框架);模型部署則與具體的深度學習框架相關,部署方式的選擇需要考慮併發性能和時延,但總之最後一定要打包成 Docker 鏡像,便於自動化運維。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"微服務與容器化","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":"none"},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"SaaS 平臺要實現自動化運維,K8s + Istio + Docker 是一套不錯的組合。但K8s 和 Istio 比較複雜,這需要技術團隊對其有足夠深刻的瞭解,還需要有一定的定製化開發能力。當然不一定非要用 K8s + Istio,也可以用 Spring Cloud + Go + Docker 的方式來開發,優點是方案會更加簡單可控,缺點是需要自己實現一整套運維管理監控機制,到最後可能其實就是 K8s + Istio 功能的一個子集。","attrs":{}}]}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"關於技術選型,最重要的其實是在保證功能需求和特性需求的基礎上能夠做到簡單和統一,有利於後續的開發和維護。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"3.5. 小結","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"本節首先對SaaS平臺的需求進行了分析,然後介紹了SaaS 平臺的架構設定,最後簡單探討了技術選型。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":"4. 結束語","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"這篇文章描述了 SaaS 平臺架構的基本設計,融入了一些我的思考過程,更詳細的如模塊設計、核心業務流程設計……等限於篇幅不再一一詳述,後續有時間再聊。未來的規劃中,我會基於當前的設計,先實現 Chatbot,然後再實現 SaaS 平臺,歡迎繼續關注。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"最後,感謝各位朋友的閱讀,如有任何建議,請給我留言。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":"5. 參考資料","attrs":{}}]},{"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":"杜振東, 塗銘. 會話式AI:自然語言處理於人機交互[M]. 北京:機械工業出版社, 2020","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":2,"align":null,"origin":null},"content":[{"type":"text","text":"段楠, 周明. 智能問答[M]. 北京:高等教育出版社, 2018","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":3,"align":null,"origin":null},"content":[{"type":"text","text":"夕小瑤. 多輪對話之對話管理(Dialog Management)[EB/OL]. [2018-12-24]. https://www.jiqizhixin.com/articles/2018-12-24-23","attrs":{}}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":4,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/16/163c7f16649700506a2ff355a1f15673.png","alt":null,"title":"文章版本記錄","style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}}]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章