作者:小傅哥
博客:https://bugstack.cn
积淀、分享、成长,让本人和别人都能有所播种!
首先我想通知你,从事编程开发这一行,要学会的是学习的形式办法。方向对了,能力事倍功半。而我认为最快且卓有成效的技术技能学习,就是上手实际。先不要搞太多的实践,买回来的自行车不能上来就拆,得先想方法骑起来。
所以小傅哥就是这样,学货色嘛。以指标为驱动,搭建可运行测试的最小单元版本。因为康威定律说;问题越小,越容易被了解和解决。所以在接触 ChatGPT 当前,我时常都在想怎么本人训练和部署一个这样的聊天对话模型,哪怕是很少的训练数据,让我测试也好。所以这个会喷人的傻狗机器人来了!
一、傻狗机器聊天
在基于前文小傅哥《搭个ChatGPT算法模型》的学习根底之上,以 OpenAI 开源的 GPT-2 和相干的 GPT2-chitchat 模型训练代码,部署了这个会喷人的傻狗机器人。但因为训练数据的问题,这个聊天机器人对起话来,总感觉很变态。—— 不过不影响咱们做算法模型训练的学习。
此页面为小傅哥所编程的WEB版聊天对话窗口
- 拜访地址:http://120.48.169.252/ - 服务器配置无限,不能承载过大的并发拜访。
- 视频演示:https://www.bilibili.com/vide... - 也能够通过B站视频,观看GPT2模型部署演示。
二、根底配置环境
OpenAI GPT2 的模型训练和服务应用,须要用到 Python、TensorFlow 机器学习等相干配置,并且这些环境间有些版本依赖。所以为了顺利调试尽可能和我放弃一样的版本。如果你对环境装置有难度,也能够找小傅哥帮忙买一台云服务器,之后我把我的环境镜像到你的服务器上就能够间接应用了。以下是所需的根本环境、代码和数据。
- 系统配置:Centos 7.9 -
2核4GB内存200G磁盘4Mbps带宽的云服务器
- 部署环境:Python3.7、 Transformers==4.2.0、pytorch==1.7.0
- 模型代码:https://github.com/fuzhengwei... - 此代码已开源,含websocket通信页面
- 模型数据:https://pan.baidu.com/s/1iEu_... -
ju6m
1 环境依赖
yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-develyum install gcc -yyum -y install libffi-develmakemake altinstall
2 Python 3.7
cd ~# 1.下载Python安装包wget https://www.python.org/ftp/python/3.7.4/Python-3.7.4.tgz# 2.将安装包挪动到/usr/local文件夹下mv Python-3.7.4.tgz /usr/local/# 3.在local目录下创立Python3目录mkdir /usr/local/python3# 4.进入的Python安装包压缩包所在的目录cd /usr/local/# 5.解压安装包tar -xvf Python-3.7.4.tgz# 6.进入解压后的目录cd /usr/local/Python-3.7.4/# 7.配置装置目录./configure --prefix=/usr/local/python3# 8.编译源码make# 9.执行源码装置make install# 10.创立软连贯ln -s /usr/local/python3/bin/python3 /usr/bin/python3# 11. 测试python3 -V
3. 装置pip3
cd ~# 1.下载wget https://bootstrap.pypa.io/get-pip.py# 2.装置;留神咱们装置了 python3 所以是 pyhton3 get-pip.pypython3 get-pip.py # 3.查找pip装置门路find / -name pip # 4.将pip增加到系统命令ln -s /usr/local/python/bin/pip /usr/bin/pip # 5.测试pip -V # 6.更换源,如果不更换那么应用 pip 下载软件会很慢pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simplepip config set install.trusted-host mirrors.aliyun.compip config list # pip国内镜像源:# 阿里云 http://mirrors.aliyun.com/pypi/simple/# 中国科技大学 https://pypi.mirrors.ustc.edu.cn/simple/# 豆瓣 http://pypi.douban.com/simple# Python官网 https://pypi.python.org/simple/# v2ex http://pypi.v2ex.com/simple/# 中国科学院 http://pypi.mirrors.opencas.cn/simple/# 清华大学 https://pypi.tuna.tsinghua.edu.cn/simple/
4. 装置git
cd ~# 1.装置前首先得装置依赖环境yum install -y perl-devel# 2.下载源码包到 CentOS 服务器后进行解压tar -zxf git-2.9.5.tar.gzcd git-2.9.5# 3.执行如下命令进行编译装置 ./configure --prefix=/usr/local/gitmake && make install# 4.增加到零碎环境变量vim ~/.bashrcexport PATH="/usr/local/git/bin:$PATH"# 5.使配置失效source ~/.bashrc# 6.测试git version
5. 装置宝塔
yum install -y wget && wget -O install.sh https://download.bt.cn/install/install_6.0.sh && sh install.sh 12f2c1d72
- 装置后登录宝塔提醒的地址,默认它会应用8888端口,因而你须要在服务器上开启8888端口拜访权限。
- 宝塔的装置是为了在服务端部署一个网页版聊天界面,应用到了 Nginx 服务。这里用宝塔操作更加容易。
三、模型运行环境
模型训练须要用到 transformers 机器学习服务,以及 pytorch、sklearn 等组件;以下内容须要别离装置;
transformers==4.4.2pytorch==1.7.0sklearntqdmnumpyscipy==1.2.1
1. transformers
pip install transformers==4.4.2
2. pytorch
pip install torch==1.7.0+cpu torchvision==0.8.1+cpu torchaudio===0.7.0 -f https://download.pytorch.org/whl/torch_stable.html
- 这个torch版本+cpu与torchvision 须要匹配。
3. 其余装置
残余的依照应用指令 pip install 就能够,另外在运行 GTP2-chitchat 时,如果提醒短少了某些组件,间接应用 pip 依照即可。
四、聊天页面配置
这里先把小傅哥给你筹备好的websocket页面代码,通过宝塔创立站点后部署起来。代码:https://github.com/fuzhengwei...
之后通过关上你的宝塔地址,创立站点和上传Web代码。
留神:目前的这份代码中拜访websocket的配置在index.js中,你须要批改成你的服务器地址。
if(!window.WebSocket){ alert("您的浏览器不反对WebSocket协定!举荐应用谷歌浏览器进行测试。"); return;}socket = new WebSocket("ws://120.48.169.252:7397");
五、模型训练部署
1. 下载代码
cd /homegit clone https://github.com/fuzhengwei/GPT2-chitchat.git
你须要批改下 interact.py 代码,变更这里有Websocket 的 IP和端口配置;
async def start_server(): try: async with websockets.serve(server, "192.168.0.4", 7397): print("Starting server at ws://localhost:7397") await asyncio.Future() # run forever except OSError as e: print(f"Error starting server: {e}") except Exception as e: print(f"Unexpected error: {e}")
2. 上传模型
下载模型:https://pan.baidu.com/s/1iEu_... - 明码:ju6m
上传模型:这里你须要在本机装置一个 SFTP 工具,或者应用 IntelliJ IDEA 提供的工具进行链接。链接后就能够把解压的模型上传到 /home/GPT2-chitchat/model 下。
async def start_server(): try: async with websockets.serve(server, "192.168.0.4", 7397): print("Starting server at ws://localhost:7397") await asyncio.Future() # run forever except OSError as e: print(f"Error starting server: {e}") except Exception as e: print(f"Unexpected error: {e}")
批改这部分代码的IP和端口,以及在云服务上开启 7397 的拜访权限。另外为了平安起见,能够在云服务的防火墙IP起源中受权,只有你以后的台机器才能够链接到 websocket 上。
3. 启动服务
这里小傅哥通过 mac nuoshell 连贯工具,进行模型启动;模型门路:/home/GPT2-chitchat/model/model_epoch40_50w
python3 interact.py --no_cuda --model_path /home/GPT2-chitchat/model/model_epoch40_50w
- 启动后就能够把你的 websocket 页面关上了,它会主动的链接到这个 websocket 服务上。
- 如果你还须要 Socket 或者命令行的服务,也能够批改 interact.py 代码进行解决。
以上就是整个 GPT2-chitchat 一个闲聊模型的部署,你也能够尝试应用 Docker 部署。如果在部署过程中切实很难部署胜利,也能够找小傅哥买云服务,这样我能够间接把镜像部署到你的云服务上,就能够间接应用了。