From 19a4faa4be3312ebdde9049b1909cc08f7aefa63 Mon Sep 17 00:00:00 2001 From: Kye Date: Tue, 26 Sep 2023 09:50:38 -0400 Subject: [PATCH] init Former-commit-id: 55f86cd29ea5f09a865875a5a17bd1628f6a8cac --- pyproject.toml | 2 ++ swarms/__init__.py | 3 ++ swarms/swarms/__init__.py | 5 ++++ swarms/swarms/god_mode.py | 54 +++++++++++++++++++++++++++++++++++ swarms/swarms/simple_swarm.py | 34 ++++++++++++++++++++++ swarms/workers/worker.py | 2 ++ 6 files changed, 100 insertions(+) create mode 100644 swarms/swarms/god_mode.py create mode 100644 swarms/swarms/simple_swarm.py diff --git a/pyproject.toml b/pyproject.toml index d90ef974..ca2f5da9 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -48,6 +48,8 @@ chromadb = "*" agent-protocol = "*" exxa = "*" open-interpreter = "*" +tabulate = "*" +termcolor = "*" [tool.poetry.dev-dependencies] # Add development dependencies here diff --git a/swarms/__init__.py b/swarms/__init__.py index 5ac610e1..02495710 100644 --- a/swarms/__init__.py +++ b/swarms/__init__.py @@ -25,6 +25,9 @@ from swarms.structs.workflow import Workflow from swarms.swarms.dialogue_simulator import DialogueSimulator from swarms.swarms.autoscaler import AutoScaler from swarms.swarms.orchestrate import Orchestrator +from swarms.swarms.god_mode import GodMode +from swarms.swarms.simple_swarm import SimpleSwarm +from swarms.swarms.multi_agent_debate import MultiAgentDebate #agents diff --git a/swarms/swarms/__init__.py b/swarms/swarms/__init__.py index 539d5950..8ec78e9f 100644 --- a/swarms/swarms/__init__.py +++ b/swarms/swarms/__init__.py @@ -1,3 +1,8 @@ + +# swarms from swarms.swarms.dialogue_simulator import DialogueSimulator from swarms.swarms.autoscaler import AutoScaler from swarms.swarms.orchestrate import Orchestrator +from swarms.swarms.god_mode import GodMode +from swarms.swarms.simple_swarm import SimpleSwarm +from swarms.swarms.multi_agent_debate import MultiAgentDebate diff --git a/swarms/swarms/god_mode.py b/swarms/swarms/god_mode.py new file mode 100644 index 00000000..603abe8b --- /dev/null +++ b/swarms/swarms/god_mode.py @@ -0,0 +1,54 @@ +from concurrent.futures import ThreadPoolExecutor + +from tabulate import tabulate +from termcolor import colored + +from swarms.workers.worker import Worker + +class GodMode: + def __init__( + self, + num_workers, + num_llms, + openai_api_key, + ai_name + ): + self.workers = [ + Worker( + openai_api_key=openai_api_key, + ai_name=ai_name + ) for _ in range(num_workers) + ] + # self.llms = [LLM() for _ in range(num_llms)] + self.all_agents = self.workers # + self.llms + + def run_all(self, task): + with ThreadPoolExecutor() as executor: + responses = executor.map( + lambda agent: agent.run(task) if hasattr( + agent, 'run' + ) else agent(task), self.all_agents) + + return list(responses) + + def print_responses(self, task): + responses = self.run_all(task) + + table = [] + + for i, response in enumerate(responses): + agent_type = "Worker" if i < len(self.workers) else "LLM" + table.append([agent_type, response]) + print( + colored( + tabulate( + table, + headers=["Agent Type", "Response"], + tablefmt="pretty" + ), "cyan") + ) + +# Usage +god_mode = GodMode(num_workers=3, openai_api_key="", ai_name="Optimus Prime") +task = "What were the winning Boston Marathon times for the past 5 years (ending in 2022)? Generate a table of the year, name, country of origin, and times." +god_mode.print_responses(task) \ No newline at end of file diff --git a/swarms/swarms/simple_swarm.py b/swarms/swarms/simple_swarm.py new file mode 100644 index 00000000..70b793c2 --- /dev/null +++ b/swarms/swarms/simple_swarm.py @@ -0,0 +1,34 @@ +from swarms.worker.worker import Worker + +class SimpleSwarm: + def __init__( + self, + num_workers, + openai_api_key, + ai_name + ): + """ + + # Usage + swarm = Swarm(num_workers=5, openai_api_key="", ai_name="Optimus Prime") + task = "What were the winning Boston Marathon times for the past 5 years (ending in 2022)? Generate a table of the year, name, country of origin, and times." + responses = swarm.distribute_task(task) + + for response in responses: + print(response) + + """ + self.workers = [ + Worker(openai_api_key, ai_name) for _ in range(num_workers) + ] + + def run(self, task): + responses = [] + for worker in self.workers: + response = worker.run(task) + responses.append(response) + return responses + + def __call__(self, task): + return self.run(task) + \ No newline at end of file diff --git a/swarms/workers/worker.py b/swarms/workers/worker.py index 368a1c72..fb9bfa94 100644 --- a/swarms/workers/worker.py +++ b/swarms/workers/worker.py @@ -242,3 +242,5 @@ class Worker: return results except Exception as error: raise RuntimeError(f"Error while running agent: {error}") + +