-
-
Notifications
You must be signed in to change notification settings - Fork 781
Expand file tree
/
Copy pathworkflow-state-example.py
More file actions
222 lines (179 loc) · 8.22 KB
/
workflow-state-example.py
File metadata and controls
222 lines (179 loc) · 8.22 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
#!/usr/bin/env python3
"""
Stateful Workflow Example
Demonstrates advanced state management in multi-agent workflows with
persistence, conditional execution, and cross-task communication.
"""
from praisonaiagents import AgentTeam, Agent, Task
def research_tool(topic: str, num_sources: int = 5):
"""Simulated research tool that updates workflow state"""
# In a real implementation, this would perform actual research
findings = [
f"Finding 1 about {topic}: Key insight on applications",
f"Finding 2 about {topic}: Recent breakthrough in methodology",
f"Finding 3 about {topic}: Performance benchmarks and comparisons"
][:num_sources]
return {
"topic": topic,
"sources_found": len(findings),
"findings": findings,
"confidence": 0.85
}
def analysis_tool(research_data: dict):
"""Simulated analysis tool"""
findings = research_data.get("findings", [])
analysis = {
"key_themes": ["applications", "methodology", "performance"],
"findings_count": len(findings),
"confidence_score": research_data.get("confidence", 0.0),
"recommendation": "Proceed with detailed investigation" if len(findings) >= 3 else "Need more research"
}
return analysis
def main():
print("🔄 Stateful Workflow Example")
print("=" * 50)
# Create agents with tools
researcher = Agent(
name="Research Agent",
role="AI Research Specialist",
instructions="Conduct thorough research and track progress",
tools=[research_tool]
)
analyzer = Agent(
name="Analysis Agent",
role="Data Analysis Expert",
instructions="Analyze research findings and provide insights",
tools=[analysis_tool]
)
writer = Agent(
name="Report Writer",
role="Technical Writer",
instructions="Create comprehensive reports based on analysis"
)
# Create tasks with conditional logic
research_task = Task(
name="research_task",
description="Research the specified topic using available tools. Track findings in workflow state.",
agent=researcher,
expected_output="Research findings with confidence scores"
)
analysis_task = Task(
name="analysis_task",
description="Analyze research findings if sufficient data is available. Use workflow state to check progress.",
agent=analyzer,
expected_output="Analysis summary with recommendations",
context=[research_task] # Depends on research task
)
report_task = Task(
name="report_task",
description="Write final report if analysis shows high confidence. Check workflow state for quality metrics.",
agent=writer,
expected_output="Final research report",
context=[research_task, analysis_task] # Depends on both previous tasks
)
# Create stateful workflow
workflow = AgentTeam(
agents=[researcher, analyzer, writer],
tasks=[research_task, analysis_task, report_task],
memory=True,
process="workflow",
user_id="research_project_001",
)
print(f"🤖 Created workflow with {len(workflow.agents)} agents and {len(workflow.tasks)} tasks")
# Set initial workflow state
workflow.set_state("research_topic", "artificial intelligence safety")
workflow.set_state("target_sources", 5)
workflow.set_state("quality_threshold", 0.8)
workflow.set_state("project_deadline", "2024-12-31")
print("📝 Set initial workflow state:")
print(f" Topic: {workflow.get_state('research_topic')}")
print(f" Target sources: {workflow.get_state('target_sources')}")
print(f" Quality threshold: {workflow.get_state('quality_threshold')}")
# Save session state before starting
workflow.save_session_state("ai_safety_research_session")
print("💾 Saved session state")
# Simulate workflow execution with state updates
print("\n🚀 Starting workflow execution...")
# Task 1: Research
print("\n--- Research Phase ---")
workflow.set_state("current_phase", "research")
workflow.increment_state("tasks_completed", 0, default=0)
# Simulate research execution
topic = workflow.get_state("research_topic")
target = workflow.get_state("target_sources", 5)
research_result = research_tool(topic, target)
workflow.set_state("research_results", research_result)
workflow.increment_state("tasks_completed", 1)
print(f"✅ Research completed: {research_result['sources_found']} sources found")
# Task 2: Analysis (conditional on research quality)
print("\n--- Analysis Phase ---")
research_data = workflow.get_state("research_results", {})
if research_data.get("sources_found", 0) >= 3:
workflow.set_state("current_phase", "analysis")
analysis_result = analysis_tool(research_data)
workflow.set_state("analysis_results", analysis_result)
workflow.increment_state("tasks_completed", 1)
print(f"✅ Analysis completed: {analysis_result['recommendation']}")
else:
workflow.set_state("current_phase", "research_insufficient")
print("⚠️ Insufficient research data for analysis")
# Task 3: Report Writing (conditional on analysis confidence)
print("\n--- Report Writing Phase ---")
analysis_data = workflow.get_state("analysis_results", {})
quality_threshold = workflow.get_state("quality_threshold", 0.8)
if analysis_data.get("confidence_score", 0) >= quality_threshold:
workflow.set_state("current_phase", "report_writing")
# Simulate report writing
report_sections = ["Introduction", "Research Findings", "Analysis", "Conclusions"]
workflow.set_state("report_sections", report_sections)
workflow.increment_state("tasks_completed", 1)
print(f"✅ Report completed with {len(report_sections)} sections")
else:
workflow.set_state("current_phase", "quality_insufficient")
print("⚠️ Analysis confidence too low for final report")
# Display final workflow state
print("\n📊 Final Workflow State:")
print("=" * 30)
all_state = workflow.get_all_state()
for key, value in all_state.items():
if isinstance(value, dict):
print(f" {key}: {type(value).__name__} with {len(value)} items")
elif isinstance(value, list):
print(f" {key}: {len(value)} items")
else:
print(f" {key}: {value}")
# Demonstrate state persistence
print("\n🔄 Demonstrating state persistence...")
# Save current state
session_id = "ai_safety_research_session"
workflow.save_session_state(session_id)
# Clear state and restore
print("Clearing state...")
workflow.clear_state()
print(f"State after clearing: {len(workflow.get_all_state())} items")
print("Restoring state...")
restored = workflow.restore_session_state(session_id)
print(f"State restoration: {'✅ Success' if restored else '❌ Failed'}")
print(f"State after restoring: {len(workflow.get_all_state())} items")
# Demonstrate convenience methods
print("\n🛠️ Demonstrating convenience methods:")
# Check if keys exist
has_topic = workflow.has_state("research_topic")
has_results = workflow.has_state("research_results")
print(f"Has research topic: {has_topic}")
print(f"Has research results: {has_results}")
# Work with list state
workflow.append_to_state("project_notes", "Initial research phase completed")
workflow.append_to_state("project_notes", "Analysis phase successful")
workflow.append_to_state("project_notes", "Report writing finalized")
notes = workflow.get_state("project_notes", [])
print(f"Project notes: {len(notes)} entries")
for i, note in enumerate(notes, 1):
print(f" {i}. {note}")
# Increment counters
final_tasks = workflow.get_state("tasks_completed", 0)
print(f"Total tasks completed: {final_tasks}")
print("\n✅ Stateful workflow example completed!")
print("The workflow maintained state across all phases and demonstrated persistence.")
if __name__ == "__main__":
main()