Dify AI 應用開發平台

使用 Dify 快速建構 AI 應用,支援 RAG、Agent、Workflow,提供視覺化開發介面

專案簡介

Dify 是一個開源的 LLM 應用開發平台,提供視覺化的介面來建構 AI 應用。支援 RAG、Agent、Workflow 等功能,讓非技術人員也能快速打造 AI 產品。

GitHub Stars: 129K+

主要功能

  • 視覺化 Workflow - 拖拉式建構複雜 AI 流程
  • RAG Pipeline - 內建知識庫管理
  • Agent 模式 - 自動選擇工具完成任務
  • 多模型支援 - OpenAI、Anthropic、本地模型
  • API 發布 - 一鍵將應用轉為 API

部署

Docker Compose(推薦)

1
2
3
4
git clone https://github.com/langgenius/dify.git
cd dify/docker
cp .env.example .env
docker compose up -d

訪問 http://localhost/install 完成初始設定。

環境變數設定

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
# .env
# 資料庫
DB_USERNAME=postgres
DB_PASSWORD=your-password
DB_HOST=db
DB_PORT=5432

# Redis
REDIS_HOST=redis
REDIS_PORT=6379

# 向量資料庫
VECTOR_STORE=weaviate

建立應用

1. 聊天助手

  1. 進入「工作室」→「建立應用」
  2. 選擇「聊天助手」
  3. 設定系統提示詞
  4. 選擇 LLM 模型
  5. 發布應用

2. 知識庫 RAG

  1. 進入「知識庫」→「建立知識庫」
  2. 上傳文件(PDF、Word、Markdown)
  3. 設定切割和索引策略
  4. 在應用中啟用知識庫

3. Workflow

視覺化建構複雜流程:

1
2
3
開始 → LLM → 條件判斷 → 知識檢索 → LLM → 結束
            HTTP 請求

API 使用

對話 API

1
2
3
4
5
6
7
8
9
curl -X POST 'https://api.dify.ai/v1/chat-messages' \
  -H 'Authorization: Bearer {api_key}' \
  -H 'Content-Type: application/json' \
  -d '{
    "inputs": {},
    "query": "什麼是 OWASP Top 10?",
    "user": "user-123",
    "response_mode": "blocking"
  }'

串流回應

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
import requests

response = requests.post(
    'https://api.dify.ai/v1/chat-messages',
    headers={
        'Authorization': 'Bearer {api_key}',
        'Content-Type': 'application/json'
    },
    json={
        'inputs': {},
        'query': '解釋 SQL Injection',
        'user': 'user-123',
        'response_mode': 'streaming'
    },
    stream=True
)

for line in response.iter_lines():
    if line:
        print(line.decode('utf-8'))

進階功能

變數設定

在 Workflow 中使用變數:

1
2
3
{{#context#}}  - 知識庫檢索結果
{{#query#}}    - 使用者輸入
{{#files#}}    - 上傳的檔案

自訂工具

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
openapi: 3.0.0
info:
  title: Security Scanner API
  version: 1.0.0
paths:
  /scan:
    post:
      summary: 掃描目標網站
      requestBody:
        content:
          application/json:
            schema:
              type: object
              properties:
                url:
                  type: string
      responses:
        '200':
          description: 掃描結果

Webhook 整合

1
2
3
4
5
6
7
8
9
from flask import Flask, request

app = Flask(__name__)

@app.route('/webhook', methods=['POST'])
def webhook():
    data = request.json
    # 處理 Dify 回呼
    return {'status': 'ok'}

整合本地模型

Ollama

  1. 設定 → 模型供應商 → 新增
  2. 選擇 Ollama
  3. 輸入 API 端點:http://host.docker.internal:11434
  4. 選擇模型

LocalAI

1
2
3
4
5
6
7
8
# docker-compose.yml 新增
services:
  localai:
    image: localai/localai
    ports:
      - "8080:8080"
    volumes:
      - ./models:/models

生產部署

Kubernetes

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
apiVersion: apps/v1
kind: Deployment
metadata:
  name: dify-api
spec:
  replicas: 3
  selector:
    matchLabels:
      app: dify-api
  template:
    spec:
      containers:
      - name: api
        image: langgenius/dify-api
        resources:
          limits:
            memory: "2Gi"
            cpu: "1000m"

負載平衡

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
upstream dify {
    server dify-api-1:5001;
    server dify-api-2:5001;
    server dify-api-3:5001;
}

server {
    location /v1 {
        proxy_pass http://dify;
    }
}

相關連結

延伸閱讀

comments powered by Disqus
Built with Hugo
Theme Stack designed by Jimmy