chatterbot是一款python接口的,基于一系列规则和机器学习算法完成的聊天机器人。具有结构清晰,可扩展性好,简单实用的特点。本文通过chatterbot 的不同adapter来介绍如何构建自己的聊天机器人,关与chatterbot详细资料请请阅读源码,纯Python写的,阅读性比较强。好啦,我就直接上代码了。PS:现在正在收集语料库,过段时间更新基于深度循环网络LSTM的带有记忆的ChatBot。

安装


是的,安装超级简单(Ubuntu),用pip就可以啦~

sudo pip install chatterbot

各式各样的Adapter

大家已经知道chatterbot的聊天逻辑和输入输出以及存储,是由各种adapter来限定的,我们先看看流程图,一会软再一起看点例子,看看怎么用。


Flask快速搭建AI聊天机器人 基于python的聊天机器人_json

基础版本

# -*- coding: utf-8 -*-
from chatterbot import ChatBot
# 构建ChatBot并指定
Adapterbot = ChatBot(
   'Default Response Example Bot',
     storage_adapter='chatterbot.storage.JsonFileStorageAdapter',
   logic_adapters=[
       {
           'import_path': 'chatterbot.logic.BestMatch'
       },
       {
           'import_path': 'chatterbot.logic.LowConfidenceAdapter',
           'threshold': 0.65,
           'default_response': 'I am sorry, but I do not understand.'
       }
   ],
   trainer='chatterbot.trainers.ListTrainer')
# 手动给定一点语料用于训练
bot.train([
   'How can I help you?',
   'I want to create a chat bot',
   'Have you read the documentation?',
   'No, I have not',
   'This should help get you started: http://chatterbot.rtfd.org/en/latest/quickstart.html'])
# 给定问题并取回结果
question = 'How do I make an omelette?'
print(question)
response = bot.get_response(question)
print(response)
print("\n")
question = 'how to make a chat bot?'
print(question)
response = bot.get_response(question)
print(response)

对话内容如下:

How do I make an omelette?
I am sorry, but I do not understand.
how to make a chat bot?
Have you read the documentation?

处理时间和数学计算的Adapter


# -*- coding: utf-8 -*-
from chatterbot import ChatBot
bot = ChatBot(
   "Math & Time Bot",
   logic_adapters=[
       "chatterbot.logic.MathematicalEvaluation",
       "chatterbot.logic.TimeLogicAdapter"
   ],  input_adapter="chatterbot.input.VariableInputTypeAdapter",
   output_adapter="chatterbot.output.OutputAdapter")
# 进行数学计算
question = "What is 4 + 9?"
print(question)response = bot.get_response(question)
print(response)
print("\n")
# 回答和时间相关的问题
question = "What time is it?"
print(question)
response = bot.get_response(question)
print(response)

对话如下:

What is 4 + 9?
( 4 + 9 ) = 13


What time is it?
The current time is 05:08 PM

导出语料到json文件

# -*- coding: utf-8 -*-
from chatterbot import ChatBot
'''如果一个已经训练好的chatbot,你想取出它的语料,用于别的chatbot构建,可以这么做'''
chatbot = ChatBot(
   'Export Example Bot',
 trainer='chatterbot.trainers.ChatterBotCorpusTrainer')
# 训练一下咯
chatbot.train('chatterbot.corpus.english')
# 把语料导出到json文件中chatbot.trainer.export_for_training('./my_export.json')

反馈式学习聊天机器人


# -*- coding: utf-8 -*-
from chatterbot import ChatBot
import logging
"""反馈式的聊天机器人,会根据你的反馈进行学习"""
# 把下面这行前的注释去掉,可以把一些信息写入日志中
# logging.basicConfig(level=logging.INFO)
# 创建一个聊天机器人
bot = ChatBot(
   'Feedback Learning Bot',
   storage_adapter='chatterbot.storage.JsonFileStorageAdapter',
   logic_adapters=[
       'chatterbot.logic.BestMatch'
   ],
   input_adapter='chatterbot.input.TerminalAdapter',
 output_adapter='chatterbot.output.TerminalAdapter')
DEFAULT_SESSION_ID = bot.default_session.id
def get_feedback():
   from chatterbot.utils import input_function
   text = input_function()
   if 'Yes' in text:
       return True
   elif 'No' in text:
       return False
   else:
       print('Please type either "Yes" or "No"')
       return get_feedback()
print('Type something to begin...')
# 每次用户有输入内容,这个循环就会开始执行
while True:
   try:
       input_statement = bot.input.process_input_statement()
       statement, response = bot.generate_response(input_statement, DEFAULT_SESSION_ID)
       print('\n Is "{}" this a coherent response to "{}"? \n'.format(response, input_statement))
       if get_feedback():
           bot.learn_response(response,input_statement)
       bot.output.process_response(response)
       # 更新chatbot的历史聊天数据
       bot.conversation_sessions.update(
           bot.default_session.id_string,
           (statement, response, )
       )
   # 直到按ctrl-c 或者 ctrl-d 才会退出
   except (KeyboardInterrupt, EOFError, SystemExit):
       break
反馈式学习聊天机器人

使用Ubuntu数据集构建聊天机器人

from chatterbot import ChatBot
import logging
'''这是一个使用Ubuntu语料构建聊天机器人的例子'''
# 允许打日志logging.basicConfig(level=logging.INFO)
chatbot = ChatBot(
   'Example Bot',   trainer='chatterbot.trainers.UbuntuCorpusTrainer')
# 使用Ubuntu数据集开始训练
chatbot.train()
# 我们来看看训练后的机器人的应答
response = chatbot.get_response('How are you doing today?')
print(response)

借助微软的聊天机器人

# -*- coding: utf-8 -*-
from chatterbot import ChatBot
from settings import Microsoft
'''关于获取微软的user access token请参考以下的文档https://docs.botframework.com/en-us/restapi/directline/'''
chatbot = ChatBot(
   'MicrosoftBot',
   directline_host = Microsoft['directline_host'],
   direct_line_token_or_secret = Microsoft['direct_line_token_or_secret'],
   conversation_id = Microsoft['conversation_id'],
   input_adapter='chatterbot.input.Microsoft',
   output_adapter='chatterbot.output.Microsoft',
   trainer='chatterbot.trainers.ChatterBotCorpusTrainer')chatbot.train('chatterbot.corpus.english')
# 是的,会一直聊下去
while True:
   try:
       response = chatbot.get_response(None)

   # 直到按ctrl-c 或者 ctrl-d 才会退出
   except (KeyboardInterrupt, EOFError, SystemExit):
       break