[EXAMPLE][Multi-AgentRouter]

pull/700/merge
Kye Gomez 3 days ago
parent 3df461bba4
commit b1365924d0

@ -1,5 +1,4 @@
from swarms import Agent from swarms import Agent, MultiAgentRouter
from swarms.structs.multi_agent_orchestrator import MultiAgentRouter
# Example usage: # Example usage:
if __name__ == "__main__": if __name__ == "__main__":

@ -1,6 +1,5 @@
import asyncio import asyncio
import json import json
import secrets
import time import time
from concurrent.futures import ThreadPoolExecutor, TimeoutError from concurrent.futures import ThreadPoolExecutor, TimeoutError
from contextlib import contextmanager from contextlib import contextmanager
@ -11,15 +10,10 @@ import pulsar
from cryptography.fernet import Fernet from cryptography.fernet import Fernet
from loguru import logger from loguru import logger
from prometheus_client import Counter, Histogram, start_http_server from prometheus_client import Counter, Histogram, start_http_server
from pydantic import BaseModel, Field, validator from pydantic import BaseModel, Field
from pydantic.v1 import validator
from tenacity import retry, stop_after_attempt, wait_exponential from tenacity import retry, stop_after_attempt, wait_exponential
from swarms.prompts.finance_agent_sys_prompt import (
FINANCIAL_AGENT_SYS_PROMPT,
)
from swarms.structs.agent import Agent
# Enhanced metrics # Enhanced metrics
TASK_COUNTER = Counter( TASK_COUNTER = Counter(
"swarm_tasks_total", "Total number of tasks processed" "swarm_tasks_total", "Total number of tasks processed"
@ -409,67 +403,67 @@ class SecurePulsarSwarm:
logger.error(f"Error during cleanup: {str(e)}") logger.error(f"Error during cleanup: {str(e)}")
if __name__ == "__main__": # if __name__ == "__main__":
# Example usage with security configuration # # Example usage with security configuration
security_config = SecurityConfig( # security_config = SecurityConfig(
encryption_key=secrets.token_urlsafe(32), # encryption_key=secrets.token_urlsafe(32),
tls_cert_path="/path/to/cert.pem", # tls_cert_path="/path/to/cert.pem",
tls_key_path="/path/to/key.pem", # tls_key_path="/path/to/key.pem",
auth_token="your-auth-token", # auth_token="your-auth-token",
max_message_size=1048576, # max_message_size=1048576,
rate_limit=100, # rate_limit=100,
) # )
# Agent factory function # # Agent factory function
def create_financial_agent() -> Agent: # def create_financial_agent() -> Agent:
"""Factory function to create a financial analysis agent.""" # """Factory function to create a financial analysis agent."""
return Agent( # return Agent(
agent_name="Financial-Analysis-Agent", # agent_name="Financial-Analysis-Agent",
system_prompt=FINANCIAL_AGENT_SYS_PROMPT, # system_prompt=FINANCIAL_AGENT_SYS_PROMPT,
model_name="gpt-4o-mini", # model_name="gpt-4o-mini",
max_loops=1, # max_loops=1,
autosave=True, # autosave=True,
dashboard=False, # dashboard=False,
verbose=True, # verbose=True,
dynamic_temperature_enabled=True, # dynamic_temperature_enabled=True,
saved_state_path="finance_agent.json", # saved_state_path="finance_agent.json",
user_name="swarms_corp", # user_name="swarms_corp",
retry_attempts=1, # retry_attempts=1,
context_length=200000, # context_length=200000,
return_step_meta=False, # return_step_meta=False,
output_type="string", # output_type="string",
streaming_on=False, # streaming_on=False,
) # )
# Initialize agents (implementation not shown) # # Initialize agents (implementation not shown)
agents = [create_financial_agent() for _ in range(3)] # agents = [create_financial_agent() for _ in range(3)]
# Initialize the secure swarm # # Initialize the secure swarm
with SecurePulsarSwarm( # with SecurePulsarSwarm(
name="Secure Financial Swarm", # name="Secure Financial Swarm",
description="Production-grade financial analysis swarm", # description="Production-grade financial analysis swarm",
agents=agents, # agents=agents,
pulsar_url="pulsar+ssl://localhost:6651", # pulsar_url="pulsar+ssl://localhost:6651",
subscription_name="secure_financial_subscription", # subscription_name="secure_financial_subscription",
topic_name="secure_financial_tasks", # topic_name="secure_financial_tasks",
security_config=security_config, # security_config=security_config,
max_workers=5, # max_workers=5,
retry_attempts=3, # retry_attempts=3,
task_timeout=300, # task_timeout=300,
metrics_port=8000, # metrics_port=8000,
) as swarm: # ) as swarm:
# Example task # # Example task
task = Task( # task = Task(
task_id=secrets.token_urlsafe(16), # task_id=secrets.token_urlsafe(16),
description="Analyze Q4 financial reports", # description="Analyze Q4 financial reports",
output_type="json", # output_type="json",
priority="high", # priority="high",
metadata={ # metadata={
"department": "finance", # "department": "finance",
"requester": "john.doe@company.com", # "requester": "john.doe@company.com",
}, # },
) # )
# Run the swarm # # Run the swarm
swarm.publish_task(task) # swarm.publish_task(task)
asyncio.run(swarm.consume_tasks()) # asyncio.run(swarm.consume_tasks())

Loading…
Cancel
Save