Update speaker selector in GroupChat and update some notebooks (#688)

* Add speaker selection methods

* Update groupchat RAG

* Update seed to cache_seed

* Update RetrieveChat notebook

* Update parameter name

* Add test

* Add more tests

* Add mock to test

* Add mock to test

* Fix typo speaking

* Add gracefully exit manual input

* Update round_robin docstring

* Add method checking

* Remove participant roles

* Fix versions in notebooks

* Minimize installation overhead

* Fix missing lower()

* Add comments for try_count 3

* Update warning for n_agents < 3

* Update warning for n_agents < 3

* Add test_n_agents_less_than_3

* Add a function for manual select

* Update version in notebooks

* Fixed bugs that allow speakers to go twice in a row even when allow_repeat_speaker = False

---------

Co-authored-by: Adam Fourney <adamfo@microsoft.com>
This commit is contained in:
Li Jiang
2023-11-17 21:56:11 +08:00
committed by GitHub
parent 3ab8c97eb6
commit 370ebf5e00
8 changed files with 2033 additions and 3721 deletions

View File

@@ -1,4 +1,6 @@
import pytest
import mock
import builtins
import autogen
import json
@@ -8,7 +10,7 @@ def test_func_call_groupchat():
"alice",
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",
@@ -56,7 +58,7 @@ def test_chat_manager():
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",
@@ -83,6 +85,150 @@ def test_chat_manager():
agent2.initiate_chat(group_chat_manager, message={"function_call": {"name": "func", "arguments": '{"x": 1}'}})
def _test_selection_method(method: str):
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(
"charlie",
max_consecutive_auto_reply=10,
human_input_mode="NEVER",
llm_config=False,
default_auto_reply="This is charlie speaking.",
)
groupchat = autogen.GroupChat(
agents=[agent1, agent2, agent3],
messages=[],
max_round=6,
speaker_selection_method=method,
allow_repeat_speaker=False if method == "manual" else True,
)
group_chat_manager = autogen.GroupChatManager(groupchat=groupchat, llm_config=False)
if method == "round_robin":
agent1.initiate_chat(group_chat_manager, message="This is alice speaking.")
assert len(agent1.chat_messages[group_chat_manager]) == 6
assert len(groupchat.messages) == 6
assert [msg["content"] for msg in agent1.chat_messages[group_chat_manager]] == [
"This is alice speaking.",
"This is bob speaking.",
"This is charlie speaking.",
] * 2
elif method == "auto":
agent1.initiate_chat(group_chat_manager, message="This is alice speaking.")
assert len(agent1.chat_messages[group_chat_manager]) == 6
assert len(groupchat.messages) == 6
elif method == "random":
agent1.initiate_chat(group_chat_manager, message="This is alice speaking.")
assert len(agent1.chat_messages[group_chat_manager]) == 6
assert len(groupchat.messages) == 6
elif method == "manual":
for user_input in ["", "q", "x", "1", "10"]:
with mock.patch.object(builtins, "input", lambda _: user_input):
group_chat_manager.reset()
agent1.reset()
agent2.reset()
agent3.reset()
agent1.initiate_chat(group_chat_manager, message="This is alice speaking.")
if user_input == "1":
assert len(agent1.chat_messages[group_chat_manager]) == 6
assert len(groupchat.messages) == 6
assert [msg["content"] for msg in agent1.chat_messages[group_chat_manager]] == [
"This is alice speaking.",
"This is bob speaking.",
"This is alice speaking.",
"This is bob speaking.",
"This is alice speaking.",
"This is bob speaking.",
]
else:
assert len(agent1.chat_messages[group_chat_manager]) == 6
assert len(groupchat.messages) == 6
elif method == "wrong":
with pytest.raises(ValueError):
agent1.initiate_chat(group_chat_manager, message="This is alice speaking.")
def test_speaker_selection_method():
for method in ["auto", "round_robin", "random", "manual", "wrong", "RounD_roBin"]:
_test_selection_method(method)
def _test_n_agents_less_than_3(method):
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.",
)
# test two agents
groupchat = autogen.GroupChat(
agents=[agent1, agent2],
messages=[],
max_round=6,
speaker_selection_method=method,
allow_repeat_speaker=True if method == "random" else False,
)
group_chat_manager = autogen.GroupChatManager(groupchat=groupchat, llm_config=False)
agent1.initiate_chat(group_chat_manager, message="This is alice speaking.")
assert len(agent1.chat_messages[group_chat_manager]) == 6
assert len(groupchat.messages) == 6
if method != "random" or method.lower() == "round_robin":
assert [msg["content"] for msg in agent1.chat_messages[group_chat_manager]] == [
"This is alice speaking.",
"This is bob speaking.",
] * 3
# test one agent
groupchat = autogen.GroupChat(
agents=[agent1],
messages=[],
max_round=6,
speaker_selection_method="round_robin",
allow_repeat_speaker=False,
)
with pytest.raises(ValueError):
group_chat_manager = autogen.GroupChatManager(groupchat=groupchat, llm_config=False)
agent1.initiate_chat(group_chat_manager, message="This is alice speaking.")
# test zero agent
groupchat = autogen.GroupChat(
agents=[],
messages=[],
max_round=6,
speaker_selection_method="round_robin",
allow_repeat_speaker=False,
)
with pytest.raises(ValueError):
group_chat_manager = autogen.GroupChatManager(groupchat=groupchat, llm_config=False)
agent1.initiate_chat(group_chat_manager, message="This is alice speaking.")
def test_n_agents_less_than_3():
for method in ["auto", "round_robin", "random", "RounD_roBin"]:
_test_n_agents_less_than_3(method)
def test_plugin():
# Give another Agent class ability to manage group chat
agent1 = autogen.ConversableAgent(
@@ -90,7 +236,7 @@ def test_plugin():
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",
@@ -185,8 +331,10 @@ def test_agent_mentions():
if __name__ == "__main__":
test_func_call_groupchat()
# test_func_call_groupchat()
# test_broadcast()
test_chat_manager()
# test_chat_manager()
# test_plugin()
test_speaker_selection_method()
test_n_agents_less_than_3()
# test_agent_mentions()