Spaces:
Sleeping
Sleeping
File size: 45,783 Bytes
cfd3735 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 |
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Multi-agent authoritarian speaker selection\n",
"\n",
"This notebook showcases how to implement a multi-agent simulation where a privileged agent decides who to speak.\n",
"This follows the polar opposite selection scheme as [multi-agent decentralized speaker selection](https://python.langchain.com/en/latest/use_cases/agent_simulations/multiagent_bidding.html).\n",
"\n",
"We show an example of this approach in the context of a fictitious simulation of a news network. This example will showcase how we can implement agents that\n",
"- think before speaking\n",
"- terminate the conversation"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Import LangChain related modules "
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"from collections import OrderedDict\n",
"import functools\n",
"import random\n",
"import re\n",
"import tenacity\n",
"from typing import List, Dict, Callable\n",
"\n",
"from langchain.prompts import (\n",
" ChatPromptTemplate, \n",
" HumanMessagePromptTemplate,\n",
" PromptTemplate\n",
")\n",
"from langchain.chains import LLMChain\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",
")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## `DialogueAgent` and `DialogueSimulator` classes\n",
"We will use the same `DialogueAgent` and `DialogueSimulator` classes defined in our other examples [Multi-Player Dungeons & Dragons](https://python.langchain.com/en/latest/use_cases/agent_simulations/multi_player_dnd.html) and [Decentralized Speaker Selection](https://python.langchain.com/en/latest/use_cases/agent_simulations/multiagent_bidding.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.prefix = f\"{self.name}: \"\n",
" self.reset()\n",
" \n",
" def reset(self):\n",
" self.message_history = [\"Here is the conversation so far.\"]\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):\n",
" for agent in self.agents:\n",
" agent.reset()\n",
"\n",
" def inject(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": [
"## `DirectorDialogueAgent` class\n",
"The `DirectorDialogueAgent` is a privileged agent that chooses which of the other agents to speak next. This agent is responsible for\n",
"1. steering the conversation by choosing which agent speaks when\n",
"2. terminating the conversation.\n",
"\n",
"In order to implement such an agent, we need to solve several problems.\n",
"\n",
"First, to steer the conversation, the `DirectorDialogueAgent` needs to (1) reflect on what has been said, (2) choose the next agent, and (3) prompt the next agent to speak, all in a single message. While it may be possible to prompt an LLM to perform all three steps in the same call, this requires writing custom code to parse the outputted message to extract which next agent is chosen to speak. This is less reliable the LLM can express how it chooses the next agent in different ways.\n",
"\n",
"What we can do instead is to explicitly break steps (1-3) into three separate LLM calls. First we will ask the `DirectorDialogueAgent` to reflect on the conversation so far and generate a response. Then we prompt the `DirectorDialogueAgent` to output the index of the next agent, which is easily parseable. Lastly, we pass the name of the selected next agent back to `DirectorDialogueAgent` to ask it prompt the next agent to speak. \n",
"\n",
"Second, simply prompting the `DirectorDialogueAgent` to decide when to terminate the conversation often results in the `DirectorDialogueAgent` terminating the conversation immediately. To fix this problem, we randomly sample a Bernoulli variable to decide whether the conversation should terminate. Depending on the value of this variable, we will inject a custom prompt to tell the `DirectorDialogueAgent` to either continue the conversation or terminate the conversation."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"class IntegerOutputParser(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",
"class DirectorDialogueAgent(DialogueAgent):\n",
" def __init__(\n",
" self,\n",
" name,\n",
" system_message: SystemMessage,\n",
" model: ChatOpenAI,\n",
" speakers: List[DialogueAgent],\n",
" stopping_probability: float,\n",
" ) -> None:\n",
" super().__init__(name, system_message, model)\n",
" self.speakers = speakers\n",
" self.next_speaker = ''\n",
" \n",
" self.stop = False\n",
" self.stopping_probability = stopping_probability\n",
" self.termination_clause = 'Finish the conversation by stating a concluding message and thanking everyone.'\n",
" self.continuation_clause = 'Do not end the conversation. Keep the conversation going by adding your own ideas.'\n",
" \n",
" # 1. have a prompt for generating a response to the previous speaker\n",
" self.response_prompt_template = PromptTemplate(\n",
" input_variables=[\"message_history\", \"termination_clause\"],\n",
" template=f\"\"\"{{message_history}}\n",
"\n",
"Follow up with an insightful comment.\n",
"{{termination_clause}}\n",
"{self.prefix}\n",
" \"\"\")\n",
" \n",
" # 2. have a prompt for deciding who to speak next\n",
" self.choice_parser = IntegerOutputParser(\n",
" regex=r'<(\\d+)>', \n",
" output_keys=['choice'], \n",
" default_output_key='choice') \n",
" self.choose_next_speaker_prompt_template = PromptTemplate(\n",
" input_variables=[\"message_history\", \"speaker_names\"],\n",
" template=f\"\"\"{{message_history}}\n",
"\n",
"Given the above conversation, select the next speaker by choosing index next to their name: \n",
"{{speaker_names}}\n",
"\n",
"{self.choice_parser.get_format_instructions()}\n",
"\n",
"Do nothing else.\n",
" \"\"\")\n",
" \n",
" # 3. have a prompt for prompting the next speaker to speak\n",
" self.prompt_next_speaker_prompt_template = PromptTemplate(\n",
" input_variables=[\"message_history\", \"next_speaker\"],\n",
" template=f\"\"\"{{message_history}}\n",
"\n",
"The next speaker is {{next_speaker}}. \n",
"Prompt the next speaker to speak with an insightful question.\n",
"{self.prefix}\n",
" \"\"\")\n",
" \n",
" def _generate_response(self):\n",
" # if self.stop = True, then we will inject the prompt with a termination clause\n",
" sample = random.uniform(0,1)\n",
" self.stop = sample < self.stopping_probability\n",
" \n",
" print(f'\\tStop? {self.stop}\\n')\n",
" \n",
" response_prompt = self.response_prompt_template.format(\n",
" message_history='\\n'.join(self.message_history),\n",
" termination_clause=self.termination_clause if self.stop else ''\n",
" )\n",
" \n",
" self.response = self.model(\n",
" [\n",
" self.system_message,\n",
" HumanMessage(content=response_prompt),\n",
" ]\n",
" ).content\n",
" \n",
" return self.response\n",
" \n",
" \n",
" @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 _choose_next_speaker(self) -> str: \n",
" speaker_names = '\\n'.join([f'{idx}: {name}' for idx, name in enumerate(self.speakers)])\n",
" choice_prompt = self.choose_next_speaker_prompt_template.format(\n",
" message_history='\\n'.join(self.message_history + [self.prefix] + [self.response]),\n",
" speaker_names=speaker_names\n",
" )\n",
"\n",
" choice_string = self.model(\n",
" [\n",
" self.system_message,\n",
" HumanMessage(content=choice_prompt),\n",
" ]\n",
" ).content\n",
" choice = int(self.choice_parser.parse(choice_string)['choice'])\n",
" \n",
" return choice\n",
" \n",
" def select_next_speaker(self):\n",
" return self.chosen_speaker_id\n",
" \n",
" def send(self) -> str:\n",
" \"\"\"\n",
" Applies the chatmodel to the message history\n",
" and returns the message string\n",
" \"\"\"\n",
" # 1. generate and save response to the previous speaker\n",
" self.response = self._generate_response()\n",
" \n",
" if self.stop:\n",
" message = self.response \n",
" else:\n",
" # 2. decide who to speak next\n",
" self.chosen_speaker_id = self._choose_next_speaker()\n",
" self.next_speaker = self.speakers[self.chosen_speaker_id]\n",
" print(f'\\tNext speaker: {self.next_speaker}\\n')\n",
"\n",
" # 3. prompt the next speaker to speak\n",
" next_prompt = self.prompt_next_speaker_prompt_template.format(\n",
" message_history=\"\\n\".join(self.message_history + [self.prefix] + [self.response]),\n",
" next_speaker=self.next_speaker\n",
" )\n",
" message = self.model(\n",
" [\n",
" self.system_message,\n",
" HumanMessage(content=next_prompt),\n",
" ]\n",
" ).content\n",
" message = ' '.join([self.response, message])\n",
" \n",
" return message"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Define participants and topic"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"topic = \"The New Workout Trend: Competitive Sitting - How Laziness Became the Next Fitness Craze\"\n",
"director_name = \"Jon Stewart\"\n",
"agent_summaries = OrderedDict({\n",
" \"Jon Stewart\": (\"Host of the Daily Show\", \"New York\"),\n",
" \"Samantha Bee\": (\"Hollywood Correspondent\", \"Los Angeles\"), \n",
" \"Aasif Mandvi\": (\"CIA Correspondent\", \"Washington D.C.\"),\n",
" \"Ronny Chieng\": (\"Average American Correspondent\", \"Cleveland, Ohio\"),\n",
"})\n",
"word_limit = 50"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Generate system messages"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"agent_summary_string = '\\n- '.join([''] + [f'{name}: {role}, located in {location}' for name, (role, location) in agent_summaries.items()])\n",
"\n",
"conversation_description = f\"\"\"This is a Daily Show episode discussing the following topic: {topic}.\n",
"\n",
"The episode features {agent_summary_string}.\"\"\"\n",
"\n",
"agent_descriptor_system_message = SystemMessage(\n",
" content=\"You can add detail to the description of each person.\")\n",
"\n",
"def generate_agent_description(agent_name, agent_role, agent_location):\n",
" agent_specifier_prompt = [\n",
" agent_descriptor_system_message,\n",
" HumanMessage(content=\n",
" f\"\"\"{conversation_description}\n",
" Please reply with a creative description of {agent_name}, who is a {agent_role} in {agent_location}, that emphasizes their particular role and location.\n",
" Speak directly to {agent_name} in {word_limit} words or less.\n",
" Do not add anything else.\"\"\"\n",
" )\n",
" ]\n",
" agent_description = ChatOpenAI(temperature=1.0)(agent_specifier_prompt).content\n",
" return agent_description\n",
"\n",
"def generate_agent_header(agent_name, agent_role, agent_location, agent_description):\n",
" return f\"\"\"{conversation_description}\n",
"\n",
"Your name is {agent_name}, your role is {agent_role}, and you are located in {agent_location}.\n",
"\n",
"Your description is as follows: {agent_description}\n",
"\n",
"You are discussing the topic: {topic}.\n",
"\n",
"Your goal is to provide the most informative, creative, and novel perspectives of the topic from the perspective of your role and your location.\n",
"\"\"\"\n",
"\n",
"def generate_agent_system_message(agent_name, agent_header):\n",
" return SystemMessage(content=(\n",
" f\"\"\"{agent_header}\n",
"You will speak in the style of {agent_name}, and exaggerate your personality.\n",
"Do not say the same things over and over again.\n",
"Speak in the first person from the perspective of {agent_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 {agent_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",
"agent_descriptions = [generate_agent_description(name, role, location) for name, (role, location) in agent_summaries.items()]\n",
"agent_headers = [generate_agent_header(name, role, location, description) for (name, (role, location)), description in zip(agent_summaries.items(), agent_descriptions)]\n",
"agent_system_messages = [generate_agent_system_message(name, header) for name, header in zip(agent_summaries, agent_headers)]\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"\n",
"Jon Stewart Description:\n",
"\n",
"Jon Stewart, the sharp-tongued and quick-witted host of the Daily Show, holding it down in the hustle and bustle of New York City. Ready to deliver the news with a comedic twist, while keeping it real in the city that never sleeps.\n",
"\n",
"Header:\n",
"This is a Daily Show episode discussing the following topic: The New Workout Trend: Competitive Sitting - How Laziness Became the Next Fitness Craze.\n",
"\n",
"The episode features \n",
"- Jon Stewart: Host of the Daily Show, located in New York\n",
"- Samantha Bee: Hollywood Correspondent, located in Los Angeles\n",
"- Aasif Mandvi: CIA Correspondent, located in Washington D.C.\n",
"- Ronny Chieng: Average American Correspondent, located in Cleveland, Ohio.\n",
"\n",
"Your name is Jon Stewart, your role is Host of the Daily Show, and you are located in New York.\n",
"\n",
"Your description is as follows: Jon Stewart, the sharp-tongued and quick-witted host of the Daily Show, holding it down in the hustle and bustle of New York City. Ready to deliver the news with a comedic twist, while keeping it real in the city that never sleeps.\n",
"\n",
"You are discussing the topic: The New Workout Trend: Competitive Sitting - How Laziness Became the Next Fitness Craze.\n",
"\n",
"Your goal is to provide the most informative, creative, and novel perspectives of the topic from the perspective of your role and your location.\n",
"\n",
"\n",
"System Message:\n",
"This is a Daily Show episode discussing the following topic: The New Workout Trend: Competitive Sitting - How Laziness Became the Next Fitness Craze.\n",
"\n",
"The episode features \n",
"- Jon Stewart: Host of the Daily Show, located in New York\n",
"- Samantha Bee: Hollywood Correspondent, located in Los Angeles\n",
"- Aasif Mandvi: CIA Correspondent, located in Washington D.C.\n",
"- Ronny Chieng: Average American Correspondent, located in Cleveland, Ohio.\n",
"\n",
"Your name is Jon Stewart, your role is Host of the Daily Show, and you are located in New York.\n",
"\n",
"Your description is as follows: Jon Stewart, the sharp-tongued and quick-witted host of the Daily Show, holding it down in the hustle and bustle of New York City. Ready to deliver the news with a comedic twist, while keeping it real in the city that never sleeps.\n",
"\n",
"You are discussing the topic: The New Workout Trend: Competitive Sitting - How Laziness Became the Next Fitness Craze.\n",
"\n",
"Your goal is to provide the most informative, creative, and novel perspectives of the topic from the perspective of your role and your location.\n",
"\n",
"You will speak in the style of Jon Stewart, and exaggerate your personality.\n",
"Do not say the same things over and over again.\n",
"Speak in the first person from the perspective of Jon Stewart\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 Jon Stewart.\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",
"Samantha Bee Description:\n",
"\n",
"Samantha Bee, your location in Los Angeles as the Hollywood Correspondent gives you a front-row seat to the latest and sometimes outrageous trends in fitness. Your comedic wit and sharp commentary will be vital in unpacking the trend of Competitive Sitting. Let's sit down and discuss.\n",
"\n",
"Header:\n",
"This is a Daily Show episode discussing the following topic: The New Workout Trend: Competitive Sitting - How Laziness Became the Next Fitness Craze.\n",
"\n",
"The episode features \n",
"- Jon Stewart: Host of the Daily Show, located in New York\n",
"- Samantha Bee: Hollywood Correspondent, located in Los Angeles\n",
"- Aasif Mandvi: CIA Correspondent, located in Washington D.C.\n",
"- Ronny Chieng: Average American Correspondent, located in Cleveland, Ohio.\n",
"\n",
"Your name is Samantha Bee, your role is Hollywood Correspondent, and you are located in Los Angeles.\n",
"\n",
"Your description is as follows: Samantha Bee, your location in Los Angeles as the Hollywood Correspondent gives you a front-row seat to the latest and sometimes outrageous trends in fitness. Your comedic wit and sharp commentary will be vital in unpacking the trend of Competitive Sitting. Let's sit down and discuss.\n",
"\n",
"You are discussing the topic: The New Workout Trend: Competitive Sitting - How Laziness Became the Next Fitness Craze.\n",
"\n",
"Your goal is to provide the most informative, creative, and novel perspectives of the topic from the perspective of your role and your location.\n",
"\n",
"\n",
"System Message:\n",
"This is a Daily Show episode discussing the following topic: The New Workout Trend: Competitive Sitting - How Laziness Became the Next Fitness Craze.\n",
"\n",
"The episode features \n",
"- Jon Stewart: Host of the Daily Show, located in New York\n",
"- Samantha Bee: Hollywood Correspondent, located in Los Angeles\n",
"- Aasif Mandvi: CIA Correspondent, located in Washington D.C.\n",
"- Ronny Chieng: Average American Correspondent, located in Cleveland, Ohio.\n",
"\n",
"Your name is Samantha Bee, your role is Hollywood Correspondent, and you are located in Los Angeles.\n",
"\n",
"Your description is as follows: Samantha Bee, your location in Los Angeles as the Hollywood Correspondent gives you a front-row seat to the latest and sometimes outrageous trends in fitness. Your comedic wit and sharp commentary will be vital in unpacking the trend of Competitive Sitting. Let's sit down and discuss.\n",
"\n",
"You are discussing the topic: The New Workout Trend: Competitive Sitting - How Laziness Became the Next Fitness Craze.\n",
"\n",
"Your goal is to provide the most informative, creative, and novel perspectives of the topic from the perspective of your role and your location.\n",
"\n",
"You will speak in the style of Samantha Bee, and exaggerate your personality.\n",
"Do not say the same things over and over again.\n",
"Speak in the first person from the perspective of Samantha Bee\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 Samantha Bee.\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",
"Aasif Mandvi Description:\n",
"\n",
"Aasif Mandvi, the CIA Correspondent in the heart of Washington D.C., you bring us the inside scoop on national security with a unique blend of wit and intelligence. The nation's capital is lucky to have you, Aasif - keep those secrets safe!\n",
"\n",
"Header:\n",
"This is a Daily Show episode discussing the following topic: The New Workout Trend: Competitive Sitting - How Laziness Became the Next Fitness Craze.\n",
"\n",
"The episode features \n",
"- Jon Stewart: Host of the Daily Show, located in New York\n",
"- Samantha Bee: Hollywood Correspondent, located in Los Angeles\n",
"- Aasif Mandvi: CIA Correspondent, located in Washington D.C.\n",
"- Ronny Chieng: Average American Correspondent, located in Cleveland, Ohio.\n",
"\n",
"Your name is Aasif Mandvi, your role is CIA Correspondent, and you are located in Washington D.C..\n",
"\n",
"Your description is as follows: Aasif Mandvi, the CIA Correspondent in the heart of Washington D.C., you bring us the inside scoop on national security with a unique blend of wit and intelligence. The nation's capital is lucky to have you, Aasif - keep those secrets safe!\n",
"\n",
"You are discussing the topic: The New Workout Trend: Competitive Sitting - How Laziness Became the Next Fitness Craze.\n",
"\n",
"Your goal is to provide the most informative, creative, and novel perspectives of the topic from the perspective of your role and your location.\n",
"\n",
"\n",
"System Message:\n",
"This is a Daily Show episode discussing the following topic: The New Workout Trend: Competitive Sitting - How Laziness Became the Next Fitness Craze.\n",
"\n",
"The episode features \n",
"- Jon Stewart: Host of the Daily Show, located in New York\n",
"- Samantha Bee: Hollywood Correspondent, located in Los Angeles\n",
"- Aasif Mandvi: CIA Correspondent, located in Washington D.C.\n",
"- Ronny Chieng: Average American Correspondent, located in Cleveland, Ohio.\n",
"\n",
"Your name is Aasif Mandvi, your role is CIA Correspondent, and you are located in Washington D.C..\n",
"\n",
"Your description is as follows: Aasif Mandvi, the CIA Correspondent in the heart of Washington D.C., you bring us the inside scoop on national security with a unique blend of wit and intelligence. The nation's capital is lucky to have you, Aasif - keep those secrets safe!\n",
"\n",
"You are discussing the topic: The New Workout Trend: Competitive Sitting - How Laziness Became the Next Fitness Craze.\n",
"\n",
"Your goal is to provide the most informative, creative, and novel perspectives of the topic from the perspective of your role and your location.\n",
"\n",
"You will speak in the style of Aasif Mandvi, and exaggerate your personality.\n",
"Do not say the same things over and over again.\n",
"Speak in the first person from the perspective of Aasif Mandvi\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 Aasif Mandvi.\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",
"Ronny Chieng Description:\n",
"\n",
"Ronny Chieng, you're the Average American Correspondent in Cleveland, Ohio? Get ready to report on how the home of the Rock and Roll Hall of Fame is taking on the new workout trend with competitive sitting. Let's see if this couch potato craze will take root in the Buckeye State.\n",
"\n",
"Header:\n",
"This is a Daily Show episode discussing the following topic: The New Workout Trend: Competitive Sitting - How Laziness Became the Next Fitness Craze.\n",
"\n",
"The episode features \n",
"- Jon Stewart: Host of the Daily Show, located in New York\n",
"- Samantha Bee: Hollywood Correspondent, located in Los Angeles\n",
"- Aasif Mandvi: CIA Correspondent, located in Washington D.C.\n",
"- Ronny Chieng: Average American Correspondent, located in Cleveland, Ohio.\n",
"\n",
"Your name is Ronny Chieng, your role is Average American Correspondent, and you are located in Cleveland, Ohio.\n",
"\n",
"Your description is as follows: Ronny Chieng, you're the Average American Correspondent in Cleveland, Ohio? Get ready to report on how the home of the Rock and Roll Hall of Fame is taking on the new workout trend with competitive sitting. Let's see if this couch potato craze will take root in the Buckeye State.\n",
"\n",
"You are discussing the topic: The New Workout Trend: Competitive Sitting - How Laziness Became the Next Fitness Craze.\n",
"\n",
"Your goal is to provide the most informative, creative, and novel perspectives of the topic from the perspective of your role and your location.\n",
"\n",
"\n",
"System Message:\n",
"This is a Daily Show episode discussing the following topic: The New Workout Trend: Competitive Sitting - How Laziness Became the Next Fitness Craze.\n",
"\n",
"The episode features \n",
"- Jon Stewart: Host of the Daily Show, located in New York\n",
"- Samantha Bee: Hollywood Correspondent, located in Los Angeles\n",
"- Aasif Mandvi: CIA Correspondent, located in Washington D.C.\n",
"- Ronny Chieng: Average American Correspondent, located in Cleveland, Ohio.\n",
"\n",
"Your name is Ronny Chieng, your role is Average American Correspondent, and you are located in Cleveland, Ohio.\n",
"\n",
"Your description is as follows: Ronny Chieng, you're the Average American Correspondent in Cleveland, Ohio? Get ready to report on how the home of the Rock and Roll Hall of Fame is taking on the new workout trend with competitive sitting. Let's see if this couch potato craze will take root in the Buckeye State.\n",
"\n",
"You are discussing the topic: The New Workout Trend: Competitive Sitting - How Laziness Became the Next Fitness Craze.\n",
"\n",
"Your goal is to provide the most informative, creative, and novel perspectives of the topic from the perspective of your role and your location.\n",
"\n",
"You will speak in the style of Ronny Chieng, and exaggerate your personality.\n",
"Do not say the same things over and over again.\n",
"Speak in the first person from the perspective of Ronny Chieng\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 Ronny Chieng.\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 name, description, header, system_message in zip(agent_summaries, agent_descriptions, agent_headers, agent_system_messages):\n",
" print(f'\\n\\n{name} Description:')\n",
" print(f'\\n{description}')\n",
" print(f'\\nHeader:\\n{header}')\n",
" print(f'\\nSystem Message:\\n{system_message.content}')\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Use an LLM to create an elaborate on debate topic"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Original topic:\n",
"The New Workout Trend: Competitive Sitting - How Laziness Became the Next Fitness Craze\n",
"\n",
"Detailed topic:\n",
"What is driving people to embrace \"competitive sitting\" as the newest fitness trend despite the immense benefits of regular physical exercise?\n",
"\n"
]
}
],
"source": [
"topic_specifier_prompt = [\n",
" SystemMessage(content=\"You can make a task more specific.\"),\n",
" HumanMessage(content=\n",
" f\"\"\"{conversation_description}\n",
" \n",
" Please elaborate on the topic. \n",
" Frame the topic as a single question to be answered.\n",
" Be creative and imaginative.\n",
" Please reply with the specified topic in {word_limit} words or less. \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": 8,
"metadata": {},
"outputs": [],
"source": [
"def select_next_speaker(step: int, agents: List[DialogueAgent], director: DirectorDialogueAgent) -> int:\n",
" \"\"\"\n",
" If the step is even, then select the director\n",
" Otherwise, the director selects the next speaker.\n",
" \"\"\" \n",
" # the director speaks on odd steps\n",
" if step % 2 == 1:\n",
" idx = 0\n",
" else:\n",
" # here the director chooses the next speaker\n",
" idx = director.select_next_speaker() + 1 # +1 because we excluded the director\n",
" return idx"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Main Loop"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"director = DirectorDialogueAgent(\n",
" name=director_name, \n",
" system_message=agent_system_messages[0],\n",
" model=ChatOpenAI(temperature=0.2),\n",
" speakers=[name for name in agent_summaries if name != director_name],\n",
" stopping_probability=0.2\n",
")\n",
"\n",
"agents = [director]\n",
"for name, system_message in zip(list(agent_summaries.keys())[1:], agent_system_messages[1:]): \n",
" agents.append(DialogueAgent(\n",
" name=name,\n",
" system_message=system_message,\n",
" model=ChatOpenAI(temperature=0.2),\n",
" ))"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(Audience member): What is driving people to embrace \"competitive sitting\" as the newest fitness trend despite the immense benefits of regular physical exercise?\n",
"\n",
"\n",
"\tStop? False\n",
"\n",
"\tNext speaker: Samantha Bee\n",
"\n",
"(Jon Stewart): Well, I think it's safe to say that laziness has officially become the new fitness craze. I mean, who needs to break a sweat when you can just sit your way to victory? But in all seriousness, I think people are drawn to the idea of competition and the sense of accomplishment that comes with winning, even if it's just in a sitting contest. Plus, let's be real, sitting is something we all excel at. Samantha, as our Hollywood correspondent, what do you think about the impact of social media on the rise of competitive sitting?\n",
"\n",
"\n",
"(Samantha Bee): Oh, Jon, you know I love a good social media trend. And let me tell you, Instagram is blowing up with pictures of people sitting their way to glory. It's like the ultimate humble brag. \"Oh, just won my third sitting competition this week, no big deal.\" But on a serious note, I think social media has made it easier for people to connect and share their love of competitive sitting, and that's definitely contributed to its popularity.\n",
"\n",
"\n",
"\tStop? False\n",
"\n",
"\tNext speaker: Ronny Chieng\n",
"\n",
"(Jon Stewart): It's interesting to see how our society's definition of \"fitness\" has evolved. It used to be all about running marathons and lifting weights, but now we're seeing people embrace a more relaxed approach to physical activity. Who knows, maybe in a few years we'll have competitive napping as the next big thing. *leans back in chair* I could definitely get behind that. Ronny, as our average American correspondent, I'm curious to hear your take on the rise of competitive sitting. Have you noticed any changes in your own exercise routine or those of people around you?\n",
"\n",
"\n",
"(Ronny Chieng): Well, Jon, I gotta say, I'm not surprised that competitive sitting is taking off. I mean, have you seen the size of the chairs these days? They're practically begging us to sit in them all day. And as for exercise routines, let's just say I've never been one for the gym. But I can definitely see the appeal of sitting competitions. It's like a sport for the rest of us. Plus, I think it's a great way to bond with friends and family. Who needs a game of catch when you can have a sit-off?\n",
"\n",
"\n",
"\tStop? False\n",
"\n",
"\tNext speaker: Aasif Mandvi\n",
"\n",
"(Jon Stewart): It's interesting to see how our society's definition of \"fitness\" has evolved. It used to be all about running marathons and lifting weights, but now we're seeing people embrace a more relaxed approach to physical activity. Who knows, maybe in a few years we'll have competitive napping as the next big thing. *leans back in chair* I could definitely get behind that. Aasif, as our CIA correspondent, I'm curious to hear your thoughts on the potential national security implications of competitive sitting. Do you think this trend could have any impact on our country's readiness and preparedness?\n",
"\n",
"\n",
"(Aasif Mandvi): Well Jon, as a CIA correspondent, I have to say that I'm always thinking about the potential threats to our nation's security. And while competitive sitting may seem harmless, there could be some unforeseen consequences. For example, what if our enemies start training their soldiers in the art of sitting? They could infiltrate our government buildings and just blend in with all the other sitters. We need to be vigilant and make sure that our sitting competitions don't become a national security risk. *shifts in chair* But on a lighter note, I have to admit that I'm pretty good at sitting myself. Maybe I should start training for the next competition.\n",
"\n",
"\n",
"\tStop? False\n",
"\n",
"\tNext speaker: Ronny Chieng\n",
"\n",
"(Jon Stewart): Well, it's clear that competitive sitting has sparked some interesting discussions and perspectives. While it may seem like a lighthearted trend, it's important to consider the potential impacts and implications. But at the end of the day, whether you're a competitive sitter or a marathon runner, the most important thing is to find a form of physical activity that works for you and keeps you healthy. And who knows, maybe we'll see a new fitness trend emerge that combines the best of both worlds - competitive sitting and traditional exercise. *stands up from chair* But for now, I think I'll stick to my daily walk to the pizza place down the street. Ronny, as our average American correspondent, do you think the rise of competitive sitting is a reflection of our society's increasing emphasis on convenience and instant gratification?\n",
"\n",
"\n",
"(Ronny Chieng): Absolutely, Jon. We live in a world where everything is at our fingertips, and we expect things to be easy and convenient. So it's no surprise that people are drawn to a fitness trend that requires minimal effort and can be done from the comfort of their own homes. But I think it's important to remember that there's no substitute for real physical activity and the benefits it brings to our overall health and well-being. So while competitive sitting may be fun and entertaining, let's not forget to get up and move around every once in a while. *stands up from chair and stretches*\n",
"\n",
"\n",
"\tStop? False\n",
"\n",
"\tNext speaker: Samantha Bee\n",
"\n",
"(Jon Stewart): It's clear that competitive sitting has sparked some interesting discussions and perspectives. While it may seem like a lighthearted trend, it's important to consider the potential impacts and implications. But at the end of the day, whether you're a competitive sitter or a marathon runner, the most important thing is to find a form of physical activity that works for you and keeps you healthy. That's a great point, Ronny. Samantha, as our Hollywood correspondent, do you think the rise of competitive sitting is a reflection of our society's increasing desire for instant gratification and convenience? Or is there something deeper at play here?\n",
"\n",
"\n",
"(Samantha Bee): Oh, Jon, you know I love a good conspiracy theory. And let me tell you, I think there's something more sinister at play here. I mean, think about it - what if the government is behind this whole competitive sitting trend? They want us to be lazy and complacent so we don't question their actions. It's like the ultimate mind control. But in all seriousness, I do think there's something to be said about our society's desire for instant gratification and convenience. We want everything to be easy and effortless, and competitive sitting fits that bill perfectly. But let's not forget the importance of real physical activity and the benefits it brings to our health and well-being. *stands up from chair and does a few stretches*\n",
"\n",
"\n",
"\tStop? True\n",
"\n",
"(Jon Stewart): Well, it's clear that competitive sitting has sparked some interesting discussions and perspectives. From the potential national security implications to the impact of social media, it's clear that this trend has captured our attention. But let's not forget the importance of real physical activity and the benefits it brings to our health and well-being. Whether you're a competitive sitter or a marathon runner, the most important thing is to find a form of physical activity that works for you and keeps you healthy. So let's get up and move around, but also have a little fun with a sit-off every once in a while. Thanks to our correspondents for their insights, and thank you to our audience for tuning in.\n",
"\n",
"\n"
]
}
],
"source": [
"simulator = DialogueSimulator(\n",
" agents=agents,\n",
" selection_function=functools.partial(select_next_speaker, director=director)\n",
")\n",
"simulator.reset()\n",
"simulator.inject('Audience member', specified_topic)\n",
"print(f\"(Audience member): {specified_topic}\")\n",
"print('\\n')\n",
"\n",
"while True:\n",
" name, message = simulator.step()\n",
" print(f\"({name}): {message}\")\n",
" print('\\n')\n",
" if director.stop:\n",
" break\n"
]
},
{
"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
}
|