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

Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐