From 71da285dc18052c377cf9222c0720bfc0a7463dc Mon Sep 17 00:00:00 2001 From: Kye Gomez Date: Wed, 26 Jun 2024 20:00:09 -0700 Subject: [PATCH] [CLEANUP] --- docs/swarms_cloud/agent_api.md | 2 + new_workflow_concurrent.py | 47 +++++++++++++++---- .../graph_workflow_example.py | 7 ++- .../rag_doc_agent.py | 14 +++--- scripts/cleanup/json_log_cleanup.py | 7 ++- swarms/structs/__init__.py | 7 ++- swarms/structs/base_workflow.py | 1 + 7 files changed, 61 insertions(+), 24 deletions(-) diff --git a/docs/swarms_cloud/agent_api.md b/docs/swarms_cloud/agent_api.md index cd7916fc..63a0bcc8 100644 --- a/docs/swarms_cloud/agent_api.md +++ b/docs/swarms_cloud/agent_api.md @@ -2,6 +2,8 @@ The Swarms API provides endpoints to interact with various language models, manage agent configurations, and handle token counting. This documentation covers the available endpoints, input and output models, and detailed examples for each endpoint. +URL: `https://api.swarms.world` + ## Endpoints ### `/v1/models` diff --git a/new_workflow_concurrent.py b/new_workflow_concurrent.py index 81595386..67f86cdc 100644 --- a/new_workflow_concurrent.py +++ b/new_workflow_concurrent.py @@ -39,7 +39,9 @@ class ConcurrentWorkflow(BaseWorkflow): return_results: bool = False stopping_condition: Optional[Callable] = None - def run(self, task: Optional[str] = None, *args, **kwargs) -> Optional[List[Any]]: + def run( + self, task: Optional[str] = None, *args, **kwargs + ) -> Optional[List[Any]]: """ Executes the tasks in parallel using multiple threads. @@ -59,7 +61,12 @@ class ConcurrentWorkflow(BaseWorkflow): logger.warning("No agents found in the workflow.") break - threads = [threading.Thread(target=self.execute_agent, args=(agent, task)) for agent in self.agents] + threads = [ + threading.Thread( + target=self.execute_agent, args=(agent, task) + ) + for agent in self.agents + ] for thread in threads: thread.start() @@ -68,11 +75,19 @@ class ConcurrentWorkflow(BaseWorkflow): thread.join() if self.return_results: - results.extend([thread.result for thread in threads if hasattr(thread, 'result')]) + results.extend( + [ + thread.result + for thread in threads + if hasattr(thread, "result") + ] + ) loop += 1 - - if self.stopping_condition and self.stopping_condition(results): + + if self.stopping_condition and self.stopping_condition( + results + ): break return results if self.return_results else None @@ -85,8 +100,10 @@ class ConcurrentWorkflow(BaseWorkflow): def save(self): """Saves the state of the workflow to a file.""" self.save_state(self.saved_state_filepath) - - def execute_agent(self, agent: Agent, task: Optional[str] = None, *args, **kwargs): + + def execute_agent( + self, agent: Agent, task: Optional[str] = None, *args, **kwargs + ): try: result = agent.run(task, *args, **kwargs) if self.print_results: @@ -95,16 +112,26 @@ class ConcurrentWorkflow(BaseWorkflow): return result except Exception as e: logger.error(f"Agent {agent} generated an exception: {e}") - api_key = os.environ["OPENAI_API_KEY"] # Model swarm = ConcurrentWorkflow( - agents = [Agent(llm=OpenAIChat(openai_api_key=api_key, max_tokens=4000,), max_loops=4, dashboard=False)], + agents=[ + Agent( + llm=OpenAIChat( + openai_api_key=api_key, + max_tokens=4000, + ), + max_loops=4, + dashboard=False, + ) + ], ) # Run the workflow -swarm.run("Generate a report on the top 3 biggest expenses for small businesses and how businesses can save 20%") \ No newline at end of file +swarm.run( + "Generate a report on the top 3 biggest expenses for small businesses and how businesses can save 20%" +) diff --git a/playground/structs/multi_agent_collaboration/graph_workflow_example.py b/playground/structs/multi_agent_collaboration/graph_workflow_example.py index ad265db1..0b40e2dc 100644 --- a/playground/structs/multi_agent_collaboration/graph_workflow_example.py +++ b/playground/structs/multi_agent_collaboration/graph_workflow_example.py @@ -1,4 +1,3 @@ - import os from dotenv import load_dotenv @@ -9,16 +8,16 @@ load_dotenv() api_key = os.environ.get("OPENAI_API_KEY") -llm = OpenAIChat( - temperature=0.5, openai_api_key=api_key, max_tokens=4000 -) +llm = OpenAIChat(temperature=0.5, openai_api_key=api_key, max_tokens=4000) agent1 = Agent(llm=llm, max_loops=1, autosave=True, dashboard=True) agent2 = Agent(llm=llm, max_loops=1, autosave=True, dashboard=True) + def sample_task(): print("Running sample task") return "Task completed" + wf_graph = GraphWorkflow() wf_graph.add_node(Node(id="agent1", type=NodeType.AGENT, agent=agent1)) wf_graph.add_node(Node(id="agent2", type=NodeType.AGENT, agent=agent2)) diff --git a/playground/swarms/geo_economic_forecast_docs/rag_doc_agent.py b/playground/swarms/geo_economic_forecast_docs/rag_doc_agent.py index d79c6c1d..a39b57aa 100644 --- a/playground/swarms/geo_economic_forecast_docs/rag_doc_agent.py +++ b/playground/swarms/geo_economic_forecast_docs/rag_doc_agent.py @@ -25,7 +25,6 @@ Always prioritize accuracy, depth of analysis, and clarity in your responses. Us """ - # Initialize the agent agent = Agent( agent_name="Geo Expert AI", @@ -57,7 +56,6 @@ agent = Agent( ) - # Initialize the agent forecaster_agent = Agent( agent_name="Forecaster Agent", @@ -91,11 +89,13 @@ forecaster_agent = Agent( # Initialize the swarm swarm = MixtureOfAgents( - agents = [agent, forecaster_agent], - final_agent = forecaster_agent, - layers = 1, + agents=[agent, forecaster_agent], + final_agent=forecaster_agent, + layers=1, ) # Run the swarm -out = swarm.run("what is the economic impact of China from technology decoupling, and how is that impact measured? What is the forecast or economic, give some numbers") -print(out) \ No newline at end of file +out = swarm.run( + "what is the economic impact of China from technology decoupling, and how is that impact measured? What is the forecast or economic, give some numbers" +) +print(out) diff --git a/scripts/cleanup/json_log_cleanup.py b/scripts/cleanup/json_log_cleanup.py index 119c11db..9d99eae3 100644 --- a/scripts/cleanup/json_log_cleanup.py +++ b/scripts/cleanup/json_log_cleanup.py @@ -2,6 +2,7 @@ import os import shutil from loguru import logger + def cleanup_json_logs(name: str = None): # Define the root directory and the target directory root_dir = os.getcwd() @@ -46,8 +47,10 @@ def cleanup_json_logs(name: str = None): shutil.rmtree(chroma_folder) logger.info(f"Deleted Chroma folder at {chroma_folder}") - logger.info(f"All JSON, LOG and TXT files have been moved to {target_dir}") + logger.info( + f"All JSON, LOG and TXT files have been moved to {target_dir}" + ) # Call the function -cleanup_json_logs("heinz_swarm") \ No newline at end of file +cleanup_json_logs("heinz_swarm") diff --git a/swarms/structs/__init__.py b/swarms/structs/__init__.py index 4ebb4a96..b50afcc7 100644 --- a/swarms/structs/__init__.py +++ b/swarms/structs/__init__.py @@ -89,7 +89,12 @@ from swarms.structs.yaml_model import ( pydantic_type_to_yaml_schema, ) from swarms.structs.mixture_of_agents import MixtureOfAgents -from swarms.structs.graph_workflow import GraphWorkflow, Node, NodeType, Edge +from swarms.structs.graph_workflow import ( + GraphWorkflow, + Node, + NodeType, + Edge, +) __all__ = [ diff --git a/swarms/structs/base_workflow.py b/swarms/structs/base_workflow.py index f4244f2f..ccd41edf 100644 --- a/swarms/structs/base_workflow.py +++ b/swarms/structs/base_workflow.py @@ -38,6 +38,7 @@ class BaseWorkflow(BaseStructure): add_objective_to_workflow: Adds an objective to the workflow. load_workflow_state: Loads the workflow state from a json file and restores the workflow state. """ + def __init__( self, agents: List[Agent] = None,