Skip to content

Commit

Permalink
explain better; simplify code
Browse files Browse the repository at this point in the history
  • Loading branch information
anakin87 committed Nov 18, 2024
1 parent c3d8afe commit eb8cac8
Showing 1 changed file with 12 additions and 16 deletions.
28 changes: 12 additions & 16 deletions notebooks/swarm.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,8 @@
"\n",
"Swarm introduces **routines**, which are natural-language instructions paired with the tools needed to execute them. Below, we’ll build an agent capable of calling tools and executing routines.\n",
"\n",
"**\n",
"\n",
"### Implementation\n",
"\n",
"- `instructions` could already be passed to the Assistant, to guide its behavior.\n",
Expand Down Expand Up @@ -255,7 +257,7 @@
" def run(self, messages: list[ChatMessage]) -> Tuple[str, list[ChatMessage]]:\n",
"\n",
" # generate response\n",
" agent_message = self.llm.run(messages=[self._system_message] + messages, tools=self.tools or None)[\"replies\"][0]\n",
" agent_message = self.llm.run(messages=[self._system_message] + messages, tools=self.tools)[\"replies\"][0]\n",
" new_messages = [agent_message]\n",
"\n",
" if agent_message.text:\n",
Expand Down Expand Up @@ -402,12 +404,12 @@
"2. Modify the Agent to return the name of the next agent along with its messages.\n",
"3. Handle the switch in `while` loop.\n",
"\n",
"The implementation is similar to the previous one, with some additions."
"The implementation is similar to the previous one, but, compared to `ToolCallingAgent`, a `SwarmAgent` also returns the name of the next agent to be called, enabling handoffs."
]
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": null,
"metadata": {
"id": "w_-0BDi1xU6Z"
},
Expand All @@ -431,7 +433,7 @@
"\n",
" def run(self, messages: list[ChatMessage]) -> Tuple[str, list[ChatMessage]]:\n",
" # generate response\n",
" agent_message = self.llm.run(messages=[self._system_message] + messages, tools=self.tools or None)[\"replies\"][0]\n",
" agent_message = self.llm.run(messages=[self._system_message] + messages, tools=self.tools)[\"replies\"][0]\n",
" new_messages = [agent_message]\n",
"\n",
" if agent_message.text:\n",
Expand Down Expand Up @@ -551,10 +553,8 @@
" break\n",
" messages.append(ChatMessage.from_user(user_input))\n",
"\n",
" new_agent_name, new_messages = agent.run(messages)\n",
" messages.extend(new_messages)\n",
"\n",
" current_agent_name = new_agent_name or current_agent_name"
" current_agent_name, new_messages = agent.run(messages)\n",
" messages.extend(new_messages)"
]
},
{
Expand Down Expand Up @@ -811,10 +811,8 @@
" break\n",
" messages.append(ChatMessage.from_user(user_input))\n",
"\n",
" new_agent_name, new_messages = agent.run(messages)\n",
" messages.extend(new_messages)\n",
"\n",
" current_agent_name = new_agent_name or current_agent_name"
" current_agent_name, new_messages = agent.run(messages)\n",
" messages.extend(new_messages)"
]
},
{
Expand Down Expand Up @@ -1052,10 +1050,8 @@
" break\n",
" messages.append(ChatMessage.from_user(user_input))\n",
"\n",
" new_agent_name, new_messages = agent.run(messages)\n",
" messages.extend(new_messages)\n",
"\n",
" current_agent_name = new_agent_name or current_agent_name"
" current_agent_name, new_messages = agent.run(messages)\n",
" messages.extend(new_messages)"
]
},
{
Expand Down

0 comments on commit eb8cac8

Please sign in to comment.