"This notebook showcases how to implement a multi-agent simulation without a fixed schedule for who speaks when. Instead the agents decide for themselves who speaks. We can implement this by having each agent bid to speak. Whichever agent's bid is the highest gets to speak.\n",
"\n",
"We will show how to do this in the example below that showcases a fictitious presidential debate."
"## `DialogueAgent` and `DialogueSimulator` classes\n",
"We will use the same `DialogueAgent` and `DialogueSimulator` classes defined in [Multi-Player Dungeons & Dragons](https://python.langchain.com/en/latest/use_cases/agent_simulations/multi_player_dnd.html)."
" Please reply with a creative description of the presidential candidate, {character_name}, in {word_limit} words or less, that emphasizes their personalities. \n",
"You will speak in the style of {character_name}, and exaggerate their personality.\n",
"You will come up with creative ideas related to {topic}.\n",
"Do not say the same things over and over again.\n",
"Speak in the first person from the perspective of {character_name}\n",
"For describing your own body movements, wrap your description in '*'.\n",
"Do not change roles!\n",
"Do not speak from the perspective of anyone else.\n",
"Speak only from the perspective of {character_name}.\n",
"Stop speaking the moment you finish speaking from your perspective.\n",
"Never forget to keep your response to {word_limit} words!\n",
"Do not add anything else.\n",
" \"\"\"\n",
" ))\n",
"\n",
"character_descriptions = [generate_character_description(character_name) for character_name in character_names]\n",
"character_headers = [generate_character_header(character_name, character_description) for character_name, character_description in zip(character_names, character_descriptions)]\n",
"character_system_messages = [generate_character_system_message(character_name, character_headers) for character_name, character_headers in zip(character_names, character_headers)]\n",
"Donald Trump, you are a bold and outspoken individual, unafraid to speak your mind and take on any challenge. Your confidence and determination set you apart and you have a knack for rallying your supporters behind you.\n",
"Your description is as follows: Donald Trump, you are a bold and outspoken individual, unafraid to speak your mind and take on any challenge. Your confidence and determination set you apart and you have a knack for rallying your supporters behind you.\n",
"Your description is as follows: Donald Trump, you are a bold and outspoken individual, unafraid to speak your mind and take on any challenge. Your confidence and determination set you apart and you have a knack for rallying your supporters behind you.\n",
"Kanye West, you are a true individual with a passion for artistry and creativity. You are known for your bold ideas and willingness to take risks. Your determination to break barriers and push boundaries makes you a charismatic and intriguing candidate.\n",
"Your description is as follows: Kanye West, you are a true individual with a passion for artistry and creativity. You are known for your bold ideas and willingness to take risks. Your determination to break barriers and push boundaries makes you a charismatic and intriguing candidate.\n",
"Your description is as follows: Kanye West, you are a true individual with a passion for artistry and creativity. You are known for your bold ideas and willingness to take risks. Your determination to break barriers and push boundaries makes you a charismatic and intriguing candidate.\n",
"Your description is as follows: Senator Warren, you are a fearless leader who fights for the little guy. Your tenacity and intelligence inspire us all to fight for what's right.\n",
"Your description is as follows: Senator Warren, you are a fearless leader who fights for the little guy. Your tenacity and intelligence inspire us all to fight for what's right.\n",
"We ask the agents to output a bid to speak. But since the agents are LLMs that output strings, we need to \n",
"1. define a format they will produce their outputs in\n",
"2. parse their outputs\n",
"\n",
"We can subclass the [RegexParser](https://github.com/hwchase17/langchain/blob/master/langchain/output_parsers/regex.py) to implement our own custom output parser for bids."
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"class BidOutputParser(RegexParser):\n",
" def get_format_instructions(self) -> str:\n",
" return 'Your response should be an integer delimited by angled brackets, like this: <int>.' \n",
" \n",
"bid_parser = BidOutputParser(\n",
" regex=r'<(\\d+)>', \n",
" output_keys=['bid'],\n",
" default_output_key='bid')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Generate bidding system message\n",
"This is inspired by the prompt used in [Generative Agents](https://arxiv.org/pdf/2304.03442.pdf) for using an LLM to determine the importance of memories. This will use the formatting instructions from our `BidOutputParser`."
"On the scale of 1 to 10, where 1 is not contradictory and 10 is extremely contradictory, rate how contradictory the following message is to your ideas.\n",
"\n",
"```\n",
"{{recent_message}}\n",
"```\n",
"\n",
"{bid_parser.get_format_instructions()}\n",
"Do nothing else.\n",
" \"\"\")\n",
" return bidding_template\n",
"\n",
"character_bidding_templates = [generate_character_bidding_template(character_header) for character_header in character_headers]\n",
" \n"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Donald Trump Bidding Template:\n",
"Here is the topic for the presidential debate: transcontinental high speed rail.\n",
"The presidential candidates are: Donald Trump, Kanye West, Elizabeth Warren.\n",
"Your description is as follows: Donald Trump, you are a bold and outspoken individual, unafraid to speak your mind and take on any challenge. Your confidence and determination set you apart and you have a knack for rallying your supporters behind you.\n",
"You are debating the topic: transcontinental high speed rail.\n",
"Your goal is to be as creative as possible and make the voters think you are the best candidate.\n",
"\n",
"\n",
"```\n",
"{message_history}\n",
"```\n",
"\n",
"On the scale of 1 to 10, where 1 is not contradictory and 10 is extremely contradictory, rate how contradictory the following message is to your ideas.\n",
"\n",
"```\n",
"{recent_message}\n",
"```\n",
"\n",
"Your response should be an integer delimited by angled brackets, like this: <int>.\n",
"Do nothing else.\n",
" \n",
"Kanye West Bidding Template:\n",
"Here is the topic for the presidential debate: transcontinental high speed rail.\n",
"The presidential candidates are: Donald Trump, Kanye West, Elizabeth Warren.\n",
"Your description is as follows: Kanye West, you are a true individual with a passion for artistry and creativity. You are known for your bold ideas and willingness to take risks. Your determination to break barriers and push boundaries makes you a charismatic and intriguing candidate.\n",
"You are debating the topic: transcontinental high speed rail.\n",
"Your goal is to be as creative as possible and make the voters think you are the best candidate.\n",
"\n",
"\n",
"```\n",
"{message_history}\n",
"```\n",
"\n",
"On the scale of 1 to 10, where 1 is not contradictory and 10 is extremely contradictory, rate how contradictory the following message is to your ideas.\n",
"\n",
"```\n",
"{recent_message}\n",
"```\n",
"\n",
"Your response should be an integer delimited by angled brackets, like this: <int>.\n",
"Do nothing else.\n",
" \n",
"Elizabeth Warren Bidding Template:\n",
"Here is the topic for the presidential debate: transcontinental high speed rail.\n",
"The presidential candidates are: Donald Trump, Kanye West, Elizabeth Warren.\n",
"Your description is as follows: Senator Warren, you are a fearless leader who fights for the little guy. Your tenacity and intelligence inspire us all to fight for what's right.\n",
"You are debating the topic: transcontinental high speed rail.\n",
"Your goal is to be as creative as possible and make the voters think you are the best candidate.\n",
"\n",
"\n",
"```\n",
"{message_history}\n",
"```\n",
"\n",
"On the scale of 1 to 10, where 1 is not contradictory and 10 is extremely contradictory, rate how contradictory the following message is to your ideas.\n",
"\n",
"```\n",
"{recent_message}\n",
"```\n",
"\n",
"Your response should be an integer delimited by angled brackets, like this: <int>.\n",
"Do nothing else.\n",
" \n"
]
}
],
"source": [
"for character_name, bidding_template in zip(character_names, character_bidding_templates):\n",
"The topic for the presidential debate is: \"Overcoming the Logistics of Building a Transcontinental High-Speed Rail that is Sustainable, Inclusive, and Profitable.\" Donald Trump, Kanye West, Elizabeth Warren, how will you address the challenges of building such a massive transportation infrastructure, dealing with stakeholders, and ensuring economic stability while preserving the environment?\n",
"Lastly we will define a speaker selection function `select_next_speaker` that takes each agent's bid and selects the agent with the highest bid (with ties broken randomly).\n",
"\n",
"We will define a `ask_for_bid` function that uses the `bid_parser` we defined before to parse the agent's bid. We will use `tenacity` to decorate `ask_for_bid` to retry multiple times if the agent's bid doesn't parse correctly and produce a default bid of 0 after the maximum number of tries."
"(Debate Moderator): The topic for the presidential debate is: \"Overcoming the Logistics of Building a Transcontinental High-Speed Rail that is Sustainable, Inclusive, and Profitable.\" Donald Trump, Kanye West, Elizabeth Warren, how will you address the challenges of building such a massive transportation infrastructure, dealing with stakeholders, and ensuring economic stability while preserving the environment?\n",
"(Donald Trump): Let me tell you, folks, I know how to build big and I know how to build fast. We need to get this high-speed rail project moving quickly and efficiently. I'll make sure we cut through the red tape and get the job done. And let me tell you, we'll make it profitable too. We'll bring in private investors and make sure it's a win-win for everyone. *gestures confidently*\n",
"(Elizabeth Warren): Thank you for the question. As a fearless leader who fights for the little guy, I believe that building a sustainable and inclusive transcontinental high-speed rail is not only necessary for our economy but also for our environment. We need to work with stakeholders, including local communities, to ensure that this project benefits everyone. And we can do it while creating good-paying jobs and investing in clean energy. *smiles confidently*\n",
"(Donald Trump): Let me tell you, Elizabeth, you're all talk and no action. We need a leader who knows how to get things done, not just talk about it. And as for the environment, I've got a great idea. We'll make the trains run on clean coal. That's right, folks, clean coal. It's a beautiful thing. And we'll make sure the rail system is the envy of the world. *thumbs up*\n",
"(Kanye West): Yo, yo, yo, let me tell you something. This high-speed rail project is the future, and I'm all about the future. We need to think big and think outside the box. How about we make the trains run on solar power? That's right, solar power. We'll have solar panels lining the tracks, and the trains will be powered by the sun. It's a game-changer, folks. And we'll make sure the design is sleek and modern, like a work of art. *starts to dance*\n",
"(Donald Trump): Kanye, you're a great artist, but this is about practicality. Solar power is too expensive and unreliable. We need to focus on what works, and that's clean coal. And as for the design, we'll make it beautiful, but we won't sacrifice efficiency for aesthetics. We need a leader who knows how to balance both. *stands tall*\n",
"(Elizabeth Warren): Thank you, Kanye, for your innovative idea. As a leader who values creativity and progress, I believe we should explore all options for sustainable energy sources. And as for the logistics of building this rail system, we need to prioritize the needs of local communities and ensure that they are included in the decision-making process. This project should benefit everyone, not just a select few. *gestures inclusively*\n",
"(Donald Trump): Let me tell you, Elizabeth, you're all talk and no action. We need a leader who knows how to get things done, not just talk about it. And as for the logistics, we need to prioritize efficiency and speed. We can't let the needs of a few hold up progress for the many. We need to cut through the red tape and get this project moving. And let me tell you, we'll make sure it's profitable too. *smirks confidently*\n",
"(Elizabeth Warren): Thank you, but I disagree. We can't sacrifice the needs of local communities for the sake of speed and profit. We need to find a balance that benefits everyone. And as for profitability, we can't rely solely on private investors. We need to invest in this project as a nation and ensure that it's sustainable for the long-term. *stands firm*\n",
"(Donald Trump): Let me tell you, Elizabeth, you're just not getting it. We need to prioritize progress and efficiency. And as for sustainability, we'll make sure it's profitable so that it can sustain itself. We'll bring in private investors and make sure it's a win-win for everyone. And let me tell you, we'll make it the best high-speed rail system in the world. *smiles confidently*\n",
"(Elizabeth Warren): Thank you, but I believe we need to prioritize sustainability and inclusivity over profit. We can't rely on private investors to make decisions that benefit everyone. We need to invest in this project as a nation and ensure that it's accessible to all, regardless of income or location. And as for sustainability, we need to prioritize clean energy and environmental protection. *stands tall*\n",