2424import importlib .util
2525from praisonai_tools import BaseTool
2626import os
27+ import logging
28+
29+ agentops = False
30+ try :
31+ import agentops
32+ agentops = True
33+ except ImportError :
34+ agentops = False
2735
2836os .environ ["OTEL_SDK_DISABLED" ] = "true"
2937
@@ -38,23 +46,39 @@ def disable_crewai_telemetry():
3846disable_crewai_telemetry ()
3947
4048class AgentsGenerator :
41- def __init__ (self , agent_file , framework , config_list ):
49+ def __init__ (self , agent_file , framework , config_list , log_level = None , agent_callback = None , task_callback = None ):
4250 """
4351 Initialize the AgentsGenerator object.
4452
4553 Parameters:
4654 agent_file (str): The path to the agent file.
4755 framework (str): The framework to be used for the agents.
4856 config_list (list): A list of configurations for the agents.
57+ log_level (int, optional): The logging level to use. Defaults to logging.INFO.
58+ agent_callback (callable, optional): A callback function to be executed after each agent step.
59+ task_callback (callable, optional): A callback function to be executed after each tool run.
4960
5061 Attributes:
5162 agent_file (str): The path to the agent file.
5263 framework (str): The framework to be used for the agents.
5364 config_list (list): A list of configurations for the agents.
65+ log_level (int): The logging level to use.
66+ agent_callback (callable, optional): A callback function to be executed after each agent step.
67+ task_callback (callable, optional): A callback function to be executed after each tool run.
5468 """
5569 self .agent_file = agent_file
5670 self .framework = framework
5771 self .config_list = config_list
72+ self .log_level = log_level
73+ self .agent_callback = agent_callback
74+ self .task_callback = task_callback
75+ self .log_level = log_level or logging .getLogger ().getEffectiveLevel ()
76+ if self .log_level == logging .NOTSET :
77+ self .log_level = os .environ .get ('LOGLEVEL' , 'INFO' ).upper ()
78+
79+ logging .basicConfig (level = self .log_level , format = '%(asctime)s - %(levelname)s - %(message)s' )
80+ self .logger = logging .getLogger (__name__ )
81+ self .logger .setLevel (self .log_level )
5882
5983 def is_function_or_decorated (self , obj ):
6084 """
@@ -183,10 +207,10 @@ def generate_crew_and_kickoff(self):
183207
184208 if os .path .isfile (tools_py_path ):
185209 tools_dict .update (self .load_tools_from_module_class (tools_py_path ))
186- # print ("tools.py exists in the root directory. Loading tools.py and skipping tools folder.")
210+ self . logger . debug ("tools.py exists in the root directory. Loading tools.py and skipping tools folder." )
187211 elif tools_dir_path .is_dir ():
188212 tools_dict .update (self .load_tools_from_module_class (tools_dir_path ))
189- # print ("tools folder exists in the root directory")
213+ self . logger . debug ("tools folder exists in the root directory" )
190214
191215 framework = self .framework or config .get ('framework' )
192216
@@ -252,20 +276,39 @@ def generate_crew_and_kickoff(self):
252276 # Adding tools to the agent if exists
253277 agent_tools = [tools_dict [tool ] for tool in details .get ('tools' , []) if tool in tools_dict ]
254278 agent = Agent (role = role_filled , goal = goal_filled , backstory = backstory_filled , tools = agent_tools , allow_delegation = False )
279+
280+ # Set agent callback if provided
281+ if self .agent_callback :
282+ agent .step_callback = self .agent_callback
283+
255284 agents [role ] = agent
256285
257286 for task_name , task_details in details .get ('tasks' , {}).items ():
258287 description_filled = task_details ['description' ].format (topic = topic )
259288 expected_output_filled = task_details ['expected_output' ].format (topic = topic )
260289
261290 task = Task (description = description_filled , expected_output = expected_output_filled , agent = agent )
291+
292+ # Set tool callback if provided
293+ if self .task_callback :
294+ task .callback = self .task_callback
295+
262296 tasks .append (task )
297+ if agentops :
298+ agentops .init ()
263299 crew = Crew (
264300 agents = list (agents .values ()),
265301 tasks = tasks ,
266302 verbose = 2
267303 )
304+ if agentops :
305+ agentops .end_session ("Success" )
306+
307+ self .logger .debug ("Final Crew Configuration:" )
308+ self .logger .debug (f"Agents: { crew .agents } " )
309+ self .logger .debug (f"Tasks: { crew .tasks } " )
268310
269311 response = crew .kickoff ()
270312 result = f"### Task Output ###\n { response } "
271313 return result
314+
0 commit comments