Skip to content

Commit 4324b27

Browse files
authored
Allow GroupChat to recieve messages from outside. (#912)
1 parent 69aaea5 commit 4324b27

File tree

2 files changed

+56
-3
lines changed

2 files changed

+56
-3
lines changed

autogen/agentchat/groupchat.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,15 @@ def agent_by_name(self, name: str) -> Agent:
6666

6767
def next_agent(self, agent: Agent, agents: List[Agent]) -> Agent:
6868
"""Return the next agent in the list."""
69+
70+
# What index is the agent? (-1 if not present)
71+
idx = self.agent_names.index(agent.name) if agent.name in self.agent_names else -1
72+
73+
# Return the next agent
6974
if agents == self.agents:
70-
return agents[(self.agent_names.index(agent.name) + 1) % len(agents)]
75+
return agents[(idx + 1) % len(agents)]
7176
else:
72-
offset = self.agent_names.index(agent.name) + 1
77+
offset = idx + 1
7378
for i in range(len(self.agents)):
7479
if self.agents[(offset + i) % len(self.agents)] in agents:
7580
return self.agents[(offset + i) % len(self.agents)]

test/agentchat/test_groupchat.py

+49-1
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,53 @@ def test_termination():
382382
assert len(groupchat.messages) == 3
383383

384384

385+
def test_next_agent():
386+
agent1 = autogen.ConversableAgent(
387+
"alice",
388+
max_consecutive_auto_reply=10,
389+
human_input_mode="NEVER",
390+
llm_config=False,
391+
default_auto_reply="This is alice speaking.",
392+
)
393+
agent2 = autogen.ConversableAgent(
394+
"bob",
395+
max_consecutive_auto_reply=10,
396+
human_input_mode="NEVER",
397+
llm_config=False,
398+
default_auto_reply="This is bob speaking.",
399+
)
400+
agent3 = autogen.ConversableAgent(
401+
"sam",
402+
max_consecutive_auto_reply=10,
403+
human_input_mode="NEVER",
404+
llm_config=False,
405+
default_auto_reply="This is sam speaking.",
406+
)
407+
agent4 = autogen.ConversableAgent(
408+
"sally",
409+
max_consecutive_auto_reply=10,
410+
human_input_mode="NEVER",
411+
llm_config=False,
412+
default_auto_reply="This is sally speaking.",
413+
)
414+
415+
# Test empty is_termination_msg function
416+
groupchat = autogen.GroupChat(
417+
agents=[agent1, agent2, agent3], messages=[], speaker_selection_method="round_robin", max_round=10
418+
)
419+
420+
assert groupchat.next_agent(agent1, [agent1, agent2, agent3]) == agent2
421+
assert groupchat.next_agent(agent2, [agent1, agent2, agent3]) == agent3
422+
assert groupchat.next_agent(agent3, [agent1, agent2, agent3]) == agent1
423+
424+
assert groupchat.next_agent(agent1, [agent1, agent3]) == agent3
425+
assert groupchat.next_agent(agent3, [agent1, agent3]) == agent1
426+
427+
assert groupchat.next_agent(agent2, [agent1, agent3]) == agent3
428+
assert groupchat.next_agent(agent4, [agent1, agent3]) == agent1
429+
assert groupchat.next_agent(agent4, [agent1, agent2, agent3]) == agent1
430+
431+
385432
if __name__ == "__main__":
386433
# test_func_call_groupchat()
387434
# test_broadcast()
@@ -390,4 +437,5 @@ def test_termination():
390437
# test_speaker_selection_method()
391438
# test_n_agents_less_than_3()
392439
# test_agent_mentions()
393-
test_termination()
440+
# test_termination()
441+
test_next_agent()

0 commit comments

Comments
 (0)