TaskQueueZytron

The TaskQueueZytron class is designed to manage and execute tasks using multiple agents concurrently. This class allows for the orchestration of multiple agents processing tasks from a shared queue, facilitating complex workflows where tasks can be distributed and processed in parallel by different agents.

Attributes

Attribute
Type
Description

agents

List[Agent]

The list of agents in the zytron.

task_queue

queue.Queue

A queue to store tasks for processing.

lock

threading.Lock

A lock for thread synchronization.

autosave_on

bool

Whether to automatically save the zytron metadata.

save_file_path

str

The file path for saving zytron metadata.

workspace_dir

str

The directory path of the workspace.

return_metadata_on

bool

Whether to return the zytron metadata after running.

max_loops

int

The maximum number of loops to run the zytron.

metadata

ZytronRunMetadata

Metadata about the zytron run.

Methods

__init__(self, agents: List[Agent], name: str = "Task-Queue-Zytron", description: str = "A zytron that processes tasks from a queue using multiple agents on different threads.", autosave_on: bool = True, save_file_path: str = "zytron_run_metadata.json", workspace_dir: str = os.getenv("WORKSPACE_DIR"), return_metadata_on: bool = False, max_loops: int = 1, *args, **kwargs)

The constructor initializes the TaskQueueZytron object.

  • Parameters:

  • agents (List[Agent]): The list of agents in the zytron.

  • name (str, optional): The name of the zytron. Defaults to "Task-Queue-Zytron".

  • description (str, optional): The description of the zytron. Defaults to "A zytron that processes tasks from a queue using multiple agents on different threads.".

  • autosave_on (bool, optional): Whether to automatically save the zytron metadata. Defaults to True.

  • save_file_path (str, optional): The file path to save the zytron metadata. Defaults to "zytron_run_metadata.json".

  • workspace_dir (str, optional): The directory path of the workspace. Defaults to os.getenv("WORKSPACE_DIR").

  • return_metadata_on (bool, optional): Whether to return the zytron metadata after running. Defaults to False.

  • max_loops (int, optional): The maximum number of loops to run the zytron. Defaults to 1.

  • *args: Variable length argument list.

  • **kwargs: Arbitrary keyword arguments.

add_task(self, task: str)

Adds a task to the queue.

  • Parameters:

  • task (str): The task to be added to the queue.

run(self)

Runs the zytron by having agents pick up tasks from the queue.

  • Returns:

  • str: JSON string of the zytron run metadata if return_metadata_on is True.

  • Usage Example:

    from zytron import Agent, TaskQueueZytron
    from zytron_models import OpenAIChat
    
    # Initialize the language model
    llm = OpenAIChat()
    
    # Initialize agents
    agent1 = Agent(agent_name="Agent1", llm=llm)
    agent2 = Agent(agent_name="Agent2", llm=llm)
    
    # Create the TaskQueueZytron
    zytron = TaskQueueZytron(agents=[agent1, agent2], max_loops=5)
    
    # Add tasks to the zytron
    zytron.add_task("Analyze the latest market trends")
    zytron.add_task("Generate a summary report")
    
    # Run the zytron
    result = zytron.run()
    print(result)  # Prints the zytron run metadata

This example initializes a TaskQueueZytron with two agents, adds tasks to the queue, and runs the zytron.

save_json_to_file(self)

Saves the zytron run metadata to a JSON file.

export_metadata(self)

Exports the zytron run metadata as a JSON string.

  • Returns:

  • str: JSON string of the zytron run metadata.

Additional Notes

  • The TaskQueueZytron uses threading to process tasks concurrently, which can significantly improve performance for I/O-bound tasks.

  • The reliability_checks method ensures that the zytron is properly configured before running.

  • The zytron automatically handles task distribution among agents and provides detailed metadata about the run.

  • Error handling and logging are implemented to track the execution flow and capture any issues during task processing.

Last updated