Loops
Implementing a workflow that will iterate on a condition
Implementing Loops in Agent Service Workflows
Agent Service allows for dynamic control flow within workflows using loops. Loops enable iterative processing, conditional execution, and controlled stopping criteria. This guide demonstrates how to configure loops in YAML-based workflows.
How Loops Work in Agent Service
Loops in Agent Service are defined within the plan
section of the YAML. A loop iterates through a workflow based on specified conditions and merges outputs at the end of execution. The key components of a loop include:
- Workflow Execution: The loop runs a workflow repeatedly until a stopping condition is met.
- Iteration Limit: The loop can be restricted to a maximum number of iterations.
- Condition Handling: Loops can terminate based on logical conditions (e.g., when a generated output matches a specified value).
- Data Merging: Outputs from loop iterations can be merged into a final output.
Example: Looping Until a Condition is Met
The following YAML defines a loop that iterates through a workflow that generates a word based on a seed input. The loop stops if the generated word contains "Zebra"
.
Breaking Down the Loop Implementation
1. Defining the Workflow
The generate_keyword
workflow generates a word based on an input seed. The generated word is then evaluated in the loop condition.
2. Configuring the Loop in the plan
Section
workflow_name: generate_keyword
→ The loop runs this workflow.condition
→ The loop terminates if the word contains “Zebra”.max_iter
: 1 → The loop executes at most once, but it can be adjusted for more iterations.loop_inputs
→ The generated word is fed back into the next iteration.merge_outputs
→ The final output from the loop is stored and passed to the next workflow.
3. Finalizing the Output
After the loop completes, the format_final_word
workflow formats the final result.
Use Cases for Loops
- Iterative Refinement: Improve model outputs by reprocessing results over multiple iterations.
- Controlled Execution: Define stopping conditions based on specific output patterns.
- Dynamic Data Processing: Merge and analyze outputs across multiple loop cycles.
This example demonstrates how loops enable flexible, condition-driven processing within Agent Service, making workflows more dynamic and adaptable.