微信对接ChatGPT

1、OpenAI 注册并获取 API

  • 注册要点:选择 OpenAI 支持的国家的节点(例如:美国、新加坡、台湾等)、节点要比较干净(用的人少)、客户端开启全局模式。
  • 创建 API,然后复制保存好:https://platform.openai.com/account/api-keys

2、下载并安装 SSH 连接工具 Finalshell:【点击进入

3、安装 python

  • 安装构建 Python 源所需的包:
1
2
apt update
apt install build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libreadline-dev libffi-dev wget
1
curl -O https://www.python.org/ftp/python/3.8.18/Python-3.8.18.tar.xz
  • 下载完成后解压 tar 压缩包
1
tar -xf Python-3.8.18.tar.xz
  • 导航到 Python 源目录并运行 configure 脚本
1
2
cd Python-3.8.18
./configure --enable-optimizations
  • 编译并安装程序
1
make && make install

4、安装 ffmpeg

1
apt install ffmpeg

5、安装 git

1
apt install git

6、克隆项目代码:

1
2
git clone https://github.com/zhayujie/chatgpt-on-wechat
cd chatgpt-on-wechat/

7、安装核心依赖 :

1
pip3 install -r requirements.txt

8、配置 config.json(配置文件的模板在根目录中config-template.json,需要复制该模板创建最终生效的config.json文件)

1
cp config-template.json config.json

9、在后台运行程序使用 nohup 命令:

1
2
touch nohup.out                                   # 首次运行需要新建日志文件
nohup python3 app.py & tail -f nohup.out # 在后台运行程序并通过日志输出二维码

第二种方法部署

本教程基于 Github 开源项目:chatgpt-on-wechat

该项目支持将 GPT 系列、文心一言、讯飞星火、MJ 绘图(第三方)接入到个人微信、群聊、公众号、企业微信

推荐使用国内服务器部署,登陆微信小号会稳定一点(微信基本两三天掉一次)

讯飞星火https://xinghuo.xfyun.cn/

文心一言https://console.bce.baidu.com/qianfan/overview

一、安装 Python

说明:使用国内机器安装较慢,可以手动去官网下载软件包上传(默认上传到 root 目录)

1
2
3
4
5
6
7
8
9
10
11
12
#安装依赖包
yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gcc make libffi-devel
#下载源码包
wget https://www.python.org/ftp/python/3.8.1/Python-3.8.1.tgz
#解压压缩包
tar -zxvf Python-3.8.1.tgz
#进入文件夹
cd Python-3.8.1
#配置安装位置
./configure prefix=/usr/local/python3
#安装
make && make install
1
2
3
4
5
6
7
#移除原有软链接
#若为全新服务器,可能不需要移除(本来就没有)
sudo rm /usr/bin/python3
sudo rm /usr/bin/pip3
#添加新的软链接
ln -s /usr/local/python3/bin/python3.8 /usr/bin/python3
ln -s /usr/local/python3/bin/pip3.8 /usr/bin/pip3
1
2
#更新 pip3
pip3 install --upgrade pip

完成以上步骤后,回到 root 目录

二、部署项目包

说明:国内机器可能无法 git 项目,可手动下载上传(默认上传到 root 目录)

1
2
3
4
5
6
7
#下载
git clone https://github.com/zhayujie/chatgpt-on-wechat
#进入目录
cd chatgpt-on-wechat/
#安装依赖
pip3 install -r requirements.txt
pip3 install -r requirements-optional.txt

以上为前置部署,后续接入个人微信、订阅号、服务号、企业微信皆基于以上配置

以下为程序配置内容

三、程序配置

具体参考:https://link-ai.tech/cow/quick-start/config

新建 config.json 文件

1
2
3
4
5
{
"use_linkai": false,
"linkai_api_key": "",
"linkai_app_code": ""
}

通用配置

#连续对话 Tokens 上限
“conversation_max_tokens”: 数值,

#触发回复文本前缀
“single_chat_prefix”: [“内容”],

#私聊回复文本前缀
“single_chat_reply_prefix”: “[内容]”,

#群组触发文本前缀
“group_chat_prefix”: [“内容”],

#按群组名称开启机器人回复
“group_name_white_list”: [“ALL_GROUP”],

#群聊共享一个上下文
“group_chat_in_one_session”: [“ALL_GROUP”],

#画图触发词(调用 ChatGPT)
“image_create_prefix”: [“画”]

模型配置:

说明:可同时添加到配置文件中,管理员模式下使用”#model model 名”来切换全局模型

1
2
3
4
5
6
7
8
9
10
11
12
13
1.文心一言
"model": "wenxin",
"baidu_wenxin_api_key":"",
"baidu_wenxin_secret_key":"",
2.ChatGPT
"model": "gpt-3.5-turbo",
"open_ai_api_key": "",
"proxy": "",
3.讯飞星火
"model": "xunfei",
"xunfei_app_id":"",
"xunfei_api_secret":"",
"xunfei_api_key":"",

渠道配置

说明:同一项目仅能接入一个渠道

1.订阅号

1
2
3
4
5
6
7
8
9
"channel_type": "wechatmp",

"single_chat_prefix": [""],
"wechatmp_app_id": "wx73f9**d1e48",
"wechatmp_app_secret": "weca**est",
"wechatmp_aes_key": "",
"wechatmp_token": "tok**est",
"wechatmp_port": 80,
"subscribe_msg": "订阅回复内容"

2.服务号

1
2
3
4
5
6
7
8
9
"channel_type": "wechatmp_service",

"single_chat_prefix": [""],
"wechatmp_app_id": "wx73f9**d1e48",
"wechatmp_app_secret": "weca**est",
"wechatmp_aes_key": "",
"wechatmp_token": "tok**est",
"wechatmp_port": 80,
"subscribe_msg": "订阅回复内容"

3.个人微信

注:企业微信不演示,因为我没有

4.企业微信

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
"channel_type": "wechatcom_app",

"single_chat_prefix": [""],

"wechatcom_corp_id": "",

"wechatcomapp_token": "",

"wechatcomapp_secret": "",

"wechatcomapp_agent_id": "",

"wechatcomapp_aes_key": "",
"wechatcomapp_port": 9898
"subscribe_msg": "订阅回复内容"

四、管理员插件配置

五、程序启动、终止、重载

#生成日志文件
touch nohup.out

#运行程序并输出日志
nohup python3 app.py & tail -f nohup.out

#关闭日志,不影响运行
crtl+c

#再次查看日志
tail -f nohup.out

#查找程序进程
ps -ef | grep app.py | grep -v grep

#杀死进程
kill -9

推荐用link.Ai

7388ed3e2feac05413722.png
https://link-ai.tech/home?share=su8mSd

openai-hk

https://openai-hk.com/?i=15831

python官网

https://www.python.org/downloads/

[up主专用,视频内嵌代码贴在这]