Skip to content

Commit 73c7522

Browse files
yenifsonichi
andauthored
def _prepare_chat for groupchat manager to reset the groupchat (#1254)
* def _prepare_chat for groupchat manager to reset the groupchat * typo Co-authored-by: Chi Wang <[email protected]> * typehint and simplify --------- Co-authored-by: Chi Wang <[email protected]>
1 parent 260e0cf commit 73c7522

File tree

3 files changed

+57
-43
lines changed

3 files changed

+57
-43
lines changed

autogen/agentchat/conversable_agent.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -616,13 +616,13 @@ async def a_receive(
616616
if reply is not None:
617617
await self.a_send(reply, sender, silent=silent)
618618

619-
def _prepare_chat(self, recipient, clear_history):
619+
def _prepare_chat(self, recipient: "ConversableAgent", clear_history: bool, prepare_recipient: bool = True) -> None:
620620
self.reset_consecutive_auto_reply_counter(recipient)
621-
recipient.reset_consecutive_auto_reply_counter(self)
622-
self.reply_at_receive[recipient] = recipient.reply_at_receive[self] = True
621+
self.reply_at_receive[recipient] = True
623622
if clear_history:
624623
self.clear_history(recipient)
625-
recipient.clear_history(self)
624+
if prepare_recipient:
625+
recipient._prepare_chat(self, clear_history, False)
626626

627627
def _raise_exception_on_async_reply_functions(self) -> None:
628628
"""Raise an exception if any async reply functions are registered.

autogen/agentchat/groupchat.py

+10
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,16 @@ def __init__(
334334
ignore_async_in_sync_chat=True,
335335
)
336336

337+
def _prepare_chat(self, recipient: ConversableAgent, clear_history: bool, prepare_recipient: bool = True) -> None:
338+
super()._prepare_chat(recipient, clear_history, prepare_recipient)
339+
340+
if clear_history:
341+
self._groupchat.reset()
342+
343+
for agent in self._groupchat.agents:
344+
if (recipient != agent or prepare_recipient) and isinstance(agent, ConversableAgent):
345+
agent._prepare_chat(self, clear_history, False)
346+
337347
def run_chat(
338348
self,
339349
messages: Optional[List[Dict]] = None,

notebook/agentchat_function_call_async.ipynb

+43-39
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
},
6262
{
6363
"cell_type": "code",
64-
"execution_count": 5,
64+
"execution_count": 1,
6565
"id": "dca301a4",
6666
"metadata": {},
6767
"outputs": [],
@@ -119,7 +119,7 @@
119119
},
120120
{
121121
"cell_type": "code",
122-
"execution_count": 6,
122+
"execution_count": 2,
123123
"id": "9fb85afb",
124124
"metadata": {},
125125
"outputs": [
@@ -134,22 +134,14 @@
134134
"--------------------------------------------------------------------------------\n",
135135
"\u001b[33mchatbot\u001b[0m (to user_proxy):\n",
136136
"\n",
137-
"\u001b[32m***** Suggested tool Call (call_mZqUtVEf6hwarnCqdiKtVhAE): timer *****\u001b[0m\n",
137+
"\u001b[32m***** Suggested tool Call (call_fGgH8U261nOnx3JGNJWslhh6): timer *****\u001b[0m\n",
138138
"Arguments: \n",
139-
"{\n",
140-
" \"num_seconds\": \"5\"\n",
141-
"}\n",
139+
"{\"num_seconds\":\"5\"}\n",
142140
"\u001b[32m**********************************************************************\u001b[0m\n",
143141
"\n",
144142
"--------------------------------------------------------------------------------\n",
145143
"\u001b[35m\n",
146-
">>>>>>>> EXECUTING ASYNC FUNCTION timer...\u001b[0m\n"
147-
]
148-
},
149-
{
150-
"name": "stdout",
151-
"output_type": "stream",
152-
"text": [
144+
">>>>>>>> EXECUTING ASYNC FUNCTION timer...\u001b[0m\n",
153145
"\u001b[33muser_proxy\u001b[0m (to chatbot):\n",
154146
"\n",
155147
"\u001b[33muser_proxy\u001b[0m (to chatbot):\n",
@@ -161,11 +153,9 @@
161153
"--------------------------------------------------------------------------------\n",
162154
"\u001b[33mchatbot\u001b[0m (to user_proxy):\n",
163155
"\n",
164-
"\u001b[32m***** Suggested tool Call (call_xHz73PpH4ipdv7kTxK2AiVCf): stopwatch *****\u001b[0m\n",
156+
"\u001b[32m***** Suggested tool Call (call_BZs6ynF8gtcZKhONiIRZkECB): stopwatch *****\u001b[0m\n",
165157
"Arguments: \n",
166-
"{\n",
167-
" \"num_seconds\": \"5\"\n",
168-
"}\n",
158+
"{\"num_seconds\":\"5\"}\n",
169159
"\u001b[32m**************************************************************************\u001b[0m\n",
170160
"\n",
171161
"--------------------------------------------------------------------------------\n",
@@ -195,7 +185,7 @@
195185
"\n",
196186
"coder = autogen.AssistantAgent(\n",
197187
" name=\"chatbot\",\n",
198-
" system_message=\"For coding tasks, only use the functions you have been provided with. Reply TERMINATE when the task is done.\",\n",
188+
" 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",
199189
" llm_config=llm_config,\n",
200190
")\n",
201191
"\n",
@@ -253,7 +243,7 @@
253243
},
254244
{
255245
"cell_type": "code",
256-
"execution_count": 7,
246+
"execution_count": 3,
257247
"id": "2472f95c",
258248
"metadata": {},
259249
"outputs": [],
@@ -288,7 +278,7 @@
288278
},
289279
{
290280
"cell_type": "code",
291-
"execution_count": 8,
281+
"execution_count": 4,
292282
"id": "e2c9267a",
293283
"metadata": {},
294284
"outputs": [
@@ -299,29 +289,27 @@
299289
"\u001b[33muser_proxy\u001b[0m (to chat_manager):\n",
300290
"\n",
301291
"\n",
302-
"1) Create a timer for 5 seconds.\n",
303-
"2) a stopwatch for 5 seconds.\n",
304-
"3) Pretty print the result as md.\n",
305-
"4) when 1-3 are done, terminate the group chat\n",
306-
"\n",
307-
"--------------------------------------------------------------------------------\n",
308-
"\u001b[33muser_proxy\u001b[0m (to chat_manager):\n",
309-
"\n",
310-
"\n",
292+
"1) Create a timer and a stopwatch for 5 seconds each in parallel.\n",
293+
"2) Pretty print the result as md.\n",
294+
"3) when 1 and 2 are done, terminate the group chat\n",
311295
"\n",
312296
"--------------------------------------------------------------------------------\n",
313297
"\u001b[33mchatbot\u001b[0m (to chat_manager):\n",
314298
"\n",
315-
"\u001b[32m***** Suggested tool Call (call_rdYofiAgFpRfaZaCT4tzAhOb): timer *****\u001b[0m\n",
299+
"\u001b[32m***** Suggested tool Call (call_zlHKR9LBzCqs1iLId5kvNvJ5): timer *****\u001b[0m\n",
316300
"Arguments: \n",
317-
"{\n",
318-
" \"num_seconds\": \"5\"\n",
319-
"}\n",
301+
"{\"num_seconds\": \"5\"}\n",
320302
"\u001b[32m**********************************************************************\u001b[0m\n",
303+
"\u001b[32m***** Suggested tool Call (call_rH1dgbS9itiJO1Gwnxxhcm35): stopwatch *****\u001b[0m\n",
304+
"Arguments: \n",
305+
"{\"num_seconds\": \"5\"}\n",
306+
"\u001b[32m**************************************************************************\u001b[0m\n",
321307
"\n",
322308
"--------------------------------------------------------------------------------\n",
323309
"\u001b[35m\n",
324310
">>>>>>>> EXECUTING ASYNC FUNCTION timer...\u001b[0m\n",
311+
"\u001b[35m\n",
312+
">>>>>>>> EXECUTING ASYNC FUNCTION stopwatch...\u001b[0m\n",
325313
"\u001b[33muser_proxy\u001b[0m (to chat_manager):\n",
326314
"\n",
327315
"\u001b[33muser_proxy\u001b[0m (to chat_manager):\n",
@@ -333,17 +321,34 @@
333321
"--------------------------------------------------------------------------------\n",
334322
"\u001b[33muser_proxy\u001b[0m (to chat_manager):\n",
335323
"\n",
336-
"\n",
324+
"\u001b[32m***** Response from calling tool \"stopwatch\" *****\u001b[0m\n",
325+
"Stopwatch is done!\n",
326+
"\u001b[32m**************************************************\u001b[0m\n",
337327
"\n",
338328
"--------------------------------------------------------------------------------\n",
339-
"\u001b[33muser_proxy\u001b[0m (to chat_manager):\n",
329+
"\u001b[33mMarkdown_agent\u001b[0m (to chat_manager):\n",
330+
"\n",
331+
"The results of the timer and stopwatch are as follows:\n",
340332
"\n",
333+
"- Timer: Timer is done!\n",
334+
"- Stopwatch: Stopwatch is done!\n",
341335
"\n",
336+
"Now, I will proceed to terminate the group chat.\n",
337+
"\u001b[32m***** Suggested tool Call (call_3Js7oU80vPatnA8IiaKXB5Xu): terminate_group_chat *****\u001b[0m\n",
338+
"Arguments: \n",
339+
"{\"message\":\"The session has concluded, and the group chat will now be terminated.\"}\n",
340+
"\u001b[32m*************************************************************************************\u001b[0m\n",
342341
"\n",
343342
"--------------------------------------------------------------------------------\n",
343+
"\u001b[35m\n",
344+
">>>>>>>> EXECUTING ASYNC FUNCTION terminate_group_chat...\u001b[0m\n",
344345
"\u001b[33muser_proxy\u001b[0m (to chat_manager):\n",
345346
"\n",
347+
"\u001b[33muser_proxy\u001b[0m (to chat_manager):\n",
346348
"\n",
349+
"\u001b[32m***** Response from calling tool \"terminate_group_chat\" *****\u001b[0m\n",
350+
"[GROUPCHAT_TERMINATE] The session has concluded, and the group chat will now be terminated.\n",
351+
"\u001b[32m*************************************************************\u001b[0m\n",
347352
"\n",
348353
"--------------------------------------------------------------------------------\n"
349354
]
@@ -354,10 +359,9 @@
354359
"await user_proxy.a_initiate_chat( # noqa: F704\n",
355360
" manager,\n",
356361
" message=\"\"\"\n",
357-
"1) Create a timer for 5 seconds.\n",
358-
"2) a stopwatch for 5 seconds.\n",
359-
"3) Pretty print the result as md.\n",
360-
"4) when 1-3 are done, terminate the group chat\"\"\",\n",
362+
"1) Create a timer and a stopwatch for 5 seconds each in parallel.\n",
363+
"2) Pretty print the result as md.\n",
364+
"3) when 1 and 2 are done, terminate the group chat\"\"\",\n",
361365
")"
362366
]
363367
},

0 commit comments

Comments
 (0)