ChatGPT WeChat development, easy to handle

foreword

In the field of artificial intelligence, Aiit is already a common thing. Why is it so popular this openAitime ? chatGPTIn fact, this also benefits from GPTthe model.

So what is GPTa model? Let's take a look at chatGPTour answer:

GPT(Generative Pre-trained Transformer)It is a transformer-based language model for natural language processing (NLP NLP) tasks such as text generation, semantic analysis, and machine translation. GPTThe 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 chatGPTI 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, chatGPTthere 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 GitHubprojects. .

  • Open source project address: https://github.com/zhayujie/chatgpt-on-wechat
  • openAIOfficial website: https://platform.openai.com/
  • LinuxBasic operation of
  • gitbasic operation
  • openAIan 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 Ubuntuas a demonstration. (Note, in order to ensure openAIa 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_listin 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 itemgroup_chat_prefix
  • Optional configuration: group_name_keyword_white_listThe configuration item supports fuzzy matching of group names, and group_chat_keywordthe 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

imgimage-20230213005953519

Note that I am using the vim editor, if that is the same, then use ithe access editing mode after accessing the file, press after editing Escand then press :wqsave 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.

img


Chat screenshot

private chat

img

group chat

img


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. OpenAIRegistration tips on the official websiteNot avaliable

Generally, it does vpnnot 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 SSLconnection 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:

  1. 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.
  2. PythonThe version is too high (3.10 或 3.11), it is recommended to use 3.7.1 ~ 3.9version Python.

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 itchatand cannot log in to the web version of WeChat. The solution is to uninstall itchatand then install itchat-uos 1.5.0.dev0the 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 linuxthe server. The reason is that after the mobile phone scans the code, there is a remote login verification, 5sand it will wait. At this time, itchatit is judged that the login has timed out, and the QR code is refreshed, resulting in the login failure. The solution is to itchatmodify login.pythe 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.jsonthe configuration below. Personal chat needs to send single_chat_prefixthe content starting with the configuration (it can also be triggered by itself); group chat needs to send the content starting with the group_chat_prefixconfiguration , or @being triggered directly by . At the same time, the group name needs to be configured group_name_white_listin .


friendly reminder

If you have not received the text content that triggers the automatic reply, check config.jsonthe configuration in the following, the personal chat needs to send single_chat_prefixthe content starting with the configuration (self-send can also be triggered); the group chat needs to send the content starting with group_chat_prefixthe configuration , or be will also@ trigger directly. At the same time, the group name needs to be configured group_name_white_listin .

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 chatGPTinto the Feishu robot. If you want to experience it directly, reply to the official account below with capital letters QCto get the group QR code, and use Feishu APPto scan the code to join the group experience.

Guess you like

Origin blog.csdn.net/qq_37215621/article/details/130517060