• Stars
    star
    304
  • Rank 133,121 (Top 3 %)
  • Language
    Python
  • License
    Apache License 2.0
  • Created about 6 years ago
  • Updated about 1 month ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

dialogbot, provide search-based dialogue, task-based dialogue and generative dialogue model. 对话机器人,基于问答型对话、任务型对话、聊天型对话等模型实现,支持网络检索问答,领域知识问答,任务引导问答,闲聊问答,开箱即用。

alt text

PyPI version Downloads Contributions welcome GitHub contributors License Apache 2.0 python_vesion GitHub issues Wechat Group

DialogBot

Dialogbot, provide complete dialogue model technology. Combining search-based dialogue model, task-based dialogue model and generative dialogue model, output the optimal dialogue response.

dialogbot实现了问答型对话,任务型对话,聊天型对话等多种对话机器人方案,支持网络检索问答,领域知识问答,任务引导问答,闲聊问答,开箱即用。

Guide

Question

人机对话系统一直是AI的重要方向,图灵测试以对话检测机器是否拥有高度的智能。

如何构建人机对话系统或者对话机器人呢?

Solution

对话系统经过三代的演变:

  1. 规则对话系统:垂直领域可以利用模板匹配方法的匹配问句和相应的答案。优点是内部逻辑透明,易于分析调试,缺点是高度依赖专家干预, 缺少灵活性和可可拓展性。
  2. 统计对话系统:基于部分可见马尔科夫决策过程的统计对话系统,先对问句进行贝叶斯推断,维护每轮对话状态,再跟进对话状态进行对话策略的选择, 从而生成自然语言回复。基本形成现代的对话系统框架,它避免了对专家的高度依赖,缺点是模型难以维护,可拓展性比较受限。
  3. 深度对话系统:基本延续了统计对话系统的框架,但各个模型采用深度网络模型。利用了深度模型强大的表征能力,语言分类和生成能力大幅提高, 缺点是需要大量标注数据才能有效训练模型。

对话系统分为三类:

  • 问答型对话:多是一问一答,用户提问,系统通过对问题解析和查找知识库返回正确答案,如搜索。
  • 任务型对话:指由任务驱动的多轮对话,机器需要通过理解、主动询问、澄清等方式确定用户目标,然后查找知识库返回结果,完成用户需求。 如:机器人售电影票。
  • 聊天型对话:目标是产生有趣且富有信息量的自然答复使人机对话持续下去,如小度音响。

Feature

问答型对话(Search Dialogue Bot)

本地检索问答

计算用户问句与问答库中问句的相似度,选择最相似的问句,给出其对应的答复。

句子相似度计算包括以下方法:

  • TFIDF
  • BM25
  • OneHot
  • Query Vector

网络检索问答

对百度、Bing的搜索结果摘要进行答案的检索

  • 百度搜索,包括百度知识图谱、百度诗词、百度万年历、百度计算器、百度知道
  • 微软Bing搜索,包括bing知识图谱、bing网典

任务型对话(Task Oriented Dialogue Bot)

  • End to End Memory Networks(memn2n)
  • BABi dataset

聊天型对话(Generative Dialogue Bot)

  • GPT2 Model
  • Sequence To Sequence Model(seq2seq)
  • Taobao dataset

Demo

Official Demo: https://www.mulanai.com/product/dialogbot/

Install

The project is based on transformers 4.4.2+, torch 1.6.0+ and Python 3.6+. Then, simply do:

pip3 install torch # conda install pytorch
pip3 install -U dialogbot

or

pip3 install torch # conda install pytorch
git clone https://github.com/shibing624/dialogbot.git
cd dialogbot
python3 setup.py install

Usage

问答型对话(Search Bot)

example: examples/bot_demo.py

from dialogbot import Bot

bot = Bot()
response = bot.answer('姚明多高呀?')
print(response)

output:

query: "姚明多高呀?"
answer: "226cm"

任务型对话(Task Bot)

example: examples/taskbot_demo.py

聊天型对话(Generative Bot)

GPT2模型使用

基于GPT2生成模型训练的聊天型对话模型。

模型已经 release 到huggingface models:shibing624/gpt2-dialogbot-base-chinese

example: examples/genbot_demo.py

from dialogbot import GPTBot
bot = GPTBot()
r = bot.answer('亲 你吃了吗?', use_history=False)
print('gpt2', r)

output:

query: "亲 吃了吗?"
answer: "吃了"

GPT2模型fine-tune

数据预处理

在项目根目录下创建data文件夹,将原始训练语料命名为train.txt,存放在该目录下。train.txt的格式如下,每段闲聊之间间隔一行,格式如下:

真想找你一起去看电影
突然很想你
我也很想你

想看你的美照
亲我一口就给你看
我亲两口
讨厌人家拿小拳拳捶你胸口

今天好点了吗?
一天比一天严重
吃药不管用,去打一针。别拖着

运行preprocess.py,对data/train.txt对话语料进行tokenize,然后进行序列化保存到data/train.pkl。train.pkl中序列化的对象的类型为List[List],记录对话列表中,每个对话包含的token。

cd dialogbot/gpt/
python preprocess.py --train_path data/train.txt --save_path data/train.pkl

训练模型

运行train.py,使用预处理后的数据,对模型进行自回归训练,模型保存在根目录下的model文件夹中。

在训练时,可以通过指定patience参数进行early stop。当patience=n时,若连续n个epoch,模型在验证集上的loss均没有下降,则进行early stop,停止训练。当patience=0时,不进行early stop。

代码中默认关闭了early stop,因为在实践中,early stop得到的模型的生成效果不一定会更好。

python train.py --epochs 40 --batch_size 8 --device 0,1 --train_path data/train.pkl

更多的训练参数介绍,可直接看train.py中的set_args()函数中的参数说明

预测模型(人机交互)

运行interact.py,使用训练好的模型,进行人机交互,输入q结束对话之后,聊天记录将保存到sample.txt文件中。

python interact.py --no_cuda --model_dir path_to_your_model

执行interact.py时,可以尝试通过调整topk、topp、repetition_penalty、max_history_len等参数,调整生成的效果。更多的参数介绍,可直接看interact.py的set_args()函数中的参数说明 如果要使用GPU进行生成,则不要调用--no_cuda参数,并且通过--device gpu_id来指定使用哪块GPU。

问答示例
  • sample1
  user :在干嘛
chatbot:睡觉
  user :这么早就睡觉啦
chatbot:嗯啊你也早点睡啊
  user :我再玩一会儿手机
chatbot:早点休息晚安
  user :拜拜啦
chatbot:晚安晚安
  user :多喝热水
chatbot:哈哈哈
  user :多喝热水
chatbot:好的咯
  • sample2
  user :在吗
chatbot:在
  user :我不开心
chatbot:怎么啦
  user :最近好累啊
chatbot:是挺累的
  user :作业好多啊
chatbot:还好还好
  user :我觉得我快要挂科了
chatbot:我也是啊,但是我觉得我快熬出头了
  user :不说了,我还有好多作业没写
chatbot:我也是啊好多啊
  user :那先这样,下次再聊,再见了
chatbot:好哒

Dataset

闲聊语料分享

中文闲聊语料 数据集地址 语料描述
常见中文闲聊 chinese_chatbot_corpus 包含小黄鸡语料、豆瓣语料、电视剧对白语料、贴吧论坛回帖语料、微博语料、PTT八卦语料、青云语料等
50w中文闲聊语料 百度网盘【提取码:4g5e】GoogleDrive 包含50w个多轮对话的原始语料、预处理数据
100w中文闲聊语料 百度网盘【提取码:s908】GoogleDrive 包含100w个多轮对话的原始语料、预处理数据

中文闲聊语料的内容样例如下:

谢谢你所做的一切
你开心就好
开心
嗯因为你的心里只有学习
某某某,还有你
这个某某某用的好

你们宿舍都是这么厉害的人吗
眼睛特别搞笑这土也不好捏但就是觉得挺可爱
特别可爱啊

今天好点了吗?
一天比一天严重
吃药不管用,去打一针。别拖着

模型分享

模型 共享地址 模型描述
model_epoch40_50w shibing624/gpt2-dialogbot-base-chinese百度网盘(提取码:taqh)GoogleDrive 使用50w多轮对话语料训练了40个epoch,loss降到2.0左右。

Contact

  • Issue(建议):GitHub issues
  • 邮件我:xuming: [email protected]
  • 微信我:加我微信号:xuming624, 进Python-NLP交流群,备注:姓名-公司名-NLP

Citation

如果你在研究中使用了dialogbot,请按如下格式引用:

@misc{dialogbot,
  title={dialogbot: Dialogue Model Technology Tool},
  author={Xu Ming},
  year={2021},
  howpublished={\url{https://github.com/shibing624/dialogbot}},
}

License

授权协议为 The Apache License 2.0,可免费用做商业用途。请在产品说明中附加dialogbot的链接和授权协议。

Contribute

项目代码还很粗糙,如果大家对代码有所改进,欢迎提交回本项目,在提交之前,注意以下两点:

  • tests添加相应的单元测试
  • 使用python -m pytest来运行所有单元测试,确保所有单测都是通过的

之后即可提交PR。

Reference

  • Wen T H, Vandyke D, Mrksic N, et al. A Network-based End-to-End Trainable Task-oriented Dialogue System[J]. 2016.
  • How NOT To Evaluate Your Dialogue System: An Empirical Study of Unsupervised Evaluation Metrics for Dialogue Response Generation
  • A. Bordes, Y. Boureau, J. Weston. Learning End-to-End Goal-Oriented Dialog 2016
  • Zhao T, Eskenazi M. Towards End-to-End Learning for Dialog State Tracking and Management using Deep Reinforcement Learning [J]. arXiv preprint arXiv:1606.02560, 2016.
  • Kulkarni T D, Narasimhan K R, Saeedi A, et al. Hierarchical deep reinforcement learning: Integrating temporal abstraction and intrinsic motivation [J]. arXiv preprint arXiv:1604.06057, 2016.
  • BBQ-Networks: Efficient Exploration in Deep Reinforcement Learning for Task-Oriented Dialogue Systems
  • Deep Reinforcement Learning with Double Q-Learning
  • Deep Attention Recurrent Q-Network
  • SimpleDS: A Simple Deep Reinforcement Learning Dialogue System
  • Deep Reinforcement Learning with a Natural Language Action Space
  • Integrating User and Agent Models: A Deep Task-Oriented Dialogue System
  • The Curious Case of Neural Text Degeneration
  • DialoGPT: Large-Scale Generative Pre-training for Conversational Response Generation
  • vyraun/chatbot-MemN2N-tensorflow
  • huggingface/transformers
  • Morizeyao/GPT2-Chinese
  • yangjianxin1/GPT2-chitchat

More Repositories

1

pycorrector

pycorrector is a toolkit for text error correction. 文本纠错,实现了Kenlm,T5,MacBERT,ChatGLM3,LLaMA等模型应用在纠错场景,开箱即用。
Python
5,202
star
2

text2vec

text2vec, text to vector. 文本向量表征工具,把文本转化为向量矩阵,实现了Word2Vec、RankBM25、Sentence-BERT、CoSENT等文本表征、文本相似度计算模型,开箱即用。
Python
4,059
star
3

MedicalGPT

MedicalGPT: Training Your Own Medical GPT Model with ChatGPT Training Pipeline. 训练医疗大模型,实现了包括增量预训练(PT)、有监督微调(SFT)、RLHF、DPO、ORPO。
Python
2,564
star
4

python-tutorial

Python实用教程,包括:Python基础,Python高级特性,面向对象编程,多线程,数据库,数据科学,Flask,爬虫开发教程。
Jupyter Notebook
1,805
star
5

similarity

similarity: Text similarity calculation Toolkit for Java. 文本相似度计算工具包,java编写,可用于文本相似度计算、情感分析等任务,开箱即用。
Java
1,337
star
6

textgen

TextGen: Implementation of Text Generation models, include LLaMA, BLOOM, GPT2, BART, T5, SongNet and so on. 文本生成模型,实现了包括LLaMA,ChatGLM,BLOOM,GPT2,Seq2Seq,BART,T5,UDA等模型的训练和预测,开箱即用。
Python
893
star
7

similarities

Similarities: a toolkit for similarity calculation and semantic search. 相似度计算、匹配搜索工具包,支持亿级数据文搜文、文搜图、图搜图,python3开发,开箱即用。
Python
597
star
8

ChatPDF

RAG for Local LLM, chat with PDF/doc/txt files, ChatPDF
Python
435
star
9

pytextclassifier

pytextclassifier is a toolkit for text classification. 文本分类,LR,Xgboost,TextCNN,FastText,TextRNN,BERT等分类模型实现,开箱即用。
Python
431
star
10

parrots

Automatic Speech Recognition(ASR), Text-To-Speech(TTS) engine. 中英语音识别、多角色语音合成,支持多语言,准确率高
Python
423
star
11

nlp-tutorial

自然语言处理(NLP)教程,包括:词向量,词法分析,预训练语言模型,文本分类,文本语义匹配,信息抽取,翻译,对话。
Jupyter Notebook
353
star
12

pke_zh

pke_zh, python keyphrase extraction for chinese(zh). 中文关键词或关键句提取工具,实现了KeyBert、PositionRank、TopicRank、TextRank等算法,开箱即用。
Python
155
star
13

lmft

ChatGLM-6B fine-tuning.
Python
137
star
14

nerpy

🌈 NERpy: Implementation of Named Entity Recognition using Python. 命名实体识别工具,支持BertSoftmax、BertSpan等模型,开箱即用。
Python
98
star
15

pysenti

Chinese Sentiment Classification Tool. 情感极性分类,基于知网、清华、BosonNLP情感词典,易扩展,基准方法,开箱即用。
Python
74
star
16

companynameparser

company name parser, extract company name brand. 中文公司名称分词工具,支持公司名称中的地名,品牌名(主词),行业词,公司名后缀提取。
Python
71
star
17

ChatPilot

ChatPilot: 实现AgentChat对话,支持Google搜索、文件网址对话(RAG)、代码解释器功能,复现了Kimi Chat(文件,拖进来;网址,发出来)。
Svelte
63
star
18

chatgpt-webui

ChatGPT WebUI using gradio. 给 LLM 对话和检索知识问答RAG提供一个简单好用的Web UI界面
Python
61
star
19

judger

自动作文评分工具,支持中文、英文作文智能评分,支持评分模型自训练,支持WEKA处理模型数据,支持自定义评分算法。java开发。
Roff
52
star
20

CodeAssist

CodeAssist is an advanced code completion tool that provides high-quality code completions for Python, Java, C++ and so on. CodeAssist 是一个高级代码补全工具,高质量为 Python、Java 和 C++ 补全代码。
Python
51
star
21

relext

RelExt: A Tool for Relation Extraction from Text. 文本实体关系抽取工具。
Python
44
star
22

text-feature

文本特征提取,适用于小说,论文,议论文等文本,提取词语、句子、依存关系等特征。python开发。
Python
37
star
23

rater

rater, recommender systems. 推荐模型,包括:DeepFM,Wide&Deep,DIN,DeepWalk,Node2Vec等模型实现,开箱即用。
Python
36
star
24

github-hot

Tracking the hot Github repos and update daily 每天自动追踪Github热门项目
Python
26
star
25

labelit

labelit, label tool with active learning, for classification task. 自动标注,基于主动学习,边标注边学习,减少人工标注量。
Python
26
star
26

title-generator

Automatic Text Summarization and Title Generation.
Python
24
star
27

pinyin-tokenizer

pinyintokenizer, 拼音分词器,将连续的拼音切分为单字拼音列表。
Python
19
star
28

case-analysis

NLP之病历分析:从病历文本之中提取关键信息,便于后续分析处理。
Java
19
star
29

EssaySocring

英文作文自动评分系统,支持评分模型自训练,支持WEKA处理模型数据,支持自定义评分算法。Java开发。
Roff
16
star
30

crf-seg

crf-seg:用于生产环境的中文分词处理工具,可自定义语料、可自定义模型、架构清晰,分词效果好。java编写。
Java
13
star
31

authorship-identification

【今日头条】文本作者身份识别比赛
Jupyter Notebook
9
star
32

text2vec-service

Service for Bert model to Vector. 高效的文本转向量(Text-To-Vector)服务,支持GPU多卡、多worker、多客户端调用,开箱即用。
Python
9
star
33

fake-news-detector

Fake News Detection Competition
Python
8
star
34

ChatGPT-API-server

build a python server for ChatGPT API.
Python
7
star
35

zh-normalization

Chinese(zh) sentence NSW(Non-Standard-Word) Normalization
Python
5
star
36

nlpcommon

NLP common tools.
Python
5
star
37

cpp-tutorial

C++开发实例教程,基础,开源库进阶,高级技巧。
C++
4
star
38

text2vec-encoder

**Text2vecEncoder** wraps the text2vec model with jina. It encodes text data into dense vectors.
Python
4
star
39

cvnet

have fun with image AI
Jupyter Notebook
4
star
40

BlogDemo

我的csdn博客中使用的代码,主要是算法。
Java
3
star
41

sbert

sbert, sentence bert.
Python
2
star
42

Diffusion-Tuning

Diffusion-Tuning: Training Your Own Diffusion model with custom dataset.
Python
2
star
43

tools

tools
JavaScript
2
star
44

pyweb

Web server use tornado.
Python
1
star
45

html5-demos

Use the html5 to show funny web demos
JavaScript
1
star
46

shibing624

1
star
47

phrase-search

短语搜索,支持公司名称、地址名称等短语的搜索,支持自定义排序、拼音处理,内置jetty提供web接口。java编写。
Java
1
star