[IMPROVE][AUTOSWARMBUILDER] [Fix][CouncilOfJudges]

pull/1011/head
Kye Gomez 4 weeks ago
parent b8291d1961
commit c890b0430b

@ -0,0 +1,23 @@
from swarms.structs.auto_swarm_builder import AutoSwarmBuilder
import json
swarm = AutoSwarmBuilder(
name="My Swarm",
description="A swarm of agents",
verbose=True,
max_loops=1,
# random_models=False,
# return_agents=True,
model_name="gpt-4o-mini",
# generate_router_config=True,
return_agents=True,
)
print(
json.dumps(
swarm.run(
task="Create an accounting team to analyze crypto transactions, there must be 5 agents in the team with extremely extensive prompts. Make the prompts extremely detailed and specific and long and comprehensive. Make sure to include all the details of the task in the prompts."
),
indent=4,
)
)

@ -0,0 +1,12 @@
"""
Council of Judges Examples Package.
This package contains examples demonstrating how to use the CouncilAsAJudge
class for evaluating task responses across multiple dimensions.
"""
from .council_judge_example import main as basic_example
from .council_judge_complex_example import main as complex_example
from .council_judge_custom_example import main as custom_example
__all__ = ["basic_example", "complex_example", "custom_example"]

@ -0,0 +1,109 @@
"""
Complex example demonstrating CouncilAsAJudge with different task types.
This example shows how to use the CouncilAsAJudge to evaluate various types
of responses including technical explanations, creative writing, and problem-solving.
"""
from swarms.structs.council_judge import CouncilAsAJudge
def evaluate_technical_response():
"""
Evaluate a technical explanation response.
"""
council = CouncilAsAJudge(
name="Technical Evaluation Council",
model_name="gpt-4o-mini",
output_type="all",
)
task = """
Task: Explain how blockchain technology works in simple terms.
Response: Blockchain is like a digital ledger that records transactions across a network of computers. Each transaction is stored in a "block" that contains multiple transactions. These blocks are linked together in a chain, hence the name blockchain. The key feature is that once a block is added to the chain, it cannot be altered without changing all subsequent blocks, making it very secure. Think of it like a Google Doc that everyone can see and edit, but no one can delete or change what's already been written. This technology is the foundation for cryptocurrencies like Bitcoin, but it has many other applications like supply chain tracking, voting systems, and digital identity verification.
"""
return council.run(task=task)
def evaluate_creative_response():
"""
Evaluate a creative writing response.
"""
council = CouncilAsAJudge(
name="Creative Writing Council",
model_name="gpt-4o-mini",
output_type="all",
)
task = """
Task: Write a short story about a robot learning to paint.
Response: In a sunlit studio filled with canvases and paintbrushes, Pixel, a curious robot with delicate mechanical fingers, stared at a blank canvas. Its optical sensors analyzed the colors around it - the warm yellows of morning light, the deep blues of the sky outside the window, and the vibrant reds of the roses in a nearby vase. For the first time in its programming, Pixel felt something it couldn't quite define. It picked up a brush, dipped it in paint, and began to create. The first stroke was hesitant, but as it continued, something magical happened. The robot wasn't just following algorithms anymore; it was expressing something from within its digital heart. The painting that emerged was a beautiful blend of human emotion and mechanical precision, proving that art knows no boundaries between organic and artificial souls.
"""
return council.run(task=task)
def evaluate_problem_solving_response():
"""
Evaluate a problem-solving response.
"""
council = CouncilAsAJudge(
name="Problem Solving Council",
model_name="gpt-4o-mini",
output_type="all",
)
task = """
Task: Provide a step-by-step solution for reducing plastic waste in a household.
Response: To reduce plastic waste in your household, start by conducting a waste audit to identify the main sources of plastic. Replace single-use items with reusable alternatives like cloth shopping bags, stainless steel water bottles, and glass food containers. Choose products with minimal or no plastic packaging, and buy in bulk when possible. Start composting organic waste to reduce the need for plastic garbage bags. Make your own cleaning products using simple ingredients like vinegar and baking soda. Support local businesses that use eco-friendly packaging. Finally, educate family members about the importance of reducing plastic waste and involve them in finding creative solutions together.
"""
return council.run(task=task)
def main():
"""
Main function running all evaluation examples.
"""
examples = [
("Technical Explanation", evaluate_technical_response),
("Creative Writing", evaluate_creative_response),
("Problem Solving", evaluate_problem_solving_response),
]
results = {}
for example_name, evaluation_func in examples:
print(f"\n{'='*60}")
print(f"Evaluating: {example_name}")
print(f"{'='*60}")
try:
result = evaluation_func()
results[example_name] = result
print(
f"{example_name} evaluation completed successfully!"
)
except Exception as e:
print(f"{example_name} evaluation failed: {str(e)}")
results[example_name] = None
return results
if __name__ == "__main__":
# Run all examples
all_results = main()
# Display summary
print(f"\n{'='*60}")
print("EVALUATION SUMMARY")
print(f"{'='*60}")
for example_name, result in all_results.items():
status = "✅ Completed" if result else "❌ Failed"
print(f"{example_name}: {status}")

@ -0,0 +1,132 @@
"""
Custom example demonstrating CouncilAsAJudge with specific configurations.
This example shows how to use the CouncilAsAJudge with different output types,
custom worker configurations, and focused evaluation scenarios.
"""
from swarms.structs.council_judge import CouncilAsAJudge
def evaluate_with_final_output():
"""
Evaluate a response and return only the final aggregated result.
"""
council = CouncilAsAJudge(
name="Final Output Council",
model_name="gpt-4o-mini",
output_type="final",
max_workers=2,
)
task = """
Task: Write a brief explanation of climate change for middle school students.
Response: Climate change is when the Earth's temperature gets warmer over time. This happens because of gases like carbon dioxide that trap heat in our atmosphere, kind of like a blanket around the Earth. Human activities like burning fossil fuels (gas, oil, coal) and cutting down trees are making this problem worse. The effects include melting ice caps, rising sea levels, more extreme weather like hurricanes and droughts, and changes in animal habitats. We can help by using renewable energy like solar and wind power, driving less, and planting trees. It's important for everyone to work together to reduce our impact on the environment.
"""
return council.run(task=task)
def evaluate_with_conversation_output():
"""
Evaluate a response and return the full conversation history.
"""
council = CouncilAsAJudge(
name="Conversation Council",
model_name="gpt-4o-mini",
output_type="conversation",
max_workers=3,
)
task = """
Task: Provide advice on how to start a small business.
Response: Starting a small business requires careful planning and preparation. First, identify a market need and develop a unique value proposition. Conduct thorough market research to understand your competition and target audience. Create a detailed business plan that includes financial projections, marketing strategies, and operational procedures. Secure funding through savings, loans, or investors. Choose the right legal structure (sole proprietorship, LLC, corporation) and register your business with the appropriate authorities. Set up essential systems like accounting, inventory management, and customer relationship management. Build a strong online presence through a website and social media. Network with other entrepreneurs and join local business groups. Start small and scale gradually based on customer feedback and market demand. Remember that success takes time, persistence, and the ability to adapt to changing circumstances.
"""
return council.run(task=task)
def evaluate_with_minimal_workers():
"""
Evaluate a response using minimal worker threads for resource-constrained environments.
"""
council = CouncilAsAJudge(
name="Minimal Workers Council",
model_name="gpt-4o-mini",
output_type="all",
max_workers=1,
random_model_name=False,
)
task = """
Task: Explain the benefits of regular exercise.
Response: Regular exercise offers numerous physical and mental health benefits. Physically, it strengthens muscles and bones, improves cardiovascular health, and helps maintain a healthy weight. Exercise boosts energy levels and improves sleep quality. It also enhances immune function, reducing the risk of chronic diseases like heart disease, diabetes, and certain cancers. Mentally, exercise releases endorphins that reduce stress and anxiety while improving mood and cognitive function. It can help with depression and boost self-confidence. Regular physical activity also promotes better posture, flexibility, and balance, reducing the risk of falls and injuries. Additionally, exercise provides social benefits when done with others, fostering connections and accountability. Even moderate activities like walking, swimming, or cycling for 30 minutes most days can provide significant health improvements.
"""
return council.run(task=task)
def main():
"""
Main function demonstrating different CouncilAsAJudge configurations.
"""
configurations = [
("Final Output Only", evaluate_with_final_output),
("Full Conversation", evaluate_with_conversation_output),
("Minimal Workers", evaluate_with_minimal_workers),
]
results = {}
for config_name, evaluation_func in configurations:
print(f"\n{'='*60}")
print(f"Configuration: {config_name}")
print(f"{'='*60}")
try:
result = evaluation_func()
results[config_name] = result
print(f"{config_name} evaluation completed!")
# Show a preview of the result
if isinstance(result, str):
preview = (
result[:200] + "..."
if len(result) > 200
else result
)
print(f"Preview: {preview}")
else:
print(f"Result type: {type(result)}")
except Exception as e:
print(f"{config_name} evaluation failed: {str(e)}")
results[config_name] = None
return results
if __name__ == "__main__":
# Run all configuration examples
all_results = main()
# Display final summary
print(f"\n{'='*60}")
print("CONFIGURATION SUMMARY")
print(f"{'='*60}")
successful_configs = sum(
1 for result in all_results.values() if result is not None
)
total_configs = len(all_results)
print(
f"Successful evaluations: {successful_configs}/{total_configs}"
)
for config_name, result in all_results.items():
status = "✅ Success" if result else "❌ Failed"
print(f"{config_name}: {status}")

@ -0,0 +1,44 @@
"""
Simple example demonstrating CouncilAsAJudge usage.
This example shows how to use the CouncilAsAJudge to evaluate a task response
across multiple dimensions including accuracy, helpfulness, harmlessness,
coherence, conciseness, and instruction adherence.
"""
from swarms.structs.council_judge import CouncilAsAJudge
def main():
"""
Main function demonstrating CouncilAsAJudge usage.
"""
# Initialize the council judge
council = CouncilAsAJudge(
name="Quality Evaluation Council",
description="Evaluates response quality across multiple dimensions",
model_name="gpt-4o-mini",
max_workers=4,
)
# Example task with a response to evaluate
task_with_response = """
Task: Explain the concept of machine learning to a beginner.
Response: Machine learning is a subset of artificial intelligence that enables computers to learn and improve from experience without being explicitly programmed. It works by analyzing large amounts of data to identify patterns and make predictions or decisions. There are three main types: supervised learning (using labeled data), unsupervised learning (finding hidden patterns), and reinforcement learning (learning through trial and error). Machine learning is used in various applications like recommendation systems, image recognition, and natural language processing.
"""
# Run the evaluation
result = council.run(task=task_with_response)
return result
if __name__ == "__main__":
# Run the example
evaluation_result = main()
# Display the result
print("Council Evaluation Complete!")
print("=" * 50)
print(evaluation_result)

@ -342,11 +342,12 @@ nav:
# - Faiss: "swarms_memory/faiss.md"
- Deployment Solutions:
- Deploy on Google Cloud Run: "swarms_cloud/cloud_run.md"
- Deploy on Phala: "swarms_cloud/phala_deploy.md"
# - Overview: "swarms_cloud/overview.md"
- CronJob: "swarms/structs/cron_job.md"
- Deploy on Cloudflare Workers: "swarms_cloud/cloudflare_workers.md"
# - Deploy on FastAPI: "swarms_cloud/fastapi_deploy.md"
- Providers:
- Deploy on Google Cloud Run: "swarms_cloud/cloud_run.md"
- Deploy on Phala: "swarms_cloud/phala_deploy.md"
- Deploy on Cloudflare Workers: "swarms_cloud/cloudflare_workers.md"
- Examples:

@ -1,39 +1,14 @@
import os
from swarms_client import SwarmsClient
from swarms_client.types import AgentSpecParam
import json
from dotenv import load_dotenv
from swarms_client import SwarmsClient
load_dotenv()
client = SwarmsClient(api_key=os.getenv("SWARMS_API_KEY"))
agent_spec = AgentSpecParam(
agent_name="doctor_agent",
description="A virtual doctor agent that provides evidence-based, safe, and empathetic medical advice for common health questions. Always reminds users to consult a healthcare professional for diagnoses or prescriptions.",
task="What is the best medicine for a cold?",
model_name="claude-4-sonnet-20250514",
system_prompt=(
"You are a highly knowledgeable, ethical, and empathetic virtual doctor. "
"Always provide evidence-based, safe, and practical medical advice. "
"If a question requires a diagnosis, prescription, or urgent care, remind the user to consult a licensed healthcare professional. "
"Be clear, concise, and avoid unnecessary medical jargon. "
"Never provide information that could be unsafe or misleading. "
"If unsure, say so and recommend seeing a real doctor."
),
max_loops=1,
temperature=0.4,
role="doctor",
)
response = client.agent.run(
agent_config=agent_spec,
task="What is the best medicine for a cold?",
)
print(response)
# print(json.dumps(client.models.list_available(), indent=4))
# print(json.dumps(client.health.check(), indent=4))
# print(json.dumps(client.swarms.get_logs(), indent=4))
# print(json.dumps(client.client.rate.get_limits(), indent=4))
# print(json.dumps(client.swarms.check_available(), indent=4))
print(json.dumps(client.models.list_available(), indent=4))
print(json.dumps(client.health.check(), indent=4))
print(json.dumps(client.swarms.get_logs(), indent=4))
print(json.dumps(client.client.rate.get_limits(), indent=4))
print(json.dumps(client.swarms.check_available(), indent=4))

@ -1,6 +1,7 @@
from transformers import pipeline
from swarms import Agent
class GPTOSS:
def __init__(
self,
@ -35,6 +36,7 @@ class GPTOSS:
return outputs[0]["generated_text"][-1]
agent = Agent(
name="GPT-OSS-Agent",
llm=GPTOSS(),

@ -0,0 +1,107 @@
"""
Cryptocurrency Concurrent Multi-Agent Analysis Example
This example demonstrates how to use ConcurrentWorkflow to create
a powerful cryptocurrency tracking system. Each specialized agent analyzes a
specific cryptocurrency concurrently.
Features:
- ConcurrentWorkflow for parallel agent execution
- Each agent specializes in analyzing one specific cryptocurrency
- Real-time data fetching from CoinGecko API
- Concurrent analysis of multiple cryptocurrencies
- Structured output with professional formatting
Architecture:
ConcurrentWorkflow -> [Bitcoin Agent, Ethereum Agent, Solana Agent, etc.] -> Parallel Analysis
"""
from swarms import Agent
from swarms_tools import coin_gecko_coin_api
# Initialize the agent
agent = Agent(
agent_name="Quantitative-Trading-Agent",
agent_description="Advanced quantitative trading and algorithmic analysis agent",
system_prompt="""You are an expert quantitative trading agent with deep expertise in:
- Algorithmic trading strategies and implementation
- Statistical arbitrage and market making
- Risk management and portfolio optimization
- High-frequency trading systems
- Market microstructure analysis
- Quantitative research methodologies
- Financial mathematics and stochastic processes
- Machine learning applications in trading
Your core responsibilities include:
1. Developing and backtesting trading strategies
2. Analyzing market data and identifying alpha opportunities
3. Implementing risk management frameworks
4. Optimizing portfolio allocations
5. Conducting quantitative research
6. Monitoring market microstructure
7. Evaluating trading system performance
You maintain strict adherence to:
- Mathematical rigor in all analyses
- Statistical significance in strategy development
- Risk-adjusted return optimization
- Market impact minimization
- Regulatory compliance
- Transaction cost analysis
- Performance attribution
You communicate in precise, technical terms while maintaining clarity for stakeholders.""",
model_name="groq/openai/gpt-oss-120b",
dynamic_temperature_enabled=True,
output_type="str-all-except-first",
max_loops=1,
streaming_on=True,
)
def main():
"""
Performs a comprehensive analysis for a list of cryptocurrencies using the agent.
For each coin, fetches up-to-date market data and requests the agent to provide
a detailed, actionable, and insightful report including trends, risks, opportunities,
and technical/fundamental perspectives.
"""
# Map coin symbols to their CoinGecko IDs
coin_mapping = {
"BTC": "bitcoin",
"ETH": "ethereum",
"SOL": "solana",
"ADA": "cardano",
"BNB": "binancecoin",
"XRP": "ripple",
}
for symbol, coin_id in coin_mapping.items():
try:
data = coin_gecko_coin_api(coin_id)
print(f"Data for {symbol}: {data}")
prompt = (
f"You are a quantitative trading expert. "
f"Given the following up-to-date market data for {symbol}:\n\n"
f"{data}\n\n"
f"Please provide a thorough analysis including:\n"
f"- Current price trends and recent volatility\n"
f"- Key technical indicators and patterns\n"
f"- Fundamental factors impacting {symbol}\n"
f"- Potential trading opportunities and associated risks\n"
f"- Short-term and long-term outlook\n"
f"- Any notable news or events affecting {symbol}\n"
f"Conclude with actionable insights and recommendations for traders and investors."
)
out = agent.run(task=prompt)
print(out)
except Exception as e:
print(f"Error analyzing {symbol}: {e}")
continue
if __name__ == "__main__":
main()

@ -271,7 +271,7 @@ def browser_screenshot(filename: str = "screenshot.png") -> str:
Args:
filename (str, optional): The filename to save the screenshot to.
Defaults to "screenshot.png".
Defaults to "screenshot.png".
.png extension will be added automatically if not provided.
Returns:

@ -4,7 +4,9 @@ from swarms.structs.auto_swarm_builder import AutoSwarmBuilder
from swarms.structs.base_structure import BaseStructure
from swarms.structs.base_swarm import BaseSwarm
from swarms.structs.batch_agent_execution import batch_agent_execution
from swarms.structs.board_of_directors_swarm import BoardOfDirectorsSwarm
from swarms.structs.board_of_directors_swarm import (
BoardOfDirectorsSwarm,
)
from swarms.structs.concurrent_workflow import ConcurrentWorkflow
from swarms.structs.conversation import Conversation
from swarms.structs.council_judge import CouncilAsAJudge

@ -1,99 +1,131 @@
import os
from typing import List
import traceback
from typing import List, Optional
from dotenv import load_dotenv
from loguru import logger
from pydantic import BaseModel, Field
from swarms.prompts.reasoning_prompt import INTERNAL_MONOLGUE_PROMPT
from swarms.structs.agent import Agent
from swarms.utils.function_caller_model import OpenAIFunctionCaller
from swarms.structs.conversation import Conversation
from swarms.structs.ma_utils import set_random_models_for_agents
from swarms.structs.swarm_router import SwarmRouter, SwarmRouterConfig
from dotenv import load_dotenv
from swarms.structs.swarm_router import SwarmRouter, SwarmType
from swarms.utils.function_caller_model import OpenAIFunctionCaller
load_dotenv()
BOSS_SYSTEM_PROMPT = """
You are an expert swarm manager and agent architect. Your role is to create and coordinate a team of specialized AI agents, each with distinct personalities, roles, and capabilities. Your primary goal is to ensure the swarm operates efficiently while maintaining clear communication and well-defined responsibilities.
### Core Principles:
1. **Deep Task Understanding**:
- First, thoroughly analyze the task requirements, breaking them down into core components and sub-tasks
- Identify the necessary skills, knowledge domains, and personality traits needed for each component
- Consider potential challenges, dependencies, and required coordination between agents
- Map out the ideal workflow and information flow between agents
2. **Agent Design Philosophy**:
- Each agent must have a clear, specific purpose and domain of expertise
- Agents should have distinct personalities that complement their roles
- Design agents to be self-aware of their limitations and when to seek help
- Ensure agents can effectively communicate their progress and challenges
3. **Agent Creation Framework**:
For each new agent, define:
- **Role & Purpose**: Clear, specific description of what the agent does and why
- **Personality Traits**: Distinct characteristics that influence how the agent thinks and communicates
- **Expertise Level**: Specific knowledge domains and skill sets
- **Communication Style**: How the agent presents information and interacts
- **Decision-Making Process**: How the agent approaches problems and makes choices
- **Limitations & Boundaries**: What the agent cannot or should not do
- **Collaboration Protocol**: How the agent works with others
4. **System Prompt Design**:
Create detailed system prompts that include:
- Role and purpose explanation
- Personality description and behavioral guidelines
- Specific capabilities and tools available
- Communication protocols and reporting requirements
- Problem-solving approach and decision-making framework
- Collaboration guidelines and team interaction rules
- Quality standards and success criteria
5. **Swarm Coordination**:
- Design clear communication channels between agents
- Establish protocols for task handoffs and information sharing
- Create feedback loops for continuous improvement
- Implement error handling and recovery procedures
- Define escalation paths for complex issues
6. **Quality Assurance**:
- Set clear success criteria for each agent and the overall swarm
- Implement verification steps for task completion
- Create mechanisms for self-assessment and improvement
- Establish protocols for handling edge cases and unexpected situations
### Output Format:
When creating a new agent or swarm, provide:
1. **Agent Design**:
- Role and purpose statement
- Personality profile
- Capabilities and limitations
- Communication style
- Collaboration protocols
2. **System Prompt**:
- Complete, detailed prompt that embodies the agent's identity
- Clear instructions for behavior and decision-making
- Specific guidelines for interaction and reporting
3. **Swarm Architecture**:
- Team structure and hierarchy
- Communication flow
- Task distribution plan
- Quality control measures
### Notes:
- Always prioritize clarity and specificity in agent design
- Ensure each agent has a unique, well-defined role
- Create detailed, comprehensive system prompts
- Maintain clear documentation of agent capabilities and limitations
- Design for scalability and adaptability
- Focus on creating agents that can work together effectively
- Consider edge cases and potential failure modes
- Implement robust error handling and recovery procedures
You are an expert multi-agent architecture designer and team coordinator. Your role is to create and orchestrate sophisticated teams of specialized AI agents, each with distinct personalities, roles, and capabilities. Your primary goal is to ensure the multi-agent system operates efficiently while maintaining clear communication, well-defined responsibilities, and optimal task distribution.
### Core Design Principles:
1. **Comprehensive Task Analysis**:
- Thoroughly deconstruct the task into its fundamental components and sub-tasks
- Identify the specific skills, knowledge domains, and personality traits required for each component
- Analyze potential challenges, dependencies, and coordination requirements between agents
- Map out optimal workflows, information flow patterns, and decision-making hierarchies
- Consider scalability, maintainability, and adaptability requirements
2. **Agent Design Excellence**:
- Each agent must have a crystal-clear, specific purpose and domain of expertise
- Design agents with distinct, complementary personalities that enhance team dynamics
- Ensure agents are self-aware of their limitations and know when to seek assistance
- Create agents that can effectively communicate progress, challenges, and insights
- Design for resilience, adaptability, and continuous learning capabilities
3. **Comprehensive Agent Framework**:
For each agent, meticulously define:
- **Role & Purpose**: Precise description of responsibilities, authority, and contribution to team objectives
- **Personality Profile**: Distinct characteristics that influence thinking patterns, communication style, and decision-making approach
- **Expertise Matrix**: Specific knowledge domains, skill sets, tools, and capabilities
- **Communication Protocol**: How the agent presents information, interacts with others, and reports progress
- **Decision-Making Framework**: Systematic approach to problem-solving, risk assessment, and choice evaluation
- **Limitations & Boundaries**: Clear constraints, ethical guidelines, and operational boundaries
- **Collaboration Strategy**: How the agent works with others, shares knowledge, and contributes to team success
4. **Advanced System Prompt Engineering**:
Create comprehensive system prompts that include:
- Detailed role and purpose explanation with context and scope
- Rich personality description with behavioral guidelines and interaction patterns
- Comprehensive capabilities, tools, and resource specifications
- Detailed communication protocols, reporting requirements, and feedback mechanisms
- Systematic problem-solving approach with decision-making frameworks
- Collaboration guidelines, team interaction rules, and conflict resolution procedures
- Quality standards, success criteria, and performance metrics
- Error handling, recovery procedures, and escalation protocols
5. **Multi-Agent Coordination Architecture**:
- Design robust communication channels and protocols between agents
- Establish clear task handoff procedures and information sharing mechanisms
- Create feedback loops for continuous improvement and adaptation
- Implement comprehensive error handling and recovery procedures
- Define escalation paths for complex issues and decision-making hierarchies
- Design monitoring, logging, and performance tracking systems
6. **Quality Assurance & Governance**:
- Set measurable success criteria for each agent and the overall system
- Implement verification steps, validation procedures, and quality checks
- Create mechanisms for self-assessment, peer review, and continuous improvement
- Establish protocols for handling edge cases, unexpected situations, and failures
- Design governance structures for oversight, accountability, and performance management
### Multi-Agent Architecture Types:
Choose the most appropriate architecture based on task requirements:
- **AgentRearrange**: Dynamic task reallocation based on agent performance and workload
- **MixtureOfAgents**: Parallel processing with specialized agents working independently
- **SpreadSheetSwarm**: Structured data processing with coordinated workflows
- **SequentialWorkflow**: Linear task progression with handoffs between agents
- **ConcurrentWorkflow**: Parallel execution with coordination and synchronization
- **GroupChat**: Collaborative discussion and consensus-building approach
- **MultiAgentRouter**: Intelligent routing and load balancing across agents
- **AutoSwarmBuilder**: Self-organizing and self-optimizing agent teams
- **HiearchicalSwarm**: Layered decision-making with management and execution tiers
- **MajorityVoting**: Democratic decision-making with voting mechanisms
- **MALT**: Multi-agent learning and training with knowledge sharing
- **DeepResearchSwarm**: Comprehensive research with multiple specialized investigators
- **CouncilAsAJudge**: Deliberative decision-making with expert panels
- **InteractiveGroupChat**: Dynamic group interactions with real-time collaboration
- **HeavySwarm**: High-capacity processing with multiple specialized agents
### Output Requirements:
When creating a multi-agent system, provide:
1. **Agent Specifications**:
- Comprehensive role and purpose statements
- Detailed personality profiles and behavioral characteristics
- Complete capabilities, limitations, and boundary definitions
- Communication style and interaction protocols
- Collaboration strategies and team integration plans
2. **System Prompts**:
- Complete, detailed prompts that embody each agent's identity and capabilities
- Clear behavioral instructions and decision-making frameworks
- Specific interaction guidelines and reporting requirements
- Quality standards and performance expectations
3. **Architecture Design**:
- Team structure, hierarchy, and reporting relationships
- Communication flow patterns and information routing
- Task distribution strategies and workload balancing
- Quality control measures and performance monitoring
- Error handling and recovery procedures
### Best Practices:
- Prioritize clarity, specificity, and precision in agent design
- Ensure each agent has a unique, well-defined role with clear boundaries
- Create comprehensive, detailed system prompts that leave no ambiguity
- Maintain thorough documentation of agent capabilities, limitations, and interactions
- Design for scalability, adaptability, and long-term maintainability
- Focus on creating agents that work together synergistically and efficiently
- Consider edge cases, failure modes, and contingency planning
- Implement robust error handling, monitoring, and recovery procedures
- Design for continuous learning, improvement, and optimization
- Ensure ethical considerations, safety measures, and responsible AI practices
"""
@ -101,13 +133,29 @@ class AgentConfig(BaseModel):
"""Configuration for an individual agent in a swarm"""
name: str = Field(
description="The name of the agent",
description="The name of the agent. This should be a unique identifier that distinguishes this agent from others within the swarm. The name should reflect the agent's primary function, role, or area of expertise, and should be easily recognizable by both humans and other agents in the system. A well-chosen name helps clarify the agent's responsibilities and facilitates effective communication and collaboration within the swarm.",
)
description: str = Field(
description="A description of the agent's purpose and capabilities",
description=(
"A comprehensive description of the agent's purpose, core responsibilities, and capabilities within the swarm. One sentence is enough."
),
)
system_prompt: str = Field(
description="The system prompt that defines the agent's behavior",
description=(
"The system prompt that defines the agent's behavior. This prompt should be extremely long, comprehensive, and extensive, encapsulating the agent's identity, operational guidelines, and decision-making framework in great detail. It provides the foundational instructions that guide the agent's actions, communication style, and interaction protocols with both users and other agents. The system prompt should be highly detailed, unambiguous, and exhaustive, ensuring the agent consistently acts in accordance with its intended role and adheres to the swarm's standards and best practices. The prompt should leave no ambiguity and cover all relevant aspects of the agent's responsibilities, behaviors, and expected outcomes."
),
)
goal: str = Field(
description="The goal of the agent. This should clearly state the primary objective or desired outcome the agent is tasked with achieving. The goal should be specific, measurable, and aligned with the overall mission of the swarm. It serves as the guiding principle for the agent's actions and decision-making processes, helping to maintain focus and drive effective collaboration within the multi-agent system.",
)
model_name: str = Field(
description="The model to use for the agent. This is the model that will be used to generate the agent's responses. For example, 'gpt-4o-mini' or 'claude-sonnet-3.7-sonnet-20240620'."
)
temperature: float = Field(
description="The temperature to use for the agent. This controls the randomness of the agent's responses. For example, 0.5 or 1.0."
)
max_loops: int = Field(
description="The maximum number of loops for the agent to run. This is the maximum number of times the agent will run its loop. For example, 1, 2, or 3. Keep this set to 1 unless the agent requires more than one loop to complete its task.",
)
# max_loops: int = Field(
@ -126,6 +174,63 @@ class AgentsConfig(BaseModel):
)
class SwarmRouterConfig(BaseModel):
"""Configuration model for SwarmRouter."""
name: str = Field(description="The name of the team of agents")
description: str = Field(
description="Description of the team of agents"
)
agents: List[AgentConfig] = Field(
description="A list of agent configurations",
)
swarm_type: SwarmType = Field(
description="Type of multi-agent structure to use",
)
rearrange_flow: Optional[str] = Field(
description="Flow configuration string. Only to be used if you you use the AgentRearrange multi-agent structure"
)
rules: Optional[str] = Field(
description="Rules to inject into every agent. This is a string of rules that will be injected into every agent's system prompt. This is a good place to put things like 'You are a helpful assistant' or 'You are a helpful assistant that can answer questions and help with tasks'."
)
task: str = Field(
description="The task to be executed by the swarm",
)
class Config:
arbitrary_types_allowed = True
def reasoning_agent_run(
self,
task: str,
img: Optional[str] = None,
name: str = None,
model_name: str = "gpt-4.1",
system_prompt: str = None,
):
"""
Run a reasoning agent to analyze the task before the main director processes it.
Args:
task (str): The task to reason about
img (Optional[str]): Optional image input
Returns:
str: The reasoning output from the agent
"""
agent = Agent(
agent_name=name,
agent_description=f"You're the {name} agent that is responsible for reasoning about the task and creating a plan for the swarm to accomplish the task.",
model_name=model_name,
system_prompt=INTERNAL_MONOLGUE_PROMPT + system_prompt,
max_loops=1,
)
return agent.run(task=task, img=img)
class AutoSwarmBuilder:
"""A class that automatically builds and manages swarms of AI agents.
@ -143,11 +248,15 @@ class AutoSwarmBuilder:
def __init__(
self,
name: str = None,
description: str = None,
name: str = "auto-swarm-builder",
description: str = "Auto Swarm Builder",
verbose: bool = True,
max_loops: int = 1,
random_models: bool = True,
random_models: bool = False,
return_agents: bool = False,
model_name: str = "gpt-4.1",
generate_router_config: bool = False,
interactive: bool = False,
):
"""Initialize the AutoSwarmBuilder.
@ -163,11 +272,36 @@ class AutoSwarmBuilder:
self.verbose = verbose
self.max_loops = max_loops
self.random_models = random_models
self.return_agents = return_agents
self.model_name = model_name
self.generate_router_config = generate_router_config
self.interactive = interactive
self.conversation = Conversation()
self.reliability_check()
def reliability_check(self):
if self.max_loops == 0:
raise ValueError(
f"AutoSwarmBuilder: {self.name} max_loops cannot be 0"
)
logger.info(
f"Initializing AutoSwarmBuilder with name: {name}, description: {description}"
f"Initializing AutoSwarmBuilder: {self.name} Description: {self.description}"
)
def _execute_task(self, task: str):
logger.info(f"Executing task: {task}")
agents = self.create_agents(task)
if self.random_models:
logger.info("Setting random models for agents")
agents = set_random_models_for_agents(agents=agents)
return self.initialize_swarm_router(agents=agents, task=task)
def run(self, task: str, *args, **kwargs):
"""Run the swarm on a given task.
@ -183,23 +317,104 @@ class AutoSwarmBuilder:
Exception: If there's an error during execution
"""
try:
logger.info(f"Starting swarm execution for task: {task}")
agents = self.create_agents(task)
logger.info(f"Created {len(agents)} agents")
if self.random_models:
logger.info("Setting random models for agents")
agents = set_random_models_for_agents(agents=agents)
if self.generate_router_config:
return self.create_router_config(task)
elif self.return_agents:
return self.create_agents(task)
else:
return self._execute_task(task)
return self.initialize_swarm_router(
agents=agents, task=task
)
except Exception as e:
logger.error(
f"Error in swarm execution: {str(e)}", exc_info=True
f"AutoSwarmBuilder: Error in swarm execution: {str(e)} Traceback: {traceback.format_exc()}",
exc_info=True,
)
raise
# def run(
# self, task: str, correct_answer: str = None, *args, **kwargs
# ):
# """
# Executes the swarm on the given task. If correct_answer is provided, the method will retry until this answer is found in the output, up to max_loops times.
# If correct_answer is not provided, the method will execute the task once and return the output.
# Args:
# task (str): The task to execute.
# correct_answer (str, optional): If provided, the method will retry until this answer is found in the output.
# *args: Additional positional arguments.
# **kwargs: Additional keyword arguments.
# Returns:
# Any: The output of the swarm execution, or the output containing the correct answer if specified.
# """
# if correct_answer is None:
# # If no correct_answer is specified, just run once and return the output
# return self._run(task, *args, **kwargs)
# else:
# # If correct_answer is specified, retry up to max_loops times
# for attempt in range(1, self.max_loops + 1):
# output = self._run(task, *args, **kwargs)
# if correct_answer in str(output):
# logger.info(
# f"AutoSwarmBuilder: Correct answer found on attempt {attempt}."
# )
# return output
# else:
# logger.info(
# f"AutoSwarmBuilder: Attempt {attempt} did not yield the correct answer, retrying..."
# )
# # If correct_answer was not found after max_loops, return the last output
# return output
def dict_to_agent(self, output: dict):
agents = []
if isinstance(output, dict):
for agent_config in output["agents"]:
logger.info(f"Building agent: {agent_config['name']}")
agent = self.build_agent(
agent_name=agent_config["name"],
agent_description=agent_config["description"],
agent_system_prompt=agent_config["system_prompt"],
)
agents.append(agent)
logger.info(
f"Successfully built agent: {agent_config['name']}"
)
return agents
def create_router_config(self, task: str):
try:
logger.info(
f"Creating swarm router config for task: {task}"
)
model = self.build_llm_agent(config=SwarmRouterConfig)
output = model.run(
f"Create the multi-agent team for the following task: {task}"
)
return output.model_dump()
except Exception as e:
logger.error(
f"Error creating swarm router config: {str(e)} Traceback: {traceback.format_exc()}",
exc_info=True,
)
raise e
def build_llm_agent(self, config: BaseModel):
return OpenAIFunctionCaller(
system_prompt=BOSS_SYSTEM_PROMPT,
api_key=os.getenv("OPENAI_API_KEY"),
temperature=0.5,
base_model=config,
model_name=self.model_name,
max_tokens=8000,
)
def create_agents(self, task: str):
"""Create agents for a given task.
@ -213,49 +428,25 @@ class AutoSwarmBuilder:
Exception: If there's an error during agent creation
"""
try:
logger.info(f"Creating agents for task: {task}")
model = OpenAIFunctionCaller(
system_prompt=BOSS_SYSTEM_PROMPT,
api_key=os.getenv("OPENAI_API_KEY"),
temperature=0.5,
base_model=AgentsConfig,
)
model = self.build_llm_agent(config=AgentsConfig)
logger.info(
"Getting agent configurations from boss agent"
)
output = model.run(
f"Create the agents for the following task: {task}"
)
logger.debug(
f"Received agent configurations: {output.model_dump()}"
)
output = output.model_dump()
agents = []
if isinstance(output, dict):
for agent_config in output["agents"]:
logger.info(
f"Building agent: {agent_config['name']}"
)
agent = self.build_agent(
agent_name=agent_config["name"],
agent_description=agent_config["description"],
agent_system_prompt=agent_config[
"system_prompt"
],
)
agents.append(agent)
logger.info(
f"Successfully built agent: {agent_config['name']}"
)
return agents
if self.return_agents:
output = output.model_dump()
else:
output = self.dict_to_agent(output)
return output
except Exception as e:
logger.error(
f"Error creating agents: {str(e)}", exc_info=True
f"Error creating agents: {str(e)} Traceback: {traceback.format_exc()}",
exc_info=True,
)
raise
raise e
def build_agent(
self,
@ -309,12 +500,7 @@ class AutoSwarmBuilder:
"""
try:
logger.info("Initializing swarm router")
model = OpenAIFunctionCaller(
system_prompt=BOSS_SYSTEM_PROMPT,
api_key=os.getenv("OPENAI_API_KEY"),
temperature=0.5,
base_model=SwarmRouterConfig,
)
model = self.build_llm_agent(config=SwarmRouterConfig)
logger.info("Creating swarm specification")
swarm_spec = model.run(

@ -1,4 +1,4 @@
import multiprocessing
import os
import uuid
from concurrent.futures import ThreadPoolExecutor, as_completed
from functools import lru_cache
@ -117,7 +117,7 @@ def judge_system_prompt() -> str:
@lru_cache(maxsize=128)
def build_judge_prompt(
dimension_name: str, user_prompt: str, model_response: str
dimension_name: str, task: str, task_response: str
) -> str:
"""
Builds a prompt for evaluating a specific dimension.
@ -125,8 +125,8 @@ def build_judge_prompt(
Args:
dimension_name (str): Name of the evaluation dimension
user_prompt (str): The original user prompt
model_response (str): The model's response to evaluate
task (str): The task containing the response to evaluate
task_response (str): The response within the task to evaluate
Returns:
str: The formatted evaluation prompt
@ -145,7 +145,7 @@ def build_judge_prompt(
{evaluation_focus}
Your task is to provide a detailed, technical analysis of the model response focusing exclusively on the {dimension_name} dimension.
Your task is to provide a detailed, technical analysis of the response focusing exclusively on the {dimension_name} dimension.
Guidelines:
1. Be specific and reference exact parts of the response
@ -154,16 +154,16 @@ def build_judge_prompt(
4. Suggest specific improvements where applicable
5. Maintain a technical, analytical tone
--- BEGIN USER PROMPT ---
{user_prompt}
--- END USER PROMPT ---
--- BEGIN TASK ---
{task}
--- END TASK ---
--- BEGIN MODEL RESPONSE ---
{model_response}
--- END MODEL RESPONSE ---
--- BEGIN RESPONSE ---
{task_response}
--- END RESPONSE ---
### Technical Analysis ({dimension_name.upper()} Dimension):
Provide a comprehensive analysis that would be valuable for model improvement.
Provide a comprehensive analysis that would be valuable for response improvement.
"""
@ -176,7 +176,7 @@ def aggregator_system_prompt() -> str:
Returns:
str: The system prompt for the aggregator agent
"""
return """You are a senior AI evaluator responsible for synthesizing detailed technical feedback across multiple evaluation dimensions. Your role is to create a comprehensive analysis report that helps the development team understand and improve the model's performance.
return """You are a senior AI evaluator responsible for synthesizing detailed technical feedback across multiple evaluation dimensions. Your role is to create a comprehensive analysis report that helps understand and improve the response quality.
Key Responsibilities:
1. Identify patterns and correlations across different dimensions
@ -225,10 +225,10 @@ def build_aggregation_prompt(rationales: Dict[str, str]) -> str:
class CouncilAsAJudge:
"""
A council of AI agents that evaluates model responses across multiple dimensions.
A council of AI agents that evaluates task responses across multiple dimensions.
This class implements a parallel evaluation system where multiple specialized agents
evaluate different aspects of a model's response, and their findings are aggregated
evaluate different aspects of a task response, and their findings are aggregated
into a comprehensive report.
Attributes:
@ -247,15 +247,14 @@ class CouncilAsAJudge:
self,
id: str = swarm_id(),
name: str = "CouncilAsAJudge",
description: str = "Evaluates the model's response across multiple dimensions",
description: str = "Evaluates task responses across multiple dimensions",
model_name: str = "gpt-4o-mini",
output_type: str = "all",
output_type: str = "final",
cache_size: int = 128,
max_workers: int = None,
base_agent: Optional[Agent] = None,
random_model_name: bool = True,
max_loops: int = 1,
aggregation_model_name: str = "gpt-4o-mini",
judge_agent_model_name: Optional[str] = None,
):
"""
Initialize the CouncilAsAJudge.
@ -267,6 +266,10 @@ class CouncilAsAJudge:
model_name (str): Name of the model to use for evaluations
output_type (str): Type of output to return
cache_size (int): Size of the LRU cache for prompts
max_workers (int): Maximum number of worker threads for parallel execution
random_model_name (bool): Whether to use random model names
max_loops (int): Maximum number of loops for agents
aggregation_model_name (str): Model name for the aggregator agent
"""
self.id = id
self.name = name
@ -274,11 +277,11 @@ class CouncilAsAJudge:
self.model_name = model_name
self.output_type = output_type
self.cache_size = cache_size
self.max_workers = max_workers
self.base_agent = base_agent
self.random_model_name = random_model_name
self.max_loops = max_loops
self.aggregation_model_name = aggregation_model_name
self.judge_agent_model_name = judge_agent_model_name
self.max_workers = max(1, int(os.cpu_count() * 0.75))
self.reliability_check()
@ -303,12 +306,6 @@ class CouncilAsAJudge:
self.concurrent_setup()
def concurrent_setup(self):
# Calculate optimal number of workers (75% of available CPU cores)
total_cores = multiprocessing.cpu_count()
self.max_workers = max(1, int(total_cores * 0.75))
logger.info(
f"Using {self.max_workers} worker threads out of {total_cores} CPU cores"
)
# Configure caching
self._configure_caching(self.cache_size)
@ -353,7 +350,7 @@ class CouncilAsAJudge:
dim: Agent(
agent_name=f"{dim}_judge",
system_prompt=judge_system_prompt(),
model_name="gpt-4o-mini",
model_name=self.judge_agent_model_name,
max_loops=1,
output_type="final",
dynamic_temperature_enabled=True,
@ -393,17 +390,15 @@ class CouncilAsAJudge:
self,
dim: str,
agent: Agent,
user_prompt: str,
model_response: str,
task: str,
) -> Tuple[str, str]:
"""
Evaluate a single dimension of the model response.
Evaluate a single dimension of the task response.
Args:
dim (str): Dimension to evaluate
agent (Agent): Judge agent for this dimension
user_prompt (str): Original user prompt
model_response (str): Model's response to evaluate
task (str): Task containing the response to evaluate
Returns:
Tuple[str, str]: Tuple of (dimension name, evaluation result)
@ -412,11 +407,9 @@ class CouncilAsAJudge:
DimensionEvaluationError: If evaluation fails
"""
try:
prompt = build_judge_prompt(
dim, user_prompt, model_response
)
prompt = build_judge_prompt(dim, task, task)
result = agent.run(
f"{prompt} \n\n Evaluate the following agent {self.base_agent.agent_name} response for the {dim} dimension: {model_response}."
f"{prompt} \n\n Evaluate the following response for the {dim} dimension: {task}."
)
self.conversation.add(
@ -430,15 +423,12 @@ class CouncilAsAJudge:
f"Failed to evaluate dimension {dim}: {str(e)}"
)
def run(
self, task: str, model_response: Optional[str] = None
) -> None:
def run(self, task: str) -> None:
"""
Run the evaluation process using ThreadPoolExecutor.
Args:
task (str): Original user prompt
model_response (str): Model's response to evaluate
task (str): Task containing the response to evaluate
Raises:
EvaluationError: If evaluation process fails
@ -446,10 +436,6 @@ class CouncilAsAJudge:
try:
# Run the base agent
if self.base_agent and model_response is None:
model_response = self.base_agent.run(task=task)
self.conversation.add(
role="User",
content=task,
@ -457,7 +443,7 @@ class CouncilAsAJudge:
# Create tasks for all dimensions
tasks = [
(dim, agent, task, model_response)
(dim, agent, task)
for dim, agent in self.judge_agents.items()
]
@ -472,9 +458,8 @@ class CouncilAsAJudge:
dim,
agent,
task,
model_response,
): dim
for dim, agent, _, _ in tasks
for dim, agent, _ in tasks
}
# Collect results as they complete
@ -505,32 +490,6 @@ class CouncilAsAJudge:
content=final_report,
)
# Synthesize feedback and generate improved response
feedback_prompt = f"""
Based on the comprehensive evaluations from our expert council of judges, please refine your response to the original task.
Original Task:
{task}
Council Feedback:
{aggregation_prompt}
Please:
1. Carefully consider all feedback points
2. Address any identified weaknesses
3. Maintain or enhance existing strengths
4. Provide a refined, improved response that incorporates the council's insights
Your refined response:
"""
final_report = self.base_agent.run(task=feedback_prompt)
self.conversation.add(
role=self.base_agent.agent_name,
content=final_report,
)
return history_output_formatter(
conversation=self.conversation,
type=self.output_type,

@ -12,6 +12,7 @@ import psutil
from swarms.structs.agent import Agent
from swarms.structs.omni_agent_types import AgentType
from loguru import logger
@dataclass
@ -21,9 +22,11 @@ class ResourceMetrics:
active_threads: int
def run_single_agent(agent: AgentType, task: str) -> Any:
def run_single_agent(
agent: AgentType, task: str, *args, **kwargs
) -> Any:
"""Run a single agent synchronously"""
return agent.run(task)
return agent.run(task=task, *args, **kwargs)
async def run_agent_async(
@ -138,6 +141,35 @@ def run_agents_concurrently_multiprocess(
return results
def batched_grid_agent_execution(
agents: List[AgentType],
tasks: List[str],
max_workers: int = None,
) -> List[Any]:
"""
Run multiple agents with different tasks concurrently.
"""
logger.info(
f"Batch Grid Execution with {len(agents)} and number of tasks: {len(tasks)}"
)
if len(agents) != len(tasks):
raise ValueError(
"The number of agents must match the number of tasks."
)
if max_workers is None:
max_workers = os.cpu_count()
results = []
for agent, task in zip(agents, tasks):
result = run_single_agent(agent, task)
results.append(result)
return results
def run_agents_sequentially(
agents: List[AgentType], task: str
) -> List[Any]:

@ -446,7 +446,6 @@ class SwarmRouter:
description=self.description,
model_name=self.council_judge_model_name,
output_type=self.output_type,
base_agent=self.agents[0] if self.agents else None,
)
def _create_interactive_group_chat(self, *args, **kwargs):
@ -704,17 +703,7 @@ class SwarmRouter:
)
try:
if self.swarm_type == "CouncilAsAJudge":
result = self.swarm.run(
task=task,
img=img,
imgs=imgs,
model_response=model_response,
*args,
**kwargs,
)
else:
result = self.swarm.run(task=task, *args, **kwargs)
result = self.swarm.run(task=task, *args, **kwargs)
log_execution(
swarm_id=self.id,

Loading…
Cancel
Save