example_usage.py
1 """ 2 Example usage of YAML Workflow Parser. 3 4 This script demonstrates how to load and execute YAML workflow files. 5 """ 6 7 import os 8 from pathlib import Path 9 10 # Ensure OPENAI_API_KEY is set 11 if not os.getenv("OPENAI_API_KEY"): 12 print("Please set OPENAI_API_KEY environment variable") 13 print("export OPENAI_API_KEY='your-api-key'") 14 exit(1) 15 16 from praisonaiagents.workflows import YAMLWorkflowParser, WorkflowManager 17 18 19 def example_1_parse_and_run(): 20 """Example 1: Parse YAML and run workflow directly.""" 21 print("=" * 60) 22 print("Example 1: Parse YAML and run workflow") 23 print("=" * 60) 24 25 yaml_content = """ 26 name: Quick Research 27 description: A quick research workflow 28 29 agents: 30 researcher: 31 name: Researcher 32 role: Research Analyst 33 goal: Research topics 34 instructions: "Provide brief, factual information." 35 36 steps: 37 - agent: researcher 38 action: "Research: {{input}}" 39 """ 40 41 parser = YAMLWorkflowParser() 42 workflow = parser.parse_string(yaml_content) 43 44 print(f"Workflow Name: {workflow.name}") 45 print(f"Steps: {len(workflow.steps)}") 46 47 # Run the workflow 48 result = workflow.start("What is Python?") 49 print(f"Result: {result['output'][:200]}...") 50 print() 51 52 53 def example_2_load_yaml_file(): 54 """Example 2: Load workflow from YAML file using WorkflowManager.""" 55 print("=" * 60) 56 print("Example 2: Load YAML file with WorkflowManager") 57 print("=" * 60) 58 59 # Get the directory of this script 60 script_dir = Path(__file__).parent 61 yaml_file = script_dir / "simple_workflow.yaml" 62 63 if not yaml_file.exists(): 64 print(f"YAML file not found: {yaml_file}") 65 return 66 67 manager = WorkflowManager() 68 workflow = manager.load_yaml(yaml_file) 69 70 print(f"Loaded workflow: {workflow.name}") 71 print(f"Steps: {len(workflow.steps)}") 72 73 # Run the workflow 74 result = workflow.start("Benefits of renewable energy") 75 print(f"Result: {result['output'][:200]}...") 76 print() 77 78 79 def example_3_execute_yaml_directly(): 80 """Example 3: Execute YAML file directly with WorkflowManager.""" 81 print("=" * 60) 82 print("Example 3: Execute YAML directly") 83 print("=" * 60) 84 85 script_dir = Path(__file__).parent 86 yaml_file = script_dir / "simple_workflow.yaml" 87 88 if not yaml_file.exists(): 89 print(f"YAML file not found: {yaml_file}") 90 return 91 92 manager = WorkflowManager() 93 result = manager.execute_yaml( 94 yaml_file, 95 input_data="The future of AI", 96 output="verbose" 97 ) 98 99 print(f"Status: {result['status']}") 100 print(f"Result: {result['output'][:200]}...") 101 print() 102 103 104 def example_4_with_variables(): 105 """Example 4: YAML workflow with variables.""" 106 print("=" * 60) 107 print("Example 4: Workflow with variables") 108 print("=" * 60) 109 110 yaml_content = """ 111 name: Variable Workflow 112 variables: 113 topic: Machine Learning 114 style: concise 115 116 agents: 117 writer: 118 name: Writer 119 role: Content Writer 120 goal: Write content 121 instructions: "Write {{style}} content about the topic." 122 123 steps: 124 - agent: writer 125 action: "Write about {{topic}}" 126 """ 127 128 parser = YAMLWorkflowParser() 129 workflow = parser.parse_string(yaml_content) 130 131 print(f"Variables: {workflow.variables}") 132 133 # Run with default variables 134 result = workflow.start("") 135 print(f"Result: {result['output'][:200]}...") 136 print() 137 138 139 if __name__ == "__main__": 140 print("YAML Workflow Examples") 141 print("=" * 60) 142 print() 143 144 # Run examples 145 example_1_parse_and_run() 146 example_2_load_yaml_file() 147 example_3_execute_yaml_directly() 148 example_4_with_variables() 149 150 print("=" * 60) 151 print("All examples completed!") 152 print("=" * 60)