clean up for multi agent collab

Former-commit-id: 85a3311dba
group-chat
Kye 2 years ago
parent 0b831e077b
commit c9fb8aeb0a

@ -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

@ -4,4 +4,15 @@ logo = """
\___ \ \ / / __ \| | \/ Y Y \\___ \
/____ > \/\_/ (____ /__| |__|_| /____ >
\/ \/ \/ \/
"""
"""
logo2 = """
_________ __ __ _____ __________ _____ _________
/ _____// \ / \ / _ \ \______ \ / \ / _____/
\_____ \ \ \/\/ // /_\ \ | _/ / \ / \ \_____ \
/ \ \ // | \| | \/ Y \ / \
/_______ / \__/\ / \____|__ /|____|_ /\____|__ //_______ /
\/ \/ \/ \/ \/ \/
"""
print(logo2)

@ -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
#from swarms.models.openai import OpenAIChat
#prompts
from swarms.models.prompts.debate import *

@ -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.
"""

@ -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

@ -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: <int>"
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
Loading…
Cancel
Save