Skip to content

Commit

Permalink
Agent string representation, documentation updates for SwarmAgent and…
Browse files Browse the repository at this point in the history
… register_hand_off

Signed-off-by: Mark Sze <[email protected]>
  • Loading branch information
marklysze committed Dec 31, 2024
1 parent b99d9ad commit 1453683
Show file tree
Hide file tree
Showing 11 changed files with 234 additions and 153 deletions.
2 changes: 1 addition & 1 deletion autogen/agentchat/contrib/swarm_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def _establish_swarm_agent(agent: ConversableAgent):

def _swarm_agent_str(self: ConversableAgent) -> str:
"""Customise the __str__ method to show the agent name for transition messages."""
return f"SwarmAgent --> {self.name}"
return f"Swarm agent --> {self.name}"

agent._swarm_after_work = None

Expand Down
43 changes: 28 additions & 15 deletions notebook/agentchat_realtime_swarm.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,17 @@
"In this notebook, we implement OpenAI's [airline customer service example](https://github.com/openai/swarm/tree/main/examples/airline) in AG2 using the RealtimeAgent for enhanced interaction."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"````mdx-code-block\n",
":::note\n",
"This notebook has been updated as swarms can now accommodate any ConversableAgent.\n",
":::\n",
"````"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down Expand Up @@ -270,22 +281,22 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from autogen import ON_CONDITION, SwarmAgent\n",
"from autogen import ON_CONDITION, ConversableAgent, register_hand_off\n",
"\n",
"# Triage Agent\n",
"triage_agent = SwarmAgent(\n",
"triage_agent = ConversableAgent(\n",
" name=\"Triage_Agent\",\n",
" system_message=triage_instructions(context_variables=context_variables),\n",
" llm_config=llm_config,\n",
" functions=[non_flight_enquiry],\n",
")\n",
"\n",
"# Flight Modification Agent\n",
"flight_modification = SwarmAgent(\n",
"flight_modification = ConversableAgent(\n",
" name=\"Flight_Modification_Agent\",\n",
" system_message=\"\"\"You are a Flight Modification Agent for a customer service airline.\n",
" Your task is to determine if the user wants to cancel or change their flight.\n",
Expand All @@ -295,23 +306,23 @@
")\n",
"\n",
"# Flight Cancel Agent\n",
"flight_cancel = SwarmAgent(\n",
"flight_cancel = ConversableAgent(\n",
" name=\"Flight_Cancel_Traversal\",\n",
" system_message=STARTER_PROMPT + FLIGHT_CANCELLATION_POLICY,\n",
" llm_config=llm_config,\n",
" functions=[initiate_refund, initiate_flight_credits, case_resolved, escalate_to_agent],\n",
")\n",
"\n",
"# Flight Change Agent\n",
"flight_change = SwarmAgent(\n",
"flight_change = ConversableAgent(\n",
" name=\"Flight_Change_Traversal\",\n",
" system_message=STARTER_PROMPT + FLIGHT_CHANGE_POLICY,\n",
" llm_config=llm_config,\n",
" functions=[valid_to_change_flight, change_flight, case_resolved, escalate_to_agent],\n",
")\n",
"\n",
"# Lost Baggage Agent\n",
"lost_baggage = SwarmAgent(\n",
"lost_baggage = ConversableAgent(\n",
" name=\"Lost_Baggage_Traversal\",\n",
" system_message=STARTER_PROMPT + LOST_BAGGAGE_POLICY,\n",
" llm_config=llm_config,\n",
Expand All @@ -330,28 +341,30 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Register hand-offs\n",
"triage_agent.register_hand_off(\n",
" [\n",
"register_hand_off(\n",
" agent=triage_agent,\n",
" hand_to=[\n",
" ON_CONDITION(flight_modification, \"To modify a flight\"),\n",
" ON_CONDITION(lost_baggage, \"To find lost baggage\"),\n",
" ]\n",
" ],\n",
")\n",
"\n",
"flight_modification.register_hand_off(\n",
" [\n",
"register_hand_off(\n",
" agent=flight_modification,\n",
" hand_to=[\n",
" ON_CONDITION(flight_cancel, \"To cancel a flight\"),\n",
" ON_CONDITION(flight_change, \"To change a flight\"),\n",
" ]\n",
" ],\n",
")\n",
"\n",
"transfer_to_triage_description = \"Call this function when a user needs to be transferred to a different agent and a different policy.\\nFor instance, if a user is asking about a topic that is not handled by the current agent, call this function.\"\n",
"for agent in [flight_modification, flight_cancel, flight_change, lost_baggage]:\n",
" agent.register_hand_off(ON_CONDITION(triage_agent, transfer_to_triage_description))"
" register_hand_off(agent=agent, hand_to=ON_CONDITION(triage_agent, transfer_to_triage_description))"
]
},
{
Expand Down
43 changes: 28 additions & 15 deletions notebook/agentchat_swarm.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,17 @@
"In this notebook, we implement OpenAI's [airline customer service example](https://github.com/openai/swarm/tree/main/examples/airline) in AG2."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"````mdx-code-block\n",
":::note\n",
"This notebook has been updated as swarms can now accommodate any ConversableAgent.\n",
":::\n",
"````"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down Expand Up @@ -222,18 +233,18 @@
"metadata": {},
"outputs": [],
"source": [
"from autogen import ON_CONDITION, AfterWorkOption, SwarmAgent, initiate_swarm_chat\n",
"from autogen import ON_CONDITION, AfterWorkOption, ConversableAgent, initiate_swarm_chat, register_hand_off\n",
"\n",
"# Triage Agent\n",
"triage_agent = SwarmAgent(\n",
"triage_agent = ConversableAgent(\n",
" name=\"Triage_Agent\",\n",
" system_message=triage_instructions(context_variables=context_variables),\n",
" llm_config=llm_config,\n",
" functions=[non_flight_enquiry],\n",
")\n",
"\n",
"# Flight Modification Agent\n",
"flight_modification = SwarmAgent(\n",
"flight_modification = ConversableAgent(\n",
" name=\"Flight_Modification_Agent\",\n",
" system_message=\"\"\"You are a Flight Modification Agent for a customer service airline.\n",
" Your task is to determine if the user wants to cancel or change their flight.\n",
Expand All @@ -243,23 +254,23 @@
")\n",
"\n",
"# Flight Cancel Agent\n",
"flight_cancel = SwarmAgent(\n",
"flight_cancel = ConversableAgent(\n",
" name=\"Flight_Cancel_Traversal\",\n",
" system_message=STARTER_PROMPT + FLIGHT_CANCELLATION_POLICY,\n",
" llm_config=llm_config,\n",
" functions=[initiate_refund, initiate_flight_credits, case_resolved, escalate_to_agent],\n",
")\n",
"\n",
"# Flight Change Agent\n",
"flight_change = SwarmAgent(\n",
"flight_change = ConversableAgent(\n",
" name=\"Flight_Change_Traversal\",\n",
" system_message=STARTER_PROMPT + FLIGHT_CHANGE_POLICY,\n",
" llm_config=llm_config,\n",
" functions=[valid_to_change_flight, change_flight, case_resolved, escalate_to_agent],\n",
")\n",
"\n",
"# Lost Baggage Agent\n",
"lost_baggage = SwarmAgent(\n",
"lost_baggage = ConversableAgent(\n",
" name=\"Lost_Baggage_Traversal\",\n",
" system_message=STARTER_PROMPT + LOST_BAGGAGE_POLICY,\n",
" llm_config=llm_config,\n",
Expand All @@ -283,23 +294,25 @@
"outputs": [],
"source": [
"# Register hand-offs\n",
"triage_agent.register_hand_off(\n",
" [\n",
"register_hand_off(\n",
" agent=triage_agent,\n",
" hand_to=[\n",
" ON_CONDITION(flight_modification, \"To modify a flight\"),\n",
" ON_CONDITION(lost_baggage, \"To find lost baggage\"),\n",
" ]\n",
" ],\n",
")\n",
"\n",
"flight_modification.register_hand_off(\n",
" [\n",
"register_hand_off(\n",
" agent=flight_modification,\n",
" hand_to=[\n",
" ON_CONDITION(flight_cancel, \"To cancel a flight\"),\n",
" ON_CONDITION(flight_change, \"To change a flight\"),\n",
" ]\n",
" ],\n",
")\n",
"\n",
"transfer_to_triage_description = \"Call this function when a user needs to be transferred to a different agent and a different policy.\\nFor instance, if a user is asking about a topic that is not handled by the current agent, call this function.\"\n",
"for agent in [flight_modification, flight_cancel, flight_change, lost_baggage]:\n",
" agent.register_hand_off(ON_CONDITION(triage_agent, transfer_to_triage_description))"
" register_hand_off(agent=agent, hand_to=ON_CONDITION(triage_agent, transfer_to_triage_description))"
]
},
{
Expand Down Expand Up @@ -373,7 +386,7 @@
"\u001b[33mTool_Execution\u001b[0m (to chat_manager):\n",
"\n",
"\u001b[32m***** Response from calling tool (call_Qgji9KAw1e3ktxykLU8v1wg7) *****\u001b[0m\n",
"SwarmAgent --> Flight_Modification_Agent\n",
"Swarm agent --> Flight_Modification_Agent\n",
"\u001b[32m**********************************************************************\u001b[0m\n",
"\n",
"--------------------------------------------------------------------------------\n",
Expand All @@ -397,7 +410,7 @@
"\u001b[33mTool_Execution\u001b[0m (to chat_manager):\n",
"\n",
"\u001b[32m***** Response from calling tool (call_QYu7uBko1EaEZ7VzxPwx2jNO) *****\u001b[0m\n",
"SwarmAgent --> Flight_Cancel_Traversal\n",
"Swarm agent --> Flight_Cancel_Traversal\n",
"\u001b[32m**********************************************************************\u001b[0m\n",
"\n",
"--------------------------------------------------------------------------------\n",
Expand Down
Loading

0 comments on commit 1453683

Please sign in to comment.