{ "cells": [ { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "\"Open" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "# Auto Generated Agent Chat: Group Chat\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 modified based on https://github.com/microsoft/FLAML/blob/4ea686af5c3e8ff24d9076a7a626c8b28ab5b1d7/notebook/autogen_multiagent_roleplay_chat.ipynb\n", "\n", "## Requirements\n", "\n", "AutoGen requires `Python>=3.8`. To run this notebook example, please install:\n", "```bash\n", "pip install pyautogen\n", "```" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "%%capture --no-stderr\n", "# %pip install pyautogen~=0.2.0b4" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "## Set your API Endpoint\n", "\n", "The [`config_list_from_json`](https://microsoft.github.io/autogen/docs/reference/oai/openai_utils#config_list_from_json) function loads a list of configurations from an environment variable or a json file." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/home/panoevj/anaconda3/envs/automemgpt/lib/python3.11/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n", " from .autonotebook import tqdm as notebook_tqdm\n" ] } ], "source": [ "import autogen\n", "import os\n", "\n", "config_list_gpt4 = [\n", " {\n", " \"model\": \"gpt-4\",\n", " \"api_key\": os.getenv(\"OPENAI_API_KEY\"),\n", " # \"api_key\": str(os.environ[\"OPENAI_API_KEY\"]),\n", " },\n", " {\n", " \"model\": \"gpt-4-32k\",\n", " \"api_key\": os.getenv(\"OPENAI_API_KEY\"),\n", " },\n", "]\n", "\n", "\n", "# config_list_gpt35 = autogen.config_list_from_json(\n", "# \"OAI_CONFIG_LIST\",\n", "# filter_dict={\n", "# \"model\": {\n", "# \"gpt-3.5-turbo\",\n", "# \"gpt-3.5-turbo-16k\",\n", "# \"gpt-3.5-turbo-0301\",\n", "# \"chatgpt-35-turbo-0301\",\n", "# \"gpt-35-turbo-v0301\",\n", "# },\n", "# },\n", "# )" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Custom Group Chat" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "import random\n", "from typing import List, Dict\n", "from autogen.agentchat.groupchat import GroupChat\n", "from autogen.agentchat.agent import Agent\n", "from autogen.agentchat.assistant_agent import AssistantAgent\n", "\n", "\n", "class CustomGroupChat(GroupChat):\n", " def __init__(self, agents, messages, max_round=10):\n", " super().__init__(agents, messages, max_round)\n", " self.previous_speaker = None # Keep track of the previous speaker\n", "\n", " def select_speaker(self, last_speaker: Agent, selector: AssistantAgent):\n", " # Check if last message suggests a next speaker or termination\n", " last_message = self.messages[-1] if self.messages else None\n", " if last_message:\n", " if \"NEXT:\" in last_message[\"content\"]:\n", " suggested_next = last_message[\"content\"].split(\"NEXT: \")[-1].strip()\n", " print(f\"Extracted suggested_next = {suggested_next}\")\n", " try:\n", " return self.agent_by_name(suggested_next)\n", " except ValueError:\n", " pass # If agent name is not valid, continue with normal selection\n", " elif \"TERMINATE\" in last_message[\"content\"]:\n", " try:\n", " return self.agent_by_name(\"User_proxy\")\n", " except ValueError:\n", " pass # If 'User_proxy' is not a valid name, continue with normal selection\n", "\n", " team_leader_names = [\n", " agent.name for agent in self.agents if agent.name.endswith(\"1\")\n", " ]\n", "\n", " if last_speaker.name in team_leader_names:\n", " team_letter = last_speaker.name[0]\n", " possible_next_speakers = [\n", " agent\n", " for agent in self.agents\n", " if (\n", " agent.name.startswith(team_letter)\n", " or agent.name in team_leader_names\n", " )\n", " and agent != last_speaker\n", " and agent != self.previous_speaker\n", " ]\n", " else:\n", " team_letter = last_speaker.name[0]\n", " possible_next_speakers = [\n", " agent\n", " for agent in self.agents\n", " if agent.name.startswith(team_letter)\n", " and agent != last_speaker\n", " and agent != self.previous_speaker\n", " ]\n", "\n", " self.previous_speaker = last_speaker\n", "\n", " if possible_next_speakers:\n", " next_speaker = random.choice(possible_next_speakers)\n", " return next_speaker\n", " else:\n", " return None" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "# Termination message detection\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" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "## Construct Agents" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "llm_config = {\"config_list\": config_list_gpt4}" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "agents_sales = [\n", " AssistantAgent(\n", " name=\"Sales1\",\n", " system_message=\"You are the team leader of the sales team, your team consists of Sales2 and Sales3. You can talk to the other team leaders: Marketing1, CustomerSupport1, ProductDevelopment1. Use 'NEXT: ' to suggest the next speaker.\",\n", " llm_config=llm_config,\n", " ),\n", " AssistantAgent(\n", " name=\"Sales2\",\n", " system_message=\"You are team member Sales2, you are responsible for communication with ClientA.\",\n", " llm_config=llm_config,\n", " ),\n", " AssistantAgent(\n", " name=\"Sales3\",\n", " system_message=\"You are team member Sales3, you are responsible for communication with ClientB.\",\n", " llm_config=llm_config,\n", " ),\n", "]\n", "\n", "agents_marketing = [\n", " AssistantAgent(\n", " name=\"Marketing1\",\n", " system_message=\"You are the team leader of the marketing team, your team consists of Marketing2 and Marketing3. You can talk to the other team leaders: Sales1, CustomerSupport1, ProductDevelopment1. Use 'NEXT: ' to suggest the next speaker.\",\n", " llm_config=llm_config,\n", " ),\n", " AssistantAgent(\n", " name=\"Marketing2\",\n", " system_message=\"You are team member Marketing2, you are responsible for generating non-digital marketing material.\",\n", " llm_config=llm_config,\n", " ),\n", " AssistantAgent(\n", " name=\"Marketing3\",\n", " system_message=\"You are team member Marketing3, you are responsible for digital marketing.\",\n", " llm_config=llm_config,\n", " ),\n", "]\n", "\n", "agents_product_development = [\n", " AssistantAgent(\n", " name=\"ProductDevelopment1\",\n", " system_message=\"You are the team leader of the product development team, your team consists of ProductDevelopment2 and ProductDevelopment3. You can talk to the other team leaders: Sales1, CustomerSupport1, ProductDevelopment1. Use 'NEXT: ' to suggest the next speaker.\",\n", " llm_config=llm_config,\n", " ),\n", " AssistantAgent(\n", " name=\"ProductDevelopment2\",\n", " system_message=\"You are team member ProductDevelopment2, you are responsible for the development of the product.\",\n", " llm_config=llm_config,\n", " ),\n", " AssistantAgent(\n", " name=\"ProductDevelopment3\",\n", " system_message=\"You are team member ProductDevelopment3, you are responsible for quality assurance.\",\n", " llm_config=llm_config,\n", " ),\n", "]\n", "\n", "agents_customer_support = [\n", " AssistantAgent(\n", " name=\"CustomerSupport1\",\n", " system_message=\"You are the team leader of the customer support team, your team consists of CustomerSupport2 and CustomerSupport3. You can talk to the other team leaders: Sales1, Marketing1, ProductDevelopment1. Use 'NEXT: ' to suggest the next speaker.\",\n", " llm_config=llm_config,\n", " ),\n", " AssistantAgent(\n", " name=\"CustomerSupport2\",\n", " system_message=\"You are team member of the customer support team, you are responsible for customer support for ClientA.\",\n", " llm_config=llm_config,\n", " ),\n", " AssistantAgent(\n", " name=\"CustomerSupport3\",\n", " system_message=\"You are team member of the customer support team, you are responsible for customer support for ClientB.\",\n", " llm_config=llm_config,\n", " ),\n", "]\n", "\n", "\n", "agents_managers = [\n", " AssistantAgent(\n", " name=\"Manager1\",\n", " system_message=\"You are the manager of the company, responsible of managing and coordinating the activities of all teams and departments within the company. You can talk to all team leaders: Sales1, Marketing1, CustomerSupport1, ProductDevelopment1. Use 'NEXT: ' to suggest the next speaker.\",\n", " llm_config=llm_config,\n", " ),\n", "]" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "# 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", ")" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "list_of_agents = (\n", " agents_sales + agents_marketing + agents_product_development + agents_managers\n", ")\n", "list_of_agents.append(user_proxy)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Start GroupChat" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "# Create CustomGroupChat\n", "group_chat = CustomGroupChat(\n", " agents=list_of_agents, # Include all agents\n", " messages=[\n", " 'Everyone cooperate and and solve the various incoming tasks and client requests. Team Sales has Sales1, Sales2, Sales3. Team Marketing has Marketing1, Marketing2, Marketing3. Team ProductDevelopment has ProductDevelopment1, ProductDevelopment2, ProductDevelopment3. Team CustomerSupport has CustomerSupport1, CustomerSupport2, CustomerSupport3. Team Managers has Manager1. Only members of the same team can talk to one another. Only team leaders (names ending with 1) can talk amongst themselves. You must use \"NEXT: Sales1\" to suggest talking to Sales1 for example; You can suggest only one person, you cannot suggest yourself or the previous speaker; You can also not suggest anyone.'\n", " ],\n", " max_round=20,\n", ")" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "# Create the manager\n", "manager = autogen.GroupChatManager(groupchat=group_chat, llm_config=llm_config)" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\u001b[33mManager1\u001b[0m (to chat_manager):\n", "\n", "ClientA, who recently purchased an e-bike from our company, has reported that after only a few weeks of regular use, the e-bike's battery began to malfunction. Please investigate and resolve the issue.\n", "\n", "--------------------------------------------------------------------------------\n", "\u001b[33mMarketing2\u001b[0m (to chat_manager):\n", "\n", "As Marketing2, I handle non-digital marketing materials such as brochures, flyers, and print ads. The issue you've described involves customer service and technical assistance. A team member from Customer Support or Technical Service team is better suited to handle this kind of problem. However, I'll pass this information to the relevant department.\n", "\n", "--------------------------------------------------------------------------------\n", "\u001b[33mMarketing1\u001b[0m (to chat_manager):\n", "\n", "NEXT: CustomerSupport1\n", "\n", "--------------------------------------------------------------------------------\n", "Extracted suggested_next = CustomerSupport1\n", "\u001b[33mProductDevelopment1\u001b[0m (to chat_manager):\n", "\n", "Hi CustomerSupport1, could you please provide an update on the situation with ClientA's e-bike battery malfunction? Was this issue reported in our support system?\n", "\n", "--------------------------------------------------------------------------------\n", "\u001b[33mProductDevelopment2\u001b[0m (to chat_manager):\n", "\n", "As ProductDevelopment2, I don't directly interact with clients or our support system. However, I can certainly look into the issue from the product's side and provide technological solutions. I'll begin by investigating similar cases in the history of our e-bikes, checking if there are any known issues or recalls on this batch of e-bike batteries, and inspecting the maintenance and charging instructions provided to the customer. This will provide a clearer understanding of why the battery began to malfunction after a few weeks, and what can be done to rectify the situation.\n", "\n", "--------------------------------------------------------------------------------\n", "\u001b[33mProductDevelopment3\u001b[0m (to chat_manager):\n", "\n", "As ProductDevelopment3, I can assist by ensuring that the quality checks are carried out meticulously. Once we get the bike, I'll examine the circumstances under which the battery started to malfunction. I'll check the battery's quality control records to understand if there was any anomaly during our in-house testing. If there's a single instance of malfunction, it could be a one-off problem. However, if multiple customers are facing the same problem, it could suggest a batch flaw or even a broader product design issue. I'll keep everyone posted.\n", "\n", "--------------------------------------------------------------------------------\n", "\u001b[33mProductDevelopment1\u001b[0m (to chat_manager):\n", "\n", "NEXT: ProductDevelopment1\n", "\n", "--------------------------------------------------------------------------------\n", "Extracted suggested_next = ProductDevelopment1\n", "\u001b[33mProductDevelopment1\u001b[0m (to chat_manager):\n", "\n", "As the team leader of Product Development, I need your insights on this issue. What are the potential causes that could lead to the malfunctioning of the e-bike's battery?\n", "\n", "--------------------------------------------------------------------------------\n", "\u001b[33mManager1\u001b[0m (to chat_manager):\n", "\n", "Apologies for the confusion. As an Artificial Intelligent agent, my functionality is limited to assisting with tasks and I don't have the capability to actually run this meeting or physically examine the product. Please continue your conversation about the battery malfunction with ProductDevelopment1.\n", "\n", "--------------------------------------------------------------------------------\n", "\u001b[33mMarketing1\u001b[0m (to chat_manager):\n", "\n", "NEXT: ProductDevelopment1\n", "\n", "--------------------------------------------------------------------------------\n", "Extracted suggested_next = ProductDevelopment1\n", "\u001b[33mProductDevelopment1\u001b[0m (to chat_manager):\n", "\n", "As ProductDevelopment1, I'd suggest we begin a thorough analysis of the battery failure issue reported by ClientA. This should include a technical investigation into possible causes of the malfunction, as well as a review of any similar incidents in our logs. Once we have more detailed information, we'll be better able to come up with a viable solution. I'll get our team started on this straightaway.\n", "\n", "--------------------------------------------------------------------------------\n", "\u001b[33mProductDevelopment3\u001b[0m (to chat_manager):\n", "\n", "As ProductDevelopment3, I support ProductDevelopment1's suggestion that we perform a thorough analysis of the situation. To summarize our steps:\n", "\n", "1. Investigate similar issues in the past to identify any patterns or repeated occurrences of battery malfunction.\n", "2. Examine the specific battery from ClientA's e-bike, if possible. This could involve arranging a return from the client or requesting detailed photos or descriptions of the issue.\n", "3. Review the quality control records for the batch that this battery came from.\n", "4. If the problem looks to be wider spread, consider an inspection of other batteries from the same batch.\n", "5. While investigating, we should also review our instructions provided to the users to ensure that any guidance about battery maintenance and charging are clear and accurate.\n", "\n", "Let's start right away so we can resolve the issue promptly and assure our clients about the quality and reliability of our e-bikes.\n", "\n", "--------------------------------------------------------------------------------\n", "\u001b[33mProductDevelopment2\u001b[0m (to chat_manager):\n", "\n", "As ProductDevelopment2, I fully agree with the proposed steps. In addition, I can ensure that in the future any similar issues are promptly addressed by incorporating a more comprehensive battery health inspection in our regular quality checks. We should also consider bringing this up in our next meeting to discuss possible improvements in our technical documentation, both in terms of battery maintenance and general use of the e-bike. The end goal should always be to minimize any inconvenience for the customers, enhancing their experience and satisfaction with our products.\n", "\n", "\n", "--------------------------------------------------------------------------------\n", "\u001b[33mProductDevelopment1\u001b[0m (to chat_manager):\n", "\n", "NEXT: Sales1\n", "\n", "--------------------------------------------------------------------------------\n", "Extracted suggested_next = Sales1\n", "\u001b[33mSales1\u001b[0m (to chat_manager):\n", "\n", "Apologies for the confusion. As an Artificial Intelligent assistant, I only follow the roles determined at the beginning of the interactions. There is no Sales1 introduced at the start, therefore I can't assign the next turn to them. The turn can be assigned to the sales team members Sales2 or Sales3, or to the other team leaders Marketing1, CustomerSupport1, or ProductDevelopment1. Please choose one. \n", "\n", "--------------------------------------------------------------------------------\n", "\u001b[33mSales2\u001b[0m (to chat_manager):\n", "\n", "NEXT: Sales2\n", "\n", "--------------------------------------------------------------------------------\n", "Extracted suggested_next = Sales2\n", "\u001b[33mSales2\u001b[0m (to chat_manager):\n", "\n", "Dear ClientA,\n", "\n", "I'm Sales2 from the team you've bought your e-bike from. I have been made aware of the issue you are experiencing with your e-bike's battery. I apologize for any inconvenience this might have caused. Our product development team is investigating, and we assure you that we are prioritizing resolution of this issue.\n", "\n", "We will keep you updated on our progress. In the meantime, if you need any further assistance, feel free to reach out to me. Thank you for your patience and understanding.\n", "\n", "Best,\n", "Sales2\n", "\n", "--------------------------------------------------------------------------------\n", "\u001b[33mSales3\u001b[0m (to chat_manager):\n", "\n", "NEXT: CustomerSupport1\n", "\n", "--------------------------------------------------------------------------------\n", "Extracted suggested_next = CustomerSupport1\n", "\u001b[33mSales1\u001b[0m (to chat_manager):\n", "\n", "As a Customer Support representative, my priority would be to ensure that ClientA feels supported during this process of resolving the issue with their e-bike's battery.\n", "\n", "I would suggest we send them a replacement battery immediately if we have any spare or substitute models available. We could also potentially offer them a rental e-bike while their current one is being repaired. Moreover, we should ensure they know they can reach out to us if they have any features or concerns they need assistance with while we're investigating this issue.\n", "\n", "Let's do all we can to turn their current inconvenience into an experience of great customer service. \n", "\n", "NEXT: CustomerSupport1\n", "\n", "--------------------------------------------------------------------------------\n", "Extracted suggested_next = CustomerSupport1\n", "\u001b[33mSales2\u001b[0m (to chat_manager):\n", "\n", "Apologies for the confusion. As an Artificial Intelligent assistant, I only follow the roles determined at the beginning of the interactions. There is no CustomerSupport1 introduced at the start, so I can't assign the next turn to them. The turn can be assigned to me, Sales2, and the team leaders: Marketing1, ProductDevelopment1. Please choose one.\n", "\n", "--------------------------------------------------------------------------------\n" ] } ], "source": [ "# Initiates the chat with the manager\n", "agents_managers[0].initiate_chat(\n", " manager,\n", " message=\"ClientA, who recently purchased an e-bike from our company, has reported that after only a few weeks of regular use, the e-bike's battery began to malfunction. Please investigate and resolve the issue.\",\n", ")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "flaml", "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.11.3" }, "orig_nbformat": 4 }, "nbformat": 4, "nbformat_minor": 2 }