From ecaf87ce0c1663b7ff37cde63b283d6f77457c3c Mon Sep 17 00:00:00 2001 From: "Jing Y." <106428961+saltism@users.noreply.github.com> Date: Wed, 9 Apr 2025 17:37:23 +0800 Subject: [PATCH] docs: loop node updated (#682) * doc: loop node updated * update: mandarin version * update: by evan * update: 3 languages * update: variable assigner * Update loop.md * Update loop.md * update: video * update: prompt modification * update: video updated --------- Co-authored-by: Hanqing Zhao --- en/guides/workflow/node/loop.md | 80 +++++++++++++---- en/guides/workflow/node/variable-assigner.md | 4 +- jp/guides/workflow/node/loop.md | 88 ++++++++++++++---- jp/guides/workflow/node/variable-assigner.md | 2 + zh_CN/guides/workflow/node/loop.md | 90 ++++++++++++++----- .../guides/workflow/node/variable-assigner.md | 4 +- 6 files changed, 214 insertions(+), 54 deletions(-) diff --git a/en/guides/workflow/node/loop.md b/en/guides/workflow/node/loop.md index d8deddee..2f5b9aad 100644 --- a/en/guides/workflow/node/loop.md +++ b/en/guides/workflow/node/loop.md @@ -28,7 +28,7 @@ A **Loop** node executes repetitive tasks that depend on previous iteration resu -## Configuration +## Configurations @@ -49,35 +49,85 @@ A **Loop** node executes repetitive tasks that depend on previous iteration resu + + + + + + + + + +
Upper limit on iterations to prevent infinite loops 10, 100, 1000
Loop VariablesValues that persist across iterations and remain accessible to nodes after the loop completesA counter x < 50 increases by 1 each iteration. Use it for calculations inside the loop, then access its final value in later workflow steps.
Exit Loop NodeImmediately ends the loop when reachedCaps execution at 10 iterations, regardless of other conditions.
-![Configuration](https://assets-docs.dify.ai/2025/03/13853bfaaa068cdbdeba1b1f75d482f2.png) +{% hint style="info" %} +The loop can be terminated by either the **Exit Loop Node** or the **Loop Termination Condition**. When either condition is met, the loop will immediately exit. -## Usage Example +If no exit conditions are specified, the loop will continue executing (similar to `while (true)`) until it reaches the **Maximum Loop Count**. +{% endhint %} -**Goal: Generate random numbers (1-100) until a value below 50 appears.** +## Example 1: Basic Loop + +**Goal: Generate random numbers between 1-100 until getting a number less than 50.** **Steps**: -1. Use `node` to generate a random number between 1-100. +1. Set up a **Loop** node by configuring its **Loop Termination Condition** to trigger when the **Template** node returns `done`. -2. Use `if` to evaluate the number: +2. Set up a **Code** node that generates random integers between `1` and `100`. - - If < 50: Output `done` and terminate loop. +3. Set up an **IF/ELSE** node with the following logic: - - If ≥ 50: Continue loop and generate another random number. + - For numbers ≥ 50: Output the `Current Number` and continue the loop -3. Set the exit criterion to random_number < 50. + - For numbers < 50: Output the `Final Number`, and then use the **Template** node to return `done` -4. Loop ends when a number below 50 appears. +4. The workflow terminates automatically once a number below `50` is generated. -![Steps](https://assets-docs.dify.ai/2025/03/b1c277001fc3cb1fbb85fe7c22a6d0fc.png) +![Basic loop workflow](https://assets-docs.dify.ai/2025/04/282013c48b46d3cc4ebf99323da10a31.png) -## Planned Enhancements +![Steps](https://assets-docs.dify.ai/2025/04/9d9fb4db7093521000ac735a26f86962.png) -**Future releases will include:** +## Example 2: Advanced Loop (with Variables and Exit Node) - - Loop variables: Store and reference values across iterations for improved state management and conditional logic. +**Goal: Design a workflow that generates a poem through four iterative refinements, with each version building upon the previous one.** - - `break` node: Terminate loops from within the execution path, enabling more sophisticated control flow patterns. +**Steps:** + +1. Set up a **Loop** node with those **Loop Variables**: + + - num: A counter starting at 0, incrementing by 1 per iteration + + - verse: A text variable initialized with `I haven't started creating yet` + +2. Set up an **IF/ELSE** node that evaluates the iteration count: + + - When num > 3: Proceed the **Exit Loop** node + + - When num ≤ 3: Proceed to the **LLM** node + +3. Set up an **LLM** node to generate poems. + +{% hint style="info" %} +Example Prompt: + +You are a European literary figure who can create poetic verses based on `sys.query`. + +`verse` is your last creation. You can progress based on your previous work. +{% endhint %} + +The first iteration begins with the initial verse value `I haven't started creating yet`. Each subsequent iteration builds upon the previous output, with the new poem replacing the verse variable’s content. + +4. Set up a **Variable Assigner** node to manage state: + + - Increment num by 1 after each iteration + + - Update verse with the newly generated poem + +5. When executed, the workflow will produce four versions of your poem, each iteration building upon its previous output. + + \ No newline at end of file diff --git a/en/guides/workflow/node/variable-assigner.md b/en/guides/workflow/node/variable-assigner.md index a1ab2379..7b4e8036 100644 --- a/en/guides/workflow/node/variable-assigner.md +++ b/en/guides/workflow/node/variable-assigner.md @@ -4,7 +4,9 @@ The variable assigner node is used to assign values to writable variables. Currently supported writable variables include: -* [conversation variables](https://docs.dify.ai/guides/workflow/key-concepts#conversation-variables). +* [Conversation variables](https://docs.dify.ai/guides/workflow/key-concepts#conversation-variables). + +* [Loop variables](https://docs.dify.ai/guides/workflow/node/loop). Usage: Through the variable assigner node, you can assign workflow variables to conversation variables for temporary storage, which can be continuously referenced in subsequent conversations. diff --git a/jp/guides/workflow/node/loop.md b/jp/guides/workflow/node/loop.md index d5a56995..f89fd942 100644 --- a/jp/guides/workflow/node/loop.md +++ b/jp/guides/workflow/node/loop.md @@ -18,7 +18,7 @@ 繰り返し処理(ループ) 各回の処理が前回の結果に依存する。 - 再帰処理や最適化問題など、前回の計算結果を必要とする処理に適している。 + 前回の計算結果を必要とする処理に適している。 反復処理(イテレーション) @@ -45,37 +45,91 @@ x < 50error_rate < 0.01 - 最大繰り返し回数(Maximum Loop Count) + 最大繰り返し回数 無限ループを防ぐための繰り返し回数の上限 10、100、1000 + + ループ変数 + 反復ごとにデータが受け渡され、ループ終了後も後続ノードで利用可能です。 + 変数 x < 50 はループごとに1ずつ増加し、ループ内では x < 50 の値に基づいて計算を実行できます。ループ終了後、x < 50 の最終値は後続の処理で利用可能です。 + + + ループ終了ノード + ループ内でこのノードに到達すると、ループが終了します。 + 終了条件に関係なく、最大10回ループします。 + -![設定](https://assets-docs.dify.ai/2025/03/13853bfaaa068cdbdeba1b1f75d482f2.png) +{% hint style="info" %} +**ループ終了ノード**と**ループ終了条件**はどちらもループの終了トリガーとして機能し、いずれかの条件が満たされるとループは早期に終了します。 -## 使用例 +終了条件が設定されていない場合、ループは**最大ループ回数**に達するまで継続して実行されます(`while (true)`と同等です)。 +{% endhint %} -**目標:50未満の値が出るまで、1から100までのランダムな数値を生成する。** +## サンプル1:ループノードの使い方 -**実装手順**: +**要件:1〜100の乱数を生成し、乱数が50未満になったら停止します。** -1. `code`ノードを使用して1-100の間のランダムな数値を生成します。 +**実装手順:** -2. `if`ノードを使用して数値を評価します: - - 50未満の場合:`done`を出力してループを終了します。 - - 50以上の場合:ループを継続し、別のランダムな数値を生成します。 +1. **ループ**ノードで、**ループ終了条件**を**テンプレートノード**の出力が`done`になるように設定します。 -3. ループ終了条件を「ランダム数値 < 50」に設定します。 +2. **コード実行ノード**を使用して、`1〜100`の乱数を生成します。 -4. 50未満の数値が出現したらループは自動的に終了します。 +3. **条件分岐ノード**を使用して、乱数が`50`より大きいかどうかを判断します。 -![使用例](https://assets-docs.dify.ai/2025/03/b1c277001fc3cb1fbb85fe7c22a6d0fc.png) + - 50以上の場合、`現在の数字`を出力し、ループを継続して新しい乱数を生成します。 -## 今後の拡張 + - 50未満の場合、`最終出力数字`を出力し、**テンプレートノード**を経由して`done`を出力し、ループを終了します。 -**今後のリリースには以下の機能が追加される予定です:** +4. 乱数が`50`未満になると、ループは自動的に停止します。 -- ループ変数:繰り返し間で値を保存・参照できるようにし、状態管理と条件付きロジックを強化します。 +![初級ループワークフロー](https://assets-docs.dify.ai/2025/04/282013c48b46d3cc4ebf99323da10a31.png) -- `break`ノード:実行パス内からループを直接終了できるようにし、より高度な制御フローパターンを実現します。 +![ステップ](https://assets-docs.dify.ai/2025/04/9d9fb4db7093521000ac735a26f86962.png) + +## サンプル2:ループ変数とループ終了ノードの使い方 + +**要件:ループワークフローを使用して詩を生成し、段階的に書き直し、各出力は前のループの結果に基づきます。ループは4回実行された後に自動的に停止します。** + +**実装手順:** + +1. **ループ**ノードで、各イテレーションでデータを渡すために使用する **ループ変数** を設定します。 + + - num(ループ回数):初期値は0で、ループごとに+1されます。 + + - verse(詩の内容):初期値は `I haven't started creating yet` で、後続の各ループで更新されます。 + +2. **条件分岐ノード**を使用して、ループ回数が`3`より大きいかどうかを判断します。 + + - 3 より大きい場合、**ループ終了ノード**に到達し、ループを終了します。 + + - 3 以下の場合、**LLMノード**に到達し、ループを継続します。 + +3. **LLMノード**を使用して、ユーザーの入力と過去の作品に基づいて詩の内容を出力するようにモデルに指示します。 + +{% hint style="info" %} +プロンプト例: + +- ユーザーの入力`sys.query`をもとに、ヨーロッパの長詩を作成してください。 + +- 前回の作成結果`verse`を参考に、新しい詩句で進歩と革新を示してください。 + +- 詩句がヨーロッパ文学のスタイルと伝統に合致し、韻律と意境に注意してください。 +{% endhint %} + +最初のループでは、verseは`I haven't started creating yet`です。それ以降の各ループでは、前のループの出力に基づいて詩が更新されます。モデルが生成した新しい詩は毎回verse変数を上書きし、次のループで使用されます。 + +4. **変数設定ノード**を使用して、各ループの後にループ内の変数を更新します。 + + - num変数を更新し、ループごとに+1します。 + + - verse変数をモデルが新しく生成した詩の内容に更新し、前のループの詩の内容を直接上書きします。 + +5. 詩のインスピレーションを入力すると、モデルは4つのバージョンの詩を出力します。各詩は前の詩に基づいて生成されます。 + + diff --git a/jp/guides/workflow/node/variable-assigner.md b/jp/guides/workflow/node/variable-assigner.md index 4fb768b9..2aad12c8 100644 --- a/jp/guides/workflow/node/variable-assigner.md +++ b/jp/guides/workflow/node/variable-assigner.md @@ -6,6 +6,8 @@ * [会話変数](../key-concept.md##hui-hua-bian-shu) +* [ループ変数](https://docs.dify.ai/ja-jp/guides/workflow/node/loop) + 使用方法:このノードを使用することで、ワークフローの中で変数の値を会話変数に一時的に保存し、後続の会話でその値を参照することができます。
diff --git a/zh_CN/guides/workflow/node/loop.md b/zh_CN/guides/workflow/node/loop.md index 29938402..a0d35fd5 100644 --- a/zh_CN/guides/workflow/node/loop.md +++ b/zh_CN/guides/workflow/node/loop.md @@ -20,70 +20,120 @@ 循环(Loop) 轮次之间存在依赖关系的优化型任务。即任务的每一轮执行都依赖上一轮的结果。 - 需要前一轮的计算结果,适用于递归、优化问题等。 + 需要前一轮的计算结果。 迭代(Iteration) 轮次之间无依赖关系的批处理任务。即每一轮任务可以独立运行,无需依赖前一轮。 - 每轮独立执行,可用于数据批量处理等。 + 每轮独立执行。 ## 如何配置循环节点? -循环节点包含以下两个关键配置项: +循环节点包含以下三个关键配置项和一个子节点: - + - - + + - + + + + + + + + + + +
配置项配置项/子节点 作用 示例
循环退出条件(Loop Termination Condition)设置循环何时停止循环终止条件(Loop Termination Condition)设置循环何时停止。 例:当变量 x < 50 时,停止循环。
最大循环次数(Maximum Loop Count)限制最多执行的轮次,避免无限循环限制最多执行的轮次,避免无限循环。例:最多执行 10 轮,不管是否满足退出条件。
循环变量(Loop Variables)用于在循环的不同轮次间传递数据,并在循环结束后继续供下游节点使用。例:变量 x < 50 在每轮循环中递增 1,循环体内部可以基于 x < 50 进行计算,循环结束后,x < 50 的最终值可用于后续流程。
退出循环节点(Exit Loop)当循环体内运行到此节点后,循环终止。 例:最多执行 10 轮,不管是否满足退出条件。
-你可以在 **循环退出条件** 中使用循环体内的变量或会话中的全局变量,让循环按照你的需求停止。 +{% hint style="info" %} +**退出循环节点** 与 **循环终止条件** 均可作为循环的终止触发点,满足任意一个条件时,循环都会提前退出。 -![Configuration](https://assets-docs.dify.ai/2025/03/13853bfaaa068cdbdeba1b1f75d482f2.png) +如果未配置任何退出条件,则循环将持续运行(相当于 while (true)),直到达到 **最大循环次数**。 +{% endhint %} -## 示例:如何使用循环节点? +## 示例一:如何使用循环节点? **需求:生成 1-100 的随机数,直到随机数小于 50 时停止。** **实现步骤**: -1. 使用 `code` 节点生成 `1-100` 的随机数。 +1. 在 **循环** 节点中,配置 **循环终止条件**,为 **模板转换** 节点输出 `done`。 -2. 使用 `if` 节点判断随机数是否小于 `50`: +2. 使用 **代码执行** 节点生成 `1-100` 的随机数。 - - 如果小于 50,输出 `done` 并结束循环。 +3. 使用 **条件分支** 节点判断随机数是否大于 `50`: - - 如果不小于 50,继续循环,生成新的随机数。 + - 如果大于或等于 50,输出 `当前数字` 并继续循环,生成新的随机数。 -3. 设置循环退出标准:随机数 `< 50`。 + - 如果小于 50,输出 `最终输出数字`,并经由 **模板转换** 节点输出 `done`,结束循环。 4. 循环将在随机数小于 `50` 时自动停止。 -![Case steps](https://assets-docs.dify.ai/2025/03/b1c277001fc3cb1fbb85fe7c22a6d0fc.png) +![Basic loop workflow](https://assets-docs.dify.ai/2025/04/282013c48b46d3cc4ebf99323da10a31.png) -## 未来扩展 +![Steps](https://assets-docs.dify.ai/2025/04/9d9fb4db7093521000ac735a26f86962.png) -**后续版本将提供:** +## 示例二:如何使用循环变量与退出循环节点? -- 循环变量:支持在循环过程中存储和引用变量,增强逻辑控制能力。 +**需求:通过一个循环工作流生成并逐步重写一首诗,每次输出都基于上一轮的结果。循环执行 4 次后自动停止。** -- `break` 节点:允许在循环体内部直接终止循环,适用于更复杂的流程控制。 +**实现步骤:** + +1. 在 **循环** 节点中,配置 **循环变量**,用于在每次迭代中传递数据: + + - num(循环次数):初始值为 0,每次循环 +1。 + + - verse(诗歌内容):初始值 `I haven’t started creating yet`,后续每次循环更新。 + +2. 使用 **条件分支** 节点判断循环次数是否大于 `3`: + + - 如果大于 3,则进入 **退出循环** 节点,结束循环。 + + - 如果小于或等于 3,则进入 **LLM** 节点继续循环。 + +3. 使用 **LLM** 节点,要求模型根据用户的输入和过往的创作输出诗歌内容。 + +{% hint style="info" %} +参考 Prompt: + +- 使用用户的输出 `sys.query` 作为灵感,创作一首欧洲长诗。 + +- 参考你上一次的创作 `verse`,尝试在新的诗句中展现进步和创新。 + +- 确保你的诗句符合欧洲文学的风格和传统,注重诗歌的韵律和意境。 +{% endhint %} + +在第一次循环时,verse 为 `I haven’t started creating yet`,之后每次循环根据上一轮的输出更新诗歌。每次模型生成的新诗歌都会覆盖 verse 变量,供下一次循环使用。 + +4. 使用 **变量赋值** 节点,在每次循环后更新循环中的变量: + + - 更新 num 变量,每次循环 +1。 + + - 更新 verse 变量为模型新生成的诗歌内容,直接覆盖前一轮循环的诗歌内容。 + +5. 输入你的诗歌灵感,模型将输出四个版本的诗歌,每一篇诗歌都以前一篇为基础而产生。 + + \ No newline at end of file diff --git a/zh_CN/guides/workflow/node/variable-assigner.md b/zh_CN/guides/workflow/node/variable-assigner.md index fc0567db..2daf2619 100644 --- a/zh_CN/guides/workflow/node/variable-assigner.md +++ b/zh_CN/guides/workflow/node/variable-assigner.md @@ -4,7 +4,9 @@ 变量赋值节点用于向可写入变量进行变量赋值,已支持以下可写入变量: -* [会话变量](../key-concept.md#hui-hua-bian-liang)。 +* [会话变量](../key-concept.md#hui-hua-bian-liang) + +* [循环变量](https://docs.dify.ai/zh-hans/guides/workflow/node/loop) 用法:通过变量赋值节点,你可以将工作流内的变量赋值到会话变量中用于临时存储,并可以在后续对话中持续引用。