-
Notifications
You must be signed in to change notification settings - Fork 118
Expand file tree
/
Copy pathconditional.py
More file actions
253 lines (187 loc) · 7.26 KB
/
conditional.py
File metadata and controls
253 lines (187 loc) · 7.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
"""
Conditional Branching Workflow Example
Demonstrates conditional routing based on step output:
Input (number to classify)
↓
[classify] ← Classifies as "high" or "low"
↓
┌─────┴─────┐
↓ ↓
category category
== "high" == "low"
↓ ↓
[process_ [process_
high] low]
↓ ↓
Output Output
Patterns demonstrated:
1. Fan-out with output_based conditions (one step → two conditional paths)
2. Equality operator (==) for mutually exclusive routing
3. Multiple end steps (workflow completes when any end step finishes)
Run: python examples/workflows/conditional.py
"""
import asyncio
from pydantic import BaseModel
from picoagents.workflow import FunctionStep, Workflow, WorkflowRunner
from picoagents.workflow.core import Context, StepMetadata, WorkflowMetadata
# =============================================================================
# Data Models
# =============================================================================
class NumberInput(BaseModel):
"""Input: a number to classify."""
value: int
class ClassifyOutput(BaseModel):
"""Output from classification: the category and original value."""
category: str # "high" or "low"
value: int
class ProcessedOutput(BaseModel):
"""Final processed output."""
result: str
original_value: int
multiplier: int
# =============================================================================
# Step Functions
# =============================================================================
async def classify_number(
input_data: NumberInput, context: Context
) -> ClassifyOutput:
"""Classify number as high (>= 50) or low (< 50)."""
print(f"\n📊 [classify] Classifying number: {input_data.value}")
await asyncio.sleep(0.3)
category = "high" if input_data.value >= 50 else "low"
print(f" Value {input_data.value} → Category: '{category}'")
print(f" Routing to the '{category}' processing branch...")
return ClassifyOutput(category=category, value=input_data.value)
async def process_high(
input_data: ClassifyOutput, context: Context
) -> ProcessedOutput:
"""Process high-value numbers (multiply by 10)."""
print(f"\n🔥 [process_high] Processing HIGH value: {input_data.value}")
await asyncio.sleep(0.3)
multiplier = 10
result = input_data.value * multiplier
print(f" Applied multiplier: x{multiplier}")
print(f" Result: {input_data.value} × {multiplier} = {result}")
return ProcessedOutput(
result=f"High value processed: {result}",
original_value=input_data.value,
multiplier=multiplier,
)
async def process_low(
input_data: ClassifyOutput, context: Context
) -> ProcessedOutput:
"""Process low-value numbers (multiply by 2)."""
print(f"\n❄️ [process_low] Processing LOW value: {input_data.value}")
await asyncio.sleep(0.3)
multiplier = 2
result = input_data.value * multiplier
print(f" Applied multiplier: x{multiplier}")
print(f" Result: {input_data.value} × {multiplier} = {result}")
return ProcessedOutput(
result=f"Low value processed: {result}",
original_value=input_data.value,
multiplier=multiplier,
)
# =============================================================================
# Workflow Construction
# =============================================================================
def get_workflow() -> Workflow:
"""
Build a conditional branching workflow.
Edge conditions use output_based routing on category field:
- classify → process_high: when category == "high" (value >= 50)
- classify → process_low: when category == "low" (value < 50)
Both branches are end steps, so the workflow completes when
either branch finishes (only one will execute per run).
"""
# Create steps
classify_step = FunctionStep(
step_id="classify",
metadata=StepMetadata(name="Classify Number"),
input_type=NumberInput,
output_type=ClassifyOutput,
func=classify_number,
)
process_high_step = FunctionStep(
step_id="process_high",
metadata=StepMetadata(name="Process High Value"),
input_type=ClassifyOutput,
output_type=ProcessedOutput,
func=process_high,
)
process_low_step = FunctionStep(
step_id="process_low",
metadata=StepMetadata(name="Process Low Value"),
input_type=ClassifyOutput,
output_type=ProcessedOutput,
func=process_low,
)
# Build workflow
workflow = Workflow(
metadata=WorkflowMetadata(
name="Conditional Branching Workflow",
description="Routes to different processors based on input value",
)
)
# Add all steps
workflow.add_step(classify_step)
workflow.add_step(process_high_step)
workflow.add_step(process_low_step)
# Fan-out: classify → two conditional branches (mutually exclusive)
workflow.add_edge(
"classify",
"process_high",
{"type": "output_based", "field": "category", "operator": "==", "value": "high"},
)
workflow.add_edge(
"classify",
"process_low",
{"type": "output_based", "field": "category", "operator": "==", "value": "low"},
)
# Set start and BOTH branches as end steps
workflow.set_start_step("classify")
workflow.add_end_step("process_high")
workflow.add_end_step("process_low")
return workflow
# Create workflow instance for import
workflow = get_workflow()
# =============================================================================
# Main: Run Examples
# =============================================================================
async def run_example(value: int, description: str):
"""Run workflow with a specific input value."""
print("\n" + "=" * 60)
print(f"TEST: {description}")
print(f"Input value: {value}")
print("=" * 60)
runner = WorkflowRunner()
input_data = {"value": value}
result = await runner.run(workflow, input_data)
# Find the completed end step
for step_id in workflow.end_step_ids:
step_exec = result.step_executions.get(step_id)
if step_exec and step_exec.output_data:
print(f"\n✨ FINAL RESULT:")
print(f" {step_exec.output_data.get('result', 'N/A')}")
break
async def main():
"""Demonstrate conditional branching with different input values."""
print("=" * 60)
print("CONDITIONAL BRANCHING WORKFLOW EXAMPLE")
print("=" * 60)
print("""
The classify step routes to process_high (x10 multiplier) when input >= 50,
or process_low (x2 multiplier) when input < 50. Only one branch executes.
""")
# Test Case 1: High value (>= 50)
await run_example(75, "High value (>= 50) → process_high branch")
# Test Case 2: Low value (< 50)
await run_example(25, "Low value (< 50) → process_low branch")
# Test Case 3: Boundary case
await run_example(50, "Boundary case (exactly 50) → process_high branch")
print("\n" + "=" * 60)
print("All test cases completed!")
print("Notice how each input value took a different conditional branch.")
print("=" * 60)
if __name__ == "__main__":
asyncio.run(main())