1+ """
2+ Advanced Dynamic Input Example for PraisonAI
3+
4+ This example demonstrates an advanced system that dynamically creates agents and tasks
5+ based on user inputs, including conditional logic, custom configurations, and file output.
6+ """
7+
8+ from praisonaiagents import Agent , Task , PraisonAIAgents
9+ from praisonaiagents .tools import duckduckgo
10+ import os
11+ from typing import Dict , List
12+
13+ class DynamicAgentSystem :
14+ """Advanced system for handling dynamic user inputs"""
15+
16+ def __init__ (self ):
17+ self .user_preferences = {}
18+ self .llm_config = {
19+ "model" : os .getenv ("LLM_MODEL" , "gpt-4o-mini" ),
20+ "temperature" : 0.7
21+ }
22+
23+ def collect_user_inputs (self ) -> Dict [str , str ]:
24+ """Collect multiple user inputs"""
25+ inputs = {}
26+ inputs ['topic' ] = input ("What topic would you like to explore? " )
27+ inputs ['depth' ] = input ("Analysis depth (quick/detailed)? " ).lower () or "quick"
28+ inputs ['output_format' ] = input ("Output format (summary/report/bullets)? " ).lower () or "summary"
29+ inputs ['language' ] = input ("Language preference (en/es/fr)? " ).lower () or "en"
30+ return inputs
31+
32+ def create_dynamic_agents (self , inputs : Dict [str , str ]) -> List [Agent ]:
33+ """Create agents based on user inputs"""
34+ agents = []
35+
36+ # Research agent with dynamic configuration
37+ research_agent = Agent (
38+ name = "ResearchExpert" ,
39+ role = f"{ 'Detailed' if inputs ['depth' ] == 'detailed' else 'Quick' } Research Specialist" ,
40+ goal = f"Conduct { inputs ['depth' ]} research on { inputs ['topic' ]} in { inputs ['language' ]} " ,
41+ backstory = f"Multilingual expert specializing in { inputs ['depth' ]} analysis" ,
42+ tools = [duckduckgo ],
43+ self_reflect = inputs ['depth' ] == 'detailed' ,
44+ llm = self .llm_config
45+ )
46+ agents .append (research_agent )
47+
48+ # Format agent based on output preference
49+ format_agent = Agent (
50+ name = "FormatExpert" ,
51+ role = f"{ inputs ['output_format' ].title ()} Formatter" ,
52+ goal = f"Format research into { inputs ['output_format' ]} style" ,
53+ backstory = f"Expert in creating { inputs ['output_format' ]} documents" ,
54+ llm = self .llm_config
55+ )
56+ agents .append (format_agent )
57+
58+ # Optional quality agent for detailed analysis
59+ if inputs ['depth' ] == 'detailed' :
60+ quality_agent = Agent (
61+ name = "QualityChecker" ,
62+ role = "Quality Assurance Specialist" ,
63+ goal = "Verify accuracy and completeness" ,
64+ backstory = "Expert in fact-checking and quality control" ,
65+ llm = self .llm_config
66+ )
67+ agents .append (quality_agent )
68+
69+ return agents
70+
71+ def create_dynamic_tasks (self , agents : List [Agent ], inputs : Dict [str , str ]) -> List [Task ]:
72+ """Create tasks based on user inputs and agents"""
73+ tasks = []
74+
75+ # Research task
76+ research_task = Task (
77+ description = f"""
78+ Research '{ inputs ['topic' ]} ' with the following requirements:
79+ - Depth: { inputs ['depth' ]}
80+ - Language: { inputs ['language' ]}
81+ - Find { '5-10' if inputs ['depth' ] == 'detailed' else '3-5' } key points
82+ - Include sources and citations
83+ """ ,
84+ expected_output = f"Research findings about { inputs ['topic' ]} with sources" ,
85+ agent = agents [0 ], # Research agent
86+ name = "research_phase"
87+ )
88+ tasks .append (research_task )
89+
90+ # Formatting task
91+ format_instructions = {
92+ 'summary' : "Create a concise paragraph summary" ,
93+ 'report' : "Create a structured report with sections" ,
94+ 'bullets' : "Create a bullet-point list of key findings"
95+ }
96+
97+ format_task = Task (
98+ description = f"""
99+ Format the research findings as follows:
100+ - Style: { format_instructions [inputs ['output_format' ]]}
101+ - Language: { inputs ['language' ]}
102+ - Maintain all source citations
103+ """ ,
104+ expected_output = f"{ inputs ['output_format' ].title ()} of { inputs ['topic' ]} " ,
105+ agent = agents [1 ], # Format agent
106+ context = [research_task ],
107+ name = "formatting_phase"
108+ )
109+ tasks .append (format_task )
110+
111+ # Quality check task (if detailed)
112+ if inputs ['depth' ] == 'detailed' and len (agents ) > 2 :
113+ quality_task = Task (
114+ description = "Verify all facts, check sources, and ensure completeness" ,
115+ expected_output = "Quality-assured final output with verification notes" ,
116+ agent = agents [2 ], # Quality agent
117+ context = [format_task ],
118+ name = "quality_phase"
119+ )
120+ tasks .append (quality_task )
121+
122+ return tasks
123+
124+ def run (self ):
125+ """Main execution flow"""
126+ # Collect inputs
127+ print ("🎯 PraisonAI Dynamic Input System" )
128+ print ("-" * 40 )
129+ inputs = self .collect_user_inputs ()
130+
131+ # Create dynamic agents and tasks
132+ agents = self .create_dynamic_agents (inputs )
133+ tasks = self .create_dynamic_tasks (agents , inputs )
134+
135+ # Configure process based on depth
136+ process = "hierarchical" if inputs ['depth' ] == 'detailed' else "sequential"
137+
138+ # Run the system
139+ print (f"\n 🚀 Starting { process } analysis for '{ inputs ['topic' ]} '..." )
140+ praison_agents = PraisonAIAgents (
141+ agents = agents ,
142+ tasks = tasks ,
143+ process = process ,
144+ verbose = inputs ['depth' ] == 'detailed'
145+ )
146+
147+ result = praison_agents .start ()
148+
149+ # Save results if detailed
150+ if inputs ['depth' ] == 'detailed' :
151+ filename = f"{ inputs ['topic' ].replace (' ' , '_' )} _{ inputs ['output_format' ]} .txt"
152+ with open (filename , 'w' , encoding = 'utf-8' ) as f :
153+ f .write (result )
154+ print (f"\n 📄 Results saved to: { filename } " )
155+
156+ return result
157+
158+ # Usage
159+ if __name__ == "__main__" :
160+ system = DynamicAgentSystem ()
161+ result = system .run ()
162+ print ("\n 📊 Final Result:" )
163+ print (result )
0 commit comments