Re-commit code from #2182 to Microsoft branch (#2233)

This commit is contained in:
Mark Sze
2024-04-02 00:49:13 +11:00
committed by GitHub
parent 54b5bbb6ee
commit 32fbfa233b
2 changed files with 124 additions and 6 deletions

View File

@@ -1241,6 +1241,97 @@ def test_role_for_select_speaker_messages():
assert "role_for_select_speaker_messages cannot be empty or None." in str(e.value)
def test_select_speaker_message_and_prompt_templates():
"""
In this test, two agents are part of a group chat which has customized select speaker message and select speaker prompt templates. Both valid and empty string values will be used.
The expected behaviour is that the customized speaker selection message and prompts will override the default values or throw exceptions if empty.
"""
agent1 = autogen.ConversableAgent(
"Alice",
description="A wonderful employee named Alice.",
human_input_mode="NEVER",
llm_config=False,
)
agent2 = autogen.ConversableAgent(
"Bob",
description="An amazing employee named Bob.",
human_input_mode="NEVER",
llm_config=False,
)
# Customised message, this is always the first message in the context
custom_msg = """You are the CEO of a niche organisation creating small software tools for the healthcare sector with a small team of specialists. Call them in sequence.
The job roles and responsibilities are:
{roles}
You must select only from {agentlist}."""
# Customised prompt, this is always the last message in the context
custom_prompt = """Read the above conversation.
Then select the next job role from {agentlist} to take action.
RETURN ONLY THE NAME OF THE NEXT ROLE."""
# Test empty is_termination_msg function
groupchat = autogen.GroupChat(
agents=[agent1, agent2],
messages=[],
speaker_selection_method="auto",
max_round=10,
select_speaker_message_template=custom_msg,
select_speaker_prompt_template=custom_prompt,
)
# Test with valid strings, checking for the correct string and roles / agentlist to be included
assert groupchat.select_speaker_msg() == custom_msg.replace(
"{roles}", "Alice: A wonderful employee named Alice.\nBob: An amazing employee named Bob."
).replace("{agentlist}", "['Alice', 'Bob']")
assert groupchat.select_speaker_prompt() == custom_prompt.replace("{agentlist}", "['Alice', 'Bob']")
# Test with empty strings
with pytest.raises(ValueError, match="select_speaker_message_template cannot be empty or None."):
groupchat = autogen.GroupChat(
agents=[agent1, agent2],
messages=[],
speaker_selection_method="auto",
max_round=10,
select_speaker_message_template="",
select_speaker_prompt_template="Not empty.",
)
with pytest.raises(ValueError, match="select_speaker_prompt_template cannot be empty or None."):
groupchat = autogen.GroupChat(
agents=[agent1, agent2],
messages=[],
speaker_selection_method="auto",
max_round=10,
select_speaker_message_template="Not empty.",
select_speaker_prompt_template=None,
)
# Test with None
with pytest.raises(ValueError, match="select_speaker_message_template cannot be empty or None."):
groupchat = autogen.GroupChat(
agents=[agent1, agent2],
messages=[],
speaker_selection_method="auto",
max_round=10,
select_speaker_message_template=None,
select_speaker_prompt_template="Not empty.",
)
with pytest.raises(ValueError, match="select_speaker_prompt_template cannot be empty or None."):
groupchat = autogen.GroupChat(
agents=[agent1, agent2],
messages=[],
speaker_selection_method="auto",
max_round=10,
select_speaker_message_template="Not empty.",
select_speaker_prompt_template="",
)
if __name__ == "__main__":
# test_func_call_groupchat()
# test_broadcast()
@@ -1256,5 +1347,6 @@ if __name__ == "__main__":
# test_graceful_exit_before_max_round()
# test_clear_agents_history()
# test_custom_speaker_selection_overrides_transition_graph()
test_role_for_select_speaker_messages()
# test_role_for_select_speaker_messages()
test_select_speaker_message_and_prompt_templates()
# pass