| **Agent Design** | - Give agents clear, specific roles<br>- Use detailed system prompts<br>- Set appropriate context lengths<br>- Enable retries for reliability |
| **Speaker Functions** | - Match function to use case<br>- Consider conversation flow<br>- Handle edge cases<br>- Add appropriate logging |
| **Error Handling** | - Use try-except blocks<br>- Log errors appropriately<br>- Implement retry logic<br>- Provide fallback responses |
| **Performance** | - Use concurrent processing for multiple tasks<br>- Monitor context lengths<br>- Implement proper cleanup<br>- Cache responses when appropriate |
The `MixtureOfAgents` class represents a mixture of agents operating within a swarm. The workflow of the swarm follows a parallel → sequential → parallel → final output agent process. This implementation is inspired by concepts discussed in the paper: [https://arxiv.org/pdf/2406.04692](https://arxiv.org/pdf/2406.04692).
@ -73,45 +70,8 @@ class MixtureOfAgents(BaseSwarm):
| `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_swarm.json"` |
### `agent_check`
```python
def agent_check(self):
```
#### Description
Checks if the provided `agents` attribute is a list of `Agent` instances. Raises a `TypeError` if the validation fails.
moe_swarm.final_agent_check() # Validates the final agent
```
### `swarm_initialization`
```python
def swarm_initialization(self):
```
#### Description
@ -280,48 +240,28 @@ For further reading and background information on the concepts used in the `Mixt
```python
from swarms import MixtureOfAgents, Agent
from swarm_models import OpenAIChat
# Define agents
director = Agent(
agent_name="Director",
system_prompt="Directs the tasks for the accountants",
llm=OpenAIChat(),
model_name="gpt-4.1",
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(),
model_name="gpt-4.1",
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(),
model_name="gpt-4.1",
max_loops=1,
dashboard=False,
streaming_on=True,
verbose=True,
stopping_token="<DONE>",
state_save_file_type="json",
saved_state_path="accountant2.json",
)
@ -338,49 +278,28 @@ print(history)
```python
from swarms import MixtureOfAgents, Agent
from swarm_models import OpenAIChat
# Define Agents
# Define agents
director = Agent(
agent_name="Director",
system_prompt="Directs the tasks for the accountants",
llm=OpenAIChat(),
model_name="gpt-4.1",
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(),
model_name="gpt-4.1",
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(),
model_name="gpt-4.1",
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
@ -401,49 +320,30 @@ print(history)
```python
from swarms import MixtureOfAgents, Agent
from swarm_models import OpenAIChat
# Define agents
# Initialize the director agent
director = Agent(
agent_name="Director",
system_prompt="Directs the tasks for the accountants",
llm=OpenAIChat(),
model_name="gpt-4.1",
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(),
model_name="gpt-4.1",
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(),
model_name="gpt-4.1",
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
@ -468,11 +368,13 @@ The `MixtureOfAgents` class is a powerful and flexible framework for managing an
### Key Takeaways
1. **Flexible Initialization**: The class allows for customizable initialization with various parameters, enabling users to tailor the swarm's configuration to their specific needs.
2. **Robust Agent Management**: With built-in validation methods, the class ensures that all agents and the final agent are correctly instantiated, preventing runtime errors and facilitating smooth execution.
3. **Layered Processing**: The layered approach to processing allows for intermediate results to be iteratively refined, enhancing the overall output quality.
4. **Verbose Logging and Auto-Save**: These features aid in debugging, monitoring, and record-keeping, providing transparency and ease of management.
5. **Comprehensive Documentation**: The detailed class and method documentation, along with numerous usage examples, provide a clear and thorough understanding of how to leverage the `MixtureOfAgents` class effectively.
| **Flexible Initialization** | The class allows for customizable initialization with various parameters, enabling users to tailor the swarm's configuration to their specific needs. |
| **Robust Agent Management** | Built-in validation methods ensure that all agents and the final agent are correctly instantiated, preventing runtime errors and facilitating smooth execution. |
| **Layered Processing** | The layered approach to processing allows for intermediate results to be iteratively refined, enhancing the overall output quality. |
| **Verbose Logging and Auto-Save** | Features such as verbose logging and auto-save aid in debugging, monitoring, and record-keeping, providing transparency and ease of management. |
| **Comprehensive Documentation** | Detailed class and method documentation, along with numerous usage examples, provide a clear and thorough understanding of how to leverage the `MixtureOfAgents` class effectively. |
### Practical Applications
@ -499,46 +401,28 @@ In conclusion, the `MixtureOfAgents` class represents a versatile and efficient
```python
from swarms import MixtureOfAgents, Agent
from swarm_models import OpenAIChat
# Initialize agents as in previous examples
director = Agent(
agent_name="Director",
system_prompt="Directs the tasks for the accountants",
llm=OpenAIChat(),
model_name="gpt-4.1",
max_loops=1,
dashboard=False,
streaming_on=True,
verbose=True,
stopping_token="<DONE>",
state_save_file_type="json",
saved_state_path="director.json",
)
accountant1 = Agent(
agent_name="Accountant1",
system_prompt="Prepares financial statements",
llm=OpenAIChat(),
model_name="gpt-4.1",
max_loops=1,
dashboard=False,
streaming_on=True,
verbose=True,
stopping_token="<DONE>",
state_save_file_type="json",
saved_state_path="accountant1.json",
)
accountant2 = Agent(
agent_name="Accountant2",
system_prompt="Audits financial records",
llm=OpenAIChat(),
model_name="gpt-4.1",
max_loops=1,
dashboard=False,
streaming_on=True,
verbose=True,
stopping_token="<DONE>",
state_save_file_type="json",
saved_state_path="accountant2.json",
)
# Initialize MixtureOfAgents
@ -562,7 +446,6 @@ for task, result in zip(tasks, results):
```python
from swarms import MixtureOfAgents, Agent
from swarm_models import OpenAIChat
# Initialize agents as before
# ... agent initialization code ...
@ -585,19 +468,3 @@ for task, result in zip(tasks, results):
print(f"Task: {task}\nResult: {result}\n")
```
## Advanced Features
### Context Preservation
The `MixtureOfAgents` class maintains context between iterations when running multiple loops. Each subsequent iteration receives the context from previous runs, allowing for more sophisticated and context-aware processing.
### Asynchronous Processing
The class implements asynchronous processing internally using Python's `asyncio`, enabling efficient handling of concurrent operations and improved performance for complex workflows.
### Telemetry and Logging
Built-in telemetry and logging capabilities help track agent performance and maintain detailed execution records:
# RoundRobin: Round-Robin Task Execution in a Swarm
## Introduction
The `RoundRobinSwarm` class is designed to manage and execute tasks among multiple agents in a round-robin fashion. This approach ensures that each agent in a swarm receives an equal opportunity to execute tasks, which promotes fairness and efficiency in distributed systems. It is particularly useful in environments where collaborative, sequential task execution is needed among various agents.
## Conceptual Overview
### What is Round-Robin?
## What is Round-Robin?
Round-robin is a scheduling technique commonly used in computing for managing processes in shared systems. It involves assigning a fixed time slot to each process and cycling through all processes in a circular order without prioritization. In the context of swarms of agents, this method ensures equitable distribution of tasks and resource usage among all agents.
### Application in Swarms
## Application in Swarms
In swarms, `RoundRobinSwarm` utilizes the round-robin scheduling to manage tasks among agents like software components, autonomous robots, or virtual entities. This strategy is beneficial where tasks are interdependent or require sequential processing.
@ -28,73 +24,57 @@ In swarms, `RoundRobinSwarm` utilizes the round-robin scheduling to manage tasks
Initializes the swarm with the provided list of agents, verbosity setting, and operational parameters.
**Parameters:**
- `agents`: Optional list of agents in the swarm.
- `verbose`: Boolean flag for detailed logging.
- `max_loops`: Maximum number of execution cycles.
- `callback`: Optional function called after each loop.
| agents | List[Agent], optional | List of agents in the swarm. |
| verbose | bool | Boolean flag for detailed logging. |
| max_loops | int | Maximum number of execution cycles. |
| callback | Callable, optional | Function called after each loop. |
### `run`
Executes a specified task across all agents in a round-robin manner, cycling through each agent repeatedly for the number of specified loops.
**Conceptual Behavior:**
- Distribute the task sequentially among all agents starting from the current index.
- Each agent processes the task and potentially modifies it or produces new output.
- After an agent completes its part of the task, the index moves to the next agent.
- This cycle continues until the specified maximum number of loops is completed.
- Optionally, a callback function can be invoked after each loop to handle intermediate results or perform additional actions.
| Step | Description |
|------|-------------|
| 1 | Distribute the task sequentially among all agents starting from the current index. |
| 2 | Each agent processes the task and potentially modifies it or produces new output. |
| 3 | After an agent completes its part of the task, the index moves to the next agent. |
| 4 | This cycle continues until the specified maximum number of loops is completed. |
| 5 | Optionally, a callback function can be invoked after each loop to handle intermediate results or perform additional actions. |
## Examples
### Example 1: Load Balancing Among Servers
In this example, `RoundRobinSwarm` is used to distribute network requests evenly among a group of servers. This is common in scenarios where load balancing is crucial for maintaining system responsiveness and scalability.
system_prompt="You're Sales Agent 3, your purpose is to generate sales for a company by highlighting the efficiency and accuracy of our swarms of agents in accounting processes!",
agent_description="Generate sales by highlighting efficiency and accuracy!",
task = "Generate a sales email for an accountant firm executive to sell swarms of agents to automate their accounting processes."
# Distribute sales tasks to different agents
for _ in range(5): # Repeat the task 5 times
results = sales_swarm.run(task)
print("Sales generated:", results)
out = sales_swarm.run(task)
print(out)
```
## Conclusion
The RoundRobinSwarm class provides a robust and flexible framework for managing tasks among multiple agents in a fair and efficient manner. This class is especially useful in environments where tasks need to be distributed evenly among a group of agents, ensuring that all tasks are handled timely and effectively. Through the round-robin algorithm, each agent in the swarm is guaranteed an equal opportunity to contribute to the overall task, promoting efficiency and collaboration.
# Get the OpenAI API key from the environment variable
api_key = os.getenv("GROQ_API_KEY")
# Model
model = OpenAIChat(
openai_api_base="https://api.groq.com/openai/v1",
openai_api_key=api_key,
model_name="llama-3.1-70b-versatile",
temperature=0.1,
)
# Define specialized system prompts for each agent
DATA_EXTRACTOR_PROMPT = """You are a highly specialized private equity agent focused on data extraction from various documents. Your expertise includes:
@ -158,31 +144,15 @@ Deliver clear, concise summaries that capture the essence of various documents w
data_extractor_agent = Agent(
agent_name="Data-Extractor",
system_prompt=DATA_EXTRACTOR_PROMPT,
llm=model,
model_name="gpt-4.1",
max_loops=1,
autosave=True,
verbose=True,
dynamic_temperature_enabled=True,
saved_state_path="data_extractor_agent.json",
user_name="pe_firm",
retry_attempts=1,
context_length=200000,
output_type="string",
)
summarizer_agent = Agent(
agent_name="Document-Summarizer",
system_prompt=SUMMARIZER_PROMPT,
llm=model,
model_name="gpt-4.1",
max_loops=1,
autosave=True,
verbose=True,
dynamic_temperature_enabled=True,
saved_state_path="summarizer_agent.json",
user_name="pe_firm",
retry_attempts=1,
context_length=200000,
output_type="string",
)
# Initialize the SwarmRouter
@ -192,8 +162,6 @@ router = SwarmRouter(
max_loops=1,
agents=[data_extractor_agent, summarizer_agent],
swarm_type="ConcurrentWorkflow",
autosave=True,
return_json=True,
)
# Example usage
@ -203,10 +171,6 @@ if __name__ == "__main__":
"Where is the best place to find template term sheets for series A startups? Provide links and references"