复制成功
请遵守本站许可
REPORT
Chapter_Post // Field_Report

Post_Ref: RL-DIFY工作流详

2026.04.08

Dify工作流详解

Echo HaoRan
Echo HaoRan
ANALYSIS
  • LLM
  • Posts
  • Python
  • 技术手册
  • 教程

Dify 工作流详解#

概述#

工作流是 Dify 中用于构建复杂 AI 应用的核心功能。通过可视化画布编排多个节点,你可以创建强大的自动化流程。本文将详细介绍工作流的各种节点类型、执行模式和最佳实践。


工作流基础#

工作流 ID#

工作流 ID 是一个唯一标识符,记录当前工作流应用中所有节点的信息。

用途

  • 跟踪和记录工作流执行
  • 调试和监控
  • 日志关联

系统变量

PRTCL // YAML
sys.workflow_run_id

执行模式#

串行执行#

节点一个接一个连接,按顺序执行。每个节点等待前一个节点完成后再开始。

适用场景

  • 有依赖关系的任务
  • 需要顺序处理的流程
  • 数据转换和清洗

并行执行#

多个节点同时执行,缩短任务处理时间。

适用场景

  • 独立任务
  • 批量处理
  • 多渠道数据获取

配置示例

PRTCL // YAML
# 迭代节点并行配置
iteration:
enabled: true
parallel_mode: true
max_parallelism: 10

节点类型详解#

输入节点#

用户输入节点#

定义从用户收集的信息作为应用程序的输入。

配置选项

  • 字段类型:短文本、段落、选择、文件列表等
  • 变量名:用于引用的变量名称
  • 标签名:显示给用户的标签
  • 最大长度:文本长度限制
  • 必填:是否必填

示例配置

PRTCL // YAML
用户输入节点配置:
- field: "platform"
type: "短文本"
variable: "platform"
label: "目标平台"
max_length: 256
required: true

开始节点#

工作流的入口点,定义应用的起始条件。


处理节点#

LLM 节点#

调用大语言模型处理文本、图像和文档。

核心功能

  • 文本生成和处理
  • 多模态输入支持
  • 结构化输出
  • 上下文变量

配置参数

PRTCL // YAML
LLM 节点配置:
model: "gpt-4o"
temperature: 0.7
top_p: 0.95
max_tokens: 2000
frequency_penalty: 0
presence_penalty: 0

温度参数

  • 0:确定性输出
  • 0.5:平衡输出
  • 1:创造性输出

预设选项

  • 精确(Precise):温度 0.3
  • 平衡(Balanced):温度 0.7
  • 创意(Creative):温度 1.0

提示词配置

PRTCL // YAML
提示词模板:
system: "你是一个 {{role}},专注于 {{expertise}}。"
user: "请处理以下内容:{{input}}"

结构化输出

PRTCL // YAML
结构化输出配置:
type: "json"
schema:
type: "object"
properties:
title:
type: "string"
content:
type: "string"
tags:
type: "array"
items:
type: "string"

参数提取器节点#

从自然语言中提取结构化参数。

使用场景

  • 解析用户意图
  • 提取关键信息
  • 标准化输入

配置示例

PRTCL // YAML
参数提取器配置:
input_variable: "User Input/platform"
extraction_parameters:
- name: "platform"
type: "Array[String]"
description: "提取的社交媒体平台列表"
required: true
instruction: |
提取文本中提到的所有社交媒体平台。
有效平台:Twitter, LinkedIn, Facebook, Instagram, TikTok
返回 JSON 数组格式。

代码节点#

执行自定义代码逻辑。

支持语言

  • Python
  • JavaScript

配置示例

PRTCL // YAML
代码节点配置:
language: "python"
code: |
def process_data(data):
# 处理逻辑
return {
"result": data.upper(),
"count": len(data)
}
input_variables:
- name: "data"
type: "string"
output_variables:
- name: "result"
type: "object"

IF/ELSE 节点#

基于条件创建分支逻辑。

配置示例

PRTCL // YAML
IF/ELSE 节点配置:
condition:
field: "Parameter Extractor/platform"
operator: "contains"
value: "error message"
branches:
if:
- 输出错误消息
else:
- 继续正常流程

支持的操作符

  • 等于(equals)
  • 不等于(not equals)
  • 包含(contains)
  • 不包含(not contains)
  • 大于(greater than)
  • 小于(less than)

迭代节点#

循环处理列表数据。

配置示例

PRTCL // YAML
迭代节点配置:
input_variable: "Parameter Extractor/platform"
output_variable: "Create Content/structured_output"
parallel_mode: true
max_parallelism: 10
internal_nodes:
- LLM 节点
- 代码节点

内部节点

  • 使用 {{Current Iteration/item}} 引用当前项
  • 使用 {{Current Iteration/index}} 引用索引

列表操作器节点#

过滤、映射、操作数组。

操作类型

  • 过滤(Filter)
  • 映射(Map)
  • 排序(Sort)
  • 去重(Unique)

配置示例

PRTCL // YAML
列表操作器配置:
input_variable: "User Input/user_file"
operation: "filter"
condition: "{x}type == 'Image'"
output_variable: "Image"

文档提取器节点#

从文档中提取文本。

支持格式

  • PDF
  • DOCX
  • TXT
  • MD

配置示例

PRTCL // YAML
文档提取器配置:
input_variable: "Document/result"
output_variable: "text"
options:
extract_images: false
preserve_formatting: true

模板节点#

使用 Jinja2 模板格式化输出。

配置示例

PRTCL // YAML
模板节点配置:
input_variable: "Iteration/output"
template: |
{% for item in output %}
# {{ item.title }}
{{ item.content }}
{% endfor %}
output_variable: "formatted_output"

Jinja2 语法

PRTCL // PLAINTEXT
{% for item in items %}
{{ item.name }}
{% endfor %}
{% if condition %}
显示内容
{% endif %}
{{ variable | filter }}

输出节点#

答案节点#

定义在对话流应用中向用户传递的内容。

配置示例

PRTCL // YAML
答案节点配置:
content: "{{formatted_output}}"
format: "markdown"
streaming: true

输出节点#

工作流的出口点,返回最终结果。


Agent 节点#

高度封装的智能单元,具备自主思考能力。

配置示例

PRTCL // YAML
Agent 节点配置:
goal: "帮助用户完成特定任务"
tools:
- "web_search"
- "file_read"
- "api_call"
reasoning:
enabled: true
max_iterations: 5

变量系统#

变量类型#

变量类型说明示例
用户输入变量从用户输入节点获取{{User Input/platform}}
节点输出变量从其他节点的输出获取{{LLM/text}}
系统变量系统提供的变量{{sys.workflow_run_id}}
环境变量环境配置的变量{{env.API_KEY}}
迭代变量迭代节点中的变量{{Current Iteration/item}}

变量引用#

使用双花括号语法引用变量:

PRTCL // PLAINTEXT
{{variable_name}}

变量转换#

支持 Jinja2 过滤器:

PRTCL // PLAINTEXT
{{variable | upper}}
{{variable | length}}
{{variable | join(', ')}}

上下文变量#

知识库上下文#

将知识库内容注入到 LLM 节点中。

配置示例

PRTCL // YAML
上下文变量配置:
knowledge_base: "product_docs"
retrieval:
top_k: 5
similarity_threshold: 0.7
variable_name: "context"

文件处理#

处理上传的文件,包括图像和文档。

配置示例

PRTCL // YAML
文件处理配置:
input_variable: "User Input/user_file"
vision:
detail: "high" # high 或 low
document:
extract_text: true

调试和测试#

单节点测试#

单独测试任何节点而无需运行整个工作流。

步骤

  1. 选择节点
  2. 在设置面板中提供测试输入
  3. 点击运行
  4. 查看输出

查看日志#

查看每个节点的执行日志和错误信息。

性能分析#

分析工作流的执行时间和资源使用。


最佳实践#

设计原则#

  • 模块化:将复杂流程分解为可复用的小组件
  • 可测试性:每个节点应可独立测试
  • 错误处理:完善的异常处理机制
  • 性能优化:合理使用并行和缓存

性能优化#

并行执行

PRTCL // YAML
迭代节点:
parallel_mode: true
max_parallelism: 10

缓存策略

PRTCL // YAML
LLM 节点:
cache_enabled: true
cache_ttl: 3600

批量处理

PRTCL // YAML
列表操作器:
batch_size: 100

错误处理#

PRTCL // YAML
IF/ELSE 节点:
error_handling:
strategy: "continue" # continue, stop, retry
max_retries: 3
error_message: "处理失败,请重试"

高级功能#

事件触发#

支持定时触发和事件触发。

PRTCL // YAML
触发器配置:
type: "schedule"
cron: "0 9 * * *" # 每天 9:00

Webhook#

支持通过 Webhook 触发工作流。

子工作流#

将工作流作为子流程调用。

PRTCL // YAML
子工作流节点:
workflow_id: "sub-workflow-id"
input_mapping:
input1: "{{parent_output}}"
output_mapping:
result: "child_output"

资源链接#


最后更新: 2026-03-12 作者: EchoHaoRan

R P
Rhine Lab Pioneer Division
Auth_Verified: 2026.04.08
// END OF POST

订阅

通过 RSS 订阅本站,新文章发布时第一时间收到通知。

Follow
Classified
Chapter_06
Protocol_Ref: CC-BY-NC-SA-4.0

Dify工作流详解

Author: CHONGXIReleased: 2026.04.08

Licensed under CC BY-NC-SA 4.0

评论

© 2025-2026 EchoSpace
Powered by Astro & echohaoran Non-Collaborative_Entity // Protocol_V.4.21