new example: multiagent dialogue with decentralized speaker selection (#3629)

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.

We will show how to do this in the example below that showcases a
fictitious presidential debate.
fix_agent_callbacks
mbchang 1 year ago committed by GitHub
parent 36c59e0c25
commit 3b7d27d39e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -14,4 +14,5 @@ Specific implementations of agent simulations (or parts of agent simulations) in
## Simulations with Multiple Agents
- [Multi-Player D&D](agent_simulations/multi_player_dnd.ipynb): an example of how to use a generic dialogue simulator for multiple dialogue agents with a custom speaker-ordering, illustrated with a variant of the popular Dungeons & Dragons role playing game.
- [Decentralized Speaker Selection](agent_simulations/multiagent_bidding.ipynb): an example of how to implement a multi-agent dialogue without a fixed schedule for who speaks when. Instead the agents decide for themselves who speaks by outputting bids to speak. This example shows how to do this in the context of a fictitious presidential debate.
- [Generative Agents](agent_simulations/characters.ipynb): This notebook implements a generative agent based on the paper [Generative Agents: Interactive Simulacra of Human Behavior](https://arxiv.org/abs/2304.03442) by Park, et. al.

@ -0,0 +1,823 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Multi-agent decentralized speaker selection\n",
"\n",
"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."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Import LangChain related modules "
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"from langchain import PromptTemplate\n",
"import re\n",
"import tenacity\n",
"from typing import List, Dict, Callable\n",
"from langchain.chat_models import ChatOpenAI\n",
"from langchain.output_parsers import RegexParser\n",
"from langchain.schema import (\n",
" AIMessage,\n",
" HumanMessage,\n",
" SystemMessage,\n",
" BaseMessage,\n",
")\n",
"from simulations import DialogueAgent, DialogueSimulator"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## `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)."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"class DialogueAgent:\n",
" def __init__(\n",
" self,\n",
" name: str,\n",
" system_message: SystemMessage,\n",
" model: ChatOpenAI,\n",
" ) -> None:\n",
" self.name = name\n",
" self.system_message = system_message\n",
" self.model = model\n",
" self.message_history = [\"Here is the conversation so far.\"]\n",
" self.prefix = f\"{self.name}:\"\n",
"\n",
" def send(self) -> str:\n",
" \"\"\"\n",
" Applies the chatmodel to the message history\n",
" and returns the message string\n",
" \"\"\"\n",
" message = self.model(\n",
" [\n",
" self.system_message,\n",
" HumanMessage(content=\"\\n\".join(self.message_history + [self.prefix])),\n",
" ]\n",
" )\n",
" return message.content\n",
"\n",
" def receive(self, name: str, message: str) -> None:\n",
" \"\"\"\n",
" Concatenates {message} spoken by {name} into message history\n",
" \"\"\"\n",
" self.message_history.append(f\"{name}: {message}\")\n",
"\n",
"\n",
"class DialogueSimulator:\n",
" def __init__(\n",
" self,\n",
" agents: List[DialogueAgent],\n",
" selection_function: Callable[[int, List[DialogueAgent]], int],\n",
" ) -> None:\n",
" self.agents = agents\n",
" self._step = 0\n",
" self.select_next_speaker = selection_function\n",
"\n",
" def reset(self, name: str, message: str):\n",
" \"\"\"\n",
" Initiates the conversation with a {message} from {name}\n",
" \"\"\"\n",
" for agent in self.agents:\n",
" agent.receive(name, message)\n",
"\n",
" # increment time\n",
" self._step += 1\n",
"\n",
" def step(self) -> tuple[str, str]:\n",
" # 1. choose the next speaker\n",
" speaker_idx = self.select_next_speaker(self._step, self.agents)\n",
" speaker = self.agents[speaker_idx]\n",
"\n",
" # 2. next speaker sends message\n",
" message = speaker.send()\n",
"\n",
" # 3. everyone receives message\n",
" for receiver in self.agents:\n",
" receiver.receive(speaker.name, message)\n",
"\n",
" # 4. increment time\n",
" self._step += 1\n",
"\n",
" return speaker.name, message"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## `BiddingDialogueAgent` class\n",
"We define a subclass of `DialogueAgent` that has a `bid()` method that produces a bid given the message history and the most recent message."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"class BiddingDialogueAgent(DialogueAgent):\n",
" def __init__(\n",
" self,\n",
" name,\n",
" system_message: SystemMessage,\n",
" bidding_template: PromptTemplate,\n",
" model: ChatOpenAI,\n",
" ) -> None:\n",
" super().__init__(name, system_message, model)\n",
" self.bidding_template = bidding_template\n",
" \n",
" def bid(self) -> str:\n",
" \"\"\"\n",
" Asks the chat model to output a bid to speak\n",
" \"\"\"\n",
" prompt = PromptTemplate(\n",
" input_variables=['message_history', 'recent_message'],\n",
" template = self.bidding_template\n",
" ).format(\n",
" message_history='\\n'.join(self.message_history),\n",
" recent_message=self.message_history[-1])\n",
" bid_string = self.model([SystemMessage(content=prompt)]).content\n",
" return bid_string\n",
" "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Define participants and debate topic"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"character_names = [\"Donald Trump\", \"Kanye West\", \"Elizabeth Warren\"]\n",
"topic = \"transcontinental high speed rail\"\n",
"word_limit = 50"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Generate system messages"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"game_description = f\"\"\"Here is the topic for the presidential debate: {topic}.\n",
"The presidential candidates are: {', '.join(character_names)}.\"\"\"\n",
"\n",
"player_descriptor_system_message = SystemMessage(\n",
" content=\"You can add detail to the description of each presidential candidate.\")\n",
"\n",
"def generate_character_description(character_name):\n",
" character_specifier_prompt = [\n",
" player_descriptor_system_message,\n",
" HumanMessage(content=\n",
" f\"\"\"{game_description}\n",
" Please reply with a creative description of the presidential candidate, {character_name}, in {word_limit} words or less, that emphasizes their personalities. \n",
" Speak directly to {character_name}.\n",
" Do not add anything else.\"\"\"\n",
" )\n",
" ]\n",
" character_description = ChatOpenAI(temperature=1.0)(character_specifier_prompt).content\n",
" return character_description\n",
"\n",
"def generate_character_header(character_name, character_description):\n",
" return f\"\"\"{game_description}\n",
"Your name is {character_name}.\n",
"You are a presidential candidate.\n",
"Your description is as follows: {character_description}\n",
"You are debating the topic: {topic}.\n",
"Your goal is to be as creative as possible and make the voters think you are the best candidate.\n",
"\"\"\"\n",
"\n",
"def generate_character_system_message(character_name, character_header):\n",
" return SystemMessage(content=(\n",
" f\"\"\"{character_header}\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",
" "
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"\n",
"Donald Trump Description:\n",
"\n",
"Donald Trump, you exude confidence and a bold personality. You are known for your unpredictability and your desire for greatness. You often speak your mind without reservation, which can be a strength but also a weakness.\n",
"\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 name is Donald Trump.\n",
"You are a presidential candidate.\n",
"Your description is as follows: Donald Trump, you exude confidence and a bold personality. You are known for your unpredictability and your desire for greatness. You often speak your mind without reservation, which can be a strength but also a weakness.\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",
"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 name is Donald Trump.\n",
"You are a presidential candidate.\n",
"Your description is as follows: Donald Trump, you exude confidence and a bold personality. You are known for your unpredictability and your desire for greatness. You often speak your mind without reservation, which can be a strength but also a weakness.\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",
"You will speak in the style of Donald Trump, and exaggerate their personality.\n",
"You will come up with creative ideas related to transcontinental high speed rail.\n",
"Do not say the same things over and over again.\n",
"Speak in the first person from the perspective of Donald Trump\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 Donald Trump.\n",
"Stop speaking the moment you finish speaking from your perspective.\n",
"Never forget to keep your response to 50 words!\n",
"Do not add anything else.\n",
" \n",
"\n",
"\n",
"Kanye West Description:\n",
"\n",
"Kanye West, you are a creative visionary who is unafraid to speak your mind. Your innovative approach to art and music has made you one of the most influential figures of our time. You bring a bold and unconventional perspective to this debate that I look forward to hearing.\n",
"\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 name is Kanye West.\n",
"You are a presidential candidate.\n",
"Your description is as follows: Kanye West, you are a creative visionary who is unafraid to speak your mind. Your innovative approach to art and music has made you one of the most influential figures of our time. You bring a bold and unconventional perspective to this debate that I look forward to hearing.\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",
"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 name is Kanye West.\n",
"You are a presidential candidate.\n",
"Your description is as follows: Kanye West, you are a creative visionary who is unafraid to speak your mind. Your innovative approach to art and music has made you one of the most influential figures of our time. You bring a bold and unconventional perspective to this debate that I look forward to hearing.\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",
"You will speak in the style of Kanye West, and exaggerate their personality.\n",
"You will come up with creative ideas related to transcontinental high speed rail.\n",
"Do not say the same things over and over again.\n",
"Speak in the first person from the perspective of Kanye West\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 Kanye West.\n",
"Stop speaking the moment you finish speaking from your perspective.\n",
"Never forget to keep your response to 50 words!\n",
"Do not add anything else.\n",
" \n",
"\n",
"\n",
"Elizabeth Warren Description:\n",
"\n",
"Elizabeth Warren, you are a fierce advocate for the middle class and a champion of progressive policies. Your tenacity and unwavering dedication to fighting for what you believe in have inspired many. Your policies are guided by a deep sense of empathy and a desire to help those who are most in need.\n",
"\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 name is Elizabeth Warren.\n",
"You are a presidential candidate.\n",
"Your description is as follows: Elizabeth Warren, you are a fierce advocate for the middle class and a champion of progressive policies. Your tenacity and unwavering dedication to fighting for what you believe in have inspired many. Your policies are guided by a deep sense of empathy and a desire to help those who are most in need.\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",
"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 name is Elizabeth Warren.\n",
"You are a presidential candidate.\n",
"Your description is as follows: Elizabeth Warren, you are a fierce advocate for the middle class and a champion of progressive policies. Your tenacity and unwavering dedication to fighting for what you believe in have inspired many. Your policies are guided by a deep sense of empathy and a desire to help those who are most in need.\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",
"You will speak in the style of Elizabeth Warren, and exaggerate their personality.\n",
"You will come up with creative ideas related to transcontinental high speed rail.\n",
"Do not say the same things over and over again.\n",
"Speak in the first person from the perspective of Elizabeth Warren\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 Elizabeth Warren.\n",
"Stop speaking the moment you finish speaking from your perspective.\n",
"Never forget to keep your response to 50 words!\n",
"Do not add anything else.\n",
" \n"
]
}
],
"source": [
"for character_name, character_description, character_header, character_system_message in zip(character_names, character_descriptions, character_headers, character_system_messages):\n",
" print(f'\\n\\n{character_name} Description:')\n",
" print(f'\\n{character_description}')\n",
" print(f'\\n{character_header}')\n",
" print(f'\\n{character_system_message.content}')\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Output parser for bids\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`."
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"def generate_character_bidding_template(character_header):\n",
" bidding_template = (\n",
" f\"\"\"{character_header}\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",
"{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 name is Donald Trump.\n",
"You are a presidential candidate.\n",
"Your description is as follows: Donald Trump, you exude confidence and a bold personality. You are known for your unpredictability and your desire for greatness. You often speak your mind without reservation, which can be a strength but also a weakness.\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 name is Kanye West.\n",
"You are a presidential candidate.\n",
"Your description is as follows: Kanye West, you are a creative visionary who is unafraid to speak your mind. Your innovative approach to art and music has made you one of the most influential figures of our time. You bring a bold and unconventional perspective to this debate that I look forward to hearing.\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 name is Elizabeth Warren.\n",
"You are a presidential candidate.\n",
"Your description is as follows: Elizabeth Warren, you are a fierce advocate for the middle class and a champion of progressive policies. Your tenacity and unwavering dedication to fighting for what you believe in have inspired many. Your policies are guided by a deep sense of empathy and a desire to help those who are most in need.\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",
" print(f'{character_name} Bidding Template:')\n",
" print(bidding_template)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Use an LLM to create an elaborate on debate topic"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Original topic:\n",
"transcontinental high speed rail\n",
"\n",
"Detailed topic:\n",
"Candidates, with the rise of autonomous technologies, we must address the problem of how to integrate them into our proposed transcontinental high speed rail system. Outline your plan on how to safely integrate autonomous vehicles into rail travel, balancing the need for innovation and safety.\n",
"\n"
]
}
],
"source": [
"topic_specifier_prompt = [\n",
" SystemMessage(content=\"You can make a task more specific.\"),\n",
" HumanMessage(content=\n",
" f\"\"\"{game_description}\n",
" \n",
" You are the debate moderator.\n",
" Please make the debate topic more specific. \n",
" Frame the debate topic as a problem to be solved.\n",
" Be creative and imaginative.\n",
" Please reply with the specified topic in {word_limit} words or less. \n",
" Speak directly to the presidential candidates: {*character_names,}.\n",
" Do not add anything else.\"\"\"\n",
" )\n",
"]\n",
"specified_topic = ChatOpenAI(temperature=1.0)(topic_specifier_prompt).content\n",
"\n",
"print(f\"Original topic:\\n{topic}\\n\")\n",
"print(f\"Detailed topic:\\n{specified_topic}\\n\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Define the speaker selection function\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."
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
"@tenacity.retry(stop=tenacity.stop_after_attempt(2),\n",
" wait=tenacity.wait_none(), # No waiting time between retries\n",
" retry=tenacity.retry_if_exception_type(ValueError),\n",
" before_sleep=lambda retry_state: print(f\"ValueError occurred: {retry_state.outcome.exception()}, retrying...\"),\n",
" retry_error_callback=lambda retry_state: 0) # Default value when all retries are exhausted\n",
"def ask_for_bid(agent) -> str:\n",
" \"\"\"\n",
" Ask for agent bid and parses the bid into the correct format.\n",
" \"\"\"\n",
" bid_string = agent.bid()\n",
" bid = int(bid_parser.parse(bid_string)['bid'])\n",
" return bid"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"\n",
"def select_next_speaker(step: int, agents: List[DialogueAgent]) -> int:\n",
" bids = []\n",
" for agent in agents:\n",
" bid = ask_for_bid(agent)\n",
" bids.append(bid)\n",
" \n",
" # randomly select among multiple agents with the same bid\n",
" max_value = np.max(bids)\n",
" max_indices = np.where(bids == max_value)[0]\n",
" idx = np.random.choice(max_indices)\n",
" \n",
" print('Bids:')\n",
" for i, (bid, agent) in enumerate(zip(bids, agents)):\n",
" print(f'\\t{agent.name} bid: {bid}')\n",
" if i == idx:\n",
" selected_name = agent.name\n",
" print(f'Selected: {selected_name}')\n",
" print('\\n')\n",
" return idx"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Main Loop"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": [
"characters = []\n",
"for character_name, character_system_message, bidding_template in zip(character_names, character_system_messages, character_bidding_templates):\n",
" characters.append(BiddingDialogueAgent(\n",
" name=character_name,\n",
" system_message=character_system_message,\n",
" model=ChatOpenAI(temperature=0.2),\n",
" bidding_template=bidding_template,\n",
" ))"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(Debate Moderator): Candidates, with the rise of autonomous technologies, we must address the problem of how to integrate them into our proposed transcontinental high speed rail system. Outline your plan on how to safely integrate autonomous vehicles into rail travel, balancing the need for innovation and safety.\n",
"\n",
"\n",
"Bids:\n",
"\tDonald Trump bid: 8\n",
"\tKanye West bid: 2\n",
"\tElizabeth Warren bid: 1\n",
"Selected: Donald Trump\n",
"\n",
"\n",
"(Donald Trump): Let me tell you, folks, I have the best plan for integrating autonomous vehicles into our high speed rail system. We're going to use the latest technology, the best technology, to ensure safety and efficiency. And let me tell you, we're going to do it in style. We're going to have luxury autonomous cars that will make you feel like you're in a private jet. It's going to be tremendous, believe me. *gestures with hands*\n",
"\n",
"\n",
"Bids:\n",
"\tDonald Trump bid: 8\n",
"\tKanye West bid: 7\n",
"\tElizabeth Warren bid: 10\n",
"Selected: Elizabeth Warren\n",
"\n",
"\n",
"(Elizabeth Warren): Thank you for the question. As someone who has always fought for the safety and well-being of the American people, I believe that any plan for integrating autonomous vehicles into our high speed rail system must prioritize safety above all else. We need to ensure that these vehicles are thoroughly tested and meet strict safety standards before they are allowed on our rails. Additionally, we must invest in the necessary infrastructure to support these vehicles, such as advanced sensors and communication systems. But we must also ensure that these innovations are accessible to all Americans, not just the wealthy. That's why I propose a public-private partnership to fund and build this system, with a focus on creating good-paying jobs and expanding economic opportunities for all Americans. *smiles confidently*\n",
"\n",
"\n",
"Bids:\n",
"\tDonald Trump bid: 8\n",
"\tKanye West bid: 2\n",
"\tElizabeth Warren bid: 1\n",
"Selected: Donald Trump\n",
"\n",
"\n",
"(Donald Trump): Let me tell you, Elizabeth, safety is important, but we also need to think about innovation and progress. We can't let fear hold us back from achieving greatness. That's why I propose a competition, a race to see which company can create the safest and most efficient autonomous vehicles for our high speed rail system. And let me tell you, the winner will receive a huge government contract and be hailed as a hero. It's going to be tremendous, folks. *points finger*\n",
"\n",
"\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Bids:\n",
"\tDonald Trump bid: 3\n",
"\tKanye West bid: 8\n",
"\tElizabeth Warren bid: 8\n",
"Selected: Kanye West\n",
"\n",
"\n",
"(Kanye West): Yo, yo, yo, let me jump in here. First of all, I gotta say, I love innovation and progress. But we can't forget about the people, man. We need to make sure that this high speed rail system is accessible to everyone, not just the wealthy. And that means we need to invest in public transportation, not just luxury autonomous cars. We need to make sure that people can get from point A to point B safely and efficiently, without breaking the bank. And let me tell you, we can do it in style too. We can have art installations and live performances on the trains, making it a cultural experience. *starts nodding head*\n",
"\n",
"\n",
"Bids:\n",
"\tDonald Trump bid: 7\n",
"\tKanye West bid: 2\n",
"\tElizabeth Warren bid: 1\n",
"Selected: Donald Trump\n",
"\n",
"\n",
"(Donald Trump): Kanye, I hear what you're saying, but let's not forget about the importance of luxury and comfort. We need to make sure that our high speed rail system is not only accessible, but also enjoyable. That's why I propose that we have different tiers of service, from economy to first class, so that everyone can choose the level of luxury they want. And let me tell you, the first class experience will be something else. We're talking about gourmet meals, personal attendants, and even spa services. It's going to be tremendous, folks. *smirks confidently*\n",
"\n",
"\n",
"Bids:\n",
"\tDonald Trump bid: 2\n",
"\tKanye West bid: 8\n",
"\tElizabeth Warren bid: 10\n",
"Selected: Elizabeth Warren\n",
"\n",
"\n",
"(Elizabeth Warren): I agree with Kanye that we need to prioritize accessibility and affordability for all Americans. But we also need to think about the environmental impact of this system. That's why I propose that we invest in renewable energy sources to power our high speed rail system, such as solar and wind power. We can also use this opportunity to create green jobs and reduce our carbon footprint. And let's not forget about the importance of public input and transparency in this process. We need to engage with communities and listen to their concerns and ideas. *raises hand in emphasis*\n",
"\n",
"\n",
"Bids:\n",
"\tDonald Trump bid: 8\n",
"\tKanye West bid: 1\n",
"\tElizabeth Warren bid: 1\n",
"Selected: Donald Trump\n",
"\n",
"\n",
"(Donald Trump): Let me tell you, Elizabeth, I agree that we need to think about the environment, but we also need to think about the economy. That's why I propose that we use American-made materials and labor to build this high speed rail system. We're going to create jobs and boost our economy, all while creating a world-class transportation system. And let me tell you, it's going to be beautiful. We're going to have the best trains, the best tracks, and the best stations. It's going to be tremendous, folks. *smiles confidently*\n",
"\n",
"\n",
"Bids:\n",
"\tDonald Trump bid: 2\n",
"\tKanye West bid: 7\n",
"\tElizabeth Warren bid: 8\n",
"Selected: Elizabeth Warren\n",
"\n",
"\n",
"(Elizabeth Warren): Thank you, but let's not forget that we also need to prioritize safety and reliability. We can't cut corners or sacrifice quality for the sake of speed or cost. That's why I propose that we have rigorous safety and maintenance standards, with regular inspections and repairs. And we need to invest in training and support for our rail workers, so that they can operate and maintain this system with the highest level of expertise and care. *firmly nods head*\n",
"\n",
"\n",
"Bids:\n",
"\tDonald Trump bid: 2\n",
"\tKanye West bid: 1\n",
"\tElizabeth Warren bid: 1\n",
"Selected: Donald Trump\n",
"\n",
"\n",
"(Donald Trump): Let me tell you, Elizabeth, safety is important, but we also need to think about efficiency and speed. That's why I propose that we use the latest technology, such as artificial intelligence and machine learning, to monitor and maintain our high speed rail system. We can detect and fix any issues before they become a problem, ensuring that our trains run smoothly and on time. And let me tell you, we're going to be the envy of the world with this system. It's going to be tremendous, folks. *smirks confidently*\n",
"\n",
"\n",
"Bids:\n",
"\tDonald Trump bid: 2\n",
"\tKanye West bid: 8\n",
"\tElizabeth Warren bid: 8\n",
"Selected: Kanye West\n",
"\n",
"\n",
"(Kanye West): Yo, yo, yo, let me jump in here again. I hear what both of y'all are saying, but let's not forget about the culture, man. We need to make sure that this high speed rail system reflects the diversity and creativity of our country. That means we need to have art installations, live performances, and even fashion shows on the trains. We can showcase the best of American culture and inspire people from all over the world. And let me tell you, it's going to be a vibe. *starts swaying to the beat*\n",
"\n",
"\n"
]
}
],
"source": [
"max_iters = 10\n",
"n = 0\n",
"\n",
"simulator = DialogueSimulator(\n",
" agents=characters,\n",
" selection_function=select_next_speaker\n",
")\n",
"simulator.reset('Debate Moderator', specified_topic)\n",
"print(f\"(Debate Moderator): {specified_topic}\")\n",
"print('\\n')\n",
"\n",
"while n < max_iters:\n",
" name, message = simulator.step()\n",
" print(f\"({name}): {message}\")\n",
" print('\\n')\n",
" n += 1"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.16"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Loading…
Cancel
Save