Added ability to ignore the addition of the select speaker prompt for a group chat (#2726)

Co-authored-by: Eric Zhu <ekzhu@users.noreply.github.com>
This commit is contained in:
Mark Sze 2024-05-23 05:55:26 +10:00 committed by GitHub
parent 4b5f5996a2
commit 3d8fd5cc91
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 62 additions and 30 deletions

View File

@ -39,6 +39,7 @@ class GroupChat:
Then select the next role from {agentlist} to play. Only return the role." 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: - 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." "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: - 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: "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 1. If the context refers to themselves as a speaker e.g. "As the..." , choose that speaker's name
@ -225,8 +226,8 @@ class GroupChat:
if self.select_speaker_message_template is None or len(self.select_speaker_message_template) == 0: 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.") 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: if self.select_speaker_prompt_template is not None and len(self.select_speaker_prompt_template) == 0:
raise ValueError("select_speaker_prompt_template cannot be empty or None.") self.select_speaker_prompt_template = None
if self.role_for_select_speaker_messages is None or len(self.role_for_select_speaker_messages) == 0: 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.") raise ValueError("role_for_select_speaker_messages cannot be empty or None.")
@ -330,7 +331,13 @@ class GroupChat:
return return_msg return return_msg
def select_speaker_prompt(self, agents: Optional[List[Agent]] = None) -> str: 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: if agents is None:
agents = self.agents agents = self.agents
@ -624,23 +631,35 @@ class GroupChat:
remove_other_reply_funcs=True, 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 # Agent for selecting a single agent name from the response
speaker_selection_agent = ConversableAgent( speaker_selection_agent = ConversableAgent(
"speaker_selection_agent", "speaker_selection_agent",
system_message=self.select_speaker_msg(agents), 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, llm_config=selector.llm_config,
human_input_mode="NEVER", # Suppresses some extra terminal outputs, outputs will be handled by select_speaker_auto_verbose 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 # Run the speaker selection chat
result = checking_agent.initiate_chat( result = checking_agent.initiate_chat(
speaker_selection_agent, speaker_selection_agent,
cache=None, # don't use caching for the speaker selection chat cache=None, # don't use caching for the speaker selection chat
message={ message=start_message,
"content": self.select_speaker_prompt(agents),
"override_role": self.role_for_select_speaker_messages,
},
max_turns=2 max_turns=2
* max(1, max_attempts), # Limiting the chat to the number of attempts, including the initial one * max(1, max_attempts), # Limiting the chat to the number of attempts, including the initial one
clear_history=False, clear_history=False,
@ -711,6 +730,8 @@ class GroupChat:
remove_other_reply_funcs=True, 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 # Agent for selecting a single agent name from the response
speaker_selection_agent = ConversableAgent( speaker_selection_agent = ConversableAgent(
"speaker_selection_agent", "speaker_selection_agent",
@ -720,11 +741,20 @@ class GroupChat:
human_input_mode="NEVER", # Suppresses some extra terminal outputs, outputs will be handled by select_speaker_auto_verbose 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 # Run the speaker selection chat
result = await checking_agent.a_initiate_chat( result = await checking_agent.a_initiate_chat(
speaker_selection_agent, speaker_selection_agent,
cache=None, # don't use caching for the speaker selection chat cache=None, # don't use caching for the speaker selection chat
message=self.select_speaker_prompt(agents), message=start_message,
max_turns=2 max_turns=2
* max(1, max_attempts), # Limiting the chat to the number of attempts, including the initial one * max(1, max_attempts), # Limiting the chat to the number of attempts, including the initial one
clear_history=False, clear_history=False,

View File

@ -1321,7 +1321,7 @@ def test_select_speaker_message_and_prompt_templates():
select_speaker_prompt_template="Not empty.", select_speaker_prompt_template="Not empty.",
) )
with pytest.raises(ValueError, match="select_speaker_prompt_template cannot be empty or None."): # Will not throw an exception, prompt can be empty/None (empty is converted to None)
groupchat = autogen.GroupChat( groupchat = autogen.GroupChat(
agents=[agent1, agent2], agents=[agent1, agent2],
messages=[], messages=[],
@ -1331,6 +1331,8 @@ def test_select_speaker_message_and_prompt_templates():
select_speaker_prompt_template="", select_speaker_prompt_template="",
) )
assert groupchat.select_speaker_prompt_template is None
# Test with None # Test with None
with pytest.raises(ValueError, match="select_speaker_message_template cannot be empty or None."): with pytest.raises(ValueError, match="select_speaker_message_template cannot be empty or None."):
groupchat = autogen.GroupChat( groupchat = autogen.GroupChat(
@ -1342,7 +1344,7 @@ def test_select_speaker_message_and_prompt_templates():
select_speaker_prompt_template="Not empty.", select_speaker_prompt_template="Not empty.",
) )
with pytest.raises(ValueError, match="select_speaker_prompt_template cannot be empty or None."): # Will not throw an exception, prompt can be empty/None (empty is converted to None)
groupchat = autogen.GroupChat( groupchat = autogen.GroupChat(
agents=[agent1, agent2], agents=[agent1, agent2],
messages=[], messages=[],
@ -1352,6 +1354,8 @@ def test_select_speaker_message_and_prompt_templates():
select_speaker_prompt_template=None, select_speaker_prompt_template=None,
) )
assert groupchat.select_speaker_prompt_template is None
def test_speaker_selection_agent_name_match(): def test_speaker_selection_agent_name_match():
""" """
@ -2023,14 +2027,12 @@ if __name__ == "__main__":
# test_clear_agents_history() # test_clear_agents_history()
# test_custom_speaker_selection_overrides_transition_graph() # 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() test_select_speaker_message_and_prompt_templates()
# test_speaker_selection_agent_name_match() # test_speaker_selection_agent_name_match()
# test_role_for_reflection_summary() # test_role_for_reflection_summary()
# test_speaker_selection_auto_process_result() # test_speaker_selection_auto_process_result()
# test_speaker_selection_validate_speaker_name() # test_speaker_selection_validate_speaker_name()
# test_select_speaker_auto_messages() # test_select_speaker_auto_messages()
# test_speaker_selection_auto_process_result()
# test_speaker_selection_validate_speaker_name()
# test_select_speaker_auto_messages() # test_select_speaker_auto_messages()
# test_manager_messages_to_string() # test_manager_messages_to_string()
# test_manager_messages_from_string() # test_manager_messages_from_string()