文章
ChatGPT接入微信公众号
安装python3环境
购买海外centos服务器后,直接执行下方命令,新机器99%一遍执行成功,不会有问题!
服务器每月20元 : https://www.007idc.cn/aff/EQLOPDLF
1、默认centos是python2.7,需要安装python3 我们创建一个python3文件夹
mkdir /usr/local/python3
cd /usr/local/python3
2、安装依赖,直接装就行,早晚用得到
yum -y groupinstall "Development tools"
yum -y install zlib zlib-devel
yum -y install bzip2 bzip2-devel
yum -y install ncurses ncurses-devel
yum -y install readline readline-devel
yum -y install openssl openssl-devel
yum -y install openssl-static
yum -y install xz lzma xz-devel
yum -y install sqlite sqlite-devel
yum -y install gdbm gdbm-devel
yum -y install tk tk-devel
yum -y install libffi libffi-devel
yum -y install wget
yum -y install vim
3、下载python安装包
wget
https://www.python.org/ftp/python/3.7.6/Python-3.7.6.tar.xz
4、解压python安装包
tar -xvJf Python-3.7.6.tar.xz
5、编译
cd Python-3.7.6
./configure --prefix=/usr/local/python3
make && make install
6、创建软连接
ln -s /usr/local/python3/bin/python3 /usr/bin/python3
ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3
7、升级到pip3
pip3 install --upgrade pip
9.2 安装依赖库
pip3 install openai // openai接口
pip3 install werobot // 微信聊天机器人模块
编写代码,直接CV 大法拿去用
api_key替换成你自己的
token随便设置,英文和数字,设置后等下要在公众号后台配置
import werobot
import openai
openai.api_key = "sk-***************************"
robot = werobot.WeRoBot()
class RobotConfig(object):
HOST="0.0.0.0"
PORT= 80
# token是微信公众号用来指定接入当前云服务器的服务的凭证,英文数字随便设置
TOKEN = "**********"
robot.config.from_object(RobotConfig)
def generate_response(prompt):
response = openai.Completion.create(
model="text-davinci-003",
prompt=prompt,
temperature=0.7,
max_tokens=3000,
top_p=1,
frequency_penalty=0,
presence_penalty=0
)
message = response.choices[0].text
return message.strip()
@robot.handler
def hello (messages):
print(messages.content)
return generate_response(messages.content)
if __name__ == "__main__":
robot.run()
我在本地编辑好后,直接vim粘进来的,你也可以用其他ftp工具上传文件
启动运行,显示如图代表成功
python3 index.py
如果想后台运行:
nohup python3 index.py logfile.log 2>&1 &
如果提示报错如下:
[root@mail code]# python3 index.py
Traceback (most recent call last):
File "index.py", line 2, in
import openai
File "/usr/local/python3/lib/python3.7/site-packages/openai/__init__.py", line 19, in
from openai.api_resources import (
File "/usr/local/python3/lib/python3.7/site-packages/openai/api_resources/__init__.py", line 1, in
from openai.api_resources.audio import Audio # noqa: F401
File "/usr/local/python3/lib/python3.7/site-packages/openai/api_resources/audio.py", line 4, in
from openai import api_requestor, util
File "/usr/local/python3/lib/python3.7/site-packages/openai/api_requestor.py", line 22, in
import requests
File "/usr/local/python3/lib/python3.7/site-packages/requests/__init__.py", line 43, in
import urllib3
File "/usr/local/python3/lib/python3.7/site-packages/urllib3/__init__.py", line 39, in
"urllib3 v2.0 only supports OpenSSL 1.1.1+, currently "
// 这句话就是报错原因
ImportError: urllib3 v2.0 only supports OpenSSL 1.1.1+, currently the 'ssl' module is compiled with OpenSSL 1.0.2k-fips 26 Jan 2017. See: https://github.com/urllib3/urllib3/issues/2168
直接安装一下pip3 install urllib3==1.25.11,把urllib3版本降低,再重新启动即可成功!
pip3 install urllib3==1.25.11
// 安装成功提示
Collecting urllib3==1.25.11
Downloading urllib3-1.25.11-py2.py3-none-any.whl (127 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 128.0/128.0 kB 3.7 MB/s eta 0:00:00
Installing collected packages: urllib3
Attempting uninstall: urllib3
Found existing installation: urllib3 2.0.2
Uninstalling urllib3-2.0.2:
Successfully uninstalled urllib3-2.0.2
Successfully installed urllib3-1.25.11
如果提示短口被占用
ps -ef|grep index.py
Kill -9 pid
9.3 公众号后台配置
一张图就说明问题了,照着图填入配置就行
- url:里面是你的云服务器的IP,后面加不了端口号,默认只能使用80端口,https默认就是443端口。/chatgpt可以随便写
- token:就是你代码里的token值,可以自己定义,但代码和配置要保持一致
- EncodingAESKey:随机生成就行
9.4 愉快的聊天
api访问有消息频率限制,发消息频率不要太快