Skip to content

Commit

Permalink
Added test cases for sending introductions.
Browse files Browse the repository at this point in the history
  • Loading branch information
afourney committed Dec 12, 2023
1 parent 5644569 commit 0607bc4
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 3 deletions.
14 changes: 12 additions & 2 deletions autogen/agentchat/groupchat.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,10 +333,11 @@ def run_chat(

if self.send_introductions:
# Broadcast the intro
intro = {"role": "user", "name": self.name, "content": groupchat.introductions_msg()}
intro = groupchat.introductions_msg()
for agent in groupchat.agents:
self.send(intro, agent, request_reply=False, silent=True)
groupchat.append(intro)
# NOTE: We do not also append to groupchat.messages,
# since groupchat handles its own introductions

for i in range(groupchat.max_round):
# set the name to speaker's name if the role is not function
Expand Down Expand Up @@ -388,6 +389,15 @@ async def a_run_chat(
message = messages[-1]
speaker = sender
groupchat = config

if self.send_introductions:
# Broadcast the intro
intro = groupchat.introductions_msg()
for agent in groupchat.agents:
self.a_send(intro, agent, request_reply=False, silent=True)
# NOTE: We do not also append to groupchat.messages,
# since groupchat handles its own introductions

for i in range(groupchat.max_round):
# set the name to speaker's name if the role is not function
if message["role"] != "function":
Expand Down
99 changes: 98 additions & 1 deletion test/agentchat/test_groupchat.py
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,102 @@ def test_next_agent():
assert groupchat.next_agent(agent4, [agent1, agent2, agent3]) == agent1


def test_send_intros():
agent1 = autogen.ConversableAgent(
"alice",
description="The first agent.",
max_consecutive_auto_reply=10,
human_input_mode="NEVER",
llm_config=False,
default_auto_reply="This is alice speaking. TERMINATE",
)
agent2 = autogen.ConversableAgent(
"bob",
description="The second agent.",
max_consecutive_auto_reply=10,
human_input_mode="NEVER",
llm_config=False,
default_auto_reply="This is bob speaking. TERMINATE",
)
agent3 = autogen.ConversableAgent(
"sam",
description="The third agent.",
max_consecutive_auto_reply=10,
human_input_mode="NEVER",
llm_config=False,
default_auto_reply="This is sam speaking. TERMINATE",
)
agent4 = autogen.ConversableAgent(
"sally",
description="The fourth agent.",
max_consecutive_auto_reply=10,
human_input_mode="NEVER",
llm_config=False,
default_auto_reply="This is sally speaking. TERMINATE",
)

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

intro = groupchat.introductions_msg()
assert "The first agent." in intro
assert "The second agent." in intro
assert "The third agent." in intro
assert "The fourth agent." not in intro

intro = groupchat.introductions_msg([agent1, agent2, agent4])
assert "The first agent." in intro
assert "The second agent." in intro
assert "The third agent." not in intro
assert "The fourth agent." in intro

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,
send_introductions=True,
is_termination_msg=lambda x: x.get("content", "").rstrip().find("TERMINATE") >= 0,
)

group_chat_manager.initiate_chat(group_chat_manager, message="The initiating message.")
for a in [agent1, agent2, agent3]:
messages = agent1.chat_messages[group_chat_manager]
assert len(messages) == 3
assert "The first agent." in messages[0]["content"]
assert "The second agent." in messages[0]["content"]
assert "The third agent." in messages[0]["content"]
assert "The initiating message." == messages[1]["content"]
assert messages[2]["content"] == agent1._default_auto_reply

# Reset and start again
agent1.reset()
agent2.reset()
agent3.reset()
agent4.reset()

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

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

group_chat_manager2.initiate_chat(group_chat_manager2, message="The initiating message.")
for a in [agent1, agent2, agent3]:
messages = agent1.chat_messages[group_chat_manager2]
assert len(messages) == 2
assert "The initiating message." == messages[0]["content"]
assert messages[1]["content"] == agent1._default_auto_reply


if __name__ == "__main__":
# test_func_call_groupchat()
# test_broadcast()
Expand All @@ -438,4 +534,5 @@ def test_next_agent():
# test_n_agents_less_than_3()
# test_agent_mentions()
# test_termination()
test_next_agent()
# test_next_agent()
test_send_intros()

0 comments on commit 0607bc4

Please sign in to comment.