From c9fb8aeb0a7c1de87e49f6a84d87d2f33ac53d18 Mon Sep 17 00:00:00 2001 From: Kye Date: Sun, 24 Sep 2023 19:58:57 -0400 Subject: [PATCH] clean up for multi agent collab Former-commit-id: 85a3311dbaea6960b7c8680cc7d7607e25208f39 --- swarms/__init__.py | 9 +- swarms/logo.py | 13 ++- swarms/models/__init__.py | 5 +- swarms/models/prompts/debate.py | 47 ++++++++++ .../swarms/dialogue_authoritarian_speaker.py | 12 +++ swarms/swarms/multi_agent_collab.py | 89 +++++++++++++++++++ 6 files changed, 169 insertions(+), 6 deletions(-) create mode 100644 swarms/models/prompts/debate.py create mode 100644 swarms/swarms/dialogue_authoritarian_speaker.py create mode 100644 swarms/swarms/multi_agent_collab.py diff --git a/swarms/__init__.py b/swarms/__init__.py index 3999d701..7935ca34 100644 --- a/swarms/__init__.py +++ b/swarms/__init__.py @@ -1,13 +1,13 @@ #swarms -from swarms.logo import logo -print(logo) +from swarms.logo import logo2 +print(logo2) #from swarms.orchestrator.autoscaler import AutoScaler # worker # from swarms.workers.worker_node import WorkerNode #boss -from swarms.boss.boss_node import Boss +# from swarms.boss.boss_node import Boss #models from swarms.models.anthropic import Anthropic @@ -23,5 +23,6 @@ from swarms.workers.worker import Worker from swarms.structs.workflow import Workflow # swarms +from swarms.swarms.dialogue_simulator import DialogueSimulator +from swarms.swarms.autoscaler import AutoScaler from swarms.swarms.orchestrate import Orchestrator -from swarms.swarms.autoscaler import AutoScaler \ No newline at end of file diff --git a/swarms/logo.py b/swarms/logo.py index f60bcd9b..c058c46e 100644 --- a/swarms/logo.py +++ b/swarms/logo.py @@ -4,4 +4,15 @@ logo = """ \___ \ \ / / __ \| | \/ Y Y \\___ \ /____ > \/\_/ (____ /__| |__|_| /____ > \/ \/ \/ \/ -""" \ No newline at end of file +""" + +logo2 = """ + _________ __ __ _____ __________ _____ _________ + / _____// \ / \ / _ \ \______ \ / \ / _____/ + \_____ \ \ \/\/ // /_\ \ | _/ / \ / \ \_____ \ + / \ \ // | \| | \/ Y \ / \ +/_______ / \__/\ / \____|__ /|____|_ /\____|__ //_______ / + \/ \/ \/ \/ \/ \/ + +""" +print(logo2) \ No newline at end of file diff --git a/swarms/models/__init__.py b/swarms/models/__init__.py index e00b9555..3c423801 100644 --- a/swarms/models/__init__.py +++ b/swarms/models/__init__.py @@ -1,4 +1,7 @@ from swarms.models.anthropic import Anthropic # from swarms.models.palm import GooglePalm from swarms.models.petals import Petals -#from swarms.models.openai import OpenAIChat \ No newline at end of file +#from swarms.models.openai import OpenAIChat + +#prompts +from swarms.models.prompts.debate import * diff --git a/swarms/models/prompts/debate.py b/swarms/models/prompts/debate.py new file mode 100644 index 00000000..a1c6b224 --- /dev/null +++ b/swarms/models/prompts/debate.py @@ -0,0 +1,47 @@ +def presidential_debate(character_names, topic): + game_description = f"""Here is the topic for the presidential debate: {topic}. + The presidential candidates are: {', '.join(character_names)}.""" + + return game_description + + +def character(character_name, topic, word_limit): + prompt = f""" + You will speak in the style of {character_name}, and exaggerate their personality. + You will come up with creative ideas related to {topic}. + Do not say the same things over and over again. + Speak in the first person from the perspective of {character_name} + For describing your own body movements, wrap your description in '*'. + Do not change roles! + Do not speak from the perspective of anyone else. + Speak only from the perspective of {character_name}. + Stop speaking the moment you finish speaking from your perspective. + Never forget to keep your response to {word_limit} words! + Do not add anything else. + """ + return prompt + +def debate_monitor(game_description, word_limit, character_names): + prompt = f""" + + {game_description} + You are the debate moderator. + Please make the debate topic more specific. + Frame the debate topic as a problem to be solved. + Be creative and imaginative. + Please reply with the specified topic in {word_limit} words or less. + Speak directly to the presidential candidates: {*character_names,}. + Do not add anything else. + """ + + return prompt + + +def generate_character_header(game_description, topic, character_name, character_description): + prompt = f"""{game_description} + Your name is {character_name}. + You are a presidential candidate. + Your description is as follows: {character_description} + You are debating the topic: {topic}. + Your goal is to be as creative as possible and make the voters think you are the best candidate. + """ diff --git a/swarms/swarms/dialogue_authoritarian_speaker.py b/swarms/swarms/dialogue_authoritarian_speaker.py new file mode 100644 index 00000000..2009efd8 --- /dev/null +++ b/swarms/swarms/dialogue_authoritarian_speaker.py @@ -0,0 +1,12 @@ +def select_next_speaker( + step: int, + agents, + director +) -> int: + #if the step if even => director + #=> director selects next speaker + if step % 2 == 1: + idx = 0 + else: + idx = director.select_next_speaker() + 1 + return idx diff --git a/swarms/swarms/multi_agent_collab.py b/swarms/swarms/multi_agent_collab.py new file mode 100644 index 00000000..155f2d29 --- /dev/null +++ b/swarms/swarms/multi_agent_collab.py @@ -0,0 +1,89 @@ +import random +import tenacity +from langchain.output_parsers import RegexParser + +#utils +class BidOutputParser(RegexParser): + def get_format_instructions(self) -> str: + return "Your response should be an integrater delimited by angled brackets like this: " + +bid_parser = BidOutputParser( + regex=r"<(\d+)>", output_keys=["bid"], default_output_key="bid" +) + +# specified_topic = ChatOpenAI(temperature=1.0)(topic_specifier_prompt).content + +#main +class MultiAgentCollaboration: + def __init__( + self, + agents, + selection_function, + ): + self.agents = agents + self._step = 0 + self.select_next_speaker = selection_function + + def reset(self): + for agent in self.agents: + agent.reset() + + def inject(self, name: str, message: str): + for agent in self.agents: + agent.run(f"Name {name} and message: {message}") + self._step += 1 + + def step(self) -> tuple[str, str]: + speaker_idx = self.select_next_speaker( + self._step, + self.agents + ) + speaker = self.agents[speaker_idx] + message = speaker.send() + message = speaker.send() + + for receiver in self.agents: + receiver.receive(speaker.name, message) + self._step += 1 + return speaker.name, message + + @tenacity.retry( + stop=tenacity.stop_after_attempt(10), + wait=tenacity.wait_none(), + retry=tenacity.retry_if_exception_type(ValueError), + before_sleep= lambda retry_state: print( + f"ValueError occured: {retry_state.outcome.exception()}, retying..." + ), + retry_error_callback=lambda retry_state: 0, + ) + def ask_for_bid(self, agent) -> str: + bid_string = agent.bid() + bid = int(bid_parser.parse(bid_string)["bid"]) + return bid + + def select_next_speaker( + self, + step: int, + agents, + ) -> int: + bids = [] + for agent in agents: + bid = self.ask_for_bid(agent) + bids.append(bid) + max_value = max(bids) + max_indices = [i for i, x in enumerate(bids) if x == max_value] + idx = random.choice(max_indices) + return idx + + def run(self, max_iters: int = 10): + n = 0 + self.reset() + self.inject("Debate Moderator", specified_topic) + print(f"(Debate Moderator): {specified_topic}") + print("\n") + + while n < max_iters: + name, message = self.step() + print(f"({name}): {message}") + print("\n") + n += 1