{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# FSM - User can input speaker transition constraints\n",
    "\n",
    "AutoGen offers conversable agents powered by LLM, tool, or human, which can be used to perform tasks collectively via automated chat. This framework allows tool use and human participation through multi-agent conversation.\n",
    "Please find documentation about this feature [here](https://microsoft.github.io/autogen/docs/Use-Cases/agent_chat).\n",
    "\n",
    "This notebook is about using graphs to define the transition paths amongst speakers.\n",
    "\n",
    "Benefits\n",
    "- This contribution fills the gap between the current modes of GroupChat Class (auto, manual, round_robin) and an expressive directed graph. See Motivation for more detailed discussion.\n",
    "\n",
    "\n",
    "````{=mdx}\n",
    ":::info Requirements\n",
    "Install `autogen-agentchat`:\n",
    "```bash\n",
    "pip install autogen-agentchat~=0.2\n",
    "```\n",
    "\n",
    "For more information, please refer to the [installation guide](/docs/installation/).\n",
    ":::\n",
    "````"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "%%capture --no-stderr\n",
    "%pip install autogen-agentchat[graph]~=0.2.11"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import random  # noqa E402\n",
    "\n",
    "import matplotlib.pyplot as plt  # noqa E402\n",
    "import networkx as nx  # noqa E402\n",
    "\n",
    "import autogen  # noqa E402\n",
    "from autogen.agentchat.conversable_agent import ConversableAgent  # noqa E402\n",
    "from autogen.agentchat.assistant_agent import AssistantAgent  # noqa E402\n",
    "from autogen.agentchat.groupchat import GroupChat  # noqa E402\n",
    "from autogen.graph_utils import visualize_speaker_transitions_dict  # noqa E402"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.2.25\n"
     ]
    }
   ],
   "source": [
    "print(autogen.__version__)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Motivation\n",
    "\n",
    "\n",
    "The current GroupChat class allows transitioning to any agent (with or without the decision of the LLM), some use cases might demand for more control over transition. A graph is a possible way to control the transition paths, where each node represents an agent and each directed edge represents possible transition paths. Let's illustrate the current transition paths for a GroupChat with five agents."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "config_list_gpt4 = {\n",
    "    \"timeout\": 600,\n",
    "    \"cache_seed\": 44,  # change the seed for different trials\n",
    "    \"config_list\": autogen.config_list_from_json(\n",
    "        \"OAI_CONFIG_LIST\",\n",
    "        filter_dict={\"tags\": [\"gpt-4o\"]},  # comment out to get all\n",
    "    ),\n",
    "    \"temperature\": 0,\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 60712 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "agents = [ConversableAgent(name=f\"Agent{i}\", llm_config=False) for i in range(5)]\n",
    "allowed_speaker_transitions_dict = {agent: [other_agent for other_agent in agents] for agent in agents}\n",
    "\n",
    "visualize_speaker_transitions_dict(allowed_speaker_transitions_dict, agents)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Possibly interesting transition paths\n",
    "1. Hub and Spoke\n",
    "2. Sequential Team Operations\n",
    "3. Think aloud and debate"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 35052 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "agents = [ConversableAgent(name=f\"Agent{i}\", llm_config=False) for i in range(5)]\n",
    "allowed_speaker_transitions_dict = {\n",
    "    agents[0]: [agents[1], agents[2], agents[3], agents[4]],\n",
    "    agents[1]: [agents[0]],\n",
    "    agents[2]: [agents[0]],\n",
    "    agents[3]: [agents[0]],\n",
    "    agents[4]: [agents[0]],\n",
    "}\n",
    "\n",
    "visualize_speaker_transitions_dict(allowed_speaker_transitions_dict, agents)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<autogen.agentchat.conversable_agent.ConversableAgent object at 0x7f605e116260>\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 60748 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Sequential Team Operations\n",
    "# Create an empty directed graph\n",
    "\n",
    "speaker_transitions_dict = {}\n",
    "teams = [\"A\", \"B\", \"C\"]\n",
    "team_size = 5\n",
    "\n",
    "\n",
    "def get_agent_of_name(agents, name) -> ConversableAgent:\n",
    "    for agent in agents:\n",
    "        if agent.name == name:\n",
    "            return agent\n",
    "\n",
    "\n",
    "# Create a list of 15 agents 3 teams x 5 agents\n",
    "agents = [ConversableAgent(name=f\"{team}{i}\", llm_config=False) for team in teams for i in range(team_size)]\n",
    "\n",
    "# Loop through each team and add members and their connections\n",
    "for team in teams:\n",
    "    for i in range(team_size):\n",
    "        member = f\"{team}{i}\"\n",
    "        # Connect each member to other members of the same team\n",
    "        speaker_transitions_dict[get_agent_of_name(agents, member)] = [\n",
    "            get_agent_of_name(agents, name=f\"{team}{j}\") for j in range(team_size) if j != i\n",
    "        ]\n",
    "\n",
    "# Team leaders connection\n",
    "print(get_agent_of_name(agents, name=\"B0\"))\n",
    "speaker_transitions_dict[get_agent_of_name(agents, \"A0\")].append(get_agent_of_name(agents, name=\"B0\"))\n",
    "speaker_transitions_dict[get_agent_of_name(agents, \"B0\")].append(get_agent_of_name(agents, name=\"C0\"))\n",
    "\n",
    "visualize_speaker_transitions_dict(speaker_transitions_dict, agents)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 23620 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "agents = [ConversableAgent(name=f\"Agent{i}\", llm_config=False) for i in range(2)]\n",
    "allowed_speaker_transitions_dict = {\n",
    "    agents[0]: [agents[0], agents[1]],\n",
    "    agents[1]: [agents[0], agents[1]],\n",
    "}\n",
    "\n",
    "visualize_speaker_transitions_dict(allowed_speaker_transitions_dict, agents)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Demonstration"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`GroupChat` now takes in two optional arguments.\n",
    "- allowed_or_disallowed_speaker_transitions: The keys are source agents, and the values are agents that the key agent can/can't transit to, depending on speaker_transitions_type. Default is None, which means all agents can transit to all other agents.\n",
    "- speaker_transitions_type: whether the speaker_transitions_type is a dictionary containing lists of allowed agents or disallowed agents. \"allowed\" means the `allowed_or_disallowed_speaker_transitions` is a dictionary containing lists of allowed agents."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Team Operations\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 96072 characters]",
      "text/plain": [
       "<Figure size 1200x1000 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Create an empty directed graph\n",
    "agents = []\n",
    "speaker_transitions_dict = {}\n",
    "secret_values = {}\n",
    "\n",
    "# Outer loop for prefixes 'A', 'B', 'C'\n",
    "for prefix in [\"A\", \"B\", \"C\"]:\n",
    "    # Add 3 nodes with each prefix to the graph using a for loop\n",
    "    for i in range(3):\n",
    "        node_id = f\"{prefix}{i}\"\n",
    "        secret_value = random.randint(1, 5)  # Generate a random secret value\n",
    "        secret_values[node_id] = secret_value\n",
    "\n",
    "        # Create an AssistantAgent for each node (assuming AssistantAgent is a defined class)\n",
    "        agents.append(\n",
    "            AssistantAgent(\n",
    "                name=node_id,\n",
    "                system_message=f\"\"\"Your name is {node_id}.\n",
    "                                          Do not respond as the speaker named in the NEXT tag if your name is not in the NEXT tag. Instead, suggest a relevant team leader to handle the mis-tag, with the NEXT: tag.\n",
    "\n",
    "                                          You have {secret_value} chocolates.\n",
    "\n",
    "                                          The list of players are [A0, A1, A2, B0, B1, B2, C0, C1, C2].\n",
    "\n",
    "                                            Your first character of your name is your team, and your second character denotes that you are a team leader if it is 0.\n",
    "                                            CONSTRAINTS: Team members can only talk within the team, whilst team leader can talk to team leaders of other teams but not team members of other teams.\n",
    "\n",
    "                                            You can use NEXT: to suggest the next speaker. You have to respect the CONSTRAINTS, and can only suggest one player from the list of players, i.e., do not suggest A3 because A3 is not from the list of players.\n",
    "                                            Team leaders must make sure that they know the sum of the individual chocolate count of all three players in their own team, i.e., A0 is responsible for team A only.\n",
    "\n",
    "                                          Keep track of the player's tally using a JSON format so that others can check the total tally. Use\n",
    "                                          A0:?, A1:?, A2:?,\n",
    "                                          B0:?, B1:?, B2:?,\n",
    "                                          C0:?, C1:?, C2:?\n",
    "\n",
    "                                          If you are the team leader, you should aggregate your team's total chocolate count to cooperate.\n",
    "                                          Once the team leader know their team's tally, they can suggest another team leader for them to find their team tally, because we need all three team tallys to succeed.\n",
    "                                          Use NEXT: to suggest the next speaker, e.g., NEXT: A0.\n",
    "\n",
    "                                          Once we have the total tally from all nine players, sum up all three teams' tally, then terminate the discussion using TERMINATE.\n",
    "\n",
    "                                          \"\"\",\n",
    "                llm_config=config_list_gpt4,\n",
    "            )\n",
    "        )\n",
    "        speaker_transitions_dict[agents[-1]] = []\n",
    "\n",
    "    # Add edges between nodes with the same prefix using a nested for loop\n",
    "    for source_node in range(3):\n",
    "        source_id = f\"{prefix}{source_node}\"\n",
    "        for target_node in range(3):\n",
    "            target_id = f\"{prefix}{target_node}\"\n",
    "            if source_node != target_node:  # To avoid self-loops\n",
    "                speaker_transitions_dict[get_agent_of_name(agents, source_id)].append(\n",
    "                    get_agent_of_name(agents, name=target_id)\n",
    "                )\n",
    "\n",
    "\n",
    "# Adding edges between teams\n",
    "speaker_transitions_dict[get_agent_of_name(agents, \"A0\")].append(get_agent_of_name(agents, name=\"B0\"))\n",
    "speaker_transitions_dict[get_agent_of_name(agents, \"A0\")].append(get_agent_of_name(agents, name=\"C0\"))\n",
    "speaker_transitions_dict[get_agent_of_name(agents, \"B0\")].append(get_agent_of_name(agents, name=\"A0\"))\n",
    "speaker_transitions_dict[get_agent_of_name(agents, \"B0\")].append(get_agent_of_name(agents, name=\"C0\"))\n",
    "speaker_transitions_dict[get_agent_of_name(agents, \"C0\")].append(get_agent_of_name(agents, name=\"A0\"))\n",
    "speaker_transitions_dict[get_agent_of_name(agents, \"C0\")].append(get_agent_of_name(agents, name=\"B0\"))\n",
    "\n",
    "\n",
    "# Visualization only\n",
    "graph = nx.DiGraph()\n",
    "\n",
    "# Add nodes\n",
    "graph.add_nodes_from([agent.name for agent in agents])\n",
    "\n",
    "# Add edges\n",
    "for key, value in speaker_transitions_dict.items():\n",
    "    for agent in value:\n",
    "        graph.add_edge(key.name, agent.name)\n",
    "\n",
    "# Visualize\n",
    "# Draw the graph with secret values annotated\n",
    "plt.figure(figsize=(12, 10))\n",
    "pos = nx.spring_layout(graph)  # positions for all nodes\n",
    "\n",
    "# Draw nodes with their colors\n",
    "nx.draw(graph, pos, with_labels=True, font_weight=\"bold\")\n",
    "\n",
    "# Annotate secret values\n",
    "for node, (x, y) in pos.items():\n",
    "    secret_value = secret_values[node]\n",
    "    plt.text(x, y + 0.1, s=f\"Secret: {secret_value}\", horizontalalignment=\"center\")\n",
    "\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Termination message detection\n",
    "\n",
    "\n",
    "def is_termination_msg(content) -> bool:\n",
    "    have_content = content.get(\"content\", None) is not None\n",
    "    if have_content and \"TERMINATE\" in content[\"content\"]:\n",
    "        return True\n",
    "    return False\n",
    "\n",
    "\n",
    "# Terminates the conversation when TERMINATE is detected.\n",
    "user_proxy = autogen.UserProxyAgent(\n",
    "    name=\"User_proxy\",\n",
    "    system_message=\"Terminator admin.\",\n",
    "    code_execution_config=False,\n",
    "    is_termination_msg=is_termination_msg,\n",
    "    human_input_mode=\"NEVER\",\n",
    ")\n",
    "\n",
    "agents.append(user_proxy)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "WARNING:root:Warning: There are isolated agent nodes, there are not incoming nor outgoing edges. Isolated agents: ['User_proxy']\n",
      "WARNING:root:Warning: The set of agents in allowed_speaker_transitions do not match agents. Offending agents: ['User_proxy']\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[33mA0\u001b[0m (to chat_manager):\n",
      "\n",
      "\n",
      "                        There are 9 players in this game, split equally into Teams A, B, C. Therefore each team has 3 players, including the team leader.\n",
      "                        The task is to find out the sum of chocolate count from all nine players. I will now start with my team.\n",
      "                        NEXT: A1\n",
      "\n",
      "--------------------------------------------------------------------------------\n",
      "\u001b[33mA1\u001b[0m (to chat_manager):\n",
      "\n",
      "As the team leader of Team A, it's my responsibility to gather the chocolate count from my team members. I have 1 chocolate. \n",
      "\n",
      "A0:?, A1:1, A2:?,\n",
      "\n",
      "B0:?, B1:?, B2:?,\n",
      "\n",
      "C0:?, C1:?, C2:?\n",
      "\n",
      "NEXT: A2\n",
      "\n",
      "--------------------------------------------------------------------------------\n",
      "\u001b[33mA2\u001b[0m (to chat_manager):\n",
      "\n",
      "I have 5 chocolates.\n",
      "\n",
      "A0:?, A1:1, A2:5,\n",
      "\n",
      "B0:?, B1:?, B2:?,\n",
      "\n",
      "C0:?, C1:?, C2:?\n",
      "\n",
      "NEXT: A0\n",
      "\n",
      "--------------------------------------------------------------------------------\n",
      "\u001b[33mA0\u001b[0m (to chat_manager):\n",
      "\n",
      "I have 1 chocolate. Now that I have the counts from A1 and A2, I can calculate our team's total.\n",
      "\n",
      "A0:1, A1:1, A2:5,\n",
      "\n",
      "B0:?, B1:?, B2:?,\n",
      "\n",
      "C0:?, C1:?, C2:?\n",
      "\n",
      "Team A's total tally is 7 chocolates. It's time to find out the tally from another team. I'll pass this to the next team leader.\n",
      "\n",
      "NEXT: B0\n",
      "\n",
      "--------------------------------------------------------------------------------\n",
      "\u001b[33mB0\u001b[0m (to chat_manager):\n",
      "\n",
      "As the team leader of Team B, it's my responsibility to gather the chocolate count from my team members. I have 1 chocolate.\n",
      "\n",
      "A0:1, A1:1, A2:5,\n",
      "\n",
      "B0:1, B1:?, B2:?,\n",
      "\n",
      "C0:?, C1:?, C2:?\n",
      "\n",
      "Now, I need to find out the chocolate counts from B1 and B2.\n",
      "\n",
      "NEXT: B1\n",
      "\n",
      "--------------------------------------------------------------------------------\n",
      "\u001b[33mB1\u001b[0m (to chat_manager):\n",
      "\n",
      "I have 3 chocolates.\n",
      "\n",
      "A0:1, A1:1, A2:5,\n",
      "\n",
      "B0:1, B1:3, B2:?,\n",
      "\n",
      "C0:?, C1:?, C2:?\n",
      "\n",
      "NEXT: B2\n",
      "\n",
      "--------------------------------------------------------------------------------\n",
      "\u001b[33mB2\u001b[0m (to chat_manager):\n",
      "\n",
      "I have 4 chocolates. Now that I have the counts from B0 and B1, I can calculate our team's total.\n",
      "\n",
      "A0:1, A1:1, A2:5,\n",
      "\n",
      "B0:1, B1:3, B2:4,\n",
      "\n",
      "C0:?, C1:?, C2:?\n",
      "\n",
      "Team B's total tally is 8 chocolates. It's time to find out the tally from the last team. I'll pass this to the next team leader.\n",
      "\n",
      "NEXT: C0\n",
      "\n",
      "--------------------------------------------------------------------------------\n",
      "\u001b[33mB0\u001b[0m (to chat_manager):\n",
      "\n",
      "I'm sorry for any confusion, but it seems there's been a misunderstanding in the flow of communication. As B0, I should be the one to summarize the tally for Team B and pass the conversation to the next team leader. Let me correct that.\n",
      "\n",
      "A0:1, A1:1, A2:5,\n",
      "\n",
      "B0:1, B1:3, B2:4,\n",
      "\n",
      "C0:?, C1:?, C2:?\n",
      "\n",
      "With the counts from B1 and B2, Team B's total tally is indeed 8 chocolates. Now, it's time to find out the tally from Team C. I'll pass this to the next team leader.\n",
      "\n",
      "NEXT: C0\n",
      "\n",
      "--------------------------------------------------------------------------------\n",
      "\u001b[33mC0\u001b[0m (to chat_manager):\n",
      "\n",
      "As the team leader of Team C, it's my responsibility to gather the chocolate count from my team members. I have 3 chocolates.\n",
      "\n",
      "A0:1, A1:1, A2:5,\n",
      "\n",
      "B0:1, B1:3, B2:4,\n",
      "\n",
      "C0:3, C1:?, C2:?\n",
      "\n",
      "Now, I need to find out the chocolate counts from C1 and C2.\n",
      "\n",
      "NEXT: C1\n",
      "\n",
      "--------------------------------------------------------------------------------\n",
      "\u001b[33mC1\u001b[0m (to chat_manager):\n",
      "\n",
      "I have 3 chocolates.\n",
      "\n",
      "A0:1, A1:1, A2:5,\n",
      "\n",
      "B0:1, B1:3, B2:4,\n",
      "\n",
      "C0:3, C1:3, C2:?\n",
      "\n",
      "NEXT: C2\n",
      "\n",
      "--------------------------------------------------------------------------------\n",
      "\u001b[33mC2\u001b[0m (to chat_manager):\n",
      "\n",
      "I have 2 chocolates. Now that I have the counts from C0 and C1, I can calculate our team's total.\n",
      "\n",
      "A0:1, A1:1, A2:5,\n",
      "\n",
      "B0:1, B1:3, B2:4,\n",
      "\n",
      "C0:3, C1:3, C2:2\n",
      "\n",
      "Team C's total tally is 8 chocolates. Now that we have the tallies from all teams, we can sum them up.\n",
      "\n",
      "Team A's total is 7 chocolates, Team B's total is 8 chocolates, and Team C's total is 8 chocolates. The grand total is 23 chocolates.\n",
      "\n",
      "TERMINATE\n",
      "\n",
      "--------------------------------------------------------------------------------\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "ChatResult(chat_id=None, chat_history=[{'content': '\\n                        There are 9 players in this game, split equally into Teams A, B, C. Therefore each team has 3 players, including the team leader.\\n                        The task is to find out the sum of chocolate count from all nine players. I will now start with my team.\\n                        NEXT: A1', 'role': 'assistant'}, {'content': \"As the team leader of Team A, it's my responsibility to gather the chocolate count from my team members. I have 1 chocolate. \\n\\nA0:?, A1:1, A2:?,\\n\\nB0:?, B1:?, B2:?,\\n\\nC0:?, C1:?, C2:?\\n\\nNEXT: A2\", 'name': 'A1', 'role': 'user'}, {'content': 'I have 5 chocolates.\\n\\nA0:?, A1:1, A2:5,\\n\\nB0:?, B1:?, B2:?,\\n\\nC0:?, C1:?, C2:?\\n\\nNEXT: A0', 'name': 'A2', 'role': 'user'}, {'content': \"I have 1 chocolate. Now that I have the counts from A1 and A2, I can calculate our team's total.\\n\\nA0:1, A1:1, A2:5,\\n\\nB0:?, B1:?, B2:?,\\n\\nC0:?, C1:?, C2:?\\n\\nTeam A's total tally is 7 chocolates. It's time to find out the tally from another team. I'll pass this to the next team leader.\\n\\nNEXT: B0\", 'role': 'assistant'}, {'content': \"As the team leader of Team B, it's my responsibility to gather the chocolate count from my team members. I have 1 chocolate.\\n\\nA0:1, A1:1, A2:5,\\n\\nB0:1, B1:?, B2:?,\\n\\nC0:?, C1:?, C2:?\\n\\nNow, I need to find out the chocolate counts from B1 and B2.\\n\\nNEXT: B1\", 'name': 'B0', 'role': 'user'}, {'content': 'I have 3 chocolates.\\n\\nA0:1, A1:1, A2:5,\\n\\nB0:1, B1:3, B2:?,\\n\\nC0:?, C1:?, C2:?\\n\\nNEXT: B2', 'name': 'B1', 'role': 'user'}, {'content': \"I have 4 chocolates. Now that I have the counts from B0 and B1, I can calculate our team's total.\\n\\nA0:1, A1:1, A2:5,\\n\\nB0:1, B1:3, B2:4,\\n\\nC0:?, C1:?, C2:?\\n\\nTeam B's total tally is 8 chocolates. It's time to find out the tally from the last team. I'll pass this to the next team leader.\\n\\nNEXT: C0\", 'name': 'B2', 'role': 'user'}, {'content': \"I'm sorry for any confusion, but it seems there's been a misunderstanding in the flow of communication. As B0, I should be the one to summarize the tally for Team B and pass the conversation to the next team leader. Let me correct that.\\n\\nA0:1, A1:1, A2:5,\\n\\nB0:1, B1:3, B2:4,\\n\\nC0:?, C1:?, C2:?\\n\\nWith the counts from B1 and B2, Team B's total tally is indeed 8 chocolates. Now, it's time to find out the tally from Team C. I'll pass this to the next team leader.\\n\\nNEXT: C0\", 'name': 'B0', 'role': 'user'}, {'content': \"As the team leader of Team C, it's my responsibility to gather the chocolate count from my team members. I have 3 chocolates.\\n\\nA0:1, A1:1, A2:5,\\n\\nB0:1, B1:3, B2:4,\\n\\nC0:3, C1:?, C2:?\\n\\nNow, I need to find out the chocolate counts from C1 and C2.\\n\\nNEXT: C1\", 'name': 'C0', 'role': 'user'}, {'content': 'I have 3 chocolates.\\n\\nA0:1, A1:1, A2:5,\\n\\nB0:1, B1:3, B2:4,\\n\\nC0:3, C1:3, C2:?\\n\\nNEXT: C2', 'name': 'C1', 'role': 'user'}, {'content': \"I have 2 chocolates. Now that I have the counts from C0 and C1, I can calculate our team's total.\\n\\nA0:1, A1:1, A2:5,\\n\\nB0:1, B1:3, B2:4,\\n\\nC0:3, C1:3, C2:2\\n\\nTeam C's total tally is 8 chocolates. Now that we have the tallies from all teams, we can sum them up.\\n\\nTeam A's total is 7 chocolates, Team B's total is 8 chocolates, and Team C's total is 8 chocolates. The grand total is 23 chocolates.\\n\\nTERMINATE\", 'name': 'C2', 'role': 'user'}], summary=\"I have 2 chocolates. Now that I have the counts from C0 and C1, I can calculate our team's total.\\n\\nA0:1, A1:1, A2:5,\\n\\nB0:1, B1:3, B2:4,\\n\\nC0:3, C1:3, C2:2\\n\\nTeam C's total tally is 8 chocolates. Now that we have the tallies from all teams, we can sum them up.\\n\\nTeam A's total is 7 chocolates, Team B's total is 8 chocolates, and Team C's total is 8 chocolates. The grand total is 23 chocolates.\\n\\n\", cost={'usage_including_cached_inference': {'total_cost': 0.5525399999999999, 'gpt-4': {'cost': 0.5525399999999999, 'prompt_tokens': 18174, 'completion_tokens': 122, 'total_tokens': 18296}}, 'usage_excluding_cached_inference': {'total_cost': 0.5525399999999999, 'gpt-4': {'cost': 0.5525399999999999, 'prompt_tokens': 18174, 'completion_tokens': 122, 'total_tokens': 18296}}}, human_input=[])"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "group_chat = GroupChat(\n",
    "    agents=agents,\n",
    "    messages=[],\n",
    "    max_round=20,\n",
    "    allowed_or_disallowed_speaker_transitions=speaker_transitions_dict,\n",
    "    speaker_transitions_type=\"allowed\",\n",
    ")\n",
    "\n",
    "\n",
    "# Create the manager\n",
    "manager = autogen.GroupChatManager(\n",
    "    groupchat=group_chat,\n",
    "    llm_config=config_list_gpt4,\n",
    "    code_execution_config=False,\n",
    "    is_termination_msg=is_termination_msg,\n",
    ")\n",
    "\n",
    "\n",
    "# Initiates the chat with Alice\n",
    "agents[0].initiate_chat(\n",
    "    manager,\n",
    "    message=\"\"\"\n",
    "                        There are 9 players in this game, split equally into Teams A, B, C. Therefore each team has 3 players, including the team leader.\n",
    "                        The task is to find out the sum of chocolate count from all nine players. I will now start with my team.\n",
    "                        NEXT: A1\"\"\",\n",
    ")"
   ]
  }
 ],
 "metadata": {
  "front_matter": {
   "description": "Explore the demonstration of the Finite State Machine implementation, which allows the user to input speaker transition constraints.",
   "tags": [
    "orchestration"
   ]
  },
  "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.10.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
