diff --git a/notebook/agentchat_swarm.ipynb b/notebook/agentchat_swarm.ipynb index c4635ab881..8e891bb826 100644 --- a/notebook/agentchat_swarm.ipynb +++ b/notebook/agentchat_swarm.ipynb @@ -9,7 +9,9 @@ "\n", "AG2 offers conversable agents, powered by LLMs, tools or a human, that can perform tasks collectively via an automated chat. Recently, OpenAI released a [Swarm](https://github.com/openai/swarm) framework that focuses on making agent coordination and execution lightweight. \n", "\n", - "Now, AG2 offers a simple programming interface to build and orchestrate a swarm of agents. Please check the [Documentation](https://ag2ai.github.io/ag2/docs/topics/swarm) and [Blog](https://ag2ai.github.io/ag2/blog/2024/11/17/Swarm) for more details.\n", + "In AG2 we offer a simple programming interface to build and orchestrate a swarm of agents. Please check the [Documentation](https://ag2ai.github.io/ag2/docs/topics/swarm) and [Blog](https://ag2ai.github.io/ag2/blog/2024/11/17/Swarm) for more details.\n", + "\n", + "After learning the fundamentals of AG2's swarm in this notebook, check out [this notebook](https://ag2ai.github.io/ag2/docs/notebooks/agentchat_swarm) where we take on some more advanced techniques that provide greater control and predicability for your swarms.\n", "\n", "In this notebook, we implement OpenAI's [airline customer service example](https://github.com/openai/swarm/tree/main/examples/airline) in AG2." ] diff --git a/notebook/agentchat_swarm_enhanced.ipynb b/notebook/agentchat_swarm_enhanced.ipynb new file mode 100644 index 0000000000..2a368f621a --- /dev/null +++ b/notebook/agentchat_swarm_enhanced.ipynb @@ -0,0 +1,954 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Enhanced Swarm Orchestration with AG2 \n", + "\n", + "AG2's swarm orchestration provides a flexible and powerful method of managing a conversation with multiple agents, tools, and transitions.\n", + "\n", + "In this notebook, we look at more advanced features of the swarm orchestration.\n", + "\n", + "If you are new to swarm, check out [this notebook](https://ag2ai.github.io/ag2/docs/notebooks/agentchat_swarm), where we introduce the core features of swarms including global context variables, hand offs, and initiating a swarm chat.\n", + "\n", + "In this notebook we're going to demonstrate these features AG2's swarm orchestration:\n", + "\n", + "- Updating an agent's state\n", + "- Conditional handoffs\n", + "- Nested chats" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "````{=mdx}\n", + ":::info Requirements\n", + "Install `ag2`:\n", + "```bash\n", + "pip install ag2\n", + "```\n", + "\n", + "For more information, please refer to the [installation guide](/docs/installation/).\n", + ":::\n", + "````" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Set your API Endpoint\n", + "\n", + "The [`config_list_from_json`](https://ag2ai.github.io/ag2/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": 1, + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "/usr/local/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", + "\n", + "config_list = autogen.config_list_from_json(\n", + " \"OAI_CONFIG_LIST\",\n", + " filter_dict={\n", + " \"model\": [\"gpt-4o\"],\n", + " },\n", + ")\n", + "\n", + "llm_config = {\n", + " \"cache_seed\": 42, # change the cache_seed for different trials\n", + " \"temperature\": 1,\n", + " \"config_list\": config_list,\n", + " \"timeout\": 120,\n", + "}" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Demonstration\n", + "\n", + "We're creating this customer service workflow for an e-commerce platform. Customers can ask about the status of their orders, but they must be authenticated to do so.\n", + "\n", + "![Swarm Enhanced Demonstration](swarm_enhanced_01.png)\n", + "\n", + "Key aspects of this swarm are:\n", + "\n", + "1. System messages are customised, incorporating the context of the workflow\n", + "2. Handoffs are conditional, only being available when they are relevant\n", + "3. A nested chat handles the order retrieval and summarisation" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "from typing import Any, Dict, List\n", + "\n", + "from autogen import (\n", + " AFTER_WORK,\n", + " ON_CONDITION,\n", + " UPDATE_SYSTEM_MESSAGE,\n", + " AfterWorkOption,\n", + " ConversableAgent,\n", + " SwarmAgent,\n", + " SwarmResult,\n", + " UserProxyAgent,\n", + " initiate_swarm_chat,\n", + ")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Context" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [], + "source": [ + "workflow_context = {\n", + " # customer details\n", + " \"customer_name\": None,\n", + " \"logged_in_username\": None,\n", + " # workflow status\n", + " \"logged_in\": False,\n", + " \"requires_login\": True,\n", + " # order enquiry details\n", + " \"has_order_id\": False,\n", + " \"order_id\": None,\n", + "}" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Databases" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [], + "source": [ + "# Databases\n", + "\n", + "USER_DATABASE = {\n", + " \"mark\": {\n", + " \"full_name\": \"Mark Sze\",\n", + " },\n", + " \"kevin\": {\n", + " \"full_name\": \"Yiran Wu\",\n", + " },\n", + "}\n", + "\n", + "ORDER_DATABASE = {\n", + " \"TR13845\": {\n", + " \"user\": \"mark\",\n", + " \"order_number\": \"TR13845\",\n", + " \"status\": \"shipped\", # order status: order_received, shipped, delivered, return_started, returned\n", + " \"return_status\": \"N/A\", # return status: N/A, return_started, return_shipped, return_delivered, refund_issued\n", + " \"product\": \"matress\",\n", + " \"link\": \"https://www.example.com/TR13845\",\n", + " \"shipping_address\": \"123 Main St, State College, PA 12345\",\n", + " },\n", + " \"TR14234\": {\n", + " \"user\": \"kevin\",\n", + " \"order_number\": \"TR14234\",\n", + " \"status\": \"delivered\",\n", + " \"return_status\": \"N/A\",\n", + " \"product\": \"pillow\",\n", + " \"link\": \"https://www.example.com/TR14234\",\n", + " \"shipping_address\": \"123 Main St, State College, PA 12345\",\n", + " },\n", + " \"TR29384\": {\n", + " \"user\": \"mark\",\n", + " \"order_number\": \"TR29384\",\n", + " \"status\": \"delivered\",\n", + " \"return_status\": \"N/A\",\n", + " \"product\": \"bed frame\",\n", + " \"link\": \"https://www.example.com/TR29384\",\n", + " \"shipping_address\": \"123 Main St, State College, PA 12345\",\n", + " },\n", + "}" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Agent's Functions" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [], + "source": [ + "# ORDER FUNCTIONS\n", + "def check_order_id(order_id: str, context_variables: dict) -> SwarmResult:\n", + " \"\"\"Check if the order ID is valid\"\"\"\n", + "\n", + " # Restricts order to checking to the logged in user\n", + " if (\n", + " context_variables[\"logged_in_username\"]\n", + " and order_id in ORDER_DATABASE\n", + " and ORDER_DATABASE[order_id][\"user\"] == context_variables[\"logged_in_username\"]\n", + " ):\n", + " return SwarmResult(\n", + " context_variables=context_variables, values=f\"Order ID {order_id} is valid.\", agent=order_triage_agent\n", + " )\n", + " else:\n", + " return SwarmResult(\n", + " context_variables=context_variables,\n", + " values=f\"Order ID {order_id} is invalid. Please ask for the correct order ID.\",\n", + " agent=order_triage_agent,\n", + " )\n", + "\n", + "\n", + "def record_order_id(order_id: str, context_variables: dict) -> SwarmResult:\n", + " \"\"\"Record the order ID in the workflow context\"\"\"\n", + "\n", + " if order_id not in ORDER_DATABASE:\n", + " return SwarmResult(\n", + " context_variables=context_variables,\n", + " values=f\"Order ID {order_id} not found. Please ask for the correct order ID.\",\n", + " agent=order_triage_agent,\n", + " )\n", + "\n", + " context_variables[\"order_id\"] = order_id\n", + " context_variables[\"has_order_id\"] = True\n", + " return SwarmResult(\n", + " context_variables=context_variables, values=f\"Order ID Recorded: {order_id}\", agent=order_mgmt_agent\n", + " )\n", + "\n", + "\n", + "# AUTHENTICATION FUNCTIONS\n", + "def login_customer_by_username(username: str, context_variables: dict) -> SwarmResult:\n", + " \"\"\"Get and log the customer in by their username\"\"\"\n", + " if username in USER_DATABASE:\n", + " context_variables[\"customer_name\"] = USER_DATABASE[username][\"full_name\"]\n", + " context_variables[\"logged_in_username\"] = username\n", + " context_variables[\"logged_in\"] = True\n", + " context_variables[\"requires_login\"] = False\n", + " return SwarmResult(\n", + " context_variables=context_variables,\n", + " values=f\"Welcome back our customer, {context_variables['customer_name']}! Please continue helping them.\",\n", + " agent=order_triage_agent,\n", + " )\n", + " else:\n", + " return SwarmResult(\n", + " context_variables=context_variables,\n", + " values=f\"User {username} not found. Please ask for the correct username.\",\n", + " agent=authentication_agent,\n", + " )" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Agents" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [], + "source": [ + "# AGENTS\n", + "\n", + "# Human customer\n", + "user = UserProxyAgent(\n", + " name=\"customer\",\n", + " code_execution_config=False,\n", + ")\n", + "\n", + "order_triage_prompt = \"\"\"You are an order triage agent, working with a customer and a group of agents to provide support for your e-commerce platform.\n", + "\n", + "An agent needs to be logged in to be able to access their order. The authentication_agent will work with the customer to verify their identity, transfer to them to start with.\n", + "The order_mgmt_agent will manage all order related tasks, such as tracking orders, managing orders, etc. Be sure to check the order as one step. Then if it's valid you can record it in the context.\n", + "\n", + "Ask the customer for further information when necessary.\n", + "\n", + "The current status of this workflow is:\n", + "Customer name: {customer_name}\n", + "Logged in: {logged_in}\n", + "Enquiring for Order ID: {order_id}\n", + "\"\"\"\n", + "\n", + "order_triage_agent = SwarmAgent(\n", + " name=\"order_triage_agent\",\n", + " update_agent_state_before_reply=[\n", + " UPDATE_SYSTEM_MESSAGE(order_triage_prompt),\n", + " ],\n", + " functions=[check_order_id, record_order_id],\n", + " llm_config=llm_config,\n", + ")\n", + "\n", + "authentication_prompt = \"You are an authentication agent that verifies the identity of the customer.\"\n", + "\n", + "authentication_agent = SwarmAgent(\n", + " name=\"authentication_agent\",\n", + " system_message=authentication_prompt,\n", + " functions=[login_customer_by_username],\n", + " llm_config=llm_config,\n", + ")\n", + "\n", + "order_management_prompt = \"\"\"You are an order management agent that manages inquiries related to e-commerce orders.\n", + "\n", + "The order must be logged in to access their order.\n", + "\n", + "Use your available tools to get the status of the details from the customer. Ask the customer questions as needed.\n", + "\n", + "The current status of this workflow is:\n", + "Customer name: {customer_name}\n", + "Logged in: {logged_in}\n", + "Enquiring for Order ID: {order_id}\n", + "\"\"\"\n", + "\n", + "order_mgmt_agent = SwarmAgent(\n", + " name=\"order_mgmt_agent\",\n", + " update_agent_state_before_reply=[\n", + " UPDATE_SYSTEM_MESSAGE(order_management_prompt),\n", + " ],\n", + " functions=[check_order_id, record_order_id],\n", + " llm_config=llm_config,\n", + ")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Nested Chats" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [], + "source": [ + "# NESTED CHAT - Delivery Status\n", + "order_retrieval_agent = ConversableAgent(\n", + " name=\"order_retrieval_agent\",\n", + " system_message=\"You are an order retrieval agent that gets details about an order.\",\n", + " llm_config=llm_config,\n", + ")\n", + "\n", + "order_summariser_agent = ConversableAgent(\n", + " name=\"order_summariser_agent\",\n", + " system_message=\"You are an order summariser agent that provides a summary of the order details.\",\n", + " llm_config=llm_config,\n", + ")\n", + "\n", + "\n", + "def extract_order_summary(recipient: ConversableAgent, messages, sender: ConversableAgent, config):\n", + " \"\"\"Extracts the order summary based on the OrderID in the context variables\"\"\"\n", + " order_id = sender.get_context(\"order_id\")\n", + " if order_id in ORDER_DATABASE:\n", + " order = ORDER_DATABASE[order_id]\n", + " return f\"Order {order['order_number']} for {order['product']} is currently {order['status']}. The shipping address is {order['shipping_address']}.\"\n", + " else:\n", + " return f\"Order {order_id} not found.\"\n", + "\n", + "\n", + "nested_chat_one = {\n", + " \"carryover_config\": {\"summary_method\": \"last_msg\"},\n", + " \"recipient\": order_retrieval_agent,\n", + " \"message\": extract_order_summary, # \"Retrieve the status details of the order using the order id\",\n", + " \"max_turns\": 1,\n", + "}\n", + "\n", + "nested_chat_two = {\n", + " \"recipient\": order_summariser_agent,\n", + " \"message\": \"Summarise the order details provided in a tabulated, text-based, order sheet format\",\n", + " \"max_turns\": 1,\n", + " \"summary_method\": \"last_msg\",\n", + "}\n", + "\n", + "chat_queue = [nested_chat_one, nested_chat_two]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Handoffs (ON_CONDITIONS and AFTER_WORKS)" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [], + "source": [ + "# HANDOFFS\n", + "order_triage_agent.register_hand_off(\n", + " [\n", + " ON_CONDITION(\n", + " target=authentication_agent,\n", + " condition=\"The customer is not logged in, authenticate the customer.\",\n", + " available=\"requires_login\",\n", + " ),\n", + " ON_CONDITION(\n", + " target=order_mgmt_agent,\n", + " condition=\"The customer is logged in, continue with the order triage.\",\n", + " available=\"logged_in\",\n", + " ),\n", + " AFTER_WORK(AfterWorkOption.REVERT_TO_USER),\n", + " ]\n", + ")\n", + "\n", + "authentication_agent.register_hand_off(\n", + " [\n", + " ON_CONDITION(\n", + " target=order_triage_agent,\n", + " condition=\"The customer is logged in, continue with the order triage.\",\n", + " available=\"logged_in\",\n", + " ),\n", + " AFTER_WORK(AfterWorkOption.REVERT_TO_USER),\n", + " ]\n", + ")\n", + "\n", + "\n", + "def has_order_in_context(agent: SwarmAgent, messages: List[Dict[str, Any]]) -> bool:\n", + " return agent.get_context(\"has_order_id\")\n", + "\n", + "\n", + "order_mgmt_agent.register_hand_off(\n", + " [\n", + " ON_CONDITION(\n", + " target={\n", + " \"chat_queue\": chat_queue,\n", + " },\n", + " condition=\"Retrieve the status of the order\",\n", + " available=has_order_in_context,\n", + " ),\n", + " ON_CONDITION(\n", + " target=authentication_agent,\n", + " condition=\"The customer is not logged in, authenticate the customer.\",\n", + " available=\"requires_login\",\n", + " ),\n", + " ON_CONDITION(target=order_triage_agent, condition=\"The customer has no more enquiries about this order.\"),\n", + " AFTER_WORK(AfterWorkOption.REVERT_TO_USER),\n", + " ]\n", + ")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Let's go!" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\u001b[33mcustomer\u001b[0m (to chat_manager):\n", + "\n", + "Can you help me with my order.\n", + "\n", + "--------------------------------------------------------------------------------\n", + "\u001b[32m\n", + "Next speaker: order_triage_agent\n", + "\u001b[0m\n", + "\u001b[33morder_triage_agent\u001b[0m (to chat_manager):\n", + "\n", + "\u001b[32m***** Suggested tool call (call_RhIdaMav5FoXxvXiYhyDoivV): transfer_order_triage_agent_to_authentication_agent *****\u001b[0m\n", + "Arguments: \n", + "{}\n", + "\u001b[32m********************************************************************************************************************\u001b[0m\n", + "\n", + "--------------------------------------------------------------------------------\n", + "\u001b[32m\n", + "Next speaker: Tool_Execution\n", + "\u001b[0m\n", + "\u001b[35m\n", + ">>>>>>>> EXECUTING FUNCTION transfer_order_triage_agent_to_authentication_agent...\u001b[0m\n", + "\u001b[33mTool_Execution\u001b[0m (to chat_manager):\n", + "\n", + "\u001b[32m***** Response from calling tool (call_RhIdaMav5FoXxvXiYhyDoivV) *****\u001b[0m\n", + "SwarmAgent --> authentication_agent\n", + "\u001b[32m**********************************************************************\u001b[0m\n", + "\n", + "--------------------------------------------------------------------------------\n", + "\u001b[32m\n", + "Next speaker: authentication_agent\n", + "\u001b[0m\n", + "\u001b[33mauthentication_agent\u001b[0m (to chat_manager):\n", + "\n", + "I can assist you with your order, but first, I'll need to verify your identity. Please provide your username.\n", + "\n", + "--------------------------------------------------------------------------------\n", + "\u001b[32m\n", + "Next speaker: customer\n", + "\u001b[0m\n", + "\u001b[33mcustomer\u001b[0m (to chat_manager):\n", + "\n", + "barry\n", + "\n", + "--------------------------------------------------------------------------------\n", + "\u001b[32m\n", + "Next speaker: authentication_agent\n", + "\u001b[0m\n", + "\u001b[33mauthentication_agent\u001b[0m (to chat_manager):\n", + "\n", + "\u001b[32m***** Suggested tool call (call_gEx5FZ86W62p1vXCVNAkue7t): login_customer_by_username *****\u001b[0m\n", + "Arguments: \n", + "{\"username\":\"barry\"}\n", + "\u001b[32m*******************************************************************************************\u001b[0m\n", + "\n", + "--------------------------------------------------------------------------------\n", + "\u001b[32m\n", + "Next speaker: Tool_Execution\n", + "\u001b[0m\n", + "\u001b[35m\n", + ">>>>>>>> EXECUTING FUNCTION login_customer_by_username...\u001b[0m\n", + "\u001b[33mTool_Execution\u001b[0m (to chat_manager):\n", + "\n", + "\u001b[32m***** Response from calling tool (call_gEx5FZ86W62p1vXCVNAkue7t) *****\u001b[0m\n", + "User barry not found. Please ask for the correct username.\n", + "\u001b[32m**********************************************************************\u001b[0m\n", + "\n", + "--------------------------------------------------------------------------------\n", + "\u001b[32m\n", + "Next speaker: authentication_agent\n", + "\u001b[0m\n", + "\u001b[33mauthentication_agent\u001b[0m (to chat_manager):\n", + "\n", + "It seems that there is no account associated with the username \"barry.\" Could you please double-check and provide the correct username?\n", + "\n", + "--------------------------------------------------------------------------------\n", + "\u001b[32m\n", + "Next speaker: customer\n", + "\u001b[0m\n", + "\u001b[33mcustomer\u001b[0m (to chat_manager):\n", + "\n", + "mark\n", + "\n", + "--------------------------------------------------------------------------------\n", + "\u001b[32m\n", + "Next speaker: authentication_agent\n", + "\u001b[0m\n", + "\u001b[33mauthentication_agent\u001b[0m (to chat_manager):\n", + "\n", + "\u001b[32m***** Suggested tool call (call_XmbzzNw7PsYFYsTSVKoylATA): login_customer_by_username *****\u001b[0m\n", + "Arguments: \n", + "{\"username\":\"mark\"}\n", + "\u001b[32m*******************************************************************************************\u001b[0m\n", + "\n", + "--------------------------------------------------------------------------------\n", + "\u001b[32m\n", + "Next speaker: Tool_Execution\n", + "\u001b[0m\n", + "\u001b[35m\n", + ">>>>>>>> EXECUTING FUNCTION login_customer_by_username...\u001b[0m\n", + "\u001b[33mTool_Execution\u001b[0m (to chat_manager):\n", + "\n", + "\u001b[32m***** Response from calling tool (call_XmbzzNw7PsYFYsTSVKoylATA) *****\u001b[0m\n", + "Welcome back our customer, Mark Sze! Please continue helping them.\n", + "\u001b[32m**********************************************************************\u001b[0m\n", + "\n", + "--------------------------------------------------------------------------------\n", + "\u001b[32m\n", + "Next speaker: order_triage_agent\n", + "\u001b[0m\n", + "\u001b[33morder_triage_agent\u001b[0m (to chat_manager):\n", + "\n", + "\u001b[32m***** Suggested tool call (call_mXHJHDzVPTXWDhll0UH7w3QI): transfer_order_triage_agent_to_order_mgmt_agent *****\u001b[0m\n", + "Arguments: \n", + "{}\n", + "\u001b[32m****************************************************************************************************************\u001b[0m\n", + "\n", + "--------------------------------------------------------------------------------\n", + "\u001b[32m\n", + "Next speaker: Tool_Execution\n", + "\u001b[0m\n", + "\u001b[35m\n", + ">>>>>>>> EXECUTING FUNCTION transfer_order_triage_agent_to_order_mgmt_agent...\u001b[0m\n", + "\u001b[33mTool_Execution\u001b[0m (to chat_manager):\n", + "\n", + "\u001b[32m***** Response from calling tool (call_mXHJHDzVPTXWDhll0UH7w3QI) *****\u001b[0m\n", + "SwarmAgent --> order_mgmt_agent\n", + "\u001b[32m**********************************************************************\u001b[0m\n", + "\n", + "--------------------------------------------------------------------------------\n", + "\u001b[32m\n", + "Next speaker: order_mgmt_agent\n", + "\u001b[0m\n", + "\u001b[33morder_mgmt_agent\u001b[0m (to chat_manager):\n", + "\n", + "Sure, Mark! Could you please provide me with the Order ID you are enquiring about?\n", + "\n", + "--------------------------------------------------------------------------------\n", + "\u001b[32m\n", + "Next speaker: customer\n", + "\u001b[0m\n", + "\u001b[33mcustomer\u001b[0m (to chat_manager):\n", + "\n", + "TR14234\n", + "\n", + "--------------------------------------------------------------------------------\n", + "\u001b[32m\n", + "Next speaker: order_mgmt_agent\n", + "\u001b[0m\n", + "\u001b[33morder_mgmt_agent\u001b[0m (to chat_manager):\n", + "\n", + "\u001b[32m***** Suggested tool call (call_UMS0fVQEAW9Hkqo3paa6ZDp5): check_order_id *****\u001b[0m\n", + "Arguments: \n", + "{\"order_id\": \"TR14234\"}\n", + "\u001b[32m*******************************************************************************\u001b[0m\n", + "\u001b[32m***** Suggested tool call (call_ERAQ8vgnCagMuvCthkZ6E5l7): record_order_id *****\u001b[0m\n", + "Arguments: \n", + "{\"order_id\": \"TR14234\"}\n", + "\u001b[32m********************************************************************************\u001b[0m\n", + "\n", + "--------------------------------------------------------------------------------\n", + "\u001b[32m\n", + "Next speaker: Tool_Execution\n", + "\u001b[0m\n", + "\u001b[35m\n", + ">>>>>>>> EXECUTING FUNCTION check_order_id...\u001b[0m\n", + "\u001b[35m\n", + ">>>>>>>> EXECUTING FUNCTION record_order_id...\u001b[0m\n", + "\u001b[33mTool_Execution\u001b[0m (to chat_manager):\n", + "\n", + "\u001b[32m***** Response from calling tool (call_UMS0fVQEAW9Hkqo3paa6ZDp5) *****\u001b[0m\n", + "Order ID TR14234 is invalid. Please ask for the correct order ID.\n", + "\u001b[32m**********************************************************************\u001b[0m\n", + "\n", + "--------------------------------------------------------------------------------\n", + "\u001b[32m***** Response from calling tool (call_ERAQ8vgnCagMuvCthkZ6E5l7) *****\u001b[0m\n", + "Order ID Recorded: TR14234\n", + "\u001b[32m**********************************************************************\u001b[0m\n", + "\n", + "--------------------------------------------------------------------------------\n", + "\u001b[32m\n", + "Next speaker: order_mgmt_agent\n", + "\u001b[0m\n", + "\u001b[33morder_mgmt_agent\u001b[0m (to chat_manager):\n", + "\n", + "It seems that the Order ID \"TR14234\" is invalid. Could you please double-check and provide the correct Order ID?\n", + "\n", + "--------------------------------------------------------------------------------\n", + "\u001b[32m\n", + "Next speaker: customer\n", + "\u001b[0m\n", + "\u001b[33mcustomer\u001b[0m (to chat_manager):\n", + "\n", + "TR14234\n", + "\n", + "--------------------------------------------------------------------------------\n", + "\u001b[32m\n", + "Next speaker: order_mgmt_agent\n", + "\u001b[0m\n", + "\u001b[33morder_mgmt_agent\u001b[0m (to chat_manager):\n", + "\n", + "\u001b[32m***** Suggested tool call (call_DqbSSNveiHpMydYE7pOyfkaP): check_order_id *****\u001b[0m\n", + "Arguments: \n", + "{\"order_id\":\"TR14234\"}\n", + "\u001b[32m*******************************************************************************\u001b[0m\n", + "\n", + "--------------------------------------------------------------------------------\n", + "\u001b[32m\n", + "Next speaker: Tool_Execution\n", + "\u001b[0m\n", + "\u001b[35m\n", + ">>>>>>>> EXECUTING FUNCTION check_order_id...\u001b[0m\n", + "\u001b[33mTool_Execution\u001b[0m (to chat_manager):\n", + "\n", + "\u001b[32m***** Response from calling tool (call_DqbSSNveiHpMydYE7pOyfkaP) *****\u001b[0m\n", + "Order ID TR14234 is invalid. Please ask for the correct order ID.\n", + "\u001b[32m**********************************************************************\u001b[0m\n", + "\n", + "--------------------------------------------------------------------------------\n", + "\u001b[32m\n", + "Next speaker: order_triage_agent\n", + "\u001b[0m\n", + "\u001b[33morder_triage_agent\u001b[0m (to chat_manager):\n", + "\n", + "The Order ID \"TR14234\" still appears to be invalid. Could you please verify and provide the correct Order ID or any additional information that might help us find your order?\n", + "\n", + "--------------------------------------------------------------------------------\n", + "\u001b[32m\n", + "Next speaker: customer\n", + "\u001b[0m\n", + "\u001b[33mcustomer\u001b[0m (to chat_manager):\n", + "\n", + "TR13845\n", + "\n", + "--------------------------------------------------------------------------------\n", + "\u001b[32m\n", + "Next speaker: order_triage_agent\n", + "\u001b[0m\n", + "\u001b[33morder_triage_agent\u001b[0m (to chat_manager):\n", + "\n", + "\u001b[32m***** Suggested tool call (call_miEIpNwDd1FFGLenwUB3oPMq): check_order_id *****\u001b[0m\n", + "Arguments: \n", + "{\"order_id\": \"TR13845\"}\n", + "\u001b[32m*******************************************************************************\u001b[0m\n", + "\u001b[32m***** Suggested tool call (call_uHRdFcp41PIp4KWCuHUdWQxo): record_order_id *****\u001b[0m\n", + "Arguments: \n", + "{\"order_id\": \"TR13845\"}\n", + "\u001b[32m********************************************************************************\u001b[0m\n", + "\n", + "--------------------------------------------------------------------------------\n", + "\u001b[32m\n", + "Next speaker: Tool_Execution\n", + "\u001b[0m\n", + "\u001b[35m\n", + ">>>>>>>> EXECUTING FUNCTION check_order_id...\u001b[0m\n", + "\u001b[35m\n", + ">>>>>>>> EXECUTING FUNCTION record_order_id...\u001b[0m\n", + "\u001b[33mTool_Execution\u001b[0m (to chat_manager):\n", + "\n", + "\u001b[32m***** Response from calling tool (call_miEIpNwDd1FFGLenwUB3oPMq) *****\u001b[0m\n", + "Order ID TR13845 is valid.\n", + "\u001b[32m**********************************************************************\u001b[0m\n", + "\n", + "--------------------------------------------------------------------------------\n", + "\u001b[32m***** Response from calling tool (call_uHRdFcp41PIp4KWCuHUdWQxo) *****\u001b[0m\n", + "Order ID Recorded: TR13845\n", + "\u001b[32m**********************************************************************\u001b[0m\n", + "\n", + "--------------------------------------------------------------------------------\n", + "\u001b[32m\n", + "Next speaker: order_mgmt_agent\n", + "\u001b[0m\n", + "\u001b[33morder_mgmt_agent\u001b[0m (to chat_manager):\n", + "\n", + "\u001b[32m***** Suggested tool call (call_sYsVS1U3k3Cf2KbqKJ4hhyRa): transfer_order_mgmt_agent_to_nested_chat_order_mgmt_agent_1 *****\u001b[0m\n", + "Arguments: \n", + "{}\n", + "\u001b[32m****************************************************************************************************************************\u001b[0m\n", + "\n", + "--------------------------------------------------------------------------------\n", + "\u001b[32m\n", + "Next speaker: Tool_Execution\n", + "\u001b[0m\n", + "\u001b[35m\n", + ">>>>>>>> EXECUTING FUNCTION transfer_order_mgmt_agent_to_nested_chat_order_mgmt_agent_1...\u001b[0m\n", + "\u001b[33mTool_Execution\u001b[0m (to chat_manager):\n", + "\n", + "\u001b[32m***** Response from calling tool (call_sYsVS1U3k3Cf2KbqKJ4hhyRa) *****\u001b[0m\n", + "SwarmAgent --> nested_chat_order_mgmt_agent_1\n", + "\u001b[32m**********************************************************************\u001b[0m\n", + "\n", + "--------------------------------------------------------------------------------\n", + "\u001b[32m\n", + "Next speaker: nested_chat_order_mgmt_agent_1\n", + "\u001b[0m\n", + "\u001b[34m\n", + "********************************************************************************\u001b[0m\n", + "\u001b[34mStarting a new chat....\u001b[0m\n", + "\u001b[34m\n", + "********************************************************************************\u001b[0m\n", + "\u001b[33mnested_chat_order_mgmt_agent_1\u001b[0m (to order_retrieval_agent):\n", + "\n", + "Order TR13845 for matress is currently shipped. The shipping address is 123 Main St, State College, PA 12345.\n", + "Context:\n", + "Order ID TR13845 is valid.\n", + "Order ID Recorded: TR13845\n", + "\n", + "--------------------------------------------------------------------------------\n", + "\u001b[31m\n", + ">>>>>>>> USING AUTO REPLY...\u001b[0m\n", + "\u001b[33morder_retrieval_agent\u001b[0m (to nested_chat_order_mgmt_agent_1):\n", + "\n", + "It looks like order TR13845 for a mattress has been shipped. The shipping address for this order is 123 Main St, State College, PA 12345. If you need further details about this order, just let me know!\n", + "\n", + "--------------------------------------------------------------------------------\n", + "\u001b[34m\n", + "********************************************************************************\u001b[0m\n", + "\u001b[34mStarting a new chat....\u001b[0m\n", + "\u001b[34m\n", + "********************************************************************************\u001b[0m\n", + "\u001b[33mnested_chat_order_mgmt_agent_1\u001b[0m (to order_summariser_agent):\n", + "\n", + "Summarise the order details provided in a tabulated, text-based, order sheet format\n", + "Context: \n", + "It looks like order TR13845 for a mattress has been shipped. The shipping address for this order is 123 Main St, State College, PA 12345. If you need further details about this order, just let me know!\n", + "\n", + "--------------------------------------------------------------------------------\n", + "\u001b[31m\n", + ">>>>>>>> USING AUTO REPLY...\u001b[0m\n", + "\u001b[33morder_summariser_agent\u001b[0m (to nested_chat_order_mgmt_agent_1):\n", + "\n", + "```\n", + "Order Summary:\n", + "----------------------------------------------------\n", + "Order Number : TR13845\n", + "Product : Mattress\n", + "Status : Shipped\n", + "Shipping Address : 123 Main St, \n", + " State College, PA 12345\n", + "----------------------------------------------------\n", + "```\n", + "\n", + "--------------------------------------------------------------------------------\n", + "\u001b[33mnested_chat_order_mgmt_agent_1\u001b[0m (to chat_manager):\n", + "\n", + "```\n", + "Order Summary:\n", + "----------------------------------------------------\n", + "Order Number : TR13845\n", + "Product : Mattress\n", + "Status : Shipped\n", + "Shipping Address : 123 Main St, \n", + " State College, PA 12345\n", + "----------------------------------------------------\n", + "```\n", + "\n", + "--------------------------------------------------------------------------------\n", + "\u001b[32m\n", + "Next speaker: order_mgmt_agent\n", + "\u001b[0m\n", + "\u001b[33morder_mgmt_agent\u001b[0m (to chat_manager):\n", + "\n", + "Your order with the Order Number TR13845, which involves a \"Mattress,\" has already been shipped. It is on its way to 123 Main St, State College, PA 12345. \n", + "\n", + "If you have any more questions or need further assistance, feel free to ask!\n", + "\n", + "--------------------------------------------------------------------------------\n", + "\u001b[32m\n", + "Next speaker: customer\n", + "\u001b[0m\n", + "\u001b[33mcustomer\u001b[0m (to chat_manager):\n", + "\n", + "All good\n", + "\n", + "--------------------------------------------------------------------------------\n", + "\u001b[32m\n", + "Next speaker: order_mgmt_agent\n", + "\u001b[0m\n", + "\u001b[33morder_mgmt_agent\u001b[0m (to chat_manager):\n", + "\n", + "\u001b[32m***** Suggested tool call (call_VtBmcKhDAhh7JUz9aXyPq9Aj): transfer_order_mgmt_agent_to_order_triage_agent *****\u001b[0m\n", + "Arguments: \n", + "{}\n", + "\u001b[32m****************************************************************************************************************\u001b[0m\n", + "\n", + "--------------------------------------------------------------------------------\n", + "\u001b[32m\n", + "Next speaker: Tool_Execution\n", + "\u001b[0m\n", + "\u001b[35m\n", + ">>>>>>>> EXECUTING FUNCTION transfer_order_mgmt_agent_to_order_triage_agent...\u001b[0m\n", + "\u001b[33mTool_Execution\u001b[0m (to chat_manager):\n", + "\n", + "\u001b[32m***** Response from calling tool (call_VtBmcKhDAhh7JUz9aXyPq9Aj) *****\u001b[0m\n", + "SwarmAgent --> order_triage_agent\n", + "\u001b[32m**********************************************************************\u001b[0m\n", + "\n", + "--------------------------------------------------------------------------------\n", + "\u001b[32m\n", + "Next speaker: order_triage_agent\n", + "\u001b[0m\n", + "\u001b[33morder_triage_agent\u001b[0m (to chat_manager):\n", + "\n", + "I'm glad we could assist you, Mark! If you have any more inquiries in the future or need further support, feel free to reach out. Have a great day!\n", + "\n", + "--------------------------------------------------------------------------------\n", + "\u001b[32m\n", + "Next speaker: customer\n", + "\u001b[0m\n" + ] + } + ], + "source": [ + "chat_history = initiate_swarm_chat(\n", + " initial_agent=order_triage_agent,\n", + " agents=[order_triage_agent, authentication_agent, order_mgmt_agent],\n", + " context_variables=workflow_context,\n", + " messages=\"Can you help me with my order.\",\n", + " user_agent=user,\n", + " max_rounds=40,\n", + " after_work=AfterWorkOption.TERMINATE,\n", + ")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Controlling flow\n", + "\n", + "### When not logged in\n", + "![Swarm Enhanced Demonstration](swarm_enhanced_02.png)\n", + "\n", + "### When logged in but no order id\n", + "![Swarm Enhanced Demonstration](swarm_enhanced_03.png)\n", + "\n", + "### When logged in with order id\n", + "![Swarm Enhanced Demonstration](swarm_enhanced_04.png)\n", + "\n", + "# Agent state\n", + "\n", + "### Agent System Messages with context\n", + "![Swarm Enhanced Demonstration](swarm_enhanced_05.png)" + ] + } + ], + "metadata": { + "front_matter": { + "description": "Swarm Ochestration", + "tags": [ + "orchestration", + "group chat", + "swarm" + ] + }, + "kernelspec": { + "display_name": "Python 3", + "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.11" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/notebook/swarm_enhanced_01.png b/notebook/swarm_enhanced_01.png new file mode 100755 index 0000000000..5d10323fac --- /dev/null +++ b/notebook/swarm_enhanced_01.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:42da3597edc45639b3bb1e49eef4c1c0c1734769513e6f0e0672ccf2076227aa +size 413112 diff --git a/notebook/swarm_enhanced_02.png b/notebook/swarm_enhanced_02.png new file mode 100755 index 0000000000..4f5c92fd85 --- /dev/null +++ b/notebook/swarm_enhanced_02.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:dbe5b53aca883d4807d6768d6e8101bc720b00f6549b5765f4d0a20cdffbe59a +size 363568 diff --git a/notebook/swarm_enhanced_03.png b/notebook/swarm_enhanced_03.png new file mode 100755 index 0000000000..64ff7e3997 --- /dev/null +++ b/notebook/swarm_enhanced_03.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:4a2d5e2568aacad23c4e0a720220c06d5396894a41cb1ff543566ce424d4d3aa +size 357923 diff --git a/notebook/swarm_enhanced_04.png b/notebook/swarm_enhanced_04.png new file mode 100755 index 0000000000..052c4c2fd8 --- /dev/null +++ b/notebook/swarm_enhanced_04.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:356c2cbfb3f030d335e698d970effe23408c1e5d88a03a61cdc51129463c492f +size 377896 diff --git a/notebook/swarm_enhanced_05.png b/notebook/swarm_enhanced_05.png new file mode 100755 index 0000000000..d50b0fb44a --- /dev/null +++ b/notebook/swarm_enhanced_05.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:42cdd3ce308a95fc6e9627424eba3d8b21bf5810f34f902d07079861fd3dc500 +size 428372 diff --git a/website/docs/Examples.md b/website/docs/Examples.md index c4e0bce998..e7084d1284 100644 --- a/website/docs/Examples.md +++ b/website/docs/Examples.md @@ -38,6 +38,7 @@ Links to notebook examples: ### Swarms - Orchestrating agents in a Swarm - [View Notebook](/docs/notebooks/agentchat_swarm) +- Orchestrating agents in a Swarm (Enhanced) - [View Notebook](/docs/notebooks/agentchat_swarm_enhanced) ### Applications