Understanding Dify: An Open-Source LLM Application Development Platform

Introduction to Dify

Dify is an open-source platform for developing applications powered by Large Language Models (LLMs). It combines Backend-as-a-Service (BaaS) principles with LLMOps concepts, enabling developers to quickly build production-ready generative AI applications. The platform is designed to be accessible to both technical and non-technical users, allowing anyone to participate in the AI application definition and data operations process.

Dify provides a comprehensive technology stack for LLM application development, including support for hundreds of models, an intuitive prompt orchestration interface, a high-quality RAG engine, a robust Agent framework, and flexible workflow orchestration. This platform offers both user-friendly interfaces and APIs, saving developers significant time that would otherwise be spent on buliding foundational components from scratch.

Four Types of LLM Applications

Dify supports four main types of LLM applications, each optimized for different scenarios:

  1. Chat Assistants - Conversational assistants based on LLMs
  2. Text Generators - Applications for content creation and generation
  3. Agent Applications - Intelligent agents with decision-making capabilities
  4. Workflow Applications - Complex applications with multi-step processes

Key Advantages

  • Low-Code/No-Code Development - Dify provides a visual interface for defining prompts, context, and plugins without requiring deep technical knowledge.
  • Modular Design - The platform uses a modular architecture where each component has clear functionality and interfaces, allowing developers to selectively use modules to build their API applications.
  • Rich Feature Components - Dify offers numerous components including AI workflows, RAG pipelines, agents, and model management to support the entire development lifecycle from prototype to production.
  • Multi-Model Support - The platform supports mainstream LLM providers, allowing developers to choose the most suitable models for their specific needs.

Model Orchestration

The true value of Dify lies in its model orchestration capabilities (also known as Agent Design in different contexts). Model orchestration involves combining different agents to create more complex and intelligent systems that can overcome the limitations of idnividual models.

Dify enables developers to combine various agents to build more powerful AI applications. For example, you can design a system where one agent understands user input, another selects the most appropriate specialized agent to handle the query, a third generates a response, and a fourth optimizes and refines the answer. This approach leverages the strengths of each agent while compensating for their weaknesses, resulting in a more capable and stable AI system.

The platform provides an intuitive graphical interface for managing and orchestrating these agents. Developers can define interactions and data flow between agents through drag-and-drop connections without writing complex code, significantly lowering the barrier to entry for AI application development.

Project Architecture

Backend Structure

Dify's backend is implemented in Python using the Flask framework. It utilizes SQLAlchemy as the ORM and Celery for task queues. Authentication is handled through Flask-login.

[api/]
├── constants             // Configuration constants for the codebase
├── controllers           // API route definitions and request handling
├── core                  // Core app orchestration, model integration
├── docker                // Docker and containerization configurations
├── events                // Event handling and processing
├── extensions            // Third-party framework integrations
├── fields                // Field definitions for serialization
├── libs                  // Reusable libraries and utilities
├── migrations            // Database migration scripts
├── models                // Database models and schema definitions
├── services              // Business logic implementation
├── storage               // Private key storage
├── tasks                 // Async task and background job handling
└── tests                 // Test suites

Frontend Structure

The web interface is built with Next.js using TypeScript and styled with Tailwind CSS. Internationalization is handled by react-i18next.

[web/]
├── app                   // Layouts, pages, and components
│   ├── (commonLayout)    // Application-wide layouts
│   ├── (shareLayout)     // Session-specific shared layouts
│   ├── activate          // Activation pages
│   ├── components        // Shared components
│   ├── install           // Installation pages
│   ├── signin            // Login pages
│   └── styles            // Global styles
├── assets                // Static resources
├── bin                   // Build scripts
├── config                // Configuration settings
├── context               // Shared application contexts
├── dictionaries          // Translation files
├── docker                // Container configurations
├── hooks                 // Reusable React hooks
├── i18n                  // Internationalization setup
├── models                // Data models and API response types
├── public                // Meta resources like favicons
├── service               // API operation definitions
├── test                  // Test files
├── types                 // Type definitions
└── utils                 // Shared utility functions

Model Categories

In Dify, models are categorized into four types based on their use cases:

  1. System Reasoning Models - Used for core application functionality, chat conversations, conversation name generation, and next-question suggestions.
    Supported providers: OpenAI, Azure OpenAI, Anthropic, Hugging Face Hub, Replicate, Xinference, OpenLLM, iFlytek Spark, ERNIE Bot, Tongyi Qianwen, Minimax, ZHIPU (ChatGLM)
  2. Embedding Models - Used for embedding segmented documents in datasets and processing user queries in applications that use datasets.
    Supported providers: OpenAI, ZHIPU (ChatGLM), Jina AI (Jina Embeddings)
  3. Rerank Models - Used to enhance retrieval capabilities and improve LLM search results.
    Supported providers: Cohere, Jina AI (Jina Reranker)
  4. Speech-to-Text Models - Used for converting speech to text in conversational applications.
    Supported providers: OpenAI

Model Configuration Methods

Predefined Models

Users only need to configure unified provider credentials to access predefined models under that provider.

Custom Models

Users need to configure credentials for each model individually. For example, Xinference supports both LLM and Text Embedding, but each model has a unique model_uid. To use both, you need to configure a unique model_uid for each.

Remote Fetch

Similar to predefined models, users configure unified provider credentials, and models are fetched from the provider using these credentials.

When configured for remote fetch, developers only need to configure a unified api_key to allow Dify Runtime to retrieve all fine-tuned models and integrate them into Dify.

These three configuration methods can coexist, allowing providers to support predefined + custom models or predefined + remote fetch models.

Use Cases

The name "Dify" originates from "Define + Modify," reflecting its purpose of defining and continuously improving your AI applications. It's designed to work "for you."

  • Startups - Quickly transform AI application ideas into reality. Dozens of teams have already built MVPs (Minimum Viable Products) using Dify to secure investments or win client orders through POCs (Proof of Concepts).
  • LLM Integration - Enhance existing applications by introducing LLM capabilities. Integrate with Dify's RESTful API to decouple prompts from business code, track data, costs, and usage through the management interface, and continuously improve application performance.
  • Enterprise LLM Infrastructure - Some banks and large internet companies are deploying Dify as an enterprise LLM gateway to accelerate GenAI adoption within their organizations while achieving centralized governance.
  • Exploring LLM Capabilities - Even as a technology enthusiast, you can easily practice prompt engineering and agent technologies with Dify. Before GPTs were launched, over 60,000 developers had created their first applications on Dify.

Deployment Options

Docker Compose Deployment

Prerequisites

Before installing Dify, ensure your machine meets the minimum requirements:

  • CPU >= 2 cores
  • RAM >= 4GB
Operating System Software Description
macOS 10.14 or later Docker Desktop Allocate at least 2 virtual CPUs (vCPU) and 8GB initial memory to the Docker VM, otherwise installation may fail.
Linux platforms Docker 19.03 or later Docker Compose 1.28 or later Refer to Docker and Docker Compose installation documentation for more information.
Windows with WSL 2 enabled Docker Desktop When binding source code and other data to Linux containers, store them in the Linux filesystem rather than the Windows filesystem.

Deployment Steps

  1. Clone the Dify repository: ``` git clone https://github.com/langgenius/dify.git

  2. Navigate to the Docker directory: ``` cd dify/docker

  3. Copy the environment configuration file: ``` cp .env.example .env

  4. Start the Docker containers:
    For Docker Compose V2: ``` docker compose up -d

    
    
    For Docker Compose V1: ```
    docker-compose up -d
    
    

After running the command, you should see output showing all container statuses and port mappings. Verify that all containers are running properly:

docker compose ps

You should see 3 business services (api/worker/web) and 6 infrastructure components (weaviate/db/redis/nginx/ssrf_proxy/sandbox).

Updating Dify

To update Dify, navigate to the docker directory and execute the following commands in sequence:

cd dify/docker
docker compose down
git pull origin main
docker compose pull
docker compose up -d

Important: If the .env.example file has been updated, make sure to synchronize your local .env file with the new configuration.

Accessing Dify

First, set up the administrator account by visiting the initialization page:

# Local environment
http://localhost/install

# Server environment
http://your_server_ip/install

Access the main Dify interface at:

# Local environment
http://localhost

# Server environment
http://your_server_ip

Manual Deployment

Server-Side Deployment

Environment Setup

The server requires Python 3.10.x. Using pyenv is recommended for quick Python environment installation:

pyenv install 3.10
pyenv global 3.10

Deployment Steps
  1. Navigate to the API directory: ``` cd api

  2. Copy the environment configuration file: ``` cp .env.example .env

  3. Generate a random secret key and replace the SECRET_KEY value in .env: ``` openssl rand -base64 42 sed -i 's/SECRET_KEY=.*/SECRET_KEY=<your_value>/' .env

  4. Install dependencies: ``` poetry env use 3.10 poetry install pip install grpcio pip install -r requirements.txt pip install pytest-cov==2.8.1 pip install pytest==5.3.0 pip install coveralls==1.5.1 pip install pydantic-settings pip install python-dotenv pip install pyyaml pip install flask_sqlalchemy pip install pycryptodome pip install redis pip install Flask-Migrate pip install oss2 pip install obs

  5. Execute database migrations: ``` poetry shell flask db upgrade

  6. Start the API service: ``` flask run --host 0.0.0.0 --port=5001 --debug

  7. Start the Worker service for asynchronous tasks:
    For Linux/MacOS: ``` celery -A app.celery worker -P gevent -c 1 -Q dataset,generation,mail,ops_trace --loglevel INFO

    
    
    For Windows: ```
    celery -A app.celery worker -P solo --without-gossip --without-mingle -Q dataset,generation,mail,ops_trace --loglevel INFO
    
    

Frontend Deployment

Environment Setup

The web frontend requires Node.js v18.x (LTS) and NPM version 8.x.x or Yarn.

Deployment Steps
  1. Navigate to the web directory: ``` cd web

  2. Install dependencies: ``` npm install

  3. Configure environment variables by creating a .env.local file and copying content from .env.example: ```

    For production release, change this to PRODUCTION

    NEXT_PUBLIC_DEPLOY_ENV=DEVELOPMENT

    The deployment edition, SELF_HOSTED

    NEXT_PUBLIC_EDITION=SELF_HOSTED

    The base URL of console application

    NEXT_PUBLIC_API_PREFIX=http://localhost:5001/console/api

    The URL for Web APP

    NEXT_PUBLIC_PUBLIC_API_PREFIX=http://localhost:5001/api

    SENTRY

    NEXT_PUBLIC_SENTRY_DSN= NEXT_PUBLIC_SENTRY_ORG= NEXT_PUBLIC_SENTRY_PROJECT=

  4. Build the application: ``` npm run build

  5. Start the web service: ``` npm run start

    
    

After successful startup, you can access Dify at http://127.0.0.1:3000.

Environment Management

Python Version Management with Pyenv

Pyenv allows you to easily switch between multiple Python versions. The recommended approach is to use pyenv for version management and Poetry for dependency management.

Installation

# Install environment dependencies for Python compilation
$ brew install openssl readline sqlite3 xz zlib tcl-tk

# Install pyenv
$ git clone https://github.com/pyenv/pyenv.git ~/.pyenv

# Set up shell environment for Pyenv
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bash_profile
echo 'command -v pyenv >/dev/null || export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bash_profile
echo 'eval "$(pyenv init -)"' >> ~/.bash_profile

# Reload shell configuration
$ exec "$SHELL"

# Verify pyenv installation
$ pyenv -v

Basic Commands

# Check pyenv version
$ pyenv -v
# Check current version
$ pyenv version
# List all installed Python environments
$ pyenv versions
# Get help
$ pyenv help
# Initialize tab completion
$ pyenv init

Installing Python

# List all available Python versions
$ pyenv install -l

# Install CPython 3.9.17
$ pyenv install 3.9.17

# Install with optimization flags
$ env PYTHON_CONFIGURE_OPTS='--enable-optimizations --with-lto' PYTHON_CFLAGS='-march=native -mtune=native' pyenv install 3.9.17

Switching Python Versions

# Set for current shell session (temporary)
$ pyenv shell <version>
# Set for current directory (creates .python-version file)
$ pyenv local <version>
# Set globally for user account
$ pyenv global <version>

Dependency Management with Poetry

Poetry is a comprehensive dependency management tool for Python. It replaces multiple files (setup.py, requirements.txt, setup.cfg, etc.) with a single pyproject.toml file for configuration.

Installation

# Verify Python environment
$ python -V

# Install using the official installer
$ curl -sSL --insecure https://install.python-poetry.org | python3 -

# Add Poetry to PATH
$ echo 'export PATH="$HOME/.local/bin:$PATH"' >> ~/.bash_profile

# Reload shell configuration
$ exec "$SHELL"

# Verify Poetry installation
$ poetry --version

# Enable tab completion for Zsh
$ mkdir ~/.zfunc
$ poetry completions zsh > ~/.zfunc/_poetry
$ echo 'fpath+=~/.zfunc' >> ~/.zshrc
$ echo 'autoload -Uz compinit && compinit' >> ~/.zshrc
$ exec "$SHELL"

Configuration

# List all Poetry configurations
$ poetry config --list

# Configure automatic virtual environment creation
$ poetry config virtualenvs.create true

# Create virtual environments in project directory
$ poetry config virtualenvs.in-project true

# Prevent access to system site packages
$ poetry config virtualenvs.options.system-site-packages false

# Don't install pip in new virtual environments (recommended for production)
$ poetry config virtualenvs.options.no-pip true

# Don't install setuptools in new virtual environments (recommended for production)
$ poetry config virtualenvs.options.no-setuptools true

# Set project-specific configuration
$ poetry config virtualenvs.create false --local

# Reset a configuration to default
$ poetry config virtualenvs.path --unset

Usage

# Create a new project
$ poetry new poetry-demo

# Create a project with src layout
$ poetry new --src <my-package>

# Initialize Poetry in an existing project
$ poetry init
# For non-interactive initialization
$ poetry init --no-interaction

Installing Dependencies

# Install all dependencies (recommended)
$ poetry install --sync --no-root --only main

# Install all dependencies including development
$ poetry install

# Install only main dependencies (for production)
$ poetry install --only main

# Install main and development dependencies
$ poetry install --with dev

# Sync environment with lock file
$ poetry install --sync

Managing Dependencies

# Generate lock file
$ poetry lock

# Verify lock file consistency
$ poetry lock --check

# Add a new dependency
$ poetry add package_name

# Add to development group
$ poetry add package_name --group dev

# Add specific version
$ poetry add package_name>=2.0.5

# Add with version range
$ poetry add "package_name>=2.0.5,<2.1.0"

# Add latest version
$ poetry add package_name@latest

# Add local package
$ poetry add ../my-package/dist/my_package-0.1.0.whl

# Add with extras
$ poetry add "requests[security,socks]"

# Import from requirements.txt
$ cat requirements.txt | xargs poetry add

Implementing Custom Model Providers

To implement a custom model provider, inherit from the base class __base.model_provider.ModelProvider and implement the required interface:

def validate_provider_credentials(self, credentials: dict) -> None:
    """
    Validate provider credentials
    
    You can choose any model type's validate credentials method,
    or implement a custom validation, such as a get model list API.
    Raise an exception if validation fails.
    
    :param credentials: Provider credentials as defined in 
                       provider_credential_schema
    """
    pass

The credentials object contains parameters defined by the provider's YAML configuration file in provider_credential_schema, such as api_key.

If validation fails, raise a errors.validate.CredentialsValidateFailedError exception.

Note: Predefined models require full implementation of this interface, while custom models can implement a simplified version:

class CustomProvider(Provider):
    def validate_provider_credentials(self, credentials: dict) -> None:
        pass

Model Implementation

Models are divided into five different types, each inheriting from different base classes and requiring different implementation methods.

Common Interface

All models must implement the following two methods:

Model Credential Validation

def validate_credentials(self, model: str, credentials: dict) -> None:
    """
    Validate model credentials
    
    :param model: Model name (string)
    :param credentials: Model credentials (object)
    :return: None
    """
    pass

The credentials parameters are defined by the provider's YAML configuration file in provider_credential_schema. If validation fails, raise a errors.validate.CredentialsValidateFailedError exception.

Tags: LLM docker python Flask Next.js

Posted on Wed, 10 Jun 2026 17:14:53 +0000 by CBG