Mixture of Agents

The MixtureOfAgentsZytron class represents a configuration of diverse agents operating collaboratively within the Zytron framework. The workflow leverages a structured sequence of parallel, sequential, and final aggregation phases to process tasks efficiently. Specifically, the class adopts a parallel → sequential → parallel → final aggregation methodology, enabling sophisticated multi-step task execution. This design draws inspiration from advanced multi-agent system concepts outlined in the academic paper: ArXiv Preprint.

Class Definition

MixtureOfAgents

class MixtureOfAgents(BaseZytron):

Attributes

Attribute
Type
Description
Default

agents

List[Agent]

The list of agents in the zytron.

None

flow

str

The flow of the zytron.

parallel -> sequential -> parallel -> final output agent

max_loops

int

The maximum number of loops to run.

1

verbose

bool

Flag indicating whether to print verbose output.

True

layers

int

The number of layers in the zytron.

3

rules

str

The rules for the zytron.

None

final_agent

Agent

The agent to handle the final output processing.

None

auto_save

bool

Flag indicating whether to auto-save the metadata to a file.

False

saved_file_name

str

The name of the file where the metadata will be saved.

"moe_zytron.json"

Methods

__init__

Parameters

Parameter
Type
Description
Default

name

str

The name of the zytron.

"MixtureOfAgents"

description

str

A brief description of the zytron.

"A zytron of agents that run in parallel and sequentially."

agents

List[Agent]

The list of agents in the zytron.

None

max_loops

int

The maximum number of loops to run.

1

verbose

bool

Flag indicating whether to print verbose output.

True

layers

int

The number of layers in the zytron.

3

rules

str

The rules for the zytron.

None

final_agent

Agent

The agent to handle the final output processing.

None

auto_save

bool

Flag indicating whether to auto-save the metadata to a file.

False

saved_file_name

str

The name of the file where the metadata will be saved.

"moe_zytron.json"

agent_check

def agent_check(self):

Description

Checks if the provided agents attribute is a list of Agent instances. Raises a TypeError if the validation fails.

Example Usage

moe_zytron = MixtureOfAgents(agents=[agent1, agent2])
moe_zytron.agent_check()  # Validates the agents

final_agent_check

def final_agent_check(self):

Description

Checks if the provided final_agent attribute is an instance of Agent. Raises a TypeError if the validation fails.

Example Usage

moe_zytron = MixtureOfAgents(final_agent=final_agent)
moe_zytron.final_agent_check()  # Validates the final agent

zytron_initialization

def zytron_initialization(self):

Description

Initializes the zytron by logging the zytron name, description, and the number of agents.

Example Usage

moe_zytron = MixtureOfAgents(agents=[agent1, agent2])
moe_zytron.zytron_initialization()  # Initializes the zytron

run

def run(self, task: str = None, *args, **kwargs):

Parameters

Parameter
Type
Description
Default

task

str

The task to be performed by the zytron.

None

*args

tuple

Additional arguments.

None

**kwargs

dict

Additional keyword arguments.

None

Returns

Type
Description

str

The conversation history as a string.

Description

Runs the zytron with the given task, orchestrates the execution of agents through the specified layers, and returns the conversation history.

Example Usage

moe_zytron = MixtureOfAgents(agents=[agent1, agent2], final_agent=final_agent)
history = moe_zytron.run(task="Solve this problem.")
print(history)

Detailed Explanation

Initialization

The __init__ method initializes the zytron with the provided parameters, sets up the conversation rules, and invokes the initialization of the zytron. It also ensures the validity of the agents and final_agent attributes by calling the agent_check and final_agent_check methods respectively.

Agent Validation

The agent_check method validates whether the agents attribute is a list of Agent instances, while the final_agent_check method validates whether the final_agent is an instance of Agent. These checks are crucial to ensure that the zytron operates correctly with the appropriate agent types.

Zytron Initialization

The zytron_initialization method logs essential information about the zytron, including its name, description, and the number of agents. This provides a clear starting point for the zytron's operations and facilitates debugging and monitoring.

Running the Zytron

The run method is the core of the MixtureOfAgents class. It orchestrates the execution of agents through multiple layers, collects their outputs, and processes the final output using the final_agent. The conversation history is maintained and updated throughout this process, allowing for a seamless flow of information and responses.

During each layer, the method iterates over the agents, invokes their run method with the current conversation history, and logs the outputs. These outputs are then added to the conversation, and the history is updated for the next layer.

After all layers are completed, the final output agent processes the entire conversation history, and the metadata is created and optionally saved to a file. This metadata includes details about the layers, agent runs, and final output, providing a comprehensive record of the zytron's execution.

Additional Information and Tips

Common Issues and Solutions

  • Type Errors: Ensure that all agents in the agents list and the final_agent are instances of the Agent class. The agent_check and final_agent_check methods help validate this.

  • Verbose Logging: Use the verbose flag to control the verbosity of the output. This can help with debugging or reduce clutter in the logs.

  • Auto-Save Feature: Utilize the auto_save flag to automatically save the metadata to a file. This can be useful for keeping records of the zytron's operations without manual intervention.

References and Resources

For further reading and background information on the concepts used in the MixtureOfAgents class, refer to the paper: ArXiv Prepint

Usage Examples

Example 1: Basic Initialization and Run

from zytron import MixtureOfAgents, Agent

from zytron_models import OpenAIChat

# Define agents
director = Agent(
    agent_name="Director",
    system_prompt="Directs the tasks for the accountants",
    llm=OpenAIChat(),
    max_loops=1,
    dashboard=False,
    streaming_on=True,
    verbose=True,
    stopping_token="<DONE>",
    state_save_file_type="json",
    saved_state_path="director.json",
)

# Initialize accountant 1
accountant1 = Agent(
    agent_name="Accountant1",
    system_prompt="Prepares financial statements",
    llm=OpenAIChat(),
    max_loops=1,
    dashboard=False,
    streaming_on=True,
    verbose=True,
    stopping_token="<DONE>",
    state_save_file_type="json",
    saved_state_path="accountant1.json",
)

# Initialize accountant 2
accountant2 = Agent(
    agent_name="Accountant2",
    system_prompt="Audits financial records",
    llm=OpenAIChat(),
    max_loops=1,
    dashboard=False,
    streaming_on=True,
    verbose=True,
    stopping_token="<DONE>",
    state_save_file_type="json",
    saved_state_path="accountant2.json",
)


# Initialize the MixtureOfAgents
moe_zytron = MixtureOfAgents(agents=[director, accountant1, accountant2], final_agent=director)

# Run the zytron
history = moe_zytron.run(task="Perform task X.")
print(history)

Example 2: Verbose Output and Auto-Save

from zytron import MixtureOfAgents, Agent

from zytron_models import OpenAIChat

# Define Agents
# Define agents
director = Agent(
    agent_name="Director",
    system_prompt="Directs the tasks for the accountants",
    llm=OpenAIChat(),
    max_loops=1,
    dashboard=False,
    streaming_on=True,
    verbose=True,
    stopping_token="<DONE>",
    state_save_file_type="json",
    saved_state_path="director.json",
)

# Initialize accountant 1
accountant1 = Agent(
    agent_name="Accountant1",
    system_prompt="Prepares financial statements",
    llm=OpenAIChat(),
    max_loops=1,
    dashboard=False,
    streaming_on=True,
    verbose=True,
    stopping_token="<DONE>",
    state_save_file_type="json",
    saved_state_path="accountant1.json",
)

# Initialize accountant 2
accountant2 = Agent(
    agent_name="Accountant2",
    system_prompt="Audits financial records",
    llm=OpenAIChat(),
    max_loops=1,
    dashboard=False,
    streaming_on=True,
    verbose=True,
    stopping_token="<DONE>",
    state_save_file_type="json",
    saved_state_path="accountant2.json",
)

# Initialize the MixtureOfAgents with verbose output and auto-save enabled
moe_zytron = MixtureOfAgents(
    agents=[director, accountant1, accountant2],
    final_agent=director,
    verbose=True,
    auto_save=True
)

# Run the zytron
history = moe_zytron.run(task="Analyze data set Y.")
print(history)

Example 3: Custom Rules and Multiple Layers

from zytron import MixtureOfAgents, Agent

from zytron_models import OpenAIChat

# Define agents
# Initialize the director agent
director = Agent(
    agent_name="Director",
    system_prompt="Directs the tasks for the accountants",
    llm=OpenAIChat(),
    max_loops=1,
    dashboard=False,
    streaming_on=True,
    verbose=True,
    stopping_token="<DONE>",
    state_save_file_type="json",
    saved_state_path="director.json",
)

# Initialize accountant 1
accountant1 = Agent(
    agent_name="Accountant1",
    system_prompt="Prepares financial statements",
    llm=OpenAIChat(),
    max_loops=1,
    dashboard=False,
    streaming_on=True,
    verbose=True,
    stopping_token="<DONE>",
    state_save_file_type="json",
    saved_state_path="accountant1.json",
)

# Initialize accountant 2
accountant2 = Agent(
    agent_name="Accountant2",
    system_prompt="Audits financial records",
    llm=OpenAIChat(),
    max_loops=1,
    dashboard=False,
    streaming_on=True,
    verbose=True,
    stopping_token="<DONE>",
    state_save_file_type="json",
    saved_state_path="accountant2.json",
)

# Initialize the MixtureOfAgents with custom rules and multiple layers
moe_zytron = MixtureOfAgents(
    agents=[director, accountant1, accountant2],
    final_agent=director,
    layers=5,
    rules="Custom rules for the zytron"
)

# Run the zytron
history = moe_zytron.run(task="Optimize process Z.")
print(history)

This comprehensive documentation provides a detailed understanding of the MixtureOfAgents class, its attributes, methods, and usage. The examples illustrate how to initialize and run the zytron, demonstrating its flexibility and capability to handle various tasks and configurations.

Last updated