A while ago, I shared how to connect a large model toWeChatThe robot tutorial is based on the chatgpt-on-wechat project, which uses the models of LinkAI and Zhipu AI. In subsequent tests, some problems were found, such as problems with the call of Zhipu AI knowledge base, and sometimes the hit rate was very low.
So I stopped using the robot and looked for new solutions until I found Coze (an AI robot and agent creation platform launched by ByteDance). It turned out that Coze was very easy to use and also fit my usage scenario.
Therefore, I wrote this article to record the deployment process of building my own WeChat group chat AI robot from scratch, including server purchase and configuration, project deployment, access to coze, modification of coze configuration, access to the knowledge base, plug-in installation, etc.
I originally planned to divide this article into two parts, but in the end I decided to complete the entire article directly. Although it is long, the operation is relatively simple and there is basically no problem following the instructions.
This article is generally divided into two parts:
- Deployment of chatgpt-on-wechat project
- CozeRobot configuration
There are two ways to deploy the chatgpt-on-wechat project:
- Source code deployment.
- I have created a docker image that can be deployed with one click.
Get the Docker image:
Reply to the public account (note capitalization)ChatOnWeChat
What is Coze?
Coze is a next-generation AI application and chatbot development platform for everyone. It allows users to easily create a variety of chatbots, regardless of whether the user has programming experience. Users can deploy and publish their own chatbots to different social platforms, such as Doubao, Feishu, WeChat, etc. Coze provides a wealth of features, including roles and prompts, plug-ins, knowledge base, opening dialogues, preview and debugging, workflows, etc. Users can also create their own plug-ins. In addition, Coze has a Bot Store that displays open source robots with various functions for users to browse and learn.
Register coze
Enter the official website
Come to coze official website
- China: https://www.coze.cn/
- Overseas: https://www.coze.com/
Here I use domestic coze.
If your business is overseas, it is recommended to use the foreign Coze, which I will not go into detail about here.
register

Get API key
We need to obtain the APIKey to call the coze large model interface.
After registration, click the button API at the bottom of the homepage

Click on the API Token option

Adding a New Token

Configure a personal token.
- Name: You can choose any name you want, I will keep the default name.
- Expiration time: Permanently valid.
- Select a specific team space: Personal
- Permissions: Check all.
Finally click Confirm.

The creation is successful. The token generated here needs to be saved! It will be used later. Click the Copy button to copy the token.

Creating a Bot
Go back to the home page and click Create Bot on the left

- Select the default Personal workspace
- Fill in your own Bot name
- Bot function introduction fill in
After filling in the form, click Confirm

The creation is successful and the configuration interface is entered.

Get BotID
Without doing any detailed configuration, just click Publish.

Let's skip the opening scene, we don't need it.

You can fill in the release record here at will. I filled in the version number.
Select the publishing platform: Check the button Bot Store and Bot as API. (If you did not get the API key, you will not see the Bot as API option here)

Then click Publish and you can see that it is published successfully.

Click to copy the Bot link

The link is as follows: https://www.coze.cn/store/bot/7388292182391930906?bot_id=true
Among them, 7388292182391930906 is the BotID. Save the BotID as we will use it later.
One thing to note here:
Every time you modify the Bot, the BotID will change after it is released!
Register a cloud server
There are many cloud server providers. Here I choose Alibaba Cloud for deployment. The price may not be the cheapest, so you need to compare it yourself.

Configuration reference
Here I will briefly explain the configuration of the purchase interface for your convenience.

Select CentOS 7.9 64-bit as the image

Cloud disk 40G

Check the public IP and the service will be charged based on fixed bandwidth, with a minimum bandwidth of 1Mbps.
Open IPV4 port/protocol and check it here

Select a custom password for the login credentials and save it for later use in connecting to the server.

Cloud Server Configuration
Install the pagoda panel
First you need to install the server management interface, here choose to install Baota

When connecting, you will be prompted to log in to the instance and enter the password (the password set when creating the instance)


Baota installation command
-&&-.:


After the installation is complete, save the link in the red box and the account password.

Pay attention! Here it is prompted that the corresponding port number needs to be opened. We need to configure the corresponding port number in the Alibaba Cloud security group.

Configure security group rules
Click Manage Rules

Click Add Manually (There are many port numbers in the inbound direction because I bought this server before and it was idle. The port numbers in the inbound direction were added before, so you don’t need to add them here like I did)

Add ports 8080 and 13695 here. Port 8080 is the port number we will use to deploy the project later, which is fixed. Port 13695 is the port number that needs to be opened for accessing the Baota panel. The port number generated by each person here is different. Pay attention to your port number!

Saved successfully

Pagoda Configuration
Entering the Pagoda
After the security group is set up, go to the pagoda link you just saved, select the external network panel address, copy it to the browser and open it.

After entering, you need to log in and fill in the username and password just generated
Installation Kit
After entering for the first time, an installation kit will be recommended. Select LNMP here.

Then wait patiently for the installation to complete
Install Python
Click on the website--python project--python version management

Select Python version 3.9.7 (it is best to use Python 3.10 or below. The chatgpt-on-wechat project may have some problems with Python 3.10 or above)

Wait for the installation to complete

Project deployment
Source code deployment
Download the project source code from github https://github.com/zhayujie/chatgpt-on-wechat As of the time of writing this article, the version is 1.6.8

Go to the Pagoda panel--file--path/www/wwwroot

Upload the downloaded compressed package

Right click to unzip


Then go to the website--python project--add python project

Project path: Select the folder path you just unzipped.
Project Name: Keep the default.
Run file: Select the app.py script in the project folder
Project port: 8080 (check the release port at the back)
Python version: 3.9.7 just installed
Framework: Choose Python
Run mode: python
Install dependency packages: The requirements.txt file in the project folder will be automatically selected here

Click Submit.
Wait patiently and you will see the creation completed.

Remember to modify the security group in the Alibaba Cloud instance

Management Rules

In the input direction, select Manual Add

Set the port range to 8080 and the source to 0.0.0.0 and click Save

Open the terminal. If prompted to enter the ssh key, enter the password you created when you created the server.

Terminal interface input
-.

Installation complete
Then install the optional components
--.

Installation complete
Docker Image Deployment
Come to the Docker interface of Baota. If it is a newly built server, the Docker service is not installed.

Click Install.

After the installation is complete, come to the /www/wwwroot root directory of the pagoda.

Upload the image

Import the image and select the path /www/wwwroot that you just uploaded.

Import Success

Come to the container--add container

Container name: optional
Image: Select the image you just imported
Port: 8080 (remember to check the server's security group rules)
Simply select Create.

Create Success

Click Log

You can see that the project has run normally and generated a WeChat login QR code. Don't rush to scan it at this time, you also need to modify the project configuration.

So far, the deployment of the project using the docker image has been completed. Next, you can modify the COW configuration. The red box area here simply selects the areas that need to be modified. The specific meaning of the modification will be explained below.

Restart the project after modification.

Scan the QR code again to log in.

Modify COW
Here, if you are deploying with docker image, you can skip the step of modifying to support COZE. Go directly to the step of modifying configuration to modify config.json
Modify to support COZE

In the /www/wwwroot/chatgpt-on-wechat-1.6.8 folder, add the following line after line 177 in the config.py file
: , : , : , : ,
as follows:

config.py complete code
Create a new folder under /www/wwwroot/chatgpt-on-wechat-1.6.8/bot and name it "bytedance".

Then upload the bytedance_coze_bot.py file in /www/wwwroot/chatgpt-on-wechat-1.6.8/bot/bytedance

bytedance_coze_bot.py is as follows
3. In the /www/wwwroot/chatgpt-on-wechat-1.6.8/bot folder, modify the bot_factory.py file.
== .: .. ()

Complete code
(): == .:
In the /www/wwwroot/chatgpt-on-wechat-1.6.8/common folder, modify the Const.py file
=

Complete code
Under /www/wwwroot/chatgpt-on-wechat-1.6.8/bridge, modify the bridge.py file

Complete code
.. . .... (): (): .= { : ., :().(, ), :().(, ), :().(, ), }
Modify the configuration
Go to the project root directory and find the config-template.json file, which is the configuration file for startup.

The main changes are the following four lines. You can directly clear the original file configuration and paste the following configuration into your config.json file.
: , : , : , : ,

Complete code
{ : , : , : , : , : , : , : , : , : , : , : [ , ], : , : [ ], : [ , ], : [ ], : , : , : , : 2500, : 3600, : , : 0.7, : , : , : , : }
Run the project
Start a project
Come to the python project management interface.

Stop the project and start it through the terminal, because you need to get the 0 QR code to log in when starting

Open Terminal

Enter the following command
Create a log
.
Run app.py
.&-.
If the operation is successful, a QR code will be generated in the terminal. Just scan the code with the WeChat account you need to log in.

Test results


You can see that the project is running normally and the robot is responding normally.
The reply here is related to the function introduction I set when I created the robot. Just delete it.

Restart Project
If you need to close or restart the project during debugging
Enter the query command
-|.|-
Turn off the corresponding PID program
-9 15230
For example, the pid of the program here is 20945, enter kill -9 20945 to shut down the program.

About the Knowledge Base
Go to coze homepage to create knowledge base

Configuring Knowledge Base Options
There are three types of knowledge bases: text format, table format, and photo type.
Different types of uploads are handled differently.
Here we only talk about the processing of text format.
Select the import type according to your needs.

This is the knowledge base TXT I prepared, in the form of a question and answer.

Upload the prepared TXT file

Select automatic segmentation and cleaning

Click Confirm and wait for the process to complete.

You can see that the generated segmentation fragments divide this text into 2 paragraphs, but this is not what I want. I want one question and one answer to be one paragraph.

Click Resegment

For segmentation settings, choose Custom, and for the identifier, I used the symbol ###.

And the knowledge base TXT was modified, adding the identifier ### in front of each question

Divide it into 8 sections again, which is what I want.

Go to Robot Orchestration, click the + sign in the Knowledge Options

Select the newly created knowledge base

The default setting in the knowledge base is no recall response.

You can customize a response. For example, if the question a user asks is not in the knowledge base, the response will be the statement you set.

Test the effect
Ask about the knowledge base

You can see that the response will be based on the content in our knowledge base.

Test results in WeChat.

About Group Chat
Set up group chat
If you want the robot to reply in a group chat, you need to modify config.json. Modify the group_name_white_list group chat whitelist. Fill in the group name you want to reply to here. (The WeChat account logged in by the robot must be in this group chat)
: [ , ],
Set the welcome message
When a new person joins the group chat, you can set a welcome message. Add the following to the config.json file:
: ,
Remember to restart the project after setting config.json.
Effect test

About logs
The output log is in the run.log file in the project root directory

Character Adjustment
If you think the robot's reply is too monotonous, we can add some human characters. This is a prompt from Liu Yulong.

Paste the character into the settings.
Need to wait for review

Once the review is passed, we will test it directly to see the effect.
Test Results
Very individual!

Plugin Installation
Plugin Introduction
The COW project provides plug-in functions, and we can install corresponding plug-ins according to our needs.
The source.json file in the project plugins directory shows the repositories of some plugins.
{ : { : { : , : }, : { : , : }, : { : , : }, : { : , : }, : { : , : }, : { : , : }, : { : , : }, : { : , : }, : { : , : }, : { : , : } } }
Here I need to post some content in the group at a fixed time, so I need to install the timetask timing plug-in.
Start Installation
First, make sure the robot is logged in.
Chat privately with the robot in the WeChat chat window.
Enter the administrator login command.
123456 is a custom password. You can set the password in the config.json file in the /www/wwwroot/chatgpt-on-wechat-1.6.8/plugins/godcmd directory.

Change password to your custom password and restart the service.
{ : , : [] }
Authentication successful.

Install the timetask plugin command
There may be a prompt here that the installation failed due to network reasons.

Workaround
Enter in the Baota terminal and turn off SSL verification.
--.
Then re-execute the installation command.

After successful installation, execute the scan command.

Scheduled tasks
Tips: Talk to the robot and send the following scheduled task instructions
Add a scheduled task
【Instruction format】:**$time cycle time event**
- **: Command prefix. When the chat content starts with time, it will be regarded as a timed command.
- cycle: Today, tomorrow, the day after tomorrow, every day, working day, every week X (such as every Wednesday), YYYY-MM-DD date, cron expression
- time: Time at X o'clock and X minutes (e.g. 10:10), HH:mm:ss
- event: Things you want to do (supports general reminders and extension plug-ins in the project, details are as follows)
- Group title (optional): Optional. If not, the task will be executed normally. If this option is passed, a private message can be sent to the group with the target group title to set the task (the format is: group [group title], note that the robot must be in the target group)
- : ,, . - :10:30 - :10:30 - :10:30 - :10:30 -:[0 * * * *] -:10:30- :10:30- :10:30 [] : ,- ,,. : (:) : -. -,/..

Cancel scheduled tasks
First query the task number list, then select the task number to be canceled, and cancel the scheduled task

About Risk
After the last article was published, a fan left a message asking me whether this deployment mechanism has the risk of being blocked. To be honest, I only received a risk warning once on the WeChat account I used for testing, and there was no risk after it was unblocked and continued to be used. So far, it has been running stably for about a month. It is recommended that you use a small account to operate during the testing phase to reduce the risk of warnings.
at last
Congratulations! If you have read this far, you should be able to deploy the project successfully and run it on WeChat! If you encounter any problems in the middle, please leave a message below to communicate with me.