阿里云部署ChatGPT并微信接入实现国内访问
阿里云部署ChatGPT并微信接入实现国内访问

阿里云部署ChatGPT并微信接入实现国内访问

2023/4/7更新

阿里云挂梯子貌似是违规操作,于是紧急下线了梯子。

尽管如此,还是可以通过cloudflare`中转`api.openai.com

具体可以参考下面的方案:x-dr/chatgptProxyAPI

2023/3/30更新

作者最近更新后,修改了不少代码,对于 config.json 里面的 "single_chat_prefix": [""], 貌似现在必须指定prefix才能使用。但是稍微改以下代码即可。

问题出现在 channel/wechat/wechat_channel.py``_compose_context 函数。

主要对其作如下修改

else: # 单聊
    match_prefix = check_prefix(content, conf().get('single_chat_prefix'))  
    if match_prefix == 'no prefix':
        pass
    if match_prefix is not None: # 判断如果匹配到自定义前缀,则返回过滤掉前缀+空格后的内容
        content = content.replace(match_prefix, '', 1).strip()
    elif context["origin_ctype"] == ContextType.VOICE: # 如果源消息是私聊的语音消息,允许不匹配前缀,放宽条件
        pass
    else:
        return None                                       

同时修改check_prefix函数

def check_prefix(content, prefix_list):
    if prefix_list[0] == '':
        return "no prefix"
    for prefix in prefix_list:
        if content.startswith(prefix):
            return prefix
    return None

准备

  1. 科学上网

  2. 云服务器

  3. OpenAI账户与API Key,具体可以在这里查看Account - OpenAI API

    PS:Usage里面会看到新帐号的18$额度,API的付费标准是 1000tokens / 0.002$。

OK,开始部署

在阿里云上搭建V2rayA

v2rayA 的功能依赖于 V2Ray 内核,因此需要安装内核。

安装 V2Ray 内核

V2Ray 的官方脚本

V2Ray 安装参考:Debian / Ubuntu - v2rayA

v2rayA 提供的镜像脚本(推荐)

curl -Ls https://mirrors.v2raya.org/go.sh | sudo bash

安装后可以关掉服务,因为 v2rayA 不依赖于该 systemd 服务。

sudo systemctl disable v2ray --now

安装 v2rayA

添加公钥

wget -qO - https://apt.v2raya.org/key/public-key.asc | sudo tee /etc/apt/trusted.gpg.d/v2raya.asc

添加 V2RayA 软件源

echo "deb https://apt.v2raya.org/ v2raya main" | sudo tee /etc/apt/sources.list.d/v2raya.list
sudo apt update

安装 V2RayA

sudo apt install v2raya

启动 v2rayA

sudo systemctl start v2raya.service

设置开机自动启动

sudo systemctl enable v2raya.service

配置v2rayA

V2rayA的端口为2017,所以需要现在阿里云的配置安全组中配置防火墙开放2017端口。

之后就可以在本机访问http://IP:2017 进行图形化配置,具体界面如下所示。

image-20230321154537430

在导入订阅之后,就会发现SERVER出现了节点,然后就可以选择相应的节点,在左上角点击运行。

此外还需要在设置中配置一下

image-20230321154652138

到此为止,就已经可以让服务器科学上网了。

之后可以curl google.com 查看是否成功。

接入微信

在github上有很多轮子,我参考的是如下的方法:zhayujie/chatgpt-on-wechat: Wechat robot based on ChatGPT, which using OpenAI api and itchat library. 使用ChatGPT搭建微信聊天机器人,基于GPT3.5 API和itchat实现 (github.com)

Python环境配置

首先可能需要配置一下Python环境,我安装了miniconda来进行配置。

首先先下载miniconda:Miniconda — conda documentation

然后 sh Miniconda3-latest-Linux-x86_64.sh 安装即可,在一路yes下去之后,重新连接一下服务器就会发现已经环境搭配成功。

然后创建虚拟环境 conda create -n chatgpt python=3.8

并且激活环境 conda activate chatgpt

使用chatgpt-on-wechat

参考readme即可。

(1) 克隆项目代码:

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

(2) 安装核心依赖 (必选):

pip3 install itchat-uos==1.5.0.dev0
pip3 install --upgrade openai

(3) 配置

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

cp config-template.json config.json

(4) 运行

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

但会出现Log in time out, reloading QR code问题,参考issue修改代码即可

需要注意的是,使用conda的话,需要在对应的环境下运行python -m pip show itchat-uos

image-20230321155808402

然后在/root/miniconda3/envs/chatgpt/lib/python3.8/site-packages/itchat/components/`下查看login.py文件,在如下位置加入`time.sleep(15)即可。

image-20230321155929556

此外如果出现如下问题

login.py", line 197, in process_login_info
skey = re.findall('(.*?)', r.text, re.S)[0]
IndexError: list index out of range

一般原因是微信没有实名认证,前往支付板块进行实名认证后再登录。 同时可以检查下账号是否被冻结,如果是则需要解冻。

TIPS

如果使用的是IOS手机,可以在登录之后,关闭无线网和蜂窝移动,然后切换账号,大概等待30s他会出现网络失败,这个时候在连接网络切换账号,大概率不会把服务器上已经登陆的微信挤掉。

Reference

[1] Debian / Ubuntu - v2rayA

[2] Log in time out, reloading QR code. 二维码一直刷新,手机来不及登录 · Issue #8 · zhayujie/chatgpt-on-wechat (github.com)

[3] FAQs · zhayujie/chatgpt-on-wechat Wiki (github.com)

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注