parent
0b831e077b
commit
c9fb8aeb0a
@ -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…
Reference in new issue