Skip to content

Commit

Permalink
GroupChat handle is_termination_msg (microsoft#804)
Browse files Browse the repository at this point in the history
* Have GroupChatManager check is_termination_msg

* Added test cases.
  • Loading branch information
afourney authored Nov 29, 2023
1 parent 3c2a38e commit 8d5f176
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 3 deletions.
10 changes: 10 additions & 0 deletions autogen/agentchat/groupchat.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,11 @@ def run_chat(
if message["role"] != "function":
message["name"] = speaker.name
groupchat.messages.append(message)

if self._is_termination_msg(message):
# The conversation is over
break

# broadcast the message to all agents except the speaker
for agent in groupchat.agents:
if agent != speaker:
Expand Down Expand Up @@ -302,6 +307,11 @@ async def a_run_chat(
if message["role"] != "function":
message["name"] = speaker.name
groupchat.messages.append(message)

if self._is_termination_msg(message):
# The conversation is over
break

# broadcast the message to all agents except the speaker
for agent in groupchat.agents:
if agent != speaker:
Expand Down
59 changes: 56 additions & 3 deletions test/agentchat/test_groupchat.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ def test_agent_mentions():
max_consecutive_auto_reply=2,
human_input_mode="NEVER",
llm_config=False,
default_auto_reply="This is alice sepaking.",
default_auto_reply="This is alice speaking.",
)
agent2 = autogen.ConversableAgent(
"bob",
Expand Down Expand Up @@ -330,11 +330,64 @@ def test_agent_mentions():
)


def test_termination():
agent1 = autogen.ConversableAgent(
"alice",
max_consecutive_auto_reply=10,
human_input_mode="NEVER",
llm_config=False,
default_auto_reply="This is alice speaking.",
)
agent2 = autogen.ConversableAgent(
"bob",
max_consecutive_auto_reply=10,
human_input_mode="NEVER",
llm_config=False,
default_auto_reply="This is bob speaking.",
)
agent3 = autogen.ConversableAgent(
"sam",
max_consecutive_auto_reply=10,
human_input_mode="NEVER",
llm_config=False,
default_auto_reply="This is sam speaking. TERMINATE",
)

# Test empty is_termination_msg function
groupchat = autogen.GroupChat(
agents=[agent1, agent2, agent3], messages=[], speaker_selection_method="round_robin", max_round=10
)

group_chat_manager = autogen.GroupChatManager(groupchat=groupchat, llm_config=False, is_termination_msg=None)

agent1.initiate_chat(group_chat_manager, message="'None' is_termination_msg function.")
assert len(groupchat.messages) == 10

# Test user-provided is_termination_msg function
agent1.reset()
agent2.reset()
agent3.reset()

groupchat = autogen.GroupChat(
agents=[agent1, agent2, agent3], messages=[], speaker_selection_method="round_robin", max_round=10
)

group_chat_manager = autogen.GroupChatManager(
groupchat=groupchat,
llm_config=False,
is_termination_msg=lambda x: x.get("content", "").rstrip().find("TERMINATE") >= 0,
)

agent1.initiate_chat(group_chat_manager, message="User-provided is_termination_msg function.")
assert len(groupchat.messages) == 3


if __name__ == "__main__":
# test_func_call_groupchat()
# test_broadcast()
# test_chat_manager()
# test_plugin()
test_speaker_selection_method()
test_n_agents_less_than_3()
# test_speaker_selection_method()
# test_n_agents_less_than_3()
# test_agent_mentions()
test_termination()

0 comments on commit 8d5f176

Please sign in to comment.