10分钟企业微信集成ChatGPT

322 阅读3分钟

创建一台香港服务器,初始化服务器环境

初始化服务器

dnf upgrade
dnf update

安装高版本的python

一般服务器默认的python版本比较低, 一般是3.6.8,我这边通过源码编译的方式,把版本升级到3.9.x

wget https://www.python.org/ftp/python/3.9.18/Python-3.9.18.tgz
tar -zxvf Python-3.9.18.tgz
cd Python-3.9.18/

yum install zlib-devel libffi-devel openssl-devel -y

./configure --with-system-ffi

make && make install

编译安装好之后,重新打开shell,打开python终端可以看到版本升级了

安装nginx 并配置反向代理

安装nginx

dnf install nginx

service nginx start

nginx配置文件

vi /etc/nginx/conf.d/chat.conf
upstream xxx.com {
        server 0.0.0.0:9898 weight=10;    # 本机,考虑到使用负载均衡和消息处理,只配权重10
}

server {
    root   html;
    index  index.html index.htm;
    listen 80;
    server_name xxx.com;

    location / {
        # 其他接口
            proxy_connect_timeout   10;
            proxy_send_timeout      300;
            proxy_read_timeout      300;
            proxy_pass http://xxx.com;
            proxy_set_header HOST $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

配置好之后,重新加载nginx

service nginx reload

配置企业微信服务

下载源码

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

# 安装依赖
cd chatgpt-on-wechat
pip3 install --upgrade pip
pip3 install -r requirements.txt

这里可能会遇到报错

 ModuleNotFoundError: No module named '_ctypes'

说明之前缺少了依赖库 libffi-devel,安装依赖之后,需要重新编译Python

dnf install libffi-devel -y
./configure --with-system-ffi
make && make install

配置文件

{
  "channel_type": "wechatcom_app",
  "open_ai_api_key": "sk-xxxx",
  "model": "gpt-4-1106-preview",
  "text_to_image": "dall-e-3”,
  "voice_to_text": "openai",
  "text_to_voice": "openai",
  "proxy": "http://127.0.0.1:7890",
  "hot_reload": false,
  "single_chat_prefix": [],
  "single_chat_reply_prefix": "",
  "group_chat_prefix": [
    "@xxx"
  ],
  "group_name_white_list": [],
  "group_chat_in_one_session": [],
  "image_create_prefix": [
    "画"
  ],
  "speech_recognition": true,
  "group_speech_recognition": false,
  "voice_reply_voice": false,
  "tts_voice_id": "alloy",
  "conversation_max_tokens": 1000,
  "expires_in_seconds": 3600,
  "character_desc": "你是ChatGPT, 一个由OpenAI训练的大型语言模型, 你旨在回答并解决人们的任何问题,并且可以使用多种语言与人交流。",
  "temperature": 0.7,
  "top_p": 1,
  "subscribe_msg": "感谢您的关注!\n这里是ChatGPT,可以自由对话。\n支持语音对话。\n支持图片输入。\n支持图 片输出,画字开头的消息将按要求创作图片。\n支持tool、角色扮演和文字冒险等丰富的插件。\n输入{trigger_prefix}#help 查看详细指令。",
  "use_linkai": false,
  "linkai_api_key": "",
  "linkai_app_code": "",
  "wechatcom_corp_id": "xxx",
  "wechatcomapp_token": "xxx",
  "wechatcomapp_port": 9898,
  "wechatcomapp_secret": "xxx",
  "wechatcomapp_agent_id": "1000002",
  "wechatcomapp_aes_key": "xxx"
}

企业微信配置信息:

wechatcom_corp_id:我的企业->企业ID

wechatcomapp_secret

wechatcomapp_agent_id:应用管理-> 进入应用 -> AgentId

wechatcomapp_token

wechatcomapp_aes_key: 应用管理-> 进入应用 -> 接收消息 -> EncodingAESKey

运行

python3 app.py

如果运行报错: Can't connect to HTTPS URL because the SSL module is not available

dnf install openssl-devel

重新编译Python

使用

打开企业微信,进入“工作台” tab,找到添加的应用,点击进入对话界面。

聊天窗口进行GPT问答

通过绘画关键词,“画” “Design”,进行AI绘画

后记

完成企业微信搭建之后,顺便给公司市场部,进行了GPT使用的培训。增加了企业内部使用的场景,提升工作流程中的效率