5

【自然语言处理(NLP)】聊天机器人系统评测

 1 year ago
source link: https://blog.51cto.com/u_15745546/5780287
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

【自然语言处理(NLP)】聊天机器人系统评测


作者简介:在校大学生一枚,华为云享专家,阿里云专家博主,腾云先锋(TDP)成员,云曦智划项目总负责人,全国高等学校计算机教学与产业实践资源建设专家委员会(TIPCC)志愿者,以及编程爱好者,期待和大家一起学习,一起进步~
.
博客主页 ぃ灵彧が的学习日志
.
本文专栏 人工智能
.
专栏寄语:若你决定灿烂,山无遮,海无拦
.
【自然语言处理(NLP)】聊天机器人系统评测_人工智能


(一)、任务描述

通过Wechaty获取微信接收的消息,然后使用PaddleHub的plato-mini模型根据对话的上下文内容生成新的对话文本,最终以微信消息的形式发送。


(二)、环境配置

本示例基于飞桨开源框架2.0版本。

import paddle
import paddle.nn.functional as F
import re
import numpy as np

print(paddle.__version__)

# cpu/gpu环境选择,在 paddle.set_device() 输入对应运行设备。
# device = paddle.set_device('gpu')

输出结果如下图1所示:
【自然语言处理(NLP)】聊天机器人系统评测_系统评测_02


一、运行脚本

# 将项目代码clone到当前路径下
!git clone https://github.com/KPatr1ck/paddlehub-wechaty-demo.git

#切换路径
!cd paddlehub - wechaty-demo

#安装依赖
!pip install -r requirements.txt

#安装项目所需的PaddleHub的moddule
!hub install plato-mini == 1.0.0

!export WECHATY_PUPPET = wechaty-puppet-service
!export WECHATY_PUPPET_SERVICE_TOKEN = 'your-token'

#启动闲聊机器人
!python examples/paddlehub - chatbot.py

二、生成闲聊机器人

(一)、导入相关包

from collections import deque
import os
import asyncio

from wechaty import(
    Contact,
    FileBox,
    Message,
    Wechaty,
    ScanStatus,
)

from wechaty_puppet import MessageType


(二)、实例化模型

import paddlehub as hub

model = hub.Module(name='plato-mini',version='1.0.0')
model._interactive_mode = True
model.max_turn = 10
model.context = deque(maxlen=model.max_turn)


(三) 、重写on_message()方法

通过重写on_message()方法对收到的消息进行回复,该方法是接收到消息时的回调函数,可以通过自定义的条件(譬如消息类型、消息来源、消息文字是否包含关键字、是否是群聊消息等)来判断是否回复信息。


async def on_message(msg:Message):
    if isinstance(msg.text(),str) and len(msg.text()) >0 \
        and msg._payload.type == MessageType.MESSAGE_TYPE_TEXT \
        and msg.text().startswith('[Test'):

        bot_response = model.predict(data=msg.text().replace('[Test]',''))[0]
        await msg.say(bot_response)


四、机器人实例化

通过定义一个main()函数作为程序的入口函数,完成机器人的实例化并通过调用on_message()方法对收到的消息进行回复。


async def main():
    if 'WECHATY_PUPPET_SERVICE_TOKEN' not in os.environ:
        print('''
            Error:WECHATY_PUPPET_SERVICE_TOKEN is not found in the environment variables
            You need a TOKEN to run the Python Wechaty.Please goto our README for details
            https://github.com/wechaty/python - wechaty-getting-started/#wechaty_puppet_service_token
        ''')

    #聊天机器人实例化
    bot = WeChaty()

    bot.on('scan',on_scan)
    bot.on('login',on_login)

    bot.on('message',on_message)

    #启动聊天机器人
    await bot.start()

我们通过使用asyncio.run()函数来执行异步函数main(),这种方法使得聊天机器人在启动后,该函数可以在执行过程中被挂起,只有在接收到消息时才继续执行,对收到的信息进行回复。


本系列文章内容为根据清华社出版的《自然语言处理实践》所作的相关笔记和感悟,其中代码均为基于百度飞桨开发,若有任何侵权和不妥之处,请私信于我,定积极配合处理,看到必回!!!

最后,引用本次活动的一句话,来作为文章的结语~( ̄▽ ̄~)~:

学习的最大理由是想摆脱平庸,早一天就多一份人生的精彩;迟一天就多一天平庸的困扰。

【自然语言处理(NLP)】聊天机器人系统评测_系统评测_03

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK