Skip to content

create_swarm_transition

autogen.agentchat.contrib.swarm_agent.create_swarm_transition #

create_swarm_transition(initial_agent, tool_execution, swarm_agent_names, user_agent, swarm_after_work)

Creates a transition function for swarm chat with enclosed state for the use_initial_agent.

PARAMETER DESCRIPTION
initial_agent

The first agent to speak

TYPE: ConversableAgent

tool_execution

The tool execution agent

TYPE: ConversableAgent

swarm_agent_names

List of all agent names

TYPE: list[str]

user_agent

Optional user proxy agent

TYPE: UserProxyAgent

swarm_after_work

Swarm-level after work

TYPE: Union[AfterWorkOption, Callable[..., Any]]

RETURNS DESCRIPTION
Callable[[ConversableAgent, GroupChat], Optional[Union[Agent, Literal['auto']]]]

Callable transition function (for sync and async swarm chats)

Source code in autogen/agentchat/contrib/swarm_agent.py
def create_swarm_transition(
    initial_agent: ConversableAgent,
    tool_execution: ConversableAgent,
    swarm_agent_names: list[str],
    user_agent: Optional[UserProxyAgent],
    swarm_after_work: Optional[Union[AfterWorkOption, Callable[..., Any]]],
) -> Callable[[ConversableAgent, GroupChat], Optional[Union[Agent, Literal["auto"]]]]:
    """Creates a transition function for swarm chat with enclosed state for the use_initial_agent.

    Args:
        initial_agent (ConversableAgent): The first agent to speak
        tool_execution (ConversableAgent): The tool execution agent
        swarm_agent_names (list[str]): List of all agent names
        user_agent (UserProxyAgent): Optional user proxy agent
        swarm_after_work (Union[AfterWorkOption, Callable[..., Any]]): Swarm-level after work

    Returns:
        Callable transition function (for sync and async swarm chats)
    """
    # Create enclosed state, this will be set once per creation so will only be True on the first execution
    # of swarm_transition
    state = {"use_initial_agent": True}

    def swarm_transition(
        last_speaker: ConversableAgent, groupchat: GroupChat
    ) -> Optional[Union[Agent, Literal["auto"]]]:
        result = _determine_next_agent(
            last_speaker=last_speaker,
            groupchat=groupchat,
            initial_agent=initial_agent,
            use_initial_agent=state["use_initial_agent"],
            tool_execution=tool_execution,
            swarm_agent_names=swarm_agent_names,
            user_agent=user_agent,
            swarm_after_work=swarm_after_work,
        )
        state["use_initial_agent"] = False
        return result

    return swarm_transition