# id-2/dify architecture

> Architecture diagram of the GitHub repository id-2/dify, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-01-09). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/id-2/dify
- Repository: https://github.com/id-2/dify

## Overview

##### Core Components

a) Frontend Layer:
- Web interface (Next.js application in /web directory)
- Embedded chat interface
- Share interface for public access
- Admin dashboard for configuration

b) Backend Layer (in /api directory):
- API Server (Flask application)
- Core Services:
- App Service
- Model Runtime Service
- Dataset Service
- Workflow Service
- Tools Service
- Authentication Service

c) Model Integration Layer:
- Model Providers (multiple providers supported)
- Model Runtime Management
- Model Configuration

d) Storage Layer:
- Vector Databases (multiple options supported)
- File Storage (multiple providers supported)
- Relational Database
- Redis Cache

##### Key Workflows to Highlight

a) RAG Pipeline:
- Document Ingestion
- Text Extraction
- Embedding Generation
- Vector Storage
- Retrieval Process

b) Workflow Engine:
- Node Processing
- Task Execution
- Error Handling
- State Management

c) Tool Integration:
- Built-in Tools
- External Tool Integration
- API-based Extensions

##### Integration Points

a) External Services:
- LLM Providers (OpenAI, Anthropic, etc.)
- Cloud Storage Providers
- Vector Database Services
- Authentication Services

b) APIs:
- REST API Endpoints
- WebSocket Connections
- Service Integration Points

##### Deployment Components

a) Infrastructure:
- Docker Containers
- Load Balancers
- Cache Layers
- Database Clusters

b) Monitoring & Operations:
- Logging System
- Performance Monitoring
- Error Tracking
- Usage Analytics

##### Connection Types
- WebSocket connections (wavy lines)

##### External Service Layer (Third-party integrations)

This structure will provide a clear visualization of Dify's architecture while maintaining readability and usefulness for technical discussions.

## Diagram (Mermaid)

```mermaid
flowchart TB
    subgraph Client
        WEB["Web Interface"]:::frontend
        EMB["Embedded Chat"]:::frontend
        SHARE["Share Interface"]:::frontend
        ADMIN["Admin Dashboard"]:::frontend
    end

    subgraph "API Layer"
        API["API Server"]:::backend
        WS["WebSocket"]:::backend
        REST["REST Controllers"]:::backend
    end

    subgraph "Core Services"
        APP["App Service"]:::service
        MRS["Model Runtime Service"]:::service
        DS["Dataset Service"]:::service
        WFS["Workflow Service"]:::service
        TS["Tools Service"]:::service
        AUTH["Authentication Service"]:::service
    end

    subgraph "Model Integration"
        MP["Model Providers"]:::model
        MRM["Model Runtime Management"]:::model
        MC["Model Configuration"]:::model
    end

    subgraph "Storage"
        VDB[(Vector Databases)]:::storage
        FS[(File Storage)]:::storage
        RDB[(Relational DB)]:::storage
        CACHE[(Redis Cache)]:::storage
    end

    subgraph "Core Features"
        RAG["RAG Pipeline"]:::feature
        WE["Workflow Engine"]:::feature
        TI["Tool Integration"]:::feature
    end

    subgraph "External Services"
        LLM["LLM Providers"]:::external
        CLOUD["Cloud Storage"]:::external
        VDBS["Vector DB Services"]:::external
    end

    %% Connections
    WEB & EMB & SHARE & ADMIN --> API
    API --> REST
    API --> WS
    
    REST & WS --> APP
    APP --> MRS & DS & WFS & TS
    
    MRS --> MP
    MP --> MRM
    MRM --> MC
    
    DS --> RAG
    WFS --> WE
    TS --> TI
    
    RAG --> VDB
    WE & TI --> RDB
    APP --> CACHE
    DS --> FS
    
    MP --> LLM
    FS --> CLOUD
    VDB --> VDBS

    %% Click Events
    click WEB "https://github.com/id-2/dify/tree/main/web/"
    click EMB "https://github.com/id-2/dify/tree/main/web/app/components/base/chat/embedded-chatbot/"
    click SHARE "https://github.com/id-2/dify/tree/main/web/app/components/share/"
    click ADMIN "https://github.com/id-2/dify/tree/main/web/app/components/header/account-setting/"
    click API "https://github.com/id-2/dify/blob/main/api/app.py"
    click WS "https://github.com/id-2/dify/tree/main/api/core/model_runtime/callbacks/"
    click REST "https://github.com/id-2/dify/tree/main/api/controllers/"
    click APP "https://github.com/id-2/dify/tree/main/api/core/app/"
    click MRS "https://github.com/id-2/dify/tree/main/api/core/model_runtime/"
    click DS "https://github.com/id-2/dify/tree/main/api/core/rag/"
    click WFS "https://github.com/id-2/dify/tree/main/api/core/workflow/"
    click TS "https://github.com/id-2/dify/tree/main/api/core/tools/"
    click AUTH "https://github.com/id-2/dify/tree/main/api/services/auth/"
    click MP "https://github.com/id-2/dify/tree/main/api/core/model_runtime/model_providers/"
    click MRM "https://github.com/id-2/dify/tree/main/api/core/model_runtime/entities/"
    click MC "https://github.com/id-2/dify/tree/main/api/core/model_runtime/schema_validators/"
    click VDB "https://github.com/id-2/dify/tree/main/api/core/rag/datasource/vdb/"
    click FS "https://github.com/id-2/dify/tree/main/api/extensions/storage/"
    click CACHE "https://github.com/id-2/dify/blob/main/api/configs/middleware/cache/redis_config.py"
    click RAG "https://github.com/id-2/dify/tree/main/api/core/rag/"
    click WE "https://github.com/id-2/dify/tree/main/api/core/workflow/"
    click TI "https://github.com/id-2/dify/tree/main/api/core/tools/"

    %% Styles
    classDef frontend fill:#2374e1,color:#fff
    classDef backend fill:#37b24d,color:#fff
    classDef service fill:#37b24d,color:#fff
    classDef model fill:#9775fa,color:#fff
    classDef storage fill:#f59f00,color:#fff
    classDef feature fill:#f76707,color:#fff
    classDef external fill:#868e96,color:#fff

    %% Legend
    subgraph Legend
        F[Frontend]:::frontend
        B[Backend]:::backend
        S[Service]:::service
        M[Model]:::model
        ST[Storage]:::storage
        FT[Feature]:::feature
        E[External]:::external
    end
```
