diff --git a/website/blog/2024-11-17-Swarm/index.mdx b/website/blog/2024-11-17-Swarm/index.mdx index cce3a1a7cc..6cb9b85ded 100644 --- a/website/blog/2024-11-17-Swarm/index.mdx +++ b/website/blog/2024-11-17-Swarm/index.mdx @@ -8,52 +8,52 @@ tags: [groupchat, swarm] AG2 now provides an implementation of the swarm orchestration from OpenAI's [Swarm](https://github.com/openai/swarm) framework, with some additional features! -*Background*: the swarm orchestration is a multi-agent collaboration where agents execute tasks and are responsible for handing-off to other agents. +*Background*: the swarm orchestration is a multi-agent collaboration where agents execute tasks and are responsible for handing them off to other agents. Here are the key features of the swarm orchestration: -- **Hand offs**: Agents can transfer control to another agent, enabling smooth and direct transitions within workflows. +- **Hand-offs**: Agents can transfer control to another agent, enabling smooth and direct transitions within workflows. - **Context variables**: Agents can dynamically update a shared memory through function calls, maintaining context and adaptability throughout the process. -Beside these core features, AG2 provides: -- **Simple Interface for Tool Call/Handoff Registeration**: When creating a swarm agent, you can pass in a list of functions to be registered directly. We also provide a simple method to register handoffs. -- **Transition Beyond Tool Calls**: We enable the ability to automatically transfer to a nominated swarm agent when an agent has completed their task. In a future release, we may extend this to allo passing in a callable function to determine the next agent to hand off to. +Besides these core features, AG2 provides: +- **Simple Interface for Tool Call/Handoff Registration**: When creating a swarm agent, you can pass in a list of functions to be registered directly. We also provide a simple method to register handoffs. +- **Transition Beyond Tool Calls**: We enable the ability to automatically transfer to a nominated swarm agent when an agent has completed their task. We will extend this to allow other transitions in the future (e.g., use a function to determine the next agent ). - **Built-in human-in-the-loop**: Adding a user agent (UserProxyAgent) to your swarm will allow swarm agents to transition back to the user. Provides a means to clarify and confirm with the user without breaking out of the swarm. -## Hand offs +## Handoffs -Before we dive into a swarm example, an important concept in the swarm orchestration is when and how an agent hands off to another agent. +Before we dive into a swarm example, an important concept in swarm orchestration is when and how an agent hands off to another agent. -Providing additional flexibility, we introduce the capability to define an after work hand off. Think of *after work* as the agent's next action after completing their task. It can be to hand off to another agent, revert to the user, stay with the agent for another iteration, or terminate the conversation. +Providing additional flexibility, we introduce the capability to define an after-work handoff. Think of it as the agent's next action after completing their task. It can be to hand off to another agent, revert to the user, stay with the agent for another iteration, or terminate the conversation. -The following are the prioritised hand offs for each iteration of the swarm. +The following are the prioritized handoffs for each iteration of the swarm. 1. **Agent-level: Calls a tool that returns a swarm agent**: A swarm agent's tool call returns the next agent to hand off to. -2. **Agent-level: Calls a pre-defined conditional hand off**: A swarm agent has an `ON_CONDITION` hand off that is chosen by the LLM (behaves like a tool call). -3. **Agent-level: After work hand off**: When no tool calls are made it can use an, optional, `AFTER_WORK` hand off that is a preset option or a nominated swarm agent. -4. **Swarm-level: After work hand off**: If the agent does not have an `AFTER_WORK` hand off, the swarm's `AFTER_WORK` hand off will be used. +2. **Agent-level: Calls a pre-defined conditional handoff**: A swarm agent has an `ON_CONDITION` handoff that is chosen by the LLM (behaves like a tool call). +3. **Agent-level: After work hand off**: When no tool calls are made it can use an, optional, `AFTER_WORK` handoff that is a preset option or a nominated swarm agent. +4. **Swarm-level: After work handoff**: If the agent does not have an `AFTER_WORK` handoff, the swarm's `AFTER_WORK` handoff will be used. In the following code sample a `SwarmAgent` named `responder` has: -- Two conditional hand offs registered (`ON_CONDITION`), specifying the agent to hand off to and the condition to trigger the hand off. -- An after work hand off (`AFTER_WORK`) nominated using one of the preset options (`TERMINATE`, `REVERT_TO_USER`, `STAY`). This could also be a swarm agent. +- Two conditional handoffs registered (`ON_CONDITION`), specifying the agent to hand off to and the condition to trigger the handoff. +- An after-work handoff (`AFTER_WORK`) nominated using one of the preset options (`TERMINATE`, `REVERT_TO_USER`, `STAY`). This could also be a swarm agent. ```python responder.register_hand_off( hand_to=[ - ON_CONDITION(weather, "If you need weather data, hand off to the Weather_Agent"), - ON_CONDITION(travel_advisor, "If you have weather data but need formatted recommendations, hand off to the Travel_Advisor_Agent"), - AFTER_WORK(REVERT_TO_USER), - ] + ON_CONDITION(weather, "If you need weather data, hand off to the Weather_Agent"), + ON_CONDITION(travel_advisor, "If you have weather data but need formatted recommendations, hand off to the Travel_Advisor_Agent"), + AFTER_WORK(AfterWorkOption.REVERT_TO_USER), + ] ) ``` -You can specify the swarm-level after work hand off when initiating the swarm (here we nominate to terminate): +You can specify the swarm-level after work handoff when initiating the swarm (here we nominate to terminate): ```python history, context, last_agent = initiate_swarm_chat( init_agent=responder, agents=my_list_of_swarm_agents, max_rounds=30, messages=messages, - after_work=AFTER_WORK(TERMINATE) + after_work=AFTER_WORK(AfterWorkOption.TERMINATE) ) ``` @@ -61,7 +61,7 @@ history, context, last_agent = initiate_swarm_chat( 1. Define the functions that can be used by your `SwarmAgent`s. 2. Create your `SwarmAgent`s (which derives from `ConversableAgent`). -3. For each swarm agent, specify the hand offs (transitions to another agent) and what to do when they have finished their work (termed *After Work*). +3. For each swarm agent, specify the handoffs (transitions to another agent) and what to do when they have finished their work (termed *After Work*). 4. Optionally, create your context dictionary. 5. Call `initiate_swarm_chat`. @@ -70,8 +70,7 @@ history, context, last_agent = initiate_swarm_chat( This example of managing refunds demonstrates the context handling, swarm and agent-level conditional and after work hand offs, and the human-in-the-loop feature. ```python -from autogen.agentchat.contrib.swarm_agent import initiate_swarm_chat, SwarmAgent, SwarmResult -from autogen.agentchat.contrib.swarm_agent import ON_CONDITION, AFTER_WORK, REVERT_TO_USER, TERMINATE +from autogen.agentchat.contrib.swarm_agent import initiate_swarm_chat, SwarmAgent, SwarmResult, ON_CONDITION, AFTER_WORK, AfterWorkOption from autogen import UserProxyAgent import os @@ -88,8 +87,7 @@ context_variables = { # Functions that our swarm agents will be assigned # They can return a SwarmResult, a SwarmAgent, or a string -# SwarmResult allows you to update context_variables -# and/or hand off to another agent +# SwarmResult allows you to update context_variables and/or hand off to another agent def verify_customer_identity(passport_number: str, context_variables: dict) -> str: context_variables["passport_number"] = passport_number context_variables["customer_verified"] = True @@ -103,9 +101,7 @@ def process_refund_payment(context_variables: dict) -> str: context_variables["payment_processed"] = True return SwarmResult(values="Payment processed successfully", context_variables=context_variables) -# Swarm Agents, similar to ConversableAgent, -# but with functions and hand offs (specified later) - +# Swarm Agents, similar to ConversableAgent, but with functions and hand offs (specified later) customer_service = SwarmAgent( name="CustomerServiceRep", system_message="""You are a customer service representative. @@ -144,7 +140,7 @@ satisfaction_surveyor = SwarmAgent( customer_service.register_hand_off( hand_to=[ ON_CONDITION(refund_specialist, "After customer verification, transfer to refund specialist"), - AFTER_WORK(REVERT_TO_USER) + AFTER_WORK(AfterWorkOption.REVERT_TO_USER) ] ) @@ -155,12 +151,7 @@ payment_processor.register_hand_off( ) # Our human, you, allowing swarm agents to revert back for more information -user = UserProxyAgent( - name="User", - system_message="The customer, always answers questions when agents require more information.", - human_input_mode="ALWAYS", - code_execution_config=False, -) +user = UserProxyAgent(name="User", code_execution_config=False) # Initiate the swarm # Returns the ChatResult, final context, and last speaker @@ -171,7 +162,7 @@ chat_result, context_variables, last_speaker = initiate_swarm_chat( user_agent=user, # Human user messages="Customer requesting refund for order #12345", context_variables=context_variables, # Context - after_work=AFTER_WORK(TERMINATE) # Swarm-level after work hand off + after_work=AFTER_WORK(AfterWorkOption.TERMINATE) # Swarm-level after work hand off ) diff --git a/website/docs/topics/swarm.ipynb b/website/docs/topics/swarm.ipynb index 6469bc6d10..f960144bf1 100644 --- a/website/docs/topics/swarm.ipynb +++ b/website/docs/topics/swarm.ipynb @@ -6,13 +6,12 @@ "source": [ "# Swarm Ochestration\n", "\n", - "With AG2, you can initiate a Swarm Chat similar to OpenAI's [Swarm](https://github.com/openai/swarm). In this orchestration, there are two main features:\n", - "- **Headoffs**: Agents can transfer control to another agent via function calls, enabling smooth transitions within workflows.\n", - "- **Context Variables**: Agents can dynamically update shared variables through function calls, maintaining context and adaptability throughout the process.\n", + "With AG2, you can initiate a Swarm Chat similar to OpenAI's [Swarm](https://github.com/openai/swarm). This orchestration offers two main features:\n", "\n", + "- **Headoffs**: Agents can transfer control to another agent via function calls, enabling smooth transitions within workflows. \n", + "- **Context Variables**: Agents can dynamically update shared variables through function calls, maintaining context and adaptability throughout the process.\n", "\n", - "Instead of sending a task to a sinlge LLM agent, you can send it to a swarm of agents. Each agent in the swarm can decide whether to handoff the task to another agent. \n", - "When the last active agent's response is only a string (it doesn't suggest tool call or handoff), the chat will be terminated." + "Instead of sending a task to a single LLM agent, you can assign it to a swarm of agents. Each agent in the swarm can decide whether to hand off the task to another agent. The chat terminates when the last active agent's response is a plain string (i.e., it doesn't suggest a tool call or handoff). " ] }, { @@ -24,35 +23,16 @@ "\n", "### Create a `SwarmAgent`\n", "\n", - "All the agents passed to the swarm chat should be instances of `SwarmAgent`. `SwarmAgent` is very similar to `AssistantAgent`, but it has some additional features to support function registration and handoffs. When creating a `SwarmAgent`, you can pass in a list of functions. These functions will be converted to schemas to be passed to the LLMs, and you don't need to worry about registering the functions for execution. You can also pass back a `SwarmResult` class, where you can return a value, the next agent to call and update context variables at the same time.\n", + "All the agents passed to the swarm chat should be instances of `SwarmAgent`. `SwarmAgent` is very similar to `AssistantAgent`, but it has some additional features to support function registration and handoffs. When creating a `SwarmAgent`, you can pass in a list of functions. These functions will be converted to schemas to be passed to the LLMs, and you don't need to worry about registering the functions for execution. You can also pass back a `SwarmResult` class, where you can return a value, the next agent to call, and update context variables at the same time.\n", "\n", - "**Notes for creating the function calls**\n", - "- For input arguments, you must define the type of the argument, otherwise the registration will fail (e.g. `arg_name: str`). \n", - "- If your function requires access or modification of the context variables, you must pass in `context_variables: dict` as one argument. This argument will not be visible to the LLM (removed when registering the function schema). But when being called, the global context variables will be passed in by the swarm chat.\n", - "- The docstring of the function will be used as the prompt. So make sure to write a clear description.\n", + "**Notes for creating the function calls** \n", + "- For input arguments, you must define the type of the argument, otherwise, the registration will fail (e.g. `arg_name: str`). \n", + "- If your function requires access or modification of the context variables, you must pass in `context_variables: dict` as one argument. This argument will not be visible to the LLM (removed when registering the function schema). But when called, the global context variables will be passed in by the swarm chat. \n", + "- The docstring of the function will be used as the prompt. So make sure to write a clear description. \n", "- The function name will be used as the tool name.\n", "\n", - "```python\n", - "def func_1(arg1: str, arg2: int) -> str:\n", - " \"\"\"Doc String\"\"\"\n", - " return \"response\"\n", - "\n", - "def transfer_to_some_agent() -> SwarmAgent:\n", - " return SwarmAgent(...)\n", - "\n", - "def func_3(context_variables: dict, arg2: str) -> SwarmResult:\n", - " # ... update context variables\n", - " context_variables[\"key\"] = \"value\"\n", - " return SwarmResult(value=\"value\", agent=, context_variables=context_variables)\n", - "\n", - "SwarmAgent(\n", - " ..., # same arguments as AssistantAgent\n", - " functions=[func_1, transfer_to_some_agent, func_3] # a new parameter that allows passing a list of functions\n", - ")\n", - "```\n", - "\n", "### Registering Handoffs\n", - "While you can create an function to decide what next agent to call, we provide a quick way to register the handoff use `ON_CONDITION`. We will craft this transition function and add it to the LLM config directly.\n", + "While you can create a function to decide what next agent to call, we provide a quick way to register the handoff using `ON_CONDITION`. We will craft this transition function and add it to the LLM config directly.\n", "\n", "```python\n", "agent_2 = SwarmAgent(...)\n", @@ -78,24 +58,24 @@ "### AFTER_WORK\n", "\n", "When the last active agent's response doesn't suggest a tool call or handoff, the chat will terminate by default. However, you can register an `AFTER_WORK` handoff to define a fallback agent if you don't want the chat to end at this agent. At the swarm chat level, you also pass in an `AFTER_WORK` handoff to define the fallback mechanism for the entire chat.\n", - "If this parameter is set for both the agent and the chat, we will prioritize the agent's setting.\n", + "If this parameter is set for the agent and the chat, we will prioritize the agent's setting. There should only be one `AFTER_WORK`. If multiple `AFTER_WORK` handoffs are passed, only the last one will be used.\n", "\n", - "Besides fallback to an agent, we provide 3 `AfterWorkOption`:\n", - "- `TERMINATE`: Terminate the chat\n", - "- `STAY`: Stay at the current agent\n", - "- `REVERT_TO_USER`: Revert to the user agent. Only if an user agent is passed in when initializing. (See below for more details)\n", + "Besides fallback to an agent, we provide 3 `AfterWorkOption`: \n", + "- `TERMINATE`: Terminate the chat \n", + "- `STAY`: Stay at the current agent \n", + "- `REVERT_TO_USER`: Revert to the user agent. Only if a user agent is passed in when initializing. (See below for more details) \n", "\n", "```python\n", "agent_1 = SwarmAgent(...)\n", "\n", "# Register the handoff\n", "agent_1.handoff(hand_to=[\n", - " ON_CONDITION(...), \n", - " ON_CONDITION(...),\n", - " AFTER_WORK(agent_4) # Fallback to agent_4 if no handoff is suggested\n", + " ON_CONDITION(...), \n", + " ON_CONDITION(...),\n", + " AFTER_WORK(agent_4) # Fallback to agent_4 if no handoff is suggested\n", "])\n", "\n", - "agent_2.handoff(hand_to=[AFTER_WORK(TERMINATE)]) # Terminate the chat if no handoff is suggested\n", + "agent_2.handoff(hand_to=[AFTER_WORK(AfterWorkOption.TERMINATE)]) # Terminate the chat if no handoff is suggested\n", "```\n", "\n", "### Initialize SwarmChat with `initiate_swarm_chat`\n", @@ -107,7 +87,7 @@ " init_agent=agent_1, # the first agent to start the chat\n", " agents=[agent_1, agent_2, agent_3], # a list of agents\n", " messages=[{\"role\": \"user\", \"content\": \"Hello\"}], # a list of messages to start the chat\n", - " user_agent=user_agent, # optional, if you want to revert to user agent\n", + " user_agent=user_agent, # optional, if you want to revert to the user agent\n", " context_variables={\"key\": \"value\"} # optional, initial context variables\n", ")\n", "```\n", @@ -116,14 +96,23 @@ "\n", "> How are context variables updated?\n", "\n", - "The context variables will only be updated through custom function calls when returning a `SwarmResult` object. In fact, all interactions with context variables will be done through function calls (accessing and updating). The context variables dictionary is a reference, and any modification will be done in-place.\n", + "The context variables will only be updated through custom function calls when returning a `SwarmResult` object. In fact, all interactions with context variables will be done through function calls (accessing and updating). The context variables dictionary is a reference, and any modification will be done in place.\n", + "\n", + "> What is the difference between ON_CONDITION and AFTER_WORK?\n", + "\n", + "When registering an ON_CONDITION handoff, we are creating a function schema to be passed to the LLM. The LLM will decide whether to call this function.\n", + "\n", + "When registering an AFTER_WORK handoff, we are defining the fallback mechanism when no tool calls are suggested. This is a higher level of control from the swarm chat level.\n", "\n", "> When to pass in a user agent?\n", "\n", - "If your application requires interactions with the user, you can pass in a user agent to the groupchat, so that don't need to write an outer loop to accept user inputs and calling swarm.\n", + "If your application requires interactions with the user, you can pass in a user agent to the groupchat, so that don't need to write an outer loop to accept user inputs and call swarm.\n", + "\n", "\n", + "## Demonstration\n", "\n", - "\n" + "\n", + "### Create Swarm Agents" ] }, { @@ -132,37 +121,75 @@ "metadata": {}, "outputs": [], "source": [ - "from autogen.agentchat.contrib.swarm_agent import SwarmAgent, SwarmResult\n", + "import autogen\n", "\n", - "llm_config = \"Setup your config here\"\n", + "config_list = autogen.config_list_from_json(...)\n", + "llm_config = {\"config_list\": config_list}" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Agent 1 function schema:\n", + "{'type': 'function', 'function': {'description': '', 'name': 'update_context_1', 'parameters': {'type': 'object', 'properties': {}}}}\n", + "{'type': 'function', 'function': {'description': 'Transfer to agent 2', 'name': 'transfer_to_agent_2', 'parameters': {'type': 'object', 'properties': {}}}}\n", + "Agent 3 function schema:\n", + "{'type': 'function', 'function': {'description': 'Transfer to Agent 4', 'name': 'transfer_to_Agent_4', 'parameters': {'type': 'object', 'properties': {}}}}\n" + ] + } + ], + "source": [ + "import random\n", + "\n", + "from autogen.agentchat.contrib.swarm_agent import (\n", + " AFTER_WORK,\n", + " ON_CONDITION,\n", + " AfterWorkOption,\n", + " SwarmAgent,\n", + " SwarmResult,\n", + " initiate_swarm_chat,\n", + ")\n", "\n", "\n", + "# 1. A function that returns a value of \"success\" and updates the context variable \"1\" to True\n", "def update_context_1(context_variables: dict) -> str:\n", " context_variables[\"1\"] = True\n", - " return SwarmResult(\n", - " value=\"success\", context_variables=context_variables\n", - " ) # Update context variables and return success\n", + " return SwarmResult(value=\"success\", context_variables=context_variables)\n", + "\n", + "\n", + "# 2. A function that returns an SwarmAgent object\n", + "def transfer_to_agent_2() -> SwarmAgent:\n", + " \"\"\"Transfer to agent 2\"\"\"\n", + " return agent_2\n", "\n", "\n", + "# 3. A function that returns the value of \"success\", updates the context variable and transfers to agent 3\n", "def update_context_2_and_transfer_to_3(context_variables: dict) -> str:\n", " context_variables[\"2\"] = True\n", - " return SwarmResult(\n", - " value=\"success\", context_variables=context_variables, agent=agent_3\n", - " ) # Update context variables, return success, and transfer to agent_3\n", + " return SwarmResult(value=\"success\", context_variables=context_variables, agent=agent_3)\n", + "\n", + "\n", + "# 4. A function that returns a normal value\n", + "def get_random_number() -> str:\n", + " return random.randint(1, 100)\n", "\n", "\n", - "def update_context_3(context_variables: dict) -> str:\n", - " context_variables[\"3\"] = True\n", - " return SwarmResult(\n", - " value=\"success\", context_variables=context_variables\n", - " ) # Update context variables and return success\n", + "def update_context_3_with_random_number(context_variables: dict, random_number: int) -> str:\n", + " context_variables[\"3\"] = random_number\n", + " return SwarmResult(value=\"success\", context_variables=context_variables)\n", "\n", "\n", "agent_1 = SwarmAgent(\n", " name=\"Agent_1\",\n", " system_message=\"You are Agent 1, first, call the function to update context 1, and transfer to Agent 2\",\n", " llm_config=llm_config,\n", - " functions=[update_context_1],\n", + " functions=[update_context_1, transfer_to_agent_2],\n", ")\n", "\n", "agent_2 = SwarmAgent(\n", @@ -174,50 +201,365 @@ "\n", "agent_3 = SwarmAgent(\n", " name=\"Agent_3\",\n", - " system_message=\"You are Agent 3, first, call the function to update context 3, and then reply TERMINATE\",\n", + " system_message=\"You are Agent 3, tell a joke\",\n", " llm_config=llm_config,\n", - " functions=[update_context_3],\n", ")\n", "\n", - "agent_1.hand_off(agent_2, condition=\"handoff to agent 2\") # register handoff using hand_off method." + "agent_4 = SwarmAgent(\n", + " name=\"Agent_4\",\n", + " system_message=\"You are Agent 4, call the function to get a random number\",\n", + " llm_config=llm_config,\n", + " functions=[get_random_number],\n", + ")\n", + "\n", + "agent_5 = SwarmAgent(\n", + " name=\"Agent_5\",\n", + " system_message=\"Update context 3 with the random number.\",\n", + " llm_config=llm_config,\n", + " functions=[update_context_3_with_random_number],\n", + ")\n", + "\n", + "\n", + "# This is equivalent to writing a transfer function\n", + "agent_3.register_hand_off(ON_CONDITION(agent_4, \"Transfer to Agent 4\"))\n", + "\n", + "agent_4.register_hand_off([AFTER_WORK(agent_5)])\n", + "\n", + "print(\"Agent 1 function schema:\")\n", + "for func_schema in agent_1.llm_config[\"tools\"]:\n", + " print(func_schema)\n", + "\n", + "print(\"Agent 3 function schema:\")\n", + "for func_schema in agent_3.llm_config[\"tools\"]:\n", + " print(func_schema)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "## Use initiate_swarm_chat\n", - "\n", - "To get response from the swarm agent, you can use the `initiate_swarm_chat` function. This function will return the response from the agent and the updated context variables.\n", - "\n", - "```python\n", - "chat_history, context_variables, last_agent = initialize_swarm_chat(\n", - " init_agent, # which agent to start with\n", - " messages, # list of messages\n", - " agents, # pass in all the swarm agents\n", - " max_rounds, # maximum number of rounds\n", - " context_variables, # initial context variables\n", - ")\n", - "```\n" + "### Start Chat" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\u001b[33mAgent_1\u001b[0m (to chat_manager):\n", + "\n", + "start\n", + "\n", + "--------------------------------------------------------------------------------\n", + "\u001b[32m\n", + "Next speaker: Agent_1\n", + "\u001b[0m\n", + "\u001b[33mAgent_1\u001b[0m (to chat_manager):\n", + "\n", + "\u001b[32m***** Suggested tool call (call_kfcEAY2IeRZww06CQN7lbxOf): update_context_1 *****\u001b[0m\n", + "Arguments: \n", + "{}\n", + "\u001b[32m*********************************************************************************\u001b[0m\n", + "\u001b[32m***** Suggested tool call (call_izl5eyV8IQ0Wg6XY2SaR1EJM): transfer_to_agent_2 *****\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 update_context_1...\u001b[0m\n", + "\u001b[35m\n", + ">>>>>>>> EXECUTING FUNCTION transfer_to_agent_2...\u001b[0m\n", + "\u001b[33mTool_Execution\u001b[0m (to chat_manager):\n", + "\n", + "\u001b[32m***** Response from calling tool (call_kfcEAY2IeRZww06CQN7lbxOf) *****\u001b[0m\n", + "\n", + "\u001b[32m**********************************************************************\u001b[0m\n", + "\n", + "--------------------------------------------------------------------------------\n", + "\u001b[32m***** Response from calling tool (call_izl5eyV8IQ0Wg6XY2SaR1EJM) *****\u001b[0m\n", + "SwarmAgent --> Agent_2\n", + "\u001b[32m**********************************************************************\u001b[0m\n", + "\n", + "--------------------------------------------------------------------------------\n", + "\u001b[32m\n", + "Next speaker: Agent_2\n", + "\u001b[0m\n", + "\u001b[33mAgent_2\u001b[0m (to chat_manager):\n", + "\n", + "\u001b[32m***** Suggested tool call (call_Yf5DTGaaYkA726ubnfJAvQMq): update_context_2_and_transfer_to_3 *****\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 update_context_2_and_transfer_to_3...\u001b[0m\n", + "\u001b[33mTool_Execution\u001b[0m (to chat_manager):\n", + "\n", + "\u001b[32m***** Response from calling tool (call_Yf5DTGaaYkA726ubnfJAvQMq) *****\u001b[0m\n", + "\n", + "\u001b[32m**********************************************************************\u001b[0m\n", + "\n", + "--------------------------------------------------------------------------------\n", + "\u001b[32m\n", + "Next speaker: Agent_3\n", + "\u001b[0m\n", + "\u001b[33mAgent_3\u001b[0m (to chat_manager):\n", + "\n", + "\u001b[32m***** Suggested tool call (call_jqZNHuMtQYeNh5Mq4pV2uwAj): transfer_to_Agent_4 *****\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_to_Agent_4...\u001b[0m\n", + "\u001b[33mTool_Execution\u001b[0m (to chat_manager):\n", + "\n", + "\u001b[32m***** Response from calling tool (call_jqZNHuMtQYeNh5Mq4pV2uwAj) *****\u001b[0m\n", + "SwarmAgent --> Agent_4\n", + "\u001b[32m**********************************************************************\u001b[0m\n", + "\n", + "--------------------------------------------------------------------------------\n", + "\u001b[32m\n", + "Next speaker: Agent_4\n", + "\u001b[0m\n", + "\u001b[33mAgent_4\u001b[0m (to chat_manager):\n", + "\n", + "\u001b[32m***** Suggested tool call (call_KeNGv98klvDZsrAX10Ou3I71): get_random_number *****\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 get_random_number...\u001b[0m\n", + "\u001b[33mTool_Execution\u001b[0m (to chat_manager):\n", + "\n", + "\u001b[32m***** Response from calling tool (call_KeNGv98klvDZsrAX10Ou3I71) *****\u001b[0m\n", + "27\n", + "\u001b[32m**********************************************************************\u001b[0m\n", + "\n", + "--------------------------------------------------------------------------------\n", + "\u001b[32m\n", + "Next speaker: Agent_4\n", + "\u001b[0m\n", + "\u001b[33mAgent_4\u001b[0m (to chat_manager):\n", + "\n", + "The random number generated is 27.\n", + "\n", + "--------------------------------------------------------------------------------\n", + "\u001b[32m\n", + "Next speaker: Agent_5\n", + "\u001b[0m\n", + "\u001b[33mAgent_5\u001b[0m (to chat_manager):\n", + "\n", + "\u001b[32m***** Suggested tool call (call_MlSGNNktah3m3QGssWBEzxCe): update_context_3_with_random_number *****\u001b[0m\n", + "Arguments: \n", + "{\"random_number\":27}\n", + "\u001b[32m****************************************************************************************************\u001b[0m\n", + "\n", + "--------------------------------------------------------------------------------\n", + "\u001b[32m\n", + "Next speaker: Tool_Execution\n", + "\u001b[0m\n", + "\u001b[35m\n", + ">>>>>>>> EXECUTING FUNCTION update_context_3_with_random_number...\u001b[0m\n", + "\u001b[33mTool_Execution\u001b[0m (to chat_manager):\n", + "\n", + "\u001b[32m***** Response from calling tool (call_MlSGNNktah3m3QGssWBEzxCe) *****\u001b[0m\n", + "\n", + "\u001b[32m**********************************************************************\u001b[0m\n", + "\n", + "--------------------------------------------------------------------------------\n", + "\u001b[32m\n", + "Next speaker: Agent_5\n", + "\u001b[0m\n", + "\u001b[33mAgent_5\u001b[0m (to chat_manager):\n", + "\n", + "The random number 27 has been successfully updated in context 3.\n", + "\n", + "--------------------------------------------------------------------------------\n" + ] + } + ], "source": [ - "from autogen.agentchat.contrib.swarm_agent import initialize_swarm_chat\n", - "\n", "context_variables = {\"1\": False, \"2\": False, \"3\": False}\n", - "chat_result, context_variables, last_agent = initialize_swarm_chat(\n", + "chat_result, context_variables, last_agent = initiate_swarm_chat(\n", " init_agent=agent_1,\n", - " agents=[agent_1, agent_2, agent_3],\n", + " agents=[agent_1, agent_2, agent_3, agent_4, agent_5],\n", " messages=\"start\",\n", " context_variables=context_variables,\n", - ")\n", + " after_work=AFTER_WORK(AfterWorkOption.TERMINATE), # this is the default\n", + ")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'1': True, '2': True, '3': 27}\n" + ] + } + ], + "source": [ "print(context_variables)" ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Demo with User Agent\n", + "\n", + "We pass in a user agent to the swarm chat to accept user inputs. With `agent_6`, we register an `AFTER_WORK` handoff to revert to the user agent when no tool calls are suggested. " + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\u001b[33mUser\u001b[0m (to chat_manager):\n", + "\n", + "start\n", + "\n", + "--------------------------------------------------------------------------------\n", + "\u001b[32m\n", + "Next speaker: Agent_6\n", + "\u001b[0m\n", + "\u001b[33mAgent_6\u001b[0m (to chat_manager):\n", + "\n", + "Why did the scarecrow win an award? \n", + "\n", + "Because he was outstanding in his field! \n", + "\n", + "Want to hear another one?\n", + "\n", + "--------------------------------------------------------------------------------\n", + "\u001b[32m\n", + "Next speaker: User\n", + "\u001b[0m\n", + "\u001b[33mUser\u001b[0m (to chat_manager):\n", + "\n", + "yes\n", + "\n", + "--------------------------------------------------------------------------------\n", + "\u001b[32m\n", + "Next speaker: Agent_6\n", + "\u001b[0m\n", + "\u001b[33mAgent_6\u001b[0m (to chat_manager):\n", + "\n", + "Why don't skeletons fight each other?\n", + "\n", + "They don't have the guts! \n", + "\n", + "How about another?\n", + "\n", + "--------------------------------------------------------------------------------\n", + "\u001b[32m\n", + "Next speaker: User\n", + "\u001b[0m\n", + "\u001b[33mUser\u001b[0m (to chat_manager):\n", + "\n", + "transfer\n", + "\n", + "--------------------------------------------------------------------------------\n", + "\u001b[32m\n", + "Next speaker: Agent_6\n", + "\u001b[0m\n", + "\u001b[33mAgent_6\u001b[0m (to chat_manager):\n", + "\n", + "\u001b[32m***** Suggested tool call (call_gQ9leFamxgzQp8ZVQB8rUH73): transfer_to_Agent_7 *****\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_to_Agent_7...\u001b[0m\n", + "\u001b[33mTool_Execution\u001b[0m (to chat_manager):\n", + "\n", + "\u001b[32m***** Response from calling tool (call_gQ9leFamxgzQp8ZVQB8rUH73) *****\u001b[0m\n", + "SwarmAgent --> Agent_7\n", + "\u001b[32m**********************************************************************\u001b[0m\n", + "\n", + "--------------------------------------------------------------------------------\n", + "\u001b[32m\n", + "Next speaker: Agent_7\n", + "\u001b[0m\n", + "\u001b[33mAgent_7\u001b[0m (to chat_manager):\n", + "\n", + "The joke about the scarecrow winning an award is a play on words. It utilizes the term \"outstanding,\" which can mean both exceptionally good (in the context of the scarecrow's performance) and literally being \"standing out\" in a field (where scarecrows are placed). So, the double meaning creates a pun that makes the joke humorous. \n", + "\n", + "The skeleton joke works similarly. When it says skeletons \"don't have the guts,\" it plays on the literal fact that skeletons don't have internal organs (guts), and metaphorically, \"having guts\" means having courage. The humor comes from this clever wordplay.\n", + "\n", + "--------------------------------------------------------------------------------\n" + ] + } + ], + "source": [ + "from autogen import UserProxyAgent\n", + "\n", + "user_agent = UserProxyAgent(name=\"User\", code_execution_config=False)\n", + "\n", + "agent_6 = SwarmAgent(\n", + " name=\"Agent_6\",\n", + " system_message=\"You are Agent 6. Your job is to tell jokes.\",\n", + " llm_config=llm_config,\n", + ")\n", + "\n", + "agent_7 = SwarmAgent(\n", + " name=\"Agent_7\",\n", + " system_message=\"You are Agent 7, explain the joke.\",\n", + " llm_config=llm_config,\n", + ")\n", + "\n", + "agent_6.register_hand_off(\n", + " [\n", + " ON_CONDITION(\n", + " agent_7, \"Used to transfer to Agent 7. Don't call this function, unless the user explicitly tells you to.\"\n", + " ),\n", + " AFTER_WORK(AfterWorkOption.REVERT_TO_USER),\n", + " ]\n", + ")\n", + "\n", + "chat_result, _, _ = initiate_swarm_chat(\n", + " init_agent=agent_6,\n", + " agents=[agent_6, agent_7],\n", + " user_agent=user_agent,\n", + " messages=\"start\",\n", + ")" + ] } ], "metadata": {