chat-plus 二次开发部署

157 阅读5分钟

简介

ChatGPT-PLUS

基于 AI 大语言模型 API 实现的 AI 助手全套开源解决方案,自带运营管理后台,开箱即用。集成了 OpenAI, Azure, ChatGLM,讯飞星火,文心一言等多个平台的大语言模型。集成了 MidJourney 和 Stable Diffusion AI绘画功能。主要有如下特性:

  • 完整的开源系统,前端应用和后台管理系统皆可开箱即用。

  • 基于 Websocket 实现,完美的打字机体验。

  • 内置了各种预训练好的角色应用,比如小红书写手,英语翻译大师,苏格拉底,孔子,乔布斯,周报助手等。轻松满足你的各种聊天和应用需求。

  • 支持 OPenAI,Azure,文心一言,讯飞星火,清华 ChatGLM等多个大语言模型。

  • 支持 MidJourney / Stable Diffusion AI 绘画集成,开箱即用。

  • 支持使用个人微信二维码作为充值收费的支付渠道,无需企业支付通道。

  • 已集成支付宝支付功能,支持多种会员套餐和点卡购买功能。

  • 集成插件 API 功能,可结合大语言模型的 function 功能开发各种强大的插件,已内置实现了微博热搜,今日头条,今日早报和 AI 绘画函数插件。

    github.com/xtcel/chatg…

官方部署脚本探究

项目使用 docker 部署,官方文档使用的部署命令是:

bash -c "$(curl -fsSL https://img.r9it.com/tmp/install-v3.2.4-7b5ff48154.sh)"

我并没有直接使用这个命令部署,因为想做二次开发,所以我先看一下这个 shell 脚本里面到底是什么。于是,我先使用 curl 拉取脚本到本地:

curl -fsSL https://img.r9it.com/tmp/install-v3.2.4-7b5ff48154.sh

脚本内容如下:

sudo rm -rf install.tar.gz chatgpt-plus

echo "You are installing chatgpt-plus-v3.2.0 source code:https://github.com/yangjian102621/chatgpt-plus"

source /etc/os-release
if [ "$ID" == "ubuntu" ]; then
    # Add Docker's official GPG key:
    sudo apt-get update
    sudo apt-get install ca-certificates curl gnupg -y
    sudo install -m 0755 -d /etc/apt/keyrings
    curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
    sudo chmod a+r /etc/apt/keyrings/docker.gpg

    # Add the repository to Apt sources:
    echo \
      "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
      $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
    sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
    sudo apt-get update
    echo "Try to installing docker ..."
    sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin docker-compose -y
elif [ "$ID" == "centos" ]; then
    sudo yum remove docker \
                      docker-client \
                      docker-client-latest \
                      docker-common \
                      docker-latest \
                      docker-latest-logrotate \
                      docker-logrotate \
                      docker-engine -y

    echo "Try to installing docker ..."
    sudo yum install -y yum-utils
    sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
    sudo yum install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
else
    echo "Unsupported operation system, ONLY support with Ubuntu or Centos"
    return
fi

echo "Try to download installation package..."
wget "https://img.r9it.com/tmp/install-v3.2.4-7b5ff48154.tar.gz" -O install.tar.gz

tar -xf install.tar.gz && mv deploy chatgpt-plus

cd chatgpt-plus && docker-compose up -d 

脚本的大致逻辑是先安装 docker ,然后再将 deploy 部署文件覆盖,然后再使用 docker 部署项目。

我的服务器已经安装了 docker ,所以安装 docker 这个步骤完全不需要,只需要将后面一段代码复制出来运行就可以了。

制作自己的镜像

查看 deploy 的 docker-compose 文件我们可以发现,项目直接拉取了作者的镜像,有四个镜像:

chatgpt-plus-api

chatgpt-plus-web

mysql

redis

其中 MySQL 和 Redis 是公有镜像,提供 MySQL 和 Redis 服务,而chatgpt-plus-api 和 chatgpt-plus-web为私有镜像,包含了 go 后端源码、web前端代码、go 服务、nginx 反向代理服务。

如果你想自己二次开发,必须制作自己的chatgpt-plus-api 和 chatgpt-plus-web 镜像,将自己的代码打包到镜像里面。

查看项目部署文档,我们可以看到里面有构建打包的教程。

后端构建

项目后端是GO语言开发的,所以你需要提前准备GO语言编译环境,发环境依赖的GO语言版本是1.19,请先确保当前机器已经安装好GO语言开发环境。如果你是 linux 系统,我们已经为你准备了 Makefile 文件,你只需要运行:

git clone https://github.com/yangjian102621/chatgpt-plus.git
cd chatgpt-plus/api
# AMD 架构
make clean amd64
# ARM 架构
make clean arm64

编译好的可执行文件在 bin/chatgpt-plus-linux,可以直接运行。 如果是Windows或者MacOS,你需要手动编译:

go build -o bin/chatgpt-linux main.go
前端构建

前端基于 Vue + ElementPlus 开发,Node 版本为V18以上。

cd web
# 安装依赖
npm install
# 编译
npm run build 

编译好的文件在 dist 目录下,可以直接嵌入 Nginx Web 服务器运行。

镜像打包

我们推荐你将运行程序和环境打包成 docker 镜像,使用 docker 来运行 ChatPlus 应用。

1、打包 API 应用:

# 先编译
cd api
make clean amd64
#  再打包
cd ../build
docker build -t chatgpt-plus-api:$version -f dockerfile-api-go ../

记得把上面的 $version 变量替换成你要打包的版本号,如 v3.2.2

2、打包前端应用

# 先编译
cd web
npm run build
# 再打包
cd ../build
docker build -t chatgpt-plus-web:$version -f dockerfile-vue ../

按照上面的步骤,编译打包自己的镜像。

然后再将本地打包好的镜像上传到自己的阿里云镜像仓库即可。

部署服务

制作好自己的镜像后,需要修改 deploy 目录下的 docker-compose.yaml 文件。

修改如下:

version: '3'
services:
  # mysql
  chatgpt-plus-mysql:
    image: mysql:8.0.33
    container_name: chatgpt-plus-mysql
    command: --default-authentication-plugin=mysql_native_password
    restart: always
    environment:
      - MYSQL_ROOT_PASSWORD=xxxxx
    ports:
      - "3307:3306"
    volumes :
      - ./mysql/conf/my.cnf:/etc/mysql/my.cnf
      - ./mysql/data:/var/lib/mysql
      - ./mysql/logs:/var/log/mysql
      - ./mysql/init.d:/docker-entrypoint-initdb.d/

  # redis 
  chatgpt-plus-redis:
    image: redis:6.0.16
    restart: always 
    container_name: chatgpt-plus-redis
    command: redis-server --requirepass xxxxxx
    volumes :
      - ./redis/data:/data
    ports:
      - "6380:6379"

  # 后端 API 程序
  chatgpt-plus-api:
    image: registry.cn-guangzhou.aliyuncs.com/xtcel/chatgpt-plus-api:v3.2.4
    container_name: chatgpt-plus-api
    restart: always
    depends_on:
      - chatgpt-plus-mysql
      - chatgpt-plus-redis
    environment:
      - DEBUG=false
      - LOG_LEVEL=info
      - CONFIG_FILE=config.toml
    ports:
      - "5678:5678"
      - "9999:9999"
    volumes:
      - /usr/share/zoneinfo/Asia/Shanghai:/etc/localtime
      - ./conf/config.toml:/var/www/app/config.toml
      - ./logs:/var/www/app/logs
      - ./static:/var/www/app/static

  # 前端应用
  chatgpt-plus-web:
    image: registry.cn-guangzhou.aliyuncs.com/xtcel/chatgpt-plus-web:v3.2.4
    container_name: chatgpt-plus-web
    restart: always
    depends_on:
      - chatgpt-plus-api
    ports:
      - "8080:8080"
    volumes:
      - ./logs/nginx:/var/log/nginx
      - ./conf/nginx/conf.d:/etc/nginx/conf.d
      - ./conf/nginx/nginx.conf:/etc/nginx/nginx.conf
      - ./ssl:/etc/nginx/ssl

主要做的就是将镜像替换为自己做的镜像,然后 MySQL 和 Redis 的密码最好也修改为自己设置的密码。(再 config 里面配置)

将修改好的 deploy 目录上传到自己的服务器,再服务器运行启动 docker 服务命令。

cd deploy
docker-compose up -d

👌 你可以看到,和之前的命令一样,开始拉取镜像部署了。

后记

希望以上的教程可以帮助你进行 chatgpt-plus 的二次开发和部署。如果你也想做 ChatGPT 套壳站,可以关注微信公众号:程序员 xtcel,入群一起讨论。