Skip to content

Commit

Permalink
Added ability to ignore the addition of the select speaker prompt for…
Browse files Browse the repository at this point in the history
… a group chat (microsoft#2726)

Co-authored-by: Eric Zhu <[email protected]>
  • Loading branch information
marklysze and ekzhu authored May 22, 2024
1 parent 4b5f599 commit 3d8fd5c
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 30 deletions.
48 changes: 39 additions & 9 deletions autogen/agentchat/groupchat.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class GroupChat:
Then select the next role from {agentlist} to play. Only return the role."
- select_speaker_prompt_template: customize the select speaker prompt (used in "auto" speaker selection), which appears last in the message context and generally includes the list of agents and guidance for the LLM to select the next agent. If the string contains "{agentlist}" it will be replaced with a comma-separated list of agent names in square brackets. The default value is:
"Read the above conversation. Then select the next role from {agentlist} to play. Only return the role."
To ignore this prompt being used, set this to None. If set to None, ensure your instructions for selecting a speaker are in the select_speaker_message_template string.
- select_speaker_auto_multiple_template: customize the follow-up prompt used when selecting a speaker fails with a response that contains multiple agent names. This prompt guides the LLM to return just one agent name. Applies only to "auto" speaker selection method. If the string contains "{agentlist}" it will be replaced with a comma-separated list of agent names in square brackets. The default value is:
"You provided more than one name in your text, please return just the name of the next speaker. To determine the speaker use these prioritised rules:
1. If the context refers to themselves as a speaker e.g. "As the..." , choose that speaker's name
Expand Down Expand Up @@ -225,8 +226,8 @@ def __post_init__(self):
if self.select_speaker_message_template is None or len(self.select_speaker_message_template) == 0:
raise ValueError("select_speaker_message_template cannot be empty or None.")

if self.select_speaker_prompt_template is None or len(self.select_speaker_prompt_template) == 0:
raise ValueError("select_speaker_prompt_template cannot be empty or None.")
if self.select_speaker_prompt_template is not None and len(self.select_speaker_prompt_template) == 0:
self.select_speaker_prompt_template = None

if self.role_for_select_speaker_messages is None or len(self.role_for_select_speaker_messages) == 0:
raise ValueError("role_for_select_speaker_messages cannot be empty or None.")
Expand Down Expand Up @@ -330,7 +331,13 @@ def select_speaker_msg(self, agents: Optional[List[Agent]] = None) -> str:
return return_msg

def select_speaker_prompt(self, agents: Optional[List[Agent]] = None) -> str:
"""Return the floating system prompt selecting the next speaker. This is always the *last* message in the context."""
"""Return the floating system prompt selecting the next speaker.
This is always the *last* message in the context.
Will return None if the select_speaker_prompt_template is None."""

if self.select_speaker_prompt_template is None:
return None

if agents is None:
agents = self.agents

Expand Down Expand Up @@ -624,23 +631,35 @@ def validate_speaker_name(recipient, messages, sender, config) -> Tuple[bool, Un
remove_other_reply_funcs=True,
)

# NOTE: Do we have a speaker prompt (select_speaker_prompt_template is not None)? If we don't, we need to feed in the last message to start the nested chat

# Agent for selecting a single agent name from the response
speaker_selection_agent = ConversableAgent(
"speaker_selection_agent",
system_message=self.select_speaker_msg(agents),
chat_messages={checking_agent: messages},
chat_messages=(
{checking_agent: messages}
if self.select_speaker_prompt_template is not None
else {checking_agent: messages[:-1]}
),
llm_config=selector.llm_config,
human_input_mode="NEVER", # Suppresses some extra terminal outputs, outputs will be handled by select_speaker_auto_verbose
)

# Create the starting message
if self.select_speaker_prompt_template is not None:
start_message = {
"content": self.select_speaker_prompt(agents),
"override_role": self.role_for_select_speaker_messages,
}
else:
start_message = messages[-1]

# Run the speaker selection chat
result = checking_agent.initiate_chat(
speaker_selection_agent,
cache=None, # don't use caching for the speaker selection chat
message={
"content": self.select_speaker_prompt(agents),
"override_role": self.role_for_select_speaker_messages,
},
message=start_message,
max_turns=2
* max(1, max_attempts), # Limiting the chat to the number of attempts, including the initial one
clear_history=False,
Expand Down Expand Up @@ -711,6 +730,8 @@ def validate_speaker_name(recipient, messages, sender, config) -> Tuple[bool, Un
remove_other_reply_funcs=True,
)

# NOTE: Do we have a speaker prompt (select_speaker_prompt_template is not None)? If we don't, we need to feed in the last message to start the nested chat

# Agent for selecting a single agent name from the response
speaker_selection_agent = ConversableAgent(
"speaker_selection_agent",
Expand All @@ -720,11 +741,20 @@ def validate_speaker_name(recipient, messages, sender, config) -> Tuple[bool, Un
human_input_mode="NEVER", # Suppresses some extra terminal outputs, outputs will be handled by select_speaker_auto_verbose
)

# Create the starting message
if self.select_speaker_prompt_template is not None:
start_message = {
"content": self.select_speaker_prompt(agents),
"override_role": self.role_for_select_speaker_messages,
}
else:
start_message = messages[-1]

# Run the speaker selection chat
result = await checking_agent.a_initiate_chat(
speaker_selection_agent,
cache=None, # don't use caching for the speaker selection chat
message=self.select_speaker_prompt(agents),
message=start_message,
max_turns=2
* max(1, max_attempts), # Limiting the chat to the number of attempts, including the initial one
clear_history=False,
Expand Down
44 changes: 23 additions & 21 deletions test/agentchat/test_groupchat.py
Original file line number Diff line number Diff line change
Expand Up @@ -1321,15 +1321,17 @@ def test_select_speaker_message_and_prompt_templates():
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="",
)
# Will not throw an exception, prompt can be empty/None (empty is converted to 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="",
)

assert groupchat.select_speaker_prompt_template is None

# Test with None
with pytest.raises(ValueError, match="select_speaker_message_template cannot be empty or None."):
Expand All @@ -1342,15 +1344,17 @@ def test_select_speaker_message_and_prompt_templates():
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,
)
# Will not throw an exception, prompt can be empty/None (empty is converted to 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,
)

assert groupchat.select_speaker_prompt_template is None


def test_speaker_selection_agent_name_match():
Expand Down Expand Up @@ -2023,14 +2027,12 @@ def test_manager_resume_messages():
# test_clear_agents_history()
# test_custom_speaker_selection_overrides_transition_graph()
# test_role_for_select_speaker_messages()
# test_select_speaker_message_and_prompt_templates()
test_select_speaker_message_and_prompt_templates()
# test_speaker_selection_agent_name_match()
# test_role_for_reflection_summary()
# test_speaker_selection_auto_process_result()
# test_speaker_selection_validate_speaker_name()
# test_select_speaker_auto_messages()
# test_speaker_selection_auto_process_result()
# test_speaker_selection_validate_speaker_name()
# test_select_speaker_auto_messages()
# test_manager_messages_to_string()
# test_manager_messages_from_string()
Expand Down

0 comments on commit 3d8fd5c

Please sign in to comment.