聊天機器人框架Rasa資源整理

  Rasa是一個主流的構建對話機器人的開源框架,它的優點是幾乎覆蓋了對話系統的所有功能,並且每個模組都有很好的可擴展性。參考文獻收集了一些Rasa相關的開源項目和優質文章。

一.Rasa介紹

1.Rasa本地安裝

直接Rasa本地安裝一個不好的地方就是容易把本地電腦的Python包版本弄亂,建議使用Python虛擬環境進行安裝:

pip3 install -U --user pip && pip3 install rasa

2.Rasa Docker Compose安裝

查看本機Docker和Docker Compose版本:

docker-compose.yml文件如下所示:

version: '3.0'
services:
  rasa:
    image: rasa/rasa
    ports:
      - "5005:5005"
    volumes:
      - ./:/app
    command: ["run", "--enable-api", "--debug", "--cors", "*"]

3.Rasa命令介紹

用到的相關的Rasa命令如下所示:

rasa init:創建一個新的項目,包含示例訓練數據,actions和配置文件。
rasa run:使用訓練模型開啟一個Rasa服務。
rasa shell:通過命令行的方式載入訓練模型,然後同聊天機器人進行對話。
rasa train:使用NLU數據和stories訓練模型,模型保存在./models中。
rasa interactive:開啟一個互動式的學習會話,通過會話的方式,為Rasa模型創建一個新的訓練數據。
telemetry:Configuration of Rasa Open Source telemetry reporting.
rasa test:使用測試NLU數據和stories來測試Rasa模型。
rasa visualize:可視化stories。
rasa data:訓練數據的工具。
rasa export:通過一個event broker導出會話。
rasa evaluate:評估模型的工具。
-h, --help:幫助命令。
--version:查看Rasa版本資訊。
rasa run actions:使用Rasa SDK開啟action伺服器。
rasa x:在本地啟動Rasa X。

4.Rasa GitHub源碼結構

Rasa的源碼基本上都是用Python實現的:

二.Rasa項目基本流程

1.使用rasa init初始化一個項目

使用rasa init初始化聊天機器人項目:

.
├── actions
│   ├── __init__.py
│   └── actions.py
├── config.yml
├── credentials.yml
├── data
│   ├── nlu.yml
│   └── stories.yml
├── domain.yml
├── endpoints.yml
├── models
│   └── <timestamp>.tar.gz
└── tests
   └── test_stories.yml

2.準備自定義的NLU訓練數據

nlu.yml部分數據如下:

version: "3.1"

nlu:
- intent: greet
  examples: |
    - hey
    - hello
    - hi
    - hello there
    - good morning
    - good evening
    - moin
    - hey there
    - let's go
    - hey dude
    - goodmorning
    - goodevening
    - good afternoon

上面的intent: greet表示意圖為great,下面的是具體的簡單例子。稍微複雜點的例子格式是:[實體值](實體類型名),比如[明天](日期)[上海](城市)的天氣如何?其中的日期和城市就是NLP中實體識別中的實體了。除了intent必須外,該文件還可以包含同義詞synonym、正則表達式regex和查找表lookup等。

3.配置NLU模型

最主要就是pipeline的配置了。相關的config.yml文件如下:

pipeline:
# # No configuration for the NLU pipeline was provided. The following default pipeline was used to train your model.
# # If you'd like to customize it, uncomment and adjust the pipeline.
# # See //rasa.com/docs/rasa/tuning-your-model for more information.
#   - name: WhitespaceTokenizer
#   - name: RegexFeaturizer
#   - name: LexicalSyntacticFeaturizer
#   - name: CountVectorsFeaturizer
#   - name: CountVectorsFeaturizer
#     analyzer: char_wb
#     min_ngram: 1
#     max_ngram: 4
#   - name: DIETClassifier
#     epochs: 100
#     constrain_similarities: true
#   - name: EntitySynonymMapper
#   - name: ResponseSelector
#     epochs: 100
#     constrain_similarities: true
#   - name: FallbackClassifier
#     threshold: 0.3
#     ambiguity_threshold: 0.1

pipeline主要是分片語件、特徵提取組件、NER組件和意圖分類組件等,通過NLP模型進行實現,並且組件都是可插拔可替換的。

4.準備story數據

stories.yml文件如下:

version: "3.1"

stories:

- story: happy path
  steps:
  - intent: greet
  - action: utter_greet
  - intent: mood_great
  - action: utter_happy

- story: sad path 1
  steps:
  - intent: greet
  - action: utter_greet
  - intent: mood_unhappy
  - action: utter_cheer_up
  - action: utter_did_that_help
  - intent: affirm
  - action: utter_happy

- story: sad path 2
  steps:
  - intent: greet
  - action: utter_greet
  - intent: mood_unhappy
  - action: utter_cheer_up
  - action: utter_did_that_help
  - intent: deny
  - action: utter_goodbye

這裡面可看做是用戶和機器人一個完整的真實的對話流程,對話策略可通過機器學習或者深度學習的方式從其中進行學習。

5.定義domain

domain.yml文件如下:

version: "3.1"

intents:
  - greet
  - goodbye
  - affirm
  - deny
  - mood_great
  - mood_unhappy
  - bot_challenge

responses:
  utter_greet:
  - text: "Hey! How are you?"

  utter_cheer_up:
  - text: "Here is something to cheer you up:"
    image: "//i.imgur.com/nGF1K8f.jpg"

  utter_did_that_help:
  - text: "Did that help you?"

  utter_happy:
  - text: "Great, carry on!"

  utter_goodbye:
  - text: "Bye"

  utter_iamabot:
  - text: "I am a bot, powered by Rasa."

session_config:
  session_expiration_time: 60 #單位是min,設置為0表示無失效期
  carry_over_slots_to_new_session: true #設置為false表示不繼承歷史詞槽

領域(domain)中包含了聊天機器人的所有資訊,包括意圖(intent)、實體(entity)、詞槽(slot)、動作(action)、表單(form)和回復(response)等。

6.配置Rasa Core模型

最主要就是policies的配置了。相關的config.yml文件如下:

# Configuration for Rasa Core.
# //rasa.com/docs/rasa/core/policies/
policies:
# # No configuration for policies was provided. The following default policies were used to train your model.
# # If you'd like to customize them, uncomment and adjust the policies.
# # See //rasa.com/docs/rasa/policies for more information.
#   - name: MemoizationPolicy
#   - name: RulePolicy
#   - name: UnexpecTEDIntentPolicy
#     max_history: 5
#     epochs: 100
#   - name: TEDPolicy
#     max_history: 5
#     epochs: 100
#     constrain_similarities: true

policies主要就是對話策略的配置,常用的包括TEDPolicy、UnexpecTEDIntentPolicy、MemoizationPolicy、AugmentedMemoizationPolicy、RulePolicy和Custom Policies等,並且策略之間也是有優先順序順序的。

7.使用rasa train訓練模型

rasa train
或者
rasa train nlu
rasa train core

使用data目錄中的數據作為訓練數據,使用config.yml作為配置文件,並將訓練後的模型保存到models目錄中。

8.使用rasa test測試模型

通常把數據分為訓練集和測試集,在訓練集上訓練模型,在測試集上測試模型:

rasa data split nlu
rasa test nlu -u test_set.md --model models/nlu-xxx.tar.gz

說明:當然也是可以通過交叉驗證的方式來評估模型的。

9.讓用戶使用聊天機器人

可以通過shell用指定的模型進行交互:

rasa shell -m models/nlu-xxx.tar.gz

還可以通過rasa run --enable-api這種rest方式進行交互。如下:

三.Rasa系統架構

1.Rasa處理消息流程

  下圖展示了從用戶的Message輸入到用戶收到Message的基本流程:

  步驟1:用戶輸入的Message傳遞到Interpreter(NLP模組),然後識別Message中的意圖(intent)和提取實體(entity)。
  步驟2:Rasa Core將Interpreter提取的intent和entity傳遞給Tracker,然後跟蹤記錄對話狀態。
  步驟3:Tracker把當前狀態和歷史狀態傳遞給Policy。
  步驟4:Policy根據當前狀態和歷史狀態進行預測下一個Action。
  步驟5:Action完成預測結果,並將結果傳遞到Tracker,成為歷史狀態。
  步驟6:Action將預測結果返回給用戶。

2.Rasa系統結構

  Rasa主要包括Rasa NLU(自然語言理解,即圖中的NLU Pipeline)和Rasa Core(對話狀態管理,即圖中的Dialogue Policies)兩個部分。Rasa NUL將用戶的輸入轉換為意圖和實體資訊。Rasa Core基於當前和歷史的對話記錄,決策下一個Action。

  除了核心的自然語言理解(NLU)和對話狀態管理(DSM)外,還有Agent代理系統,Action Server自定義後端服務系統,通過HTTP和Rasa Core通訊;輔助系統Tracker Store、Lock Store和Event Broker等。還有上圖沒有顯示的channel,它連接用戶和對話機器人,支援多種主流的即時通訊軟體對接Rasa。
  (1)Agent組件:從用戶角度來看,主要是接收用戶輸入消息,返回Rasa系統的回答。從Rasa角度來看,它連接自然語言理解(NLU)和對話狀態管理(DSM),根據Action得到回答,並且保存對話數據到資料庫。
  (2)Tracker Store:將用戶和Rasa機器人的對話存儲到Tracker Store中,Rasa提供的開箱即用的系統包括括PostgreSQL、SQLite、Oracle、Redis、MongoDB、DynamoDB,當然也可以自定義存儲。
  (3)Lock Store:一個ID產生器,當Rasa集群部署的時候會用到,當消息處於活動狀態時鎖定會話,以此保證消息的順序處理。
  (4)Event Broker:簡單理解就是一個消息隊列,把Rasa消息轉發給其它服務來處理,包括RabbitMQ、Kafka等。
  (5)FileSystem:保存訓練好的模型,可以放在本地磁碟、雲伺服器等位置。
  (6)Action Server:通過rasa-sdk可以實現Rasa的一個熱插拔功能,比如查詢天氣預報等。

參考文獻:
[1]Rasa 3.x官方文檔://rasa.com/docs/rasa/
[2]Rasa Action Server://rasa.com/docs/action-server/
[3]Rasa Enterprise://rasa.com/docs/rasa-enterprise/
[4]Rasa Blog://rasa.com/blog/
[5]Rasa GitHub://github.com/rasahq/rasa
[6]Awesome-Chinese-NLP://github.com/crownpku/Awesome-Chinese-NLP
[7]BotSharp文檔://botsharp.readthedocs.io/en/latest/
[8]BotSharp GitHub://github.com/SciSharp/BotSharp
[9]rasa-ui GitHub://github.com/paschmann/rasa-ui
[10]rasa-ui Gitee://gitee.com/jindao666/rasa-ui
[11]rasa_chatbot_cn://github.com/GaoQ1/rasa_chatbot_cn
[12]Rasa_NLU_Chi://github.com/crownpku/Rasa_NLU_Chi
[13]nlp-architect://github.com/IntelLabs/nlp-architect
[14]rasa-nlp-architect://github.com/GaoQ1/rasa-nlp-architect
[15]rasa_shopping_bot://github.com/whitespur/rasa_shopping_bot
[16]facebook/duckling://github.com/facebook/duckling
[17]rasa-voice-interface://github.com/RasaHQ/rasa-voice-interface
[18]Rasa://github.com/RasaHQ
[19]ymcui/Chinese-BERT-wwm://github.com/ymcui/Chinese-BERT-wwm
[20]Hybrid Chat://gitlab.expertflow.com/expertflow/hybrid-chat
[21]rasa-nlu-trainer://rasahq.github.io/rasa-nlu-trainer
[22]crownpku/Rasa_NLU_Chi://github.com/crownpku/rasa_nlu_chi
[23]jiangdongguo/ChitChatAssistant://github.com/jiangdongguo/ChitChatAssistant
[24]Rasa框架應用://www.zhihu.com/column/c_1318281710002663424
[25]Rasa開源引擎介紹://zhuanlan.zhihu.com/p/331806270
[26]Rasa聊天機器人專欄開篇://cloud.tencent.com/developer/article/1550247
[27]rasa-nlu的究極形態及rasa的一些難點://www.jianshu.com/p/553e37ffbac0
[28]Rasa官方文檔手冊://juejin.cn/post/6844903922042142734
[29]Rasa官方影片教程://www.bilibili.com/video/BV1xC4y1H7HG?p=1
[30]用Rasa NLU構建自己的中文NLU系統://www.crownpku.com/2017/07/27/用Rasa_NLU構建自己的中文NLU系統.html
[31]Rasa Core開發指南://blog.csdn.net/AndrExpert/article/details/92805022

吾愛DotNet 右边的图片 專註於.NET領域的技術分享

人工智慧乾貨推薦 左边的图片1 專註於人工智慧領域的技術分享

Tags: