mirror of
https://github.com/langgenius/dify-docs-archived.git
synced 2026-07-19 22:43:32 -04:00
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 <sherry9277@gmail.com>
This commit is contained in:
@@ -28,7 +28,7 @@ A **Loop** node executes repetitive tasks that depend on previous iteration resu
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
## Configuration
|
||||
## Configurations
|
||||
|
||||
<table>
|
||||
<thead>
|
||||
@@ -49,35 +49,85 @@ A **Loop** node executes repetitive tasks that depend on previous iteration resu
|
||||
<td>Upper limit on iterations to prevent infinite loops</td>
|
||||
<td>10, 100, 1000</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Loop Variables</td>
|
||||
<td>Values that persist across iterations and remain accessible to nodes after the loop completes</td>
|
||||
<td>A counter <code>x < 50</code> increases by 1 each iteration. Use it for calculations inside the loop, then access its final value in later workflow steps.</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Exit Loop Node</td>
|
||||
<td>Immediately ends the loop when reached</td>
|
||||
<td>Caps execution at 10 iterations, regardless of other conditions.</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||

|
||||
{% 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.
|
||||
|
||||

|
||||

|
||||
|
||||
## Planned Enhancements
|
||||

|
||||
|
||||
**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.
|
||||
|
||||
<video width="100%" controls>
|
||||
<source src="https://assets-docs.dify.ai/2025/04/7ecfc04458aa38e721baaa5f6355486c.mp4" type="video/mp4">
|
||||
</video>
|
||||
@@ -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.
|
||||
|
||||
|
||||
@@ -18,7 +18,7 @@
|
||||
<tr>
|
||||
<td><strong>繰り返し処理(ループ)</strong></td>
|
||||
<td>各回の処理が前回の結果に依存する。</td>
|
||||
<td>再帰処理や最適化問題など、前回の計算結果を必要とする処理に適している。</td>
|
||||
<td>前回の計算結果を必要とする処理に適している。</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><strong>反復処理(イテレーション)</strong></td>
|
||||
@@ -45,37 +45,91 @@
|
||||
<td><code>x < 50</code>、<code>error_rate < 0.01</code></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>最大繰り返し回数(Maximum Loop Count)</td>
|
||||
<td>最大繰り返し回数</td>
|
||||
<td>無限ループを防ぐための繰り返し回数の上限</td>
|
||||
<td>10、100、1000</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>ループ変数</td>
|
||||
<td>反復ごとにデータが受け渡され、ループ終了後も後続ノードで利用可能です。</td>
|
||||
<td>変数 <code>x < 50</code> はループごとに1ずつ増加し、ループ内では <code>x < 50</code> の値に基づいて計算を実行できます。ループ終了後、<code>x < 50</code> の最終値は後続の処理で利用可能です。</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>ループ終了ノード</td>
|
||||
<td>ループ内でこのノードに到達すると、ループが終了します。</td>
|
||||
<td>終了条件に関係なく、最大10回ループします。</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||

|
||||
{% 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`より大きいかどうかを判断します。
|
||||
|
||||

|
||||
- 50以上の場合、`現在の数字`を出力し、ループを継続して新しい乱数を生成します。
|
||||
|
||||
## 今後の拡張
|
||||
- 50未満の場合、`最終出力数字`を出力し、**テンプレートノード**を経由して`done`を出力し、ループを終了します。
|
||||
|
||||
**今後のリリースには以下の機能が追加される予定です:**
|
||||
4. 乱数が`50`未満になると、ループは自動的に停止します。
|
||||
|
||||
- ループ変数:繰り返し間で値を保存・参照できるようにし、状態管理と条件付きロジックを強化します。
|
||||

|
||||
|
||||
- `break`ノード:実行パス内からループを直接終了できるようにし、より高度な制御フローパターンを実現します。
|
||||

|
||||
|
||||
## サンプル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つのバージョンの詩を出力します。各詩は前の詩に基づいて生成されます。
|
||||
|
||||
<video width="100%" controls>
|
||||
<source src="https://assets-docs.dify.ai/2025/04/7ecfc04458aa38e721baaa5f6355486c.mp4" type="video/mp4">
|
||||
</video>
|
||||
|
||||
@@ -6,6 +6,8 @@
|
||||
|
||||
* [会話変数](../key-concept.md##hui-hua-bian-shu)
|
||||
|
||||
* [ループ変数](https://docs.dify.ai/ja-jp/guides/workflow/node/loop)
|
||||
|
||||
使用方法:このノードを使用することで、ワークフローの中で変数の値を会話変数に一時的に保存し、後続の会話でその値を参照することができます。
|
||||
|
||||
<figure><img src="https://assets-docs.dify.ai/2024/11/83d0b9ef4c1fad947b124398d472d656.png" alt="" width="375"><figcaption></figcaption></figure>
|
||||
|
||||
@@ -20,70 +20,120 @@
|
||||
<tr>
|
||||
<td><strong>循环</strong>(Loop)</td>
|
||||
<td>轮次之间存在依赖关系的优化型任务。即任务的每一轮执行都依赖上一轮的结果。</td>
|
||||
<td>需要前一轮的计算结果,适用于递归、优化问题等。</td>
|
||||
<td>需要前一轮的计算结果。</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><strong>迭代</strong>(Iteration)</td>
|
||||
<td>轮次之间无依赖关系的批处理任务。即每一轮任务可以独立运行,无需依赖前一轮。</td>
|
||||
<td>每轮独立执行,可用于数据批量处理等。</td>
|
||||
<td>每轮独立执行。</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
## 如何配置循环节点?
|
||||
|
||||
循环节点包含以下两个关键配置项:
|
||||
循环节点包含以下三个关键配置项和一个子节点:
|
||||
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>配置项</th>
|
||||
<th>配置项/子节点</th>
|
||||
<th>作用</th>
|
||||
<th>示例</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr>
|
||||
<td><strong>循环退出条件</strong>(Loop Termination Condition)</td>
|
||||
<td>设置循环何时停止</td>
|
||||
<td><strong>循环终止条件</strong>(Loop Termination Condition)</td>
|
||||
<td>设置循环何时停止。</td>
|
||||
<td>例:当变量 <code>x < 50</code> 时,停止循环。</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><strong>最大循环次数</strong>(Maximum Loop Count)</td>
|
||||
<td>限制最多执行的轮次,避免无限循环</td>
|
||||
<td>限制最多执行的轮次,避免无限循环。</td>
|
||||
<td>例:最多执行 10 轮,不管是否满足退出条件。</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><strong>循环变量</strong>(Loop Variables)</td>
|
||||
<td>用于在循环的不同轮次间传递数据,并在循环结束后继续供下游节点使用。</td>
|
||||
<td>例:变量 <code>x < 50</code> 在每轮循环中递增 1,循环体内部可以基于 <code>x < 50</code> 进行计算,循环结束后,<code>x < 50</code> 的最终值可用于后续流程。</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><strong>退出循环节点</strong>(Exit Loop)</td>
|
||||
<td>当循环体内运行到此节点后,循环终止。</td>
|
||||
<td>例:最多执行 10 轮,不管是否满足退出条件。</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
你可以在 **循环退出条件** 中使用循环体内的变量或会话中的全局变量,让循环按照你的需求停止。
|
||||
{% hint style="info" %}
|
||||
**退出循环节点** 与 **循环终止条件** 均可作为循环的终止触发点,满足任意一个条件时,循环都会提前退出。
|
||||
|
||||

|
||||
如果未配置任何退出条件,则循环将持续运行(相当于 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` 时自动停止。
|
||||
|
||||

|
||||

|
||||
|
||||
## 未来扩展
|
||||

|
||||
|
||||
**后续版本将提供:**
|
||||
## 示例二:如何使用循环变量与退出循环节点?
|
||||
|
||||
- 循环变量:支持在循环过程中存储和引用变量,增强逻辑控制能力。
|
||||
**需求:通过一个循环工作流生成并逐步重写一首诗,每次输出都基于上一轮的结果。循环执行 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. 输入你的诗歌灵感,模型将输出四个版本的诗歌,每一篇诗歌都以前一篇为基础而产生。
|
||||
|
||||
<video width="100%" controls>
|
||||
<source src="https://assets-docs.dify.ai/2025/04/7ecfc04458aa38e721baaa5f6355486c.mp4" type="video/mp4">
|
||||
</video>
|
||||
@@ -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)
|
||||
|
||||
用法:通过变量赋值节点,你可以将工作流内的变量赋值到会话变量中用于临时存储,并可以在后续对话中持续引用。
|
||||
|
||||
|
||||
Reference in New Issue
Block a user