Table of contents
-
- foreword
- access preparation
- start tutorial
-
- Pull project & install dependencies
- Create related folders
- Modify and add configuration files
- Start the service and check the result
- Scan the code to log in and start chatting (not)
- Chat screenshot
- common problem
-
- 1. `OpenAI` official website registration prompts `Not available`
- 2. Project startup error `SSL` connection failure
- 3. Login error reports XML parsing failure
- 4. Login error `KeyError: 'wxsid'`
- 5. Login error `IndexError: list index out of range`
- 6. Log in timeout QR code to refresh `Log in timeout`
- 7. Login is successful but automatic reply cannot be triggered
- friendly reminder
foreword
In the field of artificial intelligence, Ai
it is already a common thing. Why is it so popular this openAi
time ? chatGPT
In fact, this also benefits from GPT
the model.
So what is GPT
a model? Let's take a look at chatGPT
our answer:
GPT(Generative Pre-trained Transformer)
It is a transformer-based language model for natural language processing (NLPNLP
) tasks such as text generation, semantic analysis, and machine translation.GPT
The model is pre-trained on a large corpus, and it uses word embeddings, multi-layer transformers, and encoders/decoders to build models for meaningful text generation in a given context.
In fact, from the perspective of use, there is no need to go deep into the principles of AI, and an in-depth understanding of the principles is not suitable for everyone.
Since chatGPT
I was born, I have experienced it for the first time, and it is really cool. Now this trend is also at its peak, but for most domestic users, chatGPT
there are still some thresholds for using it. Therefore, in line with the principle of impartiality and love first, I also published an article on how to use it at the first time ChatGPT
. I want everyone to share good things!
Since the content of this issue mainly explains how to connect the AI of great wisdom and monsters to our WeChat & WeChat group, if you don’t have an OpenAi account, I suggest you refer to my previous article: Can’t play yet ? I want everyone to use ChatGPT [detailed registration tutorial]
access preparation
The access method to be talked about this time is relatively simple. At present, many people at home and abroad have released the official API of chatGPT in the form of open source projects in a short time, so this time it is a deployment tutorial based on one of the open source GitHub
projects. .
- Open source project address: https://github.com/zhayujie/chatgpt-on-wechat
openAI
Official website: https://platform.openai.com/Linux
Basic operation ofgit
basic operationopenAI
an account that can be used- Computer and hands (you can do without brains, ∵ this is not technically difficult )
start tutorial
In fact, this open source project itself also provides deployment documents, so let me say, if you think you can, it is recommended to send it directly to the official project documents.
As for the deployment environment, you can choose by yourself. I use it here Ubuntu
as a demonstration. (Note, in order to ensure openAI
a server that can be connected, it is recommended to use an overseas server or cast a spell on the server:) 巴啦啦能量,科学上网....魔法
As for how to surf the Internet scientifically, I will not explore it here. This problem has to be solved by yourself.
Pull project & install dependencies
# 克隆项目
git clone https://github.com/zhayujie/chatgpt-on-wechat
cd chatgpt-on-wechat/
# 安装依赖
pip3 install itchat-uos==1.5.0.dev0
pip3 install --upgrade openai
The above paragraph was moved directly from the official website, and I feel that there is no difficulty in understanding it, just use it directly.
Create related folders
Open your server, use the following command to create a series of folders, backup.
# 创建项目文件夹chat
mkdir chat
# 接入chat文件夹,创建chatgpt-on-wechat以及config.yaml文件夹
cd chat
mkdir chatgpt-on-wechat
mkdir config.yaml
Modify and add configuration files
# config.json文件内容示例
{
"open_ai_api_key": "YOUR API KEY" # 填入上面创建的 OpenAI API KEY
"single_chat_prefix": ["bot", "@bot"], # 私聊时文本需要包含该前缀才能触发机器人回复
"single_chat_reply_prefix": "[bot] ", # 私聊时自动回复的前缀,用于区分真人
"group_chat_prefix": ["@bot"], # 群聊时包含该前缀则会触发机器人回复
"group_name_white_list": ["ChatGPT测试群", "ChatGPT测试群2"], # 开启自动回复的群名称列表
"image_create_prefix": ["画", "看", "找"], # 开启图片回复的前缀
"conversation_max_tokens": 1000, # 支持上下文记忆的最多字符数
"character_desc": "你是ChatGPT, 一个由OpenAI训练的大型语言模型, 你旨在回答并解决人们的任何问题,并且可以使用多种语言与人交流。" # 人格描述
}
Configuration instructions:
1. Personal Chat
- In personal chat, the content starting with "bot" or "@bot" needs to trigger the robot, and the corresponding configuration item
single_chat_prefix
(you can fill in if you don't need to trigger with the prefix"single_chat_prefix": [""]
) - The content of the robot’s reply will be prefixed with "[bot] " to distinguish real people. The corresponding configuration items are
single_chat_reply_prefix
(you can fill in if you don’t need the prefix"single_chat_reply_prefix": ""
)
2. Group Chat
- In group chat, the group name needs to be configured
group_name_white_list
in to enable group chat auto-reply. If you want to take effect for all group chats, you can directly fill in"group_name_white_list": ["ALL_GROUP"]
- By default, as long as someone is @, the robot will automatically reply; in addition, as long as the content beginning with "@bot" is detected in the group chat, it will also automatically reply (convenient to trigger by yourself), which corresponds to the configuration item
group_chat_prefix
- Optional configuration:
group_name_keyword_white_list
The configuration item supports fuzzy matching of group names, andgroup_chat_keyword
the configuration item supports fuzzy matching of group message content. The usage is the same as the above two configuration items.
3. How to obtainopen_ai_api_key
Direct address: https://platform.openai.com/account/api-keys
image-20230213005953519
Note that I am using the vim editor, if that is the same, then use
i
the access editing mode after accessing the file, press after editingEsc
and then press:wq
save and exit editing.
Start the service and check the result
# 本地测试运行
python3 app.py
# 服务器永久运行
touch nohup.out # 首次运行需要新建日志文件
nohup python3 app.py & tail -f nohup.out # 在后台运行程序并通过日志输出二维码
Scan the code to log in and start chatting (not)
After executing the above startup command, the server will output the QR code in the form of a log, and you can log in by scanning the code with WeChat.
If the following log information appears after scanning the code to log in, it means that the login is successful, and you can start chatting with the WeChat account that is logged into the account. This WeChat is one at this moment chatGPT
.
Chat screenshot
private chat
group chat
common problem
As a caring little padded jacket, I also posted the FAQ. If there is a problem during the deployment process, please prescribe the right medicine.
1. OpenAI
Registration tips on the official websiteNot avaliable
Generally, it does vpn
not take effect. Note that the region should be South Korea, the United States, etc. If switching between several regions does not work, try clearing the browser cache, or open it with a non-traceable page.
2. Project startup error reports SSL
connection failure
During handling of the above exception, another exception occurred:
requests.exceptions.ConnectTimeout: HTTPSConnectionPool(host='webpush.wx.qq.com', port=443): Max retries exceeded with url:
you can't get access to internet or wechat domain, so exit.
There may be two reasons:
- Network problem, use a browser to open the webpage WeChat to see if you can access it, check if the computer is down
vpn
, if so, turn it off and log in again. Python
The version is too high(3.10 或 3.11)
, it is recommended to use3.7.1 ~ 3.9
versionPython
.
3. Login error reports XML parsing failure
expatbuilder.py", line 223, in parseString
parser.Parse(string, True)
xml.parsers.expat.ExpatError: mismatched tag: line 64, column 4
Check if is installed itchat-uos,
and the version is1.5.0.dev0
4. Login errorKeyError:'wxsid'
login.py", line 183, in process_login_info
core.loginInfo['wxsid'] = core.loginInfo['BaseRequest']['Sid'] = cookies["wxsid"]
KeyError: 'wxsid'
The general reason is that you have used itchat
and cannot log in to the web version of WeChat. The solution is to uninstall itchat
and then install itchat-uos 1.5.0.dev0
the version.
5. Login errorIndexError: list index out of range
login.py", line 197, in process_login_info
skey = re.findall('(.*?)', r.text, re.S)[0]
IndexError: list index out of range
The general reason is that WeChat does not have real-name authentication, go to the payment section for real-name authentication and then log in.
6. Login timeout QR code refreshLog in timeout
Log in time out, reloading QR code.
This kind of situation often occurs on linux
the server. The reason is that after the mobile phone scans the code, there is a remote login verification, 5s
and it will wait. At this time, itchat
it is judged that the login has timed out, and the QR code is refreshed, resulting in the login failure. The solution is to itchat
modify login.py
the source code, refer to the detailed steps
7. Login is successful but automatic reply cannot be triggered
The general reason is that the text content that triggers the automatic reply has not been received. Check config.json
the configuration below. Personal chat needs to send single_chat_prefix
the content starting with the configuration (it can also be triggered by itself); group chat needs to send the content starting with the group_chat_prefix
configuration , or @
being triggered directly by . At the same time, the group name needs to be configured group_name_white_list
in .
friendly reminder
If you have not received the text content that triggers the automatic reply, check config.json
the configuration in the following, the personal chat needs to send single_chat_prefix
the content starting with the configuration (self-send can also be triggered); the group chat needs to send the content starting with group_chat_prefix
the configuration , or be will also@
trigger directly. At the same time, the group name needs to be configured group_name_white_list
in .
Tencent's risk control mechanism is relatively strict. If you log in to use WeChat in this way, there is a high probability that your account will be blocked. My temporary account has been lifted, so I integrated it chatGPT
into the Feishu robot. If you want to experience it directly, reply to the official account below with capital letters QC
to get the group QR code, and use Feishu APP
to scan the code to join the group experience.