Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
yiranwu0 committed Nov 19, 2024
1 parent fabec4f commit 2318335
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 61 deletions.
19 changes: 4 additions & 15 deletions website/blog/2024-11-17-Swarm/index.mdx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
title: Building Swarm-based agents with GroupChat
title: Building Swarm-based agents with AutoGen
authors:
- yiranwu
- marklysze
Expand All @@ -12,21 +12,10 @@ Here are two key features of the `swarm` speaker selection method:
- **Context Variables**: Agents can dynamically update shared variables through function calls, maintaining context and adaptability throughout the process.


## Tips for Building Swarm-based Agents

1. The `swarm` speaker selection method needs to be used in conjunction with `SwarmAgent` and `SwarmResult` classes.

```python
from autogen.agentchat.swarm.swarm_agent import SwarmAgent, SwarmResult
```
## Getting Started

2. The `Swarm` orchestration require human involvement. When a user (human) sends requests, the swarm would transfer controls within agents so that the most suitable agent can respond.
Thus, a `UserProxyAgent` must be present in the agent list to accept user requests.
The `swarm` speaker selection method needs to be used in conjunction with `SwarmAgent` and `SwarmResult` classes. Use the `initialize_swarm_chat` to get a swarm reply.

```python
from autogen import UserProxyAgent
from autogen.agentchat.swarm.swarm_agent import SwarmAgent, SwarmResult, initialize_swarm_chat
```

## Getting Started

Please setup LLM configs.
92 changes: 46 additions & 46 deletions website/docs/topics/groupchat/using_swarm.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,18 @@
" return SwarmResult(value=\"value\", agent=<swarmagent object>, context_variables={\"key\": \"value\"})\n",
"```\n",
"\n",
"To only define transitions between agents, we provide an quick way to define the handoffs:\n",
"\n",
"```python\n",
"swarm_agent.handoff(agent, condition=\"when should transition happen\")\n",
"```\n",
"We will register a transfer function to llm config:\n",
"```\n",
"def transfer_to_agent_name():\n",
" \"\"\"<Condition string>\"\"\"\n",
" return agent_name\n",
"```\n",
"\n",
"When defining function calls, you can return one of the following types:\n",
"- `str`\n",
"- `SwarmAgent`: if a `SwarmAgent` object is returned, it will be called next.\n",
Expand All @@ -54,30 +66,37 @@
"metadata": {},
"outputs": [],
"source": [
"from autogen.agentchat.swarm.swarm_agent import SwarmAgent, SwarmResult\n",
"from autogen.agentchat.contrib.swarm_agent import SwarmAgent, SwarmResult\n",
"\n",
"llm_config = \"Setup your config here\"\n",
"\n",
"\n",
"def update_context_1(context_variables: dict) -> str:\n",
" context_variables[\"1\"] = True\n",
" return SwarmResult(value=\"success\", context_variables=context_variables)\n",
" return SwarmResult(\n",
" value=\"success\", context_variables=context_variables\n",
" ) # Update context variables and return success\n",
"\n",
"\n",
"def update_context_2_and_transfer_to_3(context_variables: dict) -> str:\n",
" context_variables[\"2\"] = True\n",
" return SwarmResult(value=\"success\", context_variables=context_variables, agent=agent_3)\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",
"\n",
"\n",
"def update_context_3(context_variables: dict) -> str:\n",
" context_variables[\"3\"] = True\n",
" return SwarmResult(value=\"success\", context_variables=context_variables)\n",
" return SwarmResult(\n",
" value=\"success\", context_variables=context_variables\n",
" ) # Update context variables and return success\n",
"\n",
"def transfer_to_agent_2() -> SwarmAgent:\n",
" return agent_2\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, transfer_to_agent_2],\n",
" functions=[update_context_1],\n",
")\n",
"\n",
"agent_2 = SwarmAgent(\n",
Expand All @@ -86,50 +105,34 @@
" llm_config=llm_config,\n",
" functions=[update_context_2_and_transfer_to_3],\n",
")\n",
"\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",
" llm_config=llm_config,\n",
" functions=[update_context_3],\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Define a UserProxyAgent\n",
"\n",
"The `Swarm` orchestration require human involvement. When a user (human) sends requests, the swarm would transfer controls within agents so that the most suitable agent can respond.\n",
")\n",
"\n",
"To use swarm, you need to define a `UserProxyAgent` class so that you can interact with the swarm. "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from autogen import UserProxyAgent\n",
"\n",
"user = UserProxyAgent(\n",
" name=\"Human_User\",\n",
" system_message=\"Human user\",\n",
" human_input_mode=\"ALWAYS\",\n",
" code_execution_config=False,\n",
")"
"agent_1.hand_off(agent_2, condition=\"handoff to agent 2\") # register handoff using hand_off method."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Start the GroupChat\n",
"## Use initialize_swarm_chat\n",
"\n",
"Follow the GroupChat convention to define the groupchat and start the conversation.\n",
"To get response from the swarm agent, you can use the `initialize_swarm_chat` function. This function will return the response from the agent and the updated context variables.\n",
"\n",
"You can pass in a `context_variables` dictionary to groupchat to initialize the context variables. Note this variable is only used for `swarm` orchestration.\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"
]
},
{
Expand All @@ -138,19 +141,16 @@
"metadata": {},
"outputs": [],
"source": [
"from autogen import GroupChat, GroupChatManager\n",
"from autogen.agentchat.contrib.swarm_agent import initialize_swarm_chat\n",
"\n",
"context_variables = {\"1\": False, \"2\": False, \"3\": False}\n",
"\n",
"groupchat = GroupChat(\n",
" agents=[user, agent_1, agent_2, agent_3],\n",
" messages=[],\n",
" max_round=10,\n",
" speaker_selection_method=\"swarm\",\n",
"chat_result, context_variables, last_agent = initialize_swarm_chat(\n",
" init_agent=agent_1,\n",
" agents=[agent_1, agent_2, agent_3],\n",
" messages=\"start\",\n",
" context_variables=context_variables,\n",
")\n",
"manager = GroupChatManager(groupchat=groupchat, llm_config=None)\n",
"chat_result = user.initiate_chat(manager, message=\"start\")"
"print(context_variables)"
]
}
],
Expand Down

0 comments on commit 2318335

Please sign in to comment.