AI Personal Learning
and practical guidance

Dify relies on ChatGPT-on-WeChat to access the WeChat ecosystem

By Han Fangyuan, "Dify on WeChat" open source project author

 

1. General

WeChat, as the most popular instant messaging software, has a huge amount of traffic.


WeChat's friendly chat window is a natural AI application LUI(Language User Interface)/CUI(Command User Interface).

WeChat not only has personal WeChat, but also provides public number, enterprise WeChat, enterprise WeChat application, enterprise WeChat customer service and other dialog channels, with a good WeChat ecology.

By plugging the Dify app into the WeChat ecosystem, you can create a powerful intelligent customer service that greatly reduces customer service costs and can also enhance the customer experience. This tutorial is to teach you how to utilize the Dify on WeChat project by touching your hands and connecting the Dify app to the WeChat ecosystem.

2. Dify access to personal microsoft ### 2.1. preparation

2.1.1 Creating chat assistants

(1) Introduction to Dify

Dify is an excellent platform for LLMOps (Large Language Model Operations). For a detailed introduction to Dify, please move to the official documentation Welcome to Dify | Chinese | Dify.

(2) Login to Dify official application platform

First of all, login to Dify official application platform, you can choose to use Github login or use Google login. In addition, you can also refer to the Dify official tutorial Docker Compose Deployment | Chinese | Dify Private Deployment, Dify is an open source project that supports private deployment.

Hands-on teaching you to connect Dify to WeChat ecosystem-1

(3) Creation of Dify Basic Orchestration Chat Assistant Application

Hands-on teaching you to connect Dify to WeChat ecosystem-1

After successfully logging in and going to the Dify page, we follow the steps below to create a basic orchestration chat assistant app

  1. Click on Studio at the top of the page
  2. Creating a Blank Application
  3. Application Type Select Chat Assistant
  4. Chat helper orchestration method selects basic orchestration
  5. Select the app icon and fill in a name for the app, such as Basic Orchestration Chat Assistant
  6. Click to create

Hands-on teaching you to connect Dify to WeChat ecosystem-1

After successful creation we are redirected to the page shown above and we continue to configure the application

  1. Select the model, e.g. gpt-3.5-turbo-0125
  2. Setting Model Parameters
  3. Fill in the application prompts

Hands-on teaching you to connect Dify to WeChat ecosystem-1

After the configuration is complete, we can test it in the dialog box on the right, and after the test is complete, do the following

  1. post
  2. update
  3. Access to the API

(4) Generate Basic Orchestration Chat Assistant API Key

Hands-on teaching you to connect Dify to WeChat ecosystem-1

After clicking "Access API", we will be redirected to the API management page shown above, in this page we follow the steps below to get the API key:

  1. Click on the API key in the upper right corner
  2. Click Create Key
  3. Copy Save Key

After saving the key, you also need to check the API server in the upper right corner, if the app is from Dify official website, the API server address is "https://api.dify.ai/v1", if it is privately deployed, please make sure you have your own API server address.

This concludes the preparations for creating a chat assistant, and there are only two things we need to save in this subsection:API keytogether withAPI server address

2.1.2. Download Dify on WeChat project

(1) Dify on WeChat Project Information

Dify on WeChatbe ChatGPT on WeChat The downstream branch of the Dify API, which additionally implements docking to the Dify API, support for the Dify Chat Assistant, support for the Agent invocation tool and knowledge base, and support for Dify workflows, see the GitHub repository Dify on WeChat for details.

(2) Download code and install dependencies

  1. Download Project Code

Copy

gitclonehttps://github.com/hanfangyuan4396/dify-on-wechat
cddify-on-wechat/
  1. Installing python

The Dify on WeChat project is written in python.python official websiteDownload and install python, it is recommended to install python 3.8 or above, I have tested version 3.11.6 in ubuntu and it works fine.

  1. Install core dependencies (mandatory):

Copy

pip3install-rrequirements.txt# Domestically, you can add the "-i https://mirrors.aliyun.com/pypi/simple" parameter at the end of this command to install the dependencies using the AliCloud mirror source
  1. Extended Dependencies (optional, recommended to install):

Copy

pip3install-rrequirements-optional.txt# Domestically, you can add the "-i https://mirrors.aliyun.com/pypi/simple" parameter at the end of this command to install the dependencies using the AliCloud mirror source

(3) Fill in the configuration file

We create a file named config.json in the root directory of the project with the following contents, and we add the following file to theSub-section 2.1.1 (4) Finally savedAPI keytogether with API server address Please putdify_api_baseconfigure as API server address .;dify_api_keyconfigure asAPI keyOther configurations remain unchanged.

(PS: Many of you may not have followed the steps given in my tutorial to the letter.Chat assistant typeof the Dify app, with a special note on thedify_app_typeconfiguration method, if you create theChat AssistantThe application should be configured to chatbot ; CreatedAgentThe application should be configured to agent ; createdworkflowThe application should be configured to workflow )

Copy

{
"dify_api_base": "https://api.dify.ai/v1",
"dify_api_key": "app-xxx",
"dify_app_type": "chatbot", "channel_type".
"channel_type": "wx", "model": "dify".
"model": "dify", "single_chat_pref".
"single_chat_prefix": [""],
"single_chat_reply_prefix":"",
"group_chat_prefix": ["@bot"],
"group_name_white_list": ["ALL_GROUP"]
}

2.2. Access to WeChat for basic orchestration chat assistants

2.2.1 Quick start tests

(1) Execute the following command in the Dify on Wechat project root directory

Copy

cddify-on-wechat
python3app.py# The command is usually python app.py on windows.

(2) Scan code login

Dify Access to WeChat Ecosystem-1

This project uses itchat to realize personal weibo login, there is a risk of blocking, it is recommended to use thereal name verificationpastWeChat miniature numberTo test, after the execution of the above command, we can see in the console to print the QR code as shown above, use WeChat to scan the code to log in, after logging in when you see "itchat: Start auto replying." characters, said that the login is successful, we can test.

(3) Conversation test

Hands-on teaching you to connect Dify to WeChat ecosystem-1

We see that the responses from the WeChat bot are consistent with the responses on the Dify test page. At this point, congratulations on successfully connecting Dify to your personal WeChat 🎉🎉🎉🎉🎉🎉!

(PS: Some friends here may see a normal reply to the message in the log, but did not receive the message in WeChat, please Don't message yourself on your own WeChat )

(4) Server deployment

  1. Source code deployment

Copy

cddify-on-wechat
nohuppython3app.py&tail-fnohup.out# Runs the program in the background and outputs the QR code via logs
  1. docker compose deployment

containerenvironment variablebe sure tooverrideconfig.json file, please modify the docker/docker-compose.yml file environment variables to your actual configuration, the configuration method is the same as theSubsection 2.1.1 (4) The configuration of config.json is consistent.

Make sure you configure it correctly! DIFY_API_BASE , DIFY_API_KEYtogether withDIFY_APP_TYPEEnvironment variables.

Copy

version:'2.0'
services.
Dify-on-wechat.
image:hanfangyuan/dify-on-wechat
container_name:dify-on-wechat
security_opt.
- seccomp:unconfined
security_opt: seccomp:unconfined
DIFY_API_BASE:'https://api.dify.ai/v1'
DIFY_API_KEY:'app-xx'
DIFY_APP_TYPE:'chatbot'
MODEL:'dify'
SINGLE_CHAT_PREFIX:'[""]'
SINGLE_CHAT_REPLY_PREFIX:'""'
GROUP_CHAT_PREFIX:'["@bot"]'
GROUP_NAME_WHITE_LIST:'["ALL_GROUP"]'

Then run the following command to start the container

Copy

cddify-on-wechat/docker# Enter the docker directory
dockercomposeup-d# Start the docker container
dockerlogs-fdify-on-wechat# View QR code and log in

2.3. Connecting the workflow orchestration chat assistant to WeChat

After accessing the basic Dify chat assistant application to WeChat, we next increase the difficulty and try to access the workflow orchestration chat assistant application to WeChat to realize a WeChat intelligent customer service with knowledge of the Dify platform, which can answer Dify workflow related knowledge for us.

2.3.1 Creating a knowledge base

(1) Download the knowledge base document

Hands-on teaching you to connect Dify to WeChat ecosystem-1

We're here.Dify Document RepositoryDownload the documentation for the Dify Workflow Introduction.

(2) Importing Knowledge Base in Dify

Hands-on teaching you to connect Dify to WeChat ecosystem-1

Go to the Knowledge Base page to create a knowledge base

Hands-on teaching you to connect Dify to WeChat ecosystem-1

Choose to import the existing text, upload the just downloaded introduce.md file, click next!

Hands-on teaching you to connect Dify to WeChat ecosystem-1

 

Hands-on teaching you to connect Dify to WeChat ecosystem-1

Select the following configuration

  • Segment setting: automatic segmentation and cleaning
  • Indexing method: high quality
  • Search Settings: Vector Search

Finally click Save and Process

Hands-on teaching you to connect Dify to WeChat ecosystem-1

We see that the knowledge base is being processed for embedding, so wait a few moments for the embedding to be successful.

2.3.2 Creating workflow orchestration chat assistants

Hands-on teaching you to connect Dify to WeChat ecosystem-1

We go into the Dify studio and click on Create from App Template

Hands-on teaching you to connect Dify to WeChat ecosystem-1

We use a Knowledge Base + Chatbot type template, set the app icon and name, and click Create!

Hands-on teaching you to connect Dify to WeChat ecosystem-1

After jumping to the workflow orchestration page, first click on the Knowledge Retrieval node, and then click on the "+" on the far right to add a knowledge base. We choose the previously uploaded introduce.md knowledge base, which is a basic introduction to the Dify workflow. Finally we click Add and the knowledge base node setup is complete.

Hands-on teaching you to connect Dify to WeChat ecosystem-1

Next, select the LLM node, click on Set Context, and we select the result variable, which holds the results of the knowledge retrieval.

Hands-on teaching you to connect Dify to WeChat ecosystem-1

After setting up the LLM node, we clicked on Preview to test it and entered the question: please introduce the dedify workflow. You can see that the correct introduction to the Dify workflow is finally output. Once the test is working properly, we return to edit mode.

Hands-on teaching you to connect Dify to WeChat ecosystem-1

After returning to edit mode, click Publish, Update, and Access API in turn

2.3.3. Generate Workflow Orchestration Chat Assistant API Key

After jumping to the API management page, we refer to the2.1.1 Subsection (4) Obtaining an API key for the Knowledge Base + Chatbot applicationtogether withAPI server address

2.3.4. Access to WeChat

together withSimilar to subsection 2.1.2 (3), we create a file named config.json in the root directory of the project with the following contents, and also put the dedify_api_baseconfigure asKnowledge Base + ChatbotThe API server address of the application, the dify_api_keyconfigure asKnowledge Base + ChatbotAPI key for the application, other configurations remain unchanged

Copy

{
"dify_api_base": "https://api.dify.ai/v1",
"dify_api_key": "app-xxx",
"dify_app_type": "chatbot", "channel_type".
"channel_type": "wx", "model": "dify".
"model": "dify", "single_chat_pref".
"single_chat_prefix": [""],
"single_chat_reply_prefix":"",
"group_chat_prefix": ["@bot"],
"group_name_white_list": ["ALL_GROUP"]
}

We follow2.2.1 SubsectionLaunch the program and scan the code to log in, then send a message to the WeChat bot for testing

Hands-on teaching you to connect Dify to WeChat ecosystem-1

The response from the WeChat bot is consistent with the response on the Dify test page. Congratulations on taking it a step further and getting the workflow orchestration app into your personal WeChat, you can import more official Dify documentation into your knowledge base and let the WeChat bot answer more Dify-related questions for you.

2.4. Connecting Agents to WeChat

2.4.1 Creating Agent Applications

Hands-on teaching you to connect Dify to WeChat ecosystem-1

Go to the Studio page, click Create Blank App, select Agent, set the icon and app name, and finally click Create

Hands-on teaching you to connect Dify to WeChat ecosystem-1

After successful creation, we will enter the Agent Application Configuration page, where we select the dialog model and then add tools. We first add the DALL-E drawing tool, the first time to use the tool requires authorization, generally we set the OpenAI API key and OpenAI base URL to use the DALL-E drawing tool.

Hands-on teaching you to connect Dify to WeChat ecosystem-1

After successful authorization, we add the DALL-E 3 painting tool

Hands-on teaching you to connect Dify to WeChat ecosystem-1

Then, go ahead and add the DuckDuckGo search engine and math tools for subsequent tool testing

Hands-on teaching you to connect Dify to WeChat ecosystem-1

We enter the question "Search for the number of stars of the open source project Dify, what is the number multiplied by 3.14", to confirm that the application can call the tool properly, we click on the release, update, access API in turn!

2.4.2. Generating Agent API Keys

We continue to refer to2.1.1 Subsection (4) Getting an Intelligent AssistantappliedAPI keytogether withAPI server address

2.4.3 Access to WeChat

We create a file named config.json in the root directory of the project with the following contents, and also put thedify_api_baseconfigure asintelligent assistantThe address of the application's API server;dify_api_keyconfigure asintelligent assistantThe API key for the application, noting that the application is theintelligent assistanttype application, you also need to put thedify_app_typeset to agent Other configurations remain unchanged

Copy

{
"dify_api_base": "https://api.dify.ai/v1",
"dify_api_key": "app-xxx",
"dify_app_type": "agent", "channel_type".
"channel_type": "wx", "model": "dfy
"model": "dify", "single_chat_prefix".
"single_chat_prefix": [""],
"single_chat_reply_prefix":"",
"group_chat_prefix": ["@bot"],
"group_name_white_list": ["ALL_GROUP"]
}

continued reference2.2.1 SubsectionLaunch the program and scan the code to log in, then send a message to the WeChat bot for testing

Hands-on teaching you to connect Dify to WeChat ecosystem-1

You can see that the WeChat bot can use the search and drawing tools normally. Once again, congratulations on getting the Dify Agent app into WeChat. Also congratulate me, I can go to bed first after writing this.

2.5. Connecting workflows to WeChat

2.5.1 Creating workflow applications

Hands-on teaching you to connect Dify to WeChat ecosystem-1

First you need to download the DSL file that I have pre-created in advance.Click here to download. Once the download is complete, go to the studio page, click Import DSL file, upload the file you downloaded in advance, and finally click Create.

Hands-on teaching you to connect Dify to WeChat ecosystem-1

Once created, follow the steps above to test. Click Run and type hello to ensure that this workflow outputs results properly.

You can build on this workflow, but for theType of workflowThe application, which has a very flexible input variable name,, in order to more easily access the WeChat robot, Dify on WeChat project engagementType of workflowspecific applicationThe input variable is named<strong>query</strong>(math.) genusThe output variable is named<strong>text</strong>The

Hands-on teaching you to connect Dify to WeChat ecosystem-1

After testing with no problems, follow the steps above to publish the application, clicking Publish, Update, and Access API in that order.

2.5.2. Generating Workflow API Keys

We similarly refer to2.1.1 Subsection (4) Acquisition of workflowsappliedAPI keytogether with API server address The

2.5.3. Access to WeChat

We create a file named config.json in the root directory of the project with the following contents, and also put thedify_api_baseconfigure asworkflowThe address of the application's API server;dify_api_keyconfigure asworkflowThe API key for the application, noting that the application is theworkflowtype application, you also need to put thedify_app_typeset to workflow Other configurations remain unchanged

Copy

{
"dify_api_base": "https://api.dify.ai/v1",
"dify_api_key": "app-xxx",
"dify_app_type": "workflow", "channel_type".
"channel_type": "wx", "model": "dify".
"model": "dify", "single_chat_pref".
"single_chat_prefix": [""],
"single_chat_reply_prefix":"",
"group_chat_prefix": ["@bot"],
"group_name_white_list": ["ALL_GROUP"]
}

cf. also2.2.1 SubsectionLaunch the program and scan the code to log in, then send a message to the WeChat bot to test it.

Hands-on teaching you to connect Dify to WeChat ecosystem-1

You can see that the robot successfully connects to the workflow api and replies, so far we have fully mastered how to create all types of Dify applications: basic chat assistant, workflow chat assistant, intelligent assistant, workflow, and we have also learned how to publish the above applications as APIs and access WeChat.

Next I will describe how to access the application to other channels of WeChat, such as public number, enterprise WeChat application, enterprise WeChat customer service and so on.

3. Dify access to enterprise wechat personal number (windows environment only)

  1. there are risk of suspension of authority (law) Please use Enterprise WeChatviolintest (machinery etc)
  2. When logging in the old version of Enterprise WeChat may appear Enterprise WeChat version is too low, can not log in the situation, refer to theissue1525Please try to change to another enterprise micro-signal and try again.

3.1. Download and install Enterprise WeChat

Make sure you have a windows computer and then download and install the specific version of enterprise microsoft on this computer.Official download link(math.) genusAlternate download linksThe

3.2. Creating the Dify application

We've been in front of the 2.1.1 , 2.3.2 ,2.4.1together with2.5.1The subsections describe the creation of 4 different Dify apps, Basic Chat Assistant, Workflow Chat Assistant, Intelligent Assistant, and Workflow, respectively, and you can create any one of them according to the tutorials above.

3.3. Downloading and installing Dify on WeChat

ground 2.1.2(2) step, download the code and install the dependencies, in order to subsequently follow the ntwork dependencies, the Make sure you have python version 3.8, 3.9 or 3.10 installed. The

3.4. Installation of ntwork dependencies

Since the installation source of ntwork is not very stable, you can download the corresponding whl file and install ntwork offline using the whl file

First you need to check your python version, type python on the command line to see the version information, then in thentwork-whldirectory to find the corresponding whl file, run thepip install xx.whlInstall the ntwork dependencies, note that "xx.whl" is replaced by the whl file's Actual path The

For example, my python version information is

"Python 3.8.5 (default, Sep 3 2020, 21:29:08) [MSC v.1916 64 bit (AMD64)]"

You can see that the python version is 3.8.5 and is AMD64 The corresponding whl file is ntwork-0.1.3-cp38-cp38-win_amd64.whl If you want to install it, you need to execute the following command

Copy

pipinstallyour-path/ntwork-0.1.3-cp38-cp38-win_amd64.whl

3.5 Filling in the configuration file

We create a file named config.json in the root directory of Dify on WeChat project, below is the configuration file with Dify intelligent assistant app as an example, please fill in the information of dedify_api_base, dedify_api_key, dedify_app_type of the app that you just created correctly, please note that channel_type is filled in as channel_key. type is wework

Copy

{
"dify_api_base": "https://api.dify.ai/v1",
"dify_api_key": "app-xxx",
"dify_app_type": "agent", "channel_type".
"channel_type": "wework", "model": "model".
"model": "dify", "single_chat_prefix".
"single_chat_prefix": [""],
"single_chat_reply_prefix":"",
"group_chat_prefix": ["@bot"],
"group_name_white_list": ["ALL_GROUP"]
}

3.6 Login to WeChat

Be sure to scan the code on your computer to log in to Enterprise WeChat in advance.

3.7. Launching the WeChat Personal Robot

Run the following command to start the robot

Copy

cddify-on-wechat
pythonapp.py

We can see that the terminal outputs the following message. Wait for the initialization of the wework program to complete And finally, the startup was successful.

Copy

[INFO][2024-04-30 21:16:04][wework_channel.py:185] - Waiting for login ------
[INFO][2024-04-30 21:16:05][wework_channel.py:190] - Login info:>>>>user_id:xxx>>>>>>>>name.
[INFO][2024-04-30 21:16:05][wework_channel.py:191] - Silent delay 60s, waiting for client to refresh data. ------
[INFO][2024-04-30 21:17:05][wework_channel.py:224] - wework program initialization complete --------

Hands-on teaching you to connect Dify to WeChat ecosystem-1

Now we send a message to the bot and we can see that the access was successful!

4. Dify Access Public

To be updated~

5. Dify access to enterprise WeChat application

To be updated~

6. Dify access to enterprise WeChat customer service

To be updated~

7. Postscript

I am a social animal workers, energy is really limited, can only work at night and weekend free time to maintain the Dify on WeChat project, relying on my personal development project progress is very slow, I hope that you can participate in this project, more PR, so that the ecology of the Dify become better ~!

May not be reproduced without permission:Chief AI Sharing Circle " Dify relies on ChatGPT-on-WeChat to access the WeChat ecosystem

Chief AI Sharing Circle

Chief AI Sharing Circle specializes in AI learning, providing comprehensive AI learning content, AI tools and hands-on guidance. Our goal is to help users master AI technology and explore the unlimited potential of AI together through high-quality content and practical experience sharing. Whether you are an AI beginner or a senior expert, this is the ideal place for you to gain knowledge, improve your skills and realize innovation.

Contact Us
en_USEnglish