A programming framework for agentic AI
def _prepare_chat for groupchat manager to reset the groupchat (#1254)
Brian Finney73c75226b6f6
diff --git a/autogen/agentchat/conversable_agent.py b/autogen/agentchat/conversable_agent.pyindex 59e7fd21c..919839b6c 100644--- a/autogen/agentchat/conversable_agent.py+++ b/autogen/agentchat/conversable_agent.py@@ -616,13 +616,13 @@ class ConversableAgent(Agent):if reply is not None:await self.a_send(reply, sender, silent=silent)- def _prepare_chat(self, recipient, clear_history):+ def _prepare_chat(self, recipient: "ConversableAgent", clear_history: bool, prepare_recipient: bool = True) -> None:self.reset_consecutive_auto_reply_counter(recipient)- recipient.reset_consecutive_auto_reply_counter(self)- self.reply_at_receive[recipient] = recipient.reply_at_receive[self] = True+ self.reply_at_receive[recipient] = Trueif clear_history:self.clear_history(recipient)- recipient.clear_history(self)+ if prepare_recipient:+ recipient._prepare_chat(self, clear_history, False)def _raise_exception_on_async_reply_functions(self) -> None:"""Raise an exception if any async reply functions are registered.diff --git a/autogen/agentchat/groupchat.py b/autogen/agentchat/groupchat.pyindex 9a7c705a5..501da7b41 100644--- a/autogen/agentchat/groupchat.py+++ b/autogen/agentchat/groupchat.py@@ -334,6 +334,16 @@ class GroupChatManager(ConversableAgent):ignore_async_in_sync_chat=True,)+ def _prepare_chat(self, recipient: ConversableAgent, clear_history: bool, prepare_recipient: bool = True) -> None:+ super()._prepare_chat(recipient, clear_history, prepare_recipient)++ if clear_history:+ self._groupchat.reset()++ for agent in self._groupchat.agents:+ if (recipient != agent or prepare_recipient) and isinstance(agent, ConversableAgent):+ agent._prepare_chat(self, clear_history, False)+def run_chat(self,messages: Optional[List[Dict]] = None,diff --git a/notebook/agentchat_function_call_async.ipynb b/notebook/agentchat_function_call_async.ipynbindex e06c28af4..613c733c3 100644--- a/notebook/agentchat_function_call_async.ipynb+++ b/notebook/agentchat_function_call_async.ipynb@@ -61,7 +61,7 @@},{"cell_type": "code",- "execution_count": 5,+ "execution_count": 1,"id": "dca301a4","metadata": {},"outputs": [],@@ -119,7 +119,7 @@},{"cell_type": "code",- "execution_count": 6,+ "execution_count": 2,"id": "9fb85afb","metadata": {},"outputs": [@@ -134,22 +134,14 @@"--------------------------------------------------------------------------------\n","\u001b[33mchatbot\u001b[0m (to user_proxy):\n","\n",- "\u001b[32m***** Suggested tool Call (call_mZqUtVEf6hwarnCqdiKtVhAE): timer *****\u001b[0m\n",+ "\u001b[32m***** Suggested tool Call (call_fGgH8U261nOnx3JGNJWslhh6): timer *****\u001b[0m\n","Arguments: \n",- "{\n",- " \"num_seconds\": \"5\"\n",- "}\n",+ "{\"num_seconds\":\"5\"}\n","\u001b[32m**********************************************************************\u001b[0m\n","\n","--------------------------------------------------------------------------------\n","\u001b[35m\n",- ">>>>>>>> EXECUTING ASYNC FUNCTION timer...\u001b[0m\n"- ]- },- {- "name": "stdout",- "output_type": "stream",- "text": [+ ">>>>>>>> EXECUTING ASYNC FUNCTION timer...\u001b[0m\n","\u001b[33muser_proxy\u001b[0m (to chatbot):\n","\n","\u001b[33muser_proxy\u001b[0m (to chatbot):\n",@@ -161,11 +153,9 @@"--------------------------------------------------------------------------------\n","\u001b[33mchatbot\u001b[0m (to user_proxy):\n","\n",- "\u001b[32m***** Suggested tool Call (call_xHz73PpH4ipdv7kTxK2AiVCf): stopwatch *****\u001b[0m\n",+ "\u001b[32m***** Suggested tool Call (call_BZs6ynF8gtcZKhONiIRZkECB): stopwatch *****\u001b[0m\n","Arguments: \n",- "{\n",- " \"num_seconds\": \"5\"\n",- "}\n",+ "{\"num_seconds\":\"5\"}\n","\u001b[32m**************************************************************************\u001b[0m\n","\n","--------------------------------------------------------------------------------\n",@@ -195,7 +185,7 @@"\n","coder = autogen.AssistantAgent(\n"," name=\"chatbot\",\n",- " system_message=\"For coding tasks, only use the functions you have been provided with. Reply TERMINATE when the task is done.\",\n",+ " system_message=\"For coding tasks, only use the functions you have been provided with. You have a stopwatch and a timer, these tools can and should be used in parallel. Reply TERMINATE when the task is done.\",\n"," llm_config=llm_config,\n",")\n","\n",@@ -253,7 +243,7 @@},{"cell_type": "code",- "execution_count": 7,+ "execution_count": 3,"id": "2472f95c","metadata": {},"outputs": [],@@ -288,7 +278,7 @@},{"cell_type": "code",- "execution_count": 8,+ "execution_count": 4,"id": "e2c9267a","metadata": {},"outputs": [@@ -299,29 +289,27 @@"\u001b[33muser_proxy\u001b[0m (to chat_manager):\n","\n","\n",- "1) Create a timer for 5 seconds.\n",- "2) a stopwatch for 5 seconds.\n",- "3) Pretty print the result as md.\n",- "4) when 1-3 are done, terminate the group chat\n",- "\n",- "--------------------------------------------------------------------------------\n",- "\u001b[33muser_proxy\u001b[0m (to chat_manager):\n",- "\n",- "\n",+ "1) Create a timer and a stopwatch for 5 seconds each in parallel.\n",+ "2) Pretty print the result as md.\n",+ "3) when 1 and 2 are done, terminate the group chat\n","\n","--------------------------------------------------------------------------------\n","\u001b[33mchatbot\u001b[0m (to chat_manager):\n","\n",- "\u001b[32m***** Suggested tool Call (call_rdYofiAgFpRfaZaCT4tzAhOb): timer *****\u001b[0m\n",+ "\u001b[32m***** Suggested tool Call (call_zlHKR9LBzCqs1iLId5kvNvJ5): timer *****\u001b[0m\n","Arguments: \n",- "{\n",- " \"num_seconds\": \"5\"\n",- "}\n",+ "{\"num_seconds\": \"5\"}\n","\u001b[32m**********************************************************************\u001b[0m\n",+ "\u001b[32m***** Suggested tool Call (call_rH1dgbS9itiJO1Gwnxxhcm35): stopwatch *****\u001b[0m\n",+ "Arguments: \n",+ "{\"num_seconds\": \"5\"}\n",+ "\u001b[32m**************************************************************************\u001b[0m\n","\n","--------------------------------------------------------------------------------\n","\u001b[35m\n",">>>>>>>> EXECUTING ASYNC FUNCTION timer...\u001b[0m\n",+ "\u001b[35m\n",+ ">>>>>>>> EXECUTING ASYNC FUNCTION stopwatch...\u001b[0m\n","\u001b[33muser_proxy\u001b[0m (to chat_manager):\n","\n","\u001b[33muser_proxy\u001b[0m (to chat_manager):\n",@@ -333,17 +321,34 @@"--------------------------------------------------------------------------------\n","\u001b[33muser_proxy\u001b[0m (to chat_manager):\n","\n",- "\n",+ "\u001b[32m***** Response from calling tool \"stopwatch\" *****\u001b[0m\n",+ "Stopwatch is done!\n",+ "\u001b[32m**************************************************\u001b[0m\n","\n","--------------------------------------------------------------------------------\n",- "\u001b[33muser_proxy\u001b[0m (to chat_manager):\n",+ "\u001b[33mMarkdown_agent\u001b[0m (to chat_manager):\n",+ "\n",+ "The results of the timer and stopwatch are as follows:\n","\n",+ "- Timer: Timer is done!\n",+ "- Stopwatch: Stopwatch is done!\n","\n",+ "Now, I will proceed to terminate the group chat.\n",+ "\u001b[32m***** Suggested tool Call (call_3Js7oU80vPatnA8IiaKXB5Xu): terminate_group_chat *****\u001b[0m\n",+ "Arguments: \n",+ "{\"message\":\"The session has concluded, and the group chat will now be terminated.\"}\n",+ "\u001b[32m*************************************************************************************\u001b[0m\n","\n","--------------------------------------------------------------------------------\n",+ "\u001b[35m\n",+ ">>>>>>>> EXECUTING ASYNC FUNCTION terminate_group_chat...\u001b[0m\n","\u001b[33muser_proxy\u001b[0m (to chat_manager):\n","\n",+ "\u001b[33muser_proxy\u001b[0m (to chat_manager):\n","\n",+ "\u001b[32m***** Response from calling tool \"terminate_group_chat\" *****\u001b[0m\n",+ "[GROUPCHAT_TERMINATE] The session has concluded, and the group chat will now be terminated.\n",+ "\u001b[32m*************************************************************\u001b[0m\n","\n","--------------------------------------------------------------------------------\n"]@@ -354,10 +359,9 @@"await user_proxy.a_initiate_chat( # noqa: F704\n"," manager,\n"," message=\"\"\"\n",- "1) Create a timer for 5 seconds.\n",- "2) a stopwatch for 5 seconds.\n",- "3) Pretty print the result as md.\n",- "4) when 1-3 are done, terminate the group chat\"\"\",\n",+ "1) Create a timer and a stopwatch for 5 seconds each in parallel.\n",+ "2) Pretty print the result as md.\n",+ "3) when 1 and 2 are done, terminate the group chat\"\"\",\n",")"]},