From 01beeda39c0ed70d319acc5f71e0d1c05002410e Mon Sep 17 00:00:00 2001 From: Kye Date: Wed, 17 Jan 2024 11:53:29 -0500 Subject: [PATCH] [FEAT][Logging][swarms.structs] --- pyproject.toml | 2 +- swarms/structs/agent.py | 6 +-- swarms/structs/model_parallizer.py | 6 ++- swarms/structs/nonlinear_workflow.py | 4 +- swarms/structs/recursive_workflow.py | 2 +- swarms/structs/sequential_workflow.py | 55 ++++++++++++++++++++++----- swarms/structs/swarm_net.py | 7 ++-- swarms/structs/task.py | 7 ++-- 8 files changed, 65 insertions(+), 24 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 65661841..de519015 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "poetry.core.masonry.api" [tool.poetry] name = "swarms" -version = "3.6.3" +version = "3.6.4" description = "Swarms - Pytorch" license = "MIT" authors = ["Kye Gomez "] diff --git a/swarms/structs/agent.py b/swarms/structs/agent.py index af770466..1b137988 100644 --- a/swarms/structs/agent.py +++ b/swarms/structs/agent.py @@ -232,7 +232,7 @@ class Agent: self.traceback_handlers = traceback_handlers self.streaming_on = streaming_on self.docs = docs - + # The max_loops will be set dynamically if the dynamic_loop if self.dynamic_loops: self.max_loops = "auto" @@ -263,7 +263,7 @@ class Agent: ) # self.short_memory_test = Conversation(time_enabled=True) - + # If the docs exist then ingest the docs if self.docs: self.ingest_docs(self.docs) @@ -1357,5 +1357,5 @@ class Agent: """ for doc in docs: data = data_to_text(doc) - + return self.short_memory.append(data) diff --git a/swarms/structs/model_parallizer.py b/swarms/structs/model_parallizer.py index 0a562f48..c6ac6150 100644 --- a/swarms/structs/model_parallizer.py +++ b/swarms/structs/model_parallizer.py @@ -73,7 +73,7 @@ class ModelParallelizer: ) return list(responses) except Exception as error: - print( + logger.error( f"[ERROR][ModelParallelizer] [ROOT CAUSE] [{error}]" ) @@ -191,15 +191,17 @@ class ModelParallelizer: self.task_history.append(task) return responses except Exception as error: - print( + logger.error( f"[ERROR][ModelParallelizer] [ROOT CAUSE] [{error}]" ) raise error def add_llm(self, llm: Callable): """Add an llm to the god mode""" + logger.info(f"[INFO][ModelParallelizer] Adding LLM {llm}") self.llms.append(llm) def remove_llm(self, llm: Callable): """Remove an llm from the god mode""" + logger.info(f"[INFO][ModelParallelizer] Removing LLM {llm}") self.llms.remove(llm) diff --git a/swarms/structs/nonlinear_workflow.py b/swarms/structs/nonlinear_workflow.py index 2a1be2d7..0fc1d200 100644 --- a/swarms/structs/nonlinear_workflow.py +++ b/swarms/structs/nonlinear_workflow.py @@ -1,5 +1,6 @@ from swarms.structs.task import Task from swarms.structs.base import BaseStructure +from swarms.utils.logger import logger # noqa: F401 class NonlinearWorkflow(BaseStructure): @@ -47,6 +48,7 @@ class NonlinearWorkflow(BaseStructure): assert task is not None, "Task cannot be None" self.tasks[task.name] = task self.edges[task.name] = list(dependencies) + logger.info(f"[NonlinearWorkflow] [Added task {task.name}]") def run(self): """ @@ -84,5 +86,5 @@ class NonlinearWorkflow(BaseStructure): if task in deps: deps.remove(task) except Exception as error: - print(f"[ERROR][NonlinearWorkflow] {error}") + logger.error(f"[ERROR][NonlinearWorkflow] {error}") raise error diff --git a/swarms/structs/recursive_workflow.py b/swarms/structs/recursive_workflow.py index 1d7ae773..64b27406 100644 --- a/swarms/structs/recursive_workflow.py +++ b/swarms/structs/recursive_workflow.py @@ -81,7 +81,7 @@ class RecursiveWorkflow(BaseStructure): and self.stop_token in result ): break - logger.info(f"{result}") + print(f"{result}") except Exception as error: logger.warning(f"[ERROR][RecursiveWorkflow] {error}") raise error diff --git a/swarms/structs/sequential_workflow.py b/swarms/structs/sequential_workflow.py index 9f7881c1..15e2c0f8 100644 --- a/swarms/structs/sequential_workflow.py +++ b/swarms/structs/sequential_workflow.py @@ -7,6 +7,7 @@ from termcolor import colored from swarms.structs.agent import Agent from swarms.structs.task import Task +from swarms.utils.logger import logger # SequentialWorkflow class definition using dataclasses @@ -83,8 +84,13 @@ class SequentialWorkflow: kwargs=kwargs, ) ) + + logger.info( + f"[INFO][SequentialWorkflow] Added task {task} to" + " workflow" + ) except Exception as error: - print( + logger.error( colored( f"Error adding task to workflow: {error}", "red" ), @@ -96,7 +102,7 @@ class SequentialWorkflow: for task in self.tasks: task.result = None except Exception as error: - print( + logger.error( colored(f"Error resetting workflow: {error}", "red"), ) @@ -112,7 +118,7 @@ class SequentialWorkflow: task.description: task.result for task in self.tasks } except Exception as error: - print( + logger.error( colored( f"Error getting task results: {error}", "red" ), @@ -126,8 +132,12 @@ class SequentialWorkflow: for task in self.tasks if task.description != task ] + logger.info( + f"[INFO][SequentialWorkflow] Removed task {task} from" + " workflow" + ) except Exception as error: - print( + logger.error( colored( f"Error removing task from workflow: {error}", "red", @@ -166,8 +176,13 @@ class SequentialWorkflow: raise ValueError( f"Task {task} not found in workflow." ) - except Exception as error: + print( + f"[INFO][SequentialWorkflow] Updated task {task} in" + " workflow" + ) + except Exception as error: + logger.error( colored( f"Error updating task in workflow: {error}", "red" ), @@ -203,8 +218,13 @@ class SequentialWorkflow: raise ValueError( f"Task {task} not found in workflow." ) - except Exception as error: + print( + f"[INFO][SequentialWorkflow] Deleted task {task} from" + " workflow" + ) + except Exception as error: + logger.error( colored( f"Error deleting task from workflow: {error}", "red", @@ -286,8 +306,13 @@ class SequentialWorkflow: "max_loops": self.max_loops, } json.dump(state, f, indent=4) - except Exception as error: + print( + "[INFO][SequentialWorkflow] Saved workflow state to" + f" {filepath}" + ) + except Exception as error: + logger.error( colored( f"Error saving workflow state: {error}", "red", @@ -376,8 +401,13 @@ class SequentialWorkflow: kwargs=kwargs["kwargs"], ) self.tasks.append(task) - except Exception as error: + print( + f"[INFO][SequentialWorkflow] Added task {task} to" + " workflow" + ) + except Exception as error: + logger.error( colored( f"Error adding objective to workflow: {error}", "red", @@ -421,8 +451,13 @@ class SequentialWorkflow: history=task_state["history"], ) self.tasks.append(task) - except Exception as error: + print( + "[INFO][SequentialWorkflow] Loaded workflow state" + f" from {filepath}" + ) + except Exception as error: + logger.error( colored( f"Error loading workflow state: {error}", "red", @@ -482,7 +517,7 @@ class SequentialWorkflow: "sequential_workflow_state.json" ) except Exception as e: - print( + logger.error( colored( ( "Error initializing the Sequential workflow:" diff --git a/swarms/structs/swarm_net.py b/swarms/structs/swarm_net.py index 15647c7c..80f339f6 100644 --- a/swarms/structs/swarm_net.py +++ b/swarms/structs/swarm_net.py @@ -8,6 +8,7 @@ from fastapi import FastAPI from swarms.structs.agent import Agent from swarms.structs.base import BaseStructure +from swarms.utils.logger import logger # noqa: F401 class SwarmNetwork(BaseStructure): @@ -168,7 +169,7 @@ class SwarmNetwork(BaseStructure): # self.logger.info(f"No agent found with ID {agent_id}") raise ValueError(f"No agent found with ID {agent_id}") except Exception as error: - print(f"Error running task on agent: {error}") + self.logger.error(f"Error running task on agent: {error}") raise error def run_many_agents( @@ -189,7 +190,7 @@ class SwarmNetwork(BaseStructure): for agent in self.agents ] except Exception as error: - print(f"Error running task on agents: {error}") + logger.error(f"Error running task on agents: {error}") raise error def list_agents(self): @@ -211,7 +212,7 @@ class SwarmNetwork(BaseStructure): f" {agent.agent_description}] [Status] [Running]" ) except Exception as error: - print(f"Error listing agents: {error}") + logger.error(f"Error listing agents: {error}") raise error def get_agent(self, agent_id): diff --git a/swarms/structs/task.py b/swarms/structs/task.py index 7d630b23..7eb9f335 100644 --- a/swarms/structs/task.py +++ b/swarms/structs/task.py @@ -11,7 +11,7 @@ from typing import ( ) from swarms.structs.agent import Agent - +from swarms.utils.logger import logger @dataclass class Task: @@ -133,7 +133,7 @@ class Task: self.scheduler.enter(delay, 1, self.execute) self.scheduler_run() except Exception as error: - print(f"[ERROR][Task] {error}") + logger.error(f"[ERROR][Task] {error}") def set_trigger(self, trigger: Callable): """ @@ -193,11 +193,12 @@ class Task: Returns: bool: True if all the dependencies have been completed, False otherwise. """ + logger.info(f"[INFO][Task] Checking dependency completion") try: for task in self.dependencies: if not task.is_completed(): return False except Exception as error: - print( + logger.error( f"[ERROR][Task][check_dependency_completion] {error}" )