From eb8cac8b3668186fc4f37de7b01376533e864e95 Mon Sep 17 00:00:00 2001 From: anakin87 Date: Mon, 18 Nov 2024 16:12:51 +0100 Subject: [PATCH] explain better; simplify code --- notebooks/swarm.ipynb | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/notebooks/swarm.ipynb b/notebooks/swarm.ipynb index 409b294..8458460 100644 --- a/notebooks/swarm.ipynb +++ b/notebooks/swarm.ipynb @@ -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", @@ -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", @@ -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" }, @@ -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", @@ -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)" ] }, { @@ -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)" ] }, { @@ -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)" ] }, {