Files
LangBot/docs/en/workshop/dify-integration.md
T
2025-01-02 23:30:44 +08:00

6.2 KiB

Integrating Dify on LangBot (NewAPI Relay Solution)

::: info This article is written by a community contributor.
Direct integration with Dify Service API is now supported. Please refer to How to Integrate Dify? for details. :::

Dify is an open-source AI application development platform that supports multiple AI models and a rich set of plugins for quickly building AI Agents, Workflows, and other applications. Through middleware, we can utilize Dify's features on LangBot.

::: warning This tutorial assumes the use of a Linux system with LangBot's messaging platform already configured. If you are unfamiliar with Linux or Debian, please do not use this tutorial. If LangBot is not deployed, please configure LangBot first before referring to this tutorial. :::

Install and Configure Docker

Dify recommends using Docker for installation. If you have already started LangBot using Docker, you can skip this step.

# For Debian:
apt update
apt install -y docker docker-compose-plugin
# For Ubuntu:
apt update
apt install -y docker docker-compose-v2

The Docker image repository is blocked in China, and the official repository is often inaccessible. Therefore, you need to switch to an available mirror site: https://docker.1panel.live

Use the vim editor to modify the /etc/docker/daemon.json file.

vim /etc/docker/daemon.json

The file should be empty. Press the "I" key to enter edit mode and paste the following content:

{
  "registry-mirrors": ["https://docker.1panel.live"]
}

Configure Docker

Press the Esc key to exit edit mode, then enter :wq to save and exit.

Restart the Docker service:

sudo systemctl daemon-reload
sudo systemctl restart docker

Use docker info to check if the change was successful.

Check Docker

Install and Configure Dify

First, install git:

apt install -y git

Use git to clone the Dify source code and enter the Docker directory:

git clone https://github.com/langgenius/dify.git
cd dify/docker

Copy the environment configuration file and start Dify:

cp .env.example .env
docker compose up -d

Start Dify

This process may take some time, so please be patient.

After all containers are running successfully, wait for 2-3 minutes, then use a browser to access http://your-server-address for the initial setup.

Set up the administrator account email and password. After setup, log in to Dify.

Click on the user icon in the top-left corner, then click on Settings.

Dify Settings

Then click on Model Providers.

Dify Model Providers

Here, I have already added the necessary configurations based on my requirements.

Go to the Studio → Create a Blank Application, and select Chat Assistant.

Basic Orchestration: Fewer features but simpler configuration
Workflow Orchestration: Complex functionality based on workflows but harder to configure, not suitable for beginners

Choose based on your needs.

Dify Create Application

Fill in the basic information such as the name, then click Create.

Dify Enter Application

Select the model in the top-right corner. After setting up, click on Publish → Update in the top-right corner to update the application. If not updated, it cannot be used.

Dify Update Application

Then click on Access API, and in the top-right corner, click on API Keys → Create Key. Copy the generated key content.

Dify Create Key

Dify Copy Key

The Dify configuration part is now complete.

Deploy the NewAPI Middleware

Dify's API does not use the standard OpenAI API, so a relay is needed.

We will use new-api.

Install wget:

apt install -y wget

Create a directory in the ~ directory to store new-api:

mkdir ~/new-api

Download New-api:

cd new-api

For arm64:

wget https://github.com/Calcium-Ion/new-api/releases/download/v0.2.9.7/one-api-arm64

For x86/64:

wget https://github.com/Calcium-Ion/new-api/releases/download/v0.2.9.7/one-api

(arm64 is generally for development boards. If you don't know what a development board is or what arm64 is, just use x86/64.)

Install screen for process management:

apt install -y screen

Use:

screen -R name

Create a new screen. If a screen with the same name exists, it will enter that screen.

cd ~/new-api

Add permissions:

chmod 775 new-api

Run new-api:

./new-api

Wait for it to finish running.

Ctrl+a+d

Exit the current window (run in the background).

Access the following URL in a browser:

http://your-server-address:3000/

Default username: root
Default password: 123456

After logging in, go to: User Management → Edit → Password Change the root password. After changing the password, click on Channels → Add Channel

New-api Add Channel

Select dify in the Type field, fill in the name, and click on Fill in the relevant models. Fill in the key, which is the key content generated during the Dify configuration.

New-api Fill in Models

Go to the Tokens page in new-api, click on Add Token, and fill in the basic information as prompted. Set the name, expiration, and quota as needed.

New-api Add Token

After submitting, find the newly created token, hover over View, and copy the sk- prefixed content.

The basic configuration of new-api is now complete. Configure the rest as needed.

Configure LangBot

Use the OpenAI provider, set the NewAPI address as the API address, and the NewAPI key as the API Key.

LangBot Modify Key

Restart the LangBot project. The integration with Dify is now complete.