Skip to content

Commit 8d5f176

Browse files
authored
GroupChat handle is_termination_msg (#804)
* Have GroupChatManager check is_termination_msg * Added test cases.
1 parent 3c2a38e commit 8d5f176

File tree

2 files changed

+66
-3
lines changed

2 files changed

+66
-3
lines changed

autogen/agentchat/groupchat.py

+10
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,11 @@ def run_chat(
257257
if message["role"] != "function":
258258
message["name"] = speaker.name
259259
groupchat.messages.append(message)
260+
261+
if self._is_termination_msg(message):
262+
# The conversation is over
263+
break
264+
260265
# broadcast the message to all agents except the speaker
261266
for agent in groupchat.agents:
262267
if agent != speaker:
@@ -302,6 +307,11 @@ async def a_run_chat(
302307
if message["role"] != "function":
303308
message["name"] = speaker.name
304309
groupchat.messages.append(message)
310+
311+
if self._is_termination_msg(message):
312+
# The conversation is over
313+
break
314+
305315
# broadcast the message to all agents except the speaker
306316
for agent in groupchat.agents:
307317
if agent != speaker:

test/agentchat/test_groupchat.py

+56-3
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ def test_agent_mentions():
265265
max_consecutive_auto_reply=2,
266266
human_input_mode="NEVER",
267267
llm_config=False,
268-
default_auto_reply="This is alice sepaking.",
268+
default_auto_reply="This is alice speaking.",
269269
)
270270
agent2 = autogen.ConversableAgent(
271271
"bob",
@@ -330,11 +330,64 @@ def test_agent_mentions():
330330
)
331331

332332

333+
def test_termination():
334+
agent1 = autogen.ConversableAgent(
335+
"alice",
336+
max_consecutive_auto_reply=10,
337+
human_input_mode="NEVER",
338+
llm_config=False,
339+
default_auto_reply="This is alice speaking.",
340+
)
341+
agent2 = autogen.ConversableAgent(
342+
"bob",
343+
max_consecutive_auto_reply=10,
344+
human_input_mode="NEVER",
345+
llm_config=False,
346+
default_auto_reply="This is bob speaking.",
347+
)
348+
agent3 = autogen.ConversableAgent(
349+
"sam",
350+
max_consecutive_auto_reply=10,
351+
human_input_mode="NEVER",
352+
llm_config=False,
353+
default_auto_reply="This is sam speaking. TERMINATE",
354+
)
355+
356+
# Test empty is_termination_msg function
357+
groupchat = autogen.GroupChat(
358+
agents=[agent1, agent2, agent3], messages=[], speaker_selection_method="round_robin", max_round=10
359+
)
360+
361+
group_chat_manager = autogen.GroupChatManager(groupchat=groupchat, llm_config=False, is_termination_msg=None)
362+
363+
agent1.initiate_chat(group_chat_manager, message="'None' is_termination_msg function.")
364+
assert len(groupchat.messages) == 10
365+
366+
# Test user-provided is_termination_msg function
367+
agent1.reset()
368+
agent2.reset()
369+
agent3.reset()
370+
371+
groupchat = autogen.GroupChat(
372+
agents=[agent1, agent2, agent3], messages=[], speaker_selection_method="round_robin", max_round=10
373+
)
374+
375+
group_chat_manager = autogen.GroupChatManager(
376+
groupchat=groupchat,
377+
llm_config=False,
378+
is_termination_msg=lambda x: x.get("content", "").rstrip().find("TERMINATE") >= 0,
379+
)
380+
381+
agent1.initiate_chat(group_chat_manager, message="User-provided is_termination_msg function.")
382+
assert len(groupchat.messages) == 3
383+
384+
333385
if __name__ == "__main__":
334386
# test_func_call_groupchat()
335387
# test_broadcast()
336388
# test_chat_manager()
337389
# test_plugin()
338-
test_speaker_selection_method()
339-
test_n_agents_less_than_3()
390+
# test_speaker_selection_method()
391+
# test_n_agents_less_than_3()
340392
# test_agent_mentions()
393+
test_termination()

0 commit comments

Comments
 (0)