RASA(一)

machine_learner發表於2020-09-27

layout: post
title: rasa_chatbot
subtitle:
date: 2020-7-24
author: RJ
header-img:
catalog: true
tags:
- NLP


環境配置

pip install -n rasa python==3.7
pip3 install rasa==2.0.0rc1
conda install pytorch==1.5.1 torchvision==0.6.1 cudatoolkit=10.1 -c pytorch
pip install "rasa[transformers]"

三種model

Chinese_models_for_SpaCy models

pip install https://github.com/explosion/spacy-models/releases//tag/zh_core_web_lg-2.3.1/zh_core_web_lg-2.3.1.tar.gz

[MITIE]

[HFTransformer] pip install “rasa[transformers]”

智慧客服機器人

[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片儲存下來直接上傳(img-btiiabny-1601180411302)(https://raw.githubusercontent.com/rejae/rejae.github.io/master/img/15964501511980.png)]

Rasa研究使用

  1. Create a New Project
  2. View Your NLU Training Data
  3. Define Your Model Configuration
  4. Write Your First Stories
  5. Define a Domain
  6. Train a Model
  7. Test Your Assistant
  8. Talk to Your Assistant
rasa init --no-prompt    (1. Create a New Project, if you use --no-prompt you will get a default project, or you could use rasa train latter)

cat data/nlu.md  (nlu.md is your train data)
cat config.yml
cat data/stories.md
cat domain.yml

rasa train
rasa test
rasa shell

Core

Stories
Domains

Responses
Actions
Reminders and External Events
Policies
Slots
Forms
Retrieval Actions
Interactive Learning
Fallback Actions
Knowledge Base Actions

Stories.md and nlu.md

  • data/nlu.md ‘*’ your NLU training data
  • data/stories.md ‘*’ your stories

1、Stories

Rasa stories are a form of training data used to train the Rasa’s dialogue management models.

A story is a representation of a conversation between a user and an AI assistant, converted into a specific format where user inputs are expressed as corresponding intents (and entities where necessary) while the responses of an assistant are expressed as corresponding action names.

##	story 標題

>> checkpoint
* intent
  - action
  - xxx_form
  - form{"name":"xxx_form"}

## story_happy
>> activate restaurant form
    - ...
* request_restaurant
    - restaurant_form
    - form{"name": "restaurant_form"}

把以上內容儲存到 stories.md檔案中

呼叫執行:

rasa train core -d domain.yml -s data/stories.md --out models -c config.yml

2、 訓練資料 nlu.md

[training-data-format](https://rasa.com/docs/rasa/nlu/training-data-format/)

The training data for Rasa NLU is structured into different parts:

- common examples  (required) 
- synonyms  
    (Synonyms will map extracted entities to the same name, for example mapping “my savings account” to simply “savings”. However, this only happens after the entities have been extracted, so you need to provide examples with the synonyms present so that Rasa can learn to pick them up)
- regex features 
    (Regex features are a tool to help the classifier detect entities or intents and improve the performance.)
- lookup tables 
    (Lookup tables may be specified as plain text files containing newline-separated words or phrases. Upon loading the training data, these files are used to generate case-insensitive regex patterns that are added to the regex features.)


synonyms: use the new format [savings account]{"entity": "source_account", "value": "savings"}

Domain.yml

domain可以理解為機器的知識庫,其中定義了意圖,動作,以及對應動作所反饋的內容。

其中槽位和實體重合度較高

intents	意圖
actions	動作
templates	回答模板
entities	實體
slots	詞槽

config.yml

language: zh
pipeline:
  - name: HFTransformersNLP
    model_name: "bert"
    model_weights: "bert-base-chinese"
    cache_dir: "D:\\model_files"
  - name: LanguageModelTokenizer
  - name: LanguageModelFeaturizer
  - name: LexicalSyntacticFeaturizer
  - name: CRFEntityExtractor
  - name: EntitySynonymMapper
  - name: DIETClassifier
    epochs: 200

policies:
  - name: "rasa.core.policies.ted_policy.TEDPolicy"
    epochs: 120
    featurizer:
      - name: MaxHistoryTrackerFeaturizer
        max_history: 5
        state_featurizer:
          - name: BinarySingleStateFeaturizer
  - name: "rasa.core.policies.memoization.MemoizationPolicy"
    max_history: 5
  - name: "rasa.core.policies.form_policy.FormPolicy"
  - name: "rasa.core.policies.mapping_policy.MappingPolicy"
  - name: "rasa.core.policies.fallback.FallbackPolicy"
    nlu_threshold: 0.4
    core_threshold: 0.3
    ambiguity_threshold: 0.05
    fallback_action_name: 'action_fallback'
# Configuration for Rasa NLU.
# https://rasa.com/docs/rasa/nlu/components/
language: zh
pipeline:
  - name: MitieNLP
    model: data/total_word_feature_extractor_zh.dat
  - name: JiebaTokenizer
    dictionary_path: D:/rasa_workspace/.rasa_p2/data/dict/user_dict.txt
  - name: MitieEntityExtractor
  - name: EntitySynonymMapper
  - name: RegexFeaturizer
  - name: MitieFeaturizer
  - name: SklearnIntentClassifier

policies:
  - name: MemoizationPolicy
  - name: TEDPolicy
    max_history: 5
    epochs: 100
  - name: MappingPolicy

reference

使用 Rasa NLU 構建一箇中文 ChatBot

基於RASA的task-orient對話系統解析(一)

基於RASA的task-orient對話系統解析(二)——對話管理核心模組

基於RASA的task-orient對話系統解析(三)——基於rasa的會議室預定對話系統例項

Rasa使用指南01

Rasa使用指南02

rasa 打標籤工具

oscova

參考

基於中文的醫療知識圖譜的問答機器人MedicalKBQA

windows下安裝MITIE

git clone https://github.com/mit-nlp/MITIE.git
cd MITIE
python setup.py install

rasa csdn blog

rasa Bert

相關文章