GroupChat

GroupChat(
    agents: list[autogen.Agent],
    messages: list[dict],
    max_round: int = 10,
    admin_name: str = 'Admin',
    func_call_filter: bool = True,
    speaker_selection_method: Literal['auto', 'manual', 'random', 'round_robin'] | Callable = 'auto',
    max_retries_for_selecting_speaker: int = 2,
    allow_repeat_speaker: bool | list[autogen.Agent] | None = None,
    allowed_or_disallowed_speaker_transitions: dict | None = None,
    speaker_transitions_type: Literal['allowed', 'disallowed', None] = None,
    enable_clear_history: bool = False,
    send_introductions: bool = False,
    select_speaker_message_template: str = 'You are in a role play game. The following roles are available:\n                \{roles}.\n                Read the following conversation.\n                Then select the next role from \{agentlist} to play. Only return the role.',
    select_speaker_prompt_template: str = 'Read the above conversation. Then select the next role from \{agentlist} to play. Only return the role.',
    select_speaker_auto_multiple_template: str = '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:\n    1. If the context refers to themselves as a speaker e.g. "As the..." , choose that speaker\'s name\n    2. If it refers to the "next" speaker name, choose that name\n    3. Otherwise, choose the first provided speaker\'s name in the context\n    The names are case-sensitive and should not be abbreviated or changed.\n    Respond with ONLY the name of the speaker and DO NOT provide a reason.',
    select_speaker_auto_none_template: str = 'You didn\'t choose a speaker. As a reminder, to determine the speaker use these prioritised rules:\n    1. If the context refers to themselves as a speaker e.g. "As the..." , choose that speaker\'s name\n    2. If it refers to the "next" speaker name, choose that name\n    3. Otherwise, choose the first provided speaker\'s name in the context\n    The names are case-sensitive and should not be abbreviated or changed.\n    The only names that are accepted are \{agentlist}.\n    Respond with ONLY the name of the speaker and DO NOT provide a reason.',
    select_speaker_transform_messages: autogen.agentchat.contrib.capabilities.transform_messages.TransformMessages | None = None,
    select_speaker_auto_verbose: bool | None = False,
    select_speaker_auto_model_client_cls: autogen.ModelClient | list[autogen.ModelClient] | None = None,
    select_speaker_auto_llm_config: dict | Literal[False] | None = None,
    role_for_select_speaker_messages: str | None = 'system'
)

(In preview) A group chat class that contains the following data fields:

  • agents: a list of participating agents.
  • messages: a list of messages in the group chat.
  • max_round: the maximum number of rounds.
  • admin_name: the name of the admin agent if there is one. Default is “Admin”. KeyBoardInterrupt will make the admin agent take over.
  • func_call_filter: whether to enforce function call filter. Default is True. When set to True and when a message is a function call suggestion, the next speaker will be chosen from an agent which contains the corresponding function name in its function_map.
  • select_speaker_message_template: customize the select speaker message (used in “auto” speaker selection), which appears first in the message context and generally includes the agent descriptions and list of agents. If the string contains “\{roles}” it will replaced with the agent’s and their role descriptions. 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 are in a role play game. The following roles are available: \{roles}. Read the following conversation. 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
    2. If it refers to the “next” speaker name, choose that name
    3. Otherwise, choose the first provided speaker’s name in the context The names are case-sensitive and should not be abbreviated or changed. Respond with ONLY the name of the speaker and DO NOT provide a reason.”
  • select_speaker_auto_none_template: customize the follow-up prompt used when selecting a speaker fails with a response that contains no agent names. This prompt guides the LLM to return an agent name and provides a list of agent names. 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 didn’t choose a speaker. As a reminder, 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
    2. If it refers to the “next” speaker name, choose that name
    3. Otherwise, choose the first provided speaker’s name in the context The names are case-sensitive and should not be abbreviated or changed. The only names that are accepted are \{agentlist}. Respond with ONLY the name of the speaker and DO NOT provide a reason.”
  • speaker_selection_method: the method for selecting the next speaker. Default is “auto”. Could be any of the following (case insensitive), will raise ValueError if not recognized:
    • “auto”: the next speaker is selected automatically by LLM.
    • “manual”: the next speaker is selected manually by user input.
    • “random”: the next speaker is selected randomly.
    • “round_robin”: the next speaker is selected in a round robin fashion, i.e., iterating in the same order as provided in agents.
    • a customized speaker selection function (Callable): the function will be called to select the next speaker. The function should take the last speaker and the group chat as input and return one of the following:
      1. an Agent class, it must be one of the agents in the group chat.
      2. a string from [‘auto’, ‘manual’, ‘random’, ‘round_robin’] to select a default method to use.
      3. None, which would terminate the conversation gracefully.
      def custom_speaker_selection_func(
          last_speaker: Agent, groupchat: GroupChat
      ) -> Union[Agent, str, None]:
      
  • max_retries_for_selecting_speaker: the maximum number of times the speaker selection requery process will run. If, during speaker selection, multiple agent names or no agent names are returned by the LLM as the next agent, it will be queried again up to the maximum number of times until a single agent is returned or it exhausts the maximum attempts. Applies only to “auto” speaker selection method. Default is 2.
  • select_speaker_transform_messages: (optional) the message transformations to apply to the nested select speaker agent-to-agent chat messages. Takes a TransformMessages object, defaults to None and is only utilised when the speaker selection method is “auto”.
  • select_speaker_auto_verbose: whether to output the select speaker responses and selections If set to True, the outputs from the two agents in the nested select speaker chat will be output, along with whether the responses were successful, or not, in selecting an agent Applies only to “auto” speaker selection method.
  • allow_repeat_speaker: whether to allow the same speaker to speak consecutively. Default is True, in which case all speakers are allowed to speak consecutively. If allow_repeat_speaker is a list of Agents, then only those listed agents are allowed to repeat. If set to False, then no speakers are allowed to repeat. allow_repeat_speaker and allowed_or_disallowed_speaker_transitions are mutually exclusive.
  • allowed_or_disallowed_speaker_transitions: dict. The keys are source agents, and the values are agents that the key agent can/can’t transit to, depending on speaker_transitions_type. Default is None, which means all agents can transit to all other agents. allow_repeat_speaker and allowed_or_disallowed_speaker_transitions are mutually exclusive.
  • speaker_transitions_type: whether the speaker_transitions_type is a dictionary containing lists of allowed agents or disallowed agents. “allowed” means the allowed_or_disallowed_speaker_transitions is a dictionary containing lists of allowed agents. If set to “disallowed”, then the allowed_or_disallowed_speaker_transitions is a dictionary containing lists of disallowed agents. Must be supplied if allowed_or_disallowed_speaker_transitions is not None.
  • enable_clear_history: enable possibility to clear history of messages for agents manually by providing “clear history” phrase in user prompt. This is experimental feature. See description of GroupChatManager.clear_agents_history function for more info.
  • send_introductions: send a round of introductions at the start of the group chat, so agents know who they can speak to (default: False)
  • select_speaker_auto_model_client_cls: Custom model client class for the internal speaker select agent used during ‘auto’ speaker selection (optional)
  • select_speaker_auto_llm_config: LLM config for the internal speaker select agent used during ‘auto’ speaker selection (optional)
  • role_for_select_speaker_messages: sets the role name for speaker selection when in ‘auto’ mode, typically ‘user’ or ‘system’. (default: ‘system’)
Parameters:
NameDescription
agentsType: list[autogen.Agent]
messagesType: list[dict]
max_roundType: int

Default: 10
admin_nameType: str

Default: ‘Admin’
func_call_filterType: bool

Default: True
speaker_selection_methodType: Literal['auto', 'manual', 'random', 'round_robin'] | Callable

Default: ‘auto’
max_retries_for_selecting_speakerType: int

Default: 2
allow_repeat_speakerType: bool | list[autogen.Agent] | None

Default: None
allowed_or_disallowed_speaker_transitionsType: dict | None

Default: None
speaker_transitions_typeType: Literal['allowed', 'disallowed', None]

Default: None
enable_clear_historyType: bool

Default: False
send_introductionsType: bool

Default: False
select_speaker_message_templateType: str

Default: ‘You are in a role play game. The following roles are available
select_speaker_prompt_templateType: str

Default: ‘Read the above conversation. Then select the next role from {agentlist} to play. Only return the role.‘
select_speaker_auto_multiple_templateType: str

Default: ‘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
select_speaker_auto_none_templateType: str

Default: ‘You didn’t choose a speaker. As a reminder, to determine the speaker use these prioritised rules
select_speaker_transform_messagesType: autogen.agentchat.contrib.capabilities.transform_messages.TransformMessages | None

Default: None
select_speaker_auto_verboseType: bool | None

Default: False
select_speaker_auto_model_client_clsType: autogen.ModelClient | list[autogen.ModelClient] | None

Default: None
select_speaker_auto_llm_configType: dict | Literal[False] | None

Default: None
role_for_select_speaker_messagesType: str | None

Default: ‘system’

Class Attributes

DEFAULT_INTRO_MSG



admin_name



agents



allow_repeat_speaker



allowed_or_disallowed_speaker_transitions



allowed_speaker_transitions_dict



enable_clear_history



func_call_filter



max_retries_for_selecting_speaker



max_round



messages



role_for_select_speaker_messages



select_speaker_auto_llm_config



select_speaker_auto_model_client_cls



select_speaker_auto_multiple_template



select_speaker_auto_none_template



select_speaker_auto_verbose



select_speaker_message_template



select_speaker_prompt_template



select_speaker_transform_messages



send_introductions



speaker_selection_method



speaker_transitions_type



Instance Attributes

agent_names


Return the names of the agents in the group chat.

Instance Methods

a_auto_select_speaker

a_auto_select_speaker(
    self,
    last_speaker: autogen.Agent,
    selector: autogen.ConversableAgent,
    messages: list[dict] | None,
    agents: list[autogen.Agent] | None
) -> autogen.Agent

(Asynchronous) Selects next speaker for the “auto” speaker selection method. Utilises its own two-agent chat to determine the next speaker and supports requerying.

Speaker selection for “auto” speaker selection method:

  1. Create a two-agent chat with a speaker selector agent and a speaker validator agent, like a nested chat
  2. Inject the group messages into the new chat
  3. Run the two-agent chat, evaluating the result of response from the speaker selector agent:
    • If a single agent is provided then we return it and finish. If not, we add an additional message to this nested chat in an attempt to guide the LLM to a single agent response
  4. Chat continues until a single agent is nominated or there are no more attempts left
  5. If we run out of turns and no single agent can be determined, the next speaker in the list of agents is returned
Parameters:
NameDescription
last_speakerType: autogen.Agent
selectorType: autogen.ConversableAgent
messagesType: list[dict] | None
agentsType: list[autogen.Agent] | None
Returns:
TypeDescription
autogen.AgentDict: a counter for mentioned agents.

a_select_speaker

a_select_speaker(
    self,
    last_speaker: autogen.Agent,
    selector: autogen.ConversableAgent
) -> autogen.Agent

Select the next speaker (with requery), asynchronously.

Parameters:
NameDescription
last_speakerType: autogen.Agent
selectorType: autogen.ConversableAgent

agent_by_name

agent_by_name(
    self,
    name: str,
    recursive: bool = False,
    raise_on_name_conflict: bool = False
) -> autogen.Agent | None

Returns the agent with a given name. If recursive is True, it will search in nested teams.

Parameters:
NameDescription
nameType: str
recursiveType: bool

Default: False
raise_on_name_conflictType: bool

Default: False

append

append(
    self,
    message: dict,
    speaker: autogen.Agent
) -> 

Append a message to the group chat. We cast the content to str here so that it can be managed by text-based model.

Parameters:
NameDescription
messageType: dict
speakerType: autogen.Agent

introductions_msg

introductions_msg(self, agents: list[autogen.Agent] | None = None) -> str

Return the system message for selecting the next speaker. This is always the first message in the context.

Parameters:
NameDescription
agentsType: list[autogen.Agent] | None

Default: None

manual_select_speaker

manual_select_speaker(self, agents: list[autogen.Agent] | None = None) -> autogen.Agent | None

Manually select the next speaker.

Parameters:
NameDescription
agentsType: list[autogen.Agent] | None

Default: None

nested_agents

nested_agents(self) -> list[autogen.Agent]

Returns all agents in the group chat manager.


next_agent

next_agent(
    self,
    agent: autogen.Agent,
    agents: list[autogen.Agent] | None = None
) -> autogen.Agent

Return the next agent in the list.

Parameters:
NameDescription
agentType: autogen.Agent
agentsType: list[autogen.Agent] | None

Default: None

random_select_speaker

random_select_speaker(self, agents: list[autogen.Agent] | None = None) -> autogen.Agent | None

Randomly select the next speaker.

Parameters:
NameDescription
agentsType: list[autogen.Agent] | None

Default: None

reset

reset(self) -> 

Reset the group chat.


select_speaker

select_speaker(
    self,
    last_speaker: autogen.Agent,
    selector: autogen.ConversableAgent
) -> autogen.Agent

Select the next speaker (with requery).

Parameters:
NameDescription
last_speakerType: autogen.Agent
selectorType: autogen.ConversableAgent

select_speaker_msg

select_speaker_msg(self, agents: list[autogen.Agent] | None = None) -> str

Return the system message for selecting the next speaker. This is always the first message in the context.

Parameters:
NameDescription
agentsType: list[autogen.Agent] | None

Default: None

select_speaker_prompt

select_speaker_prompt(self, agents: list[autogen.Agent] | None = None) -> str

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.

Parameters:
NameDescription
agentsType: list[autogen.Agent] | None

Default: None