手把手教你!Dify 工作流实战全解析 —— 附 YML 配置文件实操案例

Dify 工作流实战指南

引言

Dify 是一个开源的低代码/无代码 AI 应用开发平台,其工作流(Workflow)功能通过可视化节点连接,允许用户快速构建复杂的多步骤逻辑,适用于聊天机器人、问答系统、数据分析和自动化任务等场景。本文提供一个详细、可实施的 Dify 工作流实战指南,基于 Deep Research 模板构建一个自动化研究助手,输入研究主题(如“AI 在医疗领域的应用”),通过迭代搜索和信息整合生成带引用的综合报告。同时,补充 Dify 本地化部署所需的 YAML 文件(docker-compose.ymldocker-compose.middleware.yml),以及工作流配置文件(workflow.yaml),以支持本地运行工作流。本指南逻辑清晰,步骤明确,适合初学者和进阶用户。

实战目标

通过 Dify 的 Deep Research 工作流模板,构建一个自动化研究助手,展示以下功能:

  • 意图识别:捕获用户输入的研究主题和迭代深度。
  • 迭代搜索:使用搜索工具(如 Exa Answer 或 Serper)获取相关信息。
  • 信息合成:通过大型语言模型(LLM)分析结果,生成结构化报告。
  • 本地部署:使用 Docker Compose 部署 Dify,确保工作流在本地运行。

准备工作

在开始实战之前,确保完成以下准备:

  • Dify 环境
    • 云端部署:注册 Dify 账号(Dify 官网),无需本地安装。
    • 本地部署(推荐用于本实战):参考 Dify 文档 - 本地部署,需至少 2 vCPU 和 8GB 内存。
  • 硬件要求:2 vCPU,8GB 内存,20GB 存储空间。
  • 软件要求
    • Docker(Linux 使用 Docker Engine,Windows/macOS 使用 Docker Desktop,建议启用 WSL 2)。
    • Git(用于克隆 Dify 仓库)。
  • LLM API 密钥:获取 OpenAI、Claude 或本地模型(如 Ollama)的 API 密钥。Ollama 部署命令:
    docker run -d --name ollama --rm -p 11434:11434 ollama/ollama
    
  • 工具配置:注册并获取搜索工具的 API 密钥,如:
  • 基础知识:了解 Dify 的核心组件,如节点(Start、LLM、Tools、Answer 等)、变量和 RAG 管道。

实战步骤:构建 Deep Research 工作流

以下是基于 Dify Deep Research 模板的详细、可实施步骤,涵盖工作流创建、配置、测试和发布。

1. 本地部署 Dify

为确保工作流在本地运行,先完成 Dify 的本地化部署。

1.1 克隆 Dify 仓库
git clone https://github.com/langgenius/dify.git
cd dify/docker
1.2 配置 docker-compose.middleware.yml

此文件定义中间件服务(PostgreSQL、Redis、Weaviate),支持工作流的数据库、缓存和向量搜索功能。

version: '3.8'
services:
  db:
    image: postgres:16.4
    container_name: dify-postgres
    restart: always
    environment:
      - POSTGRES_USER=dify
      - POSTGRES_PASSWORD=difyai123456
      - POSTGRES_DB=dify
    volumes:
      - ./pgdata:/var/lib/postgresql/data
    ports:
      - "5432:5432"
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U dify"]
      interval: 5s
      timeout: 5s
      retries: 5

  redis:
    image: redis:7
    container_name: dify-redis
    restart: always
    volumes:
      - ./redis:/data
    ports:
      - "6379:6379"
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 5s
      timeout: 5s
      retries: 5

  weaviate:
    image: semitechnologies/weaviate:1.26.1
    container_name: dify-weaviate
    restart: always
    environment:
      - QUERY_DEFAULTS_LIMIT=25
      - AUTHENTICATION_ANONYMOUS_ACCESS_ENABLED=true
      - PERSISTENCE_DATA_PATH=/var/lib/weaviate
      - DEFAULT_VECTORIZER_MODULE=none
      - ENABLE_MODULES=text2vec-transformers
      - TRANSFORMERS_INFERENCE_API=http://t2v-transformers:8080
    volumes:
      - ./weaviate:/var/lib/weaviate
    ports:
      - "8080:8080"
      - "50051:50051"
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:8080/v1/.well-known/ready"]
      interval: 10s
      timeout: 5s
      retries: 5

  t2v-transformers:
    image: semitechnologies/transformers-inference:sentence-transformers-multi-qa-MiniLM-L6-cos-v1
    container_name: dify-t2v-transformers
    environment:
      - ENABLE_CUDA=0
1.3 配置 docker-compose.yml

此文件定义 Dify 的核心服务,包括前端、后端和工作进程。

version: '3.8'
services:
  api:
    image: langgenius/dify-api:0.10.0
    container_name: dify-api
    restart: always
    depends_on:
      db:
        condition: service_healthy
      redis:
        condition: service_healthy
      weaviate:
        condition: service_healthy
    environment:
      - SECRET_KEY=${SECRET_KEY:-your-secret-key}
      - DB_HOST=db
      - DB_PORT=5432
      - DB_USER=dify
      - DB_PASSWORD=difyai123456
      - DB_DATABASE=dify
      - REDIS_HOST=redis
      - REDIS_PORT=6379
      - WEAVIATE_HOST=weaviate
      - WEAVIATE_PORT=8080
      - DEPLOY_ENV=PRODUCTION
      - EDITION=SELF_HOSTED
    volumes:
      - ./api:/app/api
    ports:
      - "5001:5001"

  web:
    image: langgenius/dify-web:0.10.0
    container_name: dify-web
    restart: always
    depends_on:
      - api
    environment:
      - API_PREFIX=http://api:5001
      - DEPLOY_ENV=PRODUCTION
      - EDITION=SELF_HOSTED
    ports:
      - "3000:3000"

  worker:
    image: langgenius/dify-api:0.10.0
    container_name: dify-worker
    restart: always
    depends_on:
      db:
        condition: service_healthy
      redis:
        condition: service_healthy
      weaviate:
        condition: service_healthy
    environment:
      - SECRET_KEY=${SECRET_KEY:-your-secret-key}
      - DB_HOST=db
      - DB_PORT=5432
      - DB_USER=dify
      - DB_PASSWORD=difyai123456
      - DB_DATABASE=dify
      - REDIS_HOST=redis
      - REDIS_PORT=6379
      - WEAVIATE_HOST=weaviate
      - WEAVIATE_PORT=8080
      - DEPLOY_ENV=PRODUCTION
      - EDITION=SELF_HOSTED
    command: ["celery", "-A", "app.celery", "worker", "-P", "gevent", "-c", "1", "--loglevel", "INFO", "-Q", "dataset,generation,mail,ops_trace"]
    volumes:
      - ./api:/app/api
1.4 配置环境变量
  • 复制环境变量模板:
    cp .env.example .env
    cp middleware.env.example middleware.env
    
  • 编辑 .env,设置关键变量:
    DOCKER_COMPOSE_PORT=80
    STORAGE_TYPE=local
    OLLAMA_BASE_URL=http://localhost:11434
    SECRET_KEY=$(openssl rand -base64 42)
    
1.5 启动 Dify
  • 启动中间件服务:
    docker compose -f docker-compose.middleware.yaml up -d
    
  • 启动 Dify 服务:
    docker compose up -d --build
    
  • 访问初始化页面:打开浏览器,访问 http://localhost/install,设置管理员账户。
  • 访问 Dify:初始化完成后,访问 http://localhost

2. 创建 Deep Research 工作流

  1. 登录 Dify
    • 访问 http://localhost,使用管理员账户登录。
    • 点击“Create Application”。
  2. 选择模板
    • 在“Explore”页面,搜索“DeepResearch”模板。
    • 点击“Create”,命名应用(如“ResearchBot”),进入工作流编辑器。
    • 若无模板,从空白 Workflow 开始,手动添加节点。

3. 配置 Start 节点

  • 设置输入参数
    • research_topic:文本类型(如“AI in healthcare”)。
    • max_loop:数字类型(默认 5)。
  • 配置示例
    research_topic: AI in healthcare
    max_loop: 5
    
  • 连接:输出连接到 Iteration 节点。

4. 添加 Iteration 节点

  • 拖放节点:拖动 Iteration 节点,连接 Start 节点的 research_topic 输出。
  • 配置输入
    • 输入:search_keywords(数组类型,初始值从 research_topic 分解,如 [“AI diagnostics”, “AI patient care”])。
  • 输出iteration_results(每次循环的搜索结果)。

5. 配置搜索工具(Tools 节点)

  • 添加节点:拖动 Tools 节点,连接 Iteration 节点的 search_keywords 输出。
  • 选择工具
    • 选择“Exa Answer”或“Serper”。
    • 输入 API 密钥(需在 exa.aiserper.dev 注册)。
  • 配置输入
    • 查询:{{iteration.search_keywords}}
  • 输出变量search_results(搜索结果列表,包含标题、URL、摘要)。

6. 添加 LLM 节点

  • 选择模型
    • 选择 gpt-4o、Claude 或 Ollama(URL:http://localhost:11434)。
  • 设置提示
    Based on the search results: {{search_results}}, identify knowledge gaps and suggest the next search topic. Output in JSON format:
    {
      "nextSearchTopic": "string",
      "shouldContinue": boolean
    }
    
  • 输出变量
    • nextSearchTopic:如“AI patient outcome prediction”。
    • shouldContinue:true/false。
  • 连接:输入为 search_results,输出连接到 If/Else 节点。

7. 添加 If/Else 节点

  • 配置条件
    • 条件:{{llm.shouldContinue}} == true
  • 分支
    • True 分支:连接回 Iteration 节点。
    • False 分支:连接到 Answer 节点。

8. 配置 Answer 节点

  • 配置输出
    • 使用 Jinja2 模板:
      # Research Report on {{start.research_topic}}
      ## Findings
      {% for result in iteration.iteration_results %}
      - {{result.snippet}}
      {% endfor %}
      ## References
      {% for result in iteration.iteration_results %}
      - {{result.title}}: {{result.url}}
      {% endfor %}
      
  • 连接:输入为 iteration.iteration_results

9. 配置工作流 YAML 文件

以下是 Deep Research 工作流的 YAML 配置,用于定义节点和逻辑。

version: '1.0'
nodes:
  - id: start
    type: Start
    inputs:
      research_topic:
        type: string
        value: ''
      max_loop:
        type: number
        value: 5
    outputs:
      research_topic: string
      max_loop: number
  - id: iteration
    type: Iteration
    inputs:
      search_keywords: '{{start.research_topic}}'
      max_iterations: '{{start.max_loop}}'
    outputs:
      iteration_results: array
  - id: search_tool
    type: Tool
    tool: ExaAnswer
    inputs:
      query: '{{iteration.search_keywords}}'
      api_key: '${EXA_API_KEY}'
    outputs:
      search_results: array
  - id: llm
    type: LLM
    model: gpt-4o
    inputs:
      prompt: |
        Based on the search results: {{search_results}}, identify knowledge gaps and suggest the next search topic. Output in JSON format:
        {
          "nextSearchTopic": "string",
          "shouldContinue": boolean
        }
      search_results: '{{search_tool.search_results}}'
    outputs:
      nextSearchTopic: string
      shouldContinue: boolean
  - id: if_else
    type: IfElse
    condition: '{{llm.shouldContinue}} == true'
    true_branch: iteration
    false_branch: answer
  - id: answer
    type: Answer
    inputs:
      output: |
        # Research Report on {{start.research_topic}}
        ## Findings
        {% for result in iteration.iteration_results %}
        - {{result.snippet}}
        {% endfor %}
        ## References
        {% for result in iteration.iteration_results %}
        - {{result.title}}: {{result.url}}
        {% endfor %}
    outputs:
      report: string
  • 保存文件:保存至 dify/workflows/research_bot.yaml
  • 导入工作流
    • 在 Dify 仪表板,点击“Import Workflow”,上传 workflow.yaml
    • 验证节点连接和配置。

10. 测试与调试

  • 预览工作流
    • 点击“Preview”,输入:
      research_topic: AI in healthcare
      max_loop: 5
      
    • 检查输出报告。
  • 调试工具
    • 使用“Workflow Process”面板,检查节点输入/输出。
    • 验证变量值(如 search_resultsnextSearchTopic)。
  • 常见问题
    • 搜索结果不准确:检查 API 密钥或关键词。
    • 迭代未结束:验证 max_loopshouldContinue

11. 发布与集成

  • 发布应用
    • 点击“Publish”,生成 WebApp 链接(如 http://localhost/apps/research_bot)。
  • API 集成
    • 获取 API 端点:
      curl -X POST http://localhost:5001/v1/workflows/run \
        -H "Authorization: Bearer YOUR_API_KEY" \
        -H "Content-Type: application/json" \
        -d '{"research_topic": "AI in healthcare", "max_loop": 5}'
      

示例输出

# Research Report on AI in Healthcare
## Findings
- AI improves diagnostics through image analysis, such as MRI and CT scans, achieving over 90% accuracy.
- Predictive models reduce hospital readmissions by 15%.
## References
- AI in MRI Analysis: https://pubmed.ncbi.nlm.nih.gov/...
- WHO Report on AI in Healthcare: https://www.who.int/...

高级优化

  • 并行处理:使用 Dify v0.8.0+ 的并行分支,同时运行多个搜索工具。
  • RAG 管道:上传医疗文档,使用 Knowledge Retrieval 节点。
  • 调试增强:利用 Dify v1.5.0 的节点输出保存功能

注意事项

  • API 密钥安全:使用环境变量存储密钥。
  • 资源需求:复杂工作流建议 16GB 内存。
  • 更新维护:定期运行 git pulldocker compose pull
  • 社区支持:参考 Difyt 模板库

结论

本实战指南通过本地部署和 Deep Research 工作流构建,展示了 Dify 的强大功能。提供的 YAML 文件确保了部署和工作的可实施性。用户可快速上手,构建高效 AI 应用,适用于从原型到生产的各种场景。

### Dify 工作流实战项目示例 #### 使用 Dify 和 Moonshot API 构建 AI 应用程序 构建应用程序时,可以利用 Dify 平台及其集成的 Moonshot API 来现复杂的功能。官方文档提供了详细的指导说明如何设置环境以及编写必要的配置文件[^3]。 ```bash docker run --name my-dify-app -p 80:80 nginx:latest ``` 这段命令展示了启动一个基于 Nginx 的容器例的基础方式,在作中会涉及到更复杂的参数设定来适配具体的应用需求。 #### 创建跨平台的内容分发网络 通过结合 Dify 提供的服务接口与第三方工具,能够建立一套适用于多渠道发布的自动化流程体系。这不仅限于社交媒体平台上的图文推送,还包括视频剪辑、音频处理等多个方面的工作支持[^2]。 #### 小红书文案自动生成方案 针对特定场景下的营销推广活动策划,有开发者分享了借助 Dify 联合 OpenAI 推出的语言模型完成从小红书笔记撰写到封面设计的一站式服务体验介绍[^4]。 ```python import dify_sdk as sdk client = sdk.Client(api_key="your_api_key") def generate_post(prompt): response = client.create_completion( engine="text-davinci-003", prompt=prompt, max_tokens=150 ) return response.choices[0].text.strip() ``` 上述 Python 代码片段演示了调用 Dify SDK 中的方法创建一段由人工智能生成的文字内容的过程,其中 `engine` 参数指定了所使用的预训练模型版本号;而 `prompt` 则是要传递给算法作为输入提示的信息字符串。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值