From b1365924d011d291c745670ce1ae2622e987650a Mon Sep 17 00:00:00 2001 From: Kye Gomez Date: Sun, 5 Jan 2025 14:40:11 -0500 Subject: [PATCH] [EXAMPLE][Multi-AgentRouter] --- multi_agent_router_example.py | 3 +- .../materials_science_agents.py | 0 swarms/structs/pulsar_swarm.py | 138 +++++++++--------- 3 files changed, 67 insertions(+), 74 deletions(-) rename materials_science_agents.py => new_features_examples/materials_science_agents.py (100%) diff --git a/multi_agent_router_example.py b/multi_agent_router_example.py index af788f09..9113ada4 100644 --- a/multi_agent_router_example.py +++ b/multi_agent_router_example.py @@ -1,5 +1,4 @@ -from swarms import Agent -from swarms.structs.multi_agent_orchestrator import MultiAgentRouter +from swarms import Agent, MultiAgentRouter # Example usage: if __name__ == "__main__": diff --git a/materials_science_agents.py b/new_features_examples/materials_science_agents.py similarity index 100% rename from materials_science_agents.py rename to new_features_examples/materials_science_agents.py diff --git a/swarms/structs/pulsar_swarm.py b/swarms/structs/pulsar_swarm.py index 8549a5e4..0a042372 100644 --- a/swarms/structs/pulsar_swarm.py +++ b/swarms/structs/pulsar_swarm.py @@ -1,6 +1,5 @@ import asyncio import json -import secrets import time from concurrent.futures import ThreadPoolExecutor, TimeoutError from contextlib import contextmanager @@ -11,15 +10,10 @@ import pulsar from cryptography.fernet import Fernet from loguru import logger 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 swarms.prompts.finance_agent_sys_prompt import ( - FINANCIAL_AGENT_SYS_PROMPT, -) -from swarms.structs.agent import Agent - - # Enhanced metrics TASK_COUNTER = Counter( "swarm_tasks_total", "Total number of tasks processed" @@ -409,67 +403,67 @@ class SecurePulsarSwarm: logger.error(f"Error during cleanup: {str(e)}") -if __name__ == "__main__": - # Example usage with security configuration - security_config = SecurityConfig( - encryption_key=secrets.token_urlsafe(32), - tls_cert_path="/path/to/cert.pem", - tls_key_path="/path/to/key.pem", - auth_token="your-auth-token", - max_message_size=1048576, - rate_limit=100, - ) - - # Agent factory function - def create_financial_agent() -> Agent: - """Factory function to create a financial analysis agent.""" - return Agent( - agent_name="Financial-Analysis-Agent", - system_prompt=FINANCIAL_AGENT_SYS_PROMPT, - model_name="gpt-4o-mini", - max_loops=1, - autosave=True, - dashboard=False, - verbose=True, - dynamic_temperature_enabled=True, - saved_state_path="finance_agent.json", - user_name="swarms_corp", - retry_attempts=1, - context_length=200000, - return_step_meta=False, - output_type="string", - streaming_on=False, - ) - - # Initialize agents (implementation not shown) - agents = [create_financial_agent() for _ in range(3)] - - # Initialize the secure swarm - with SecurePulsarSwarm( - name="Secure Financial Swarm", - description="Production-grade financial analysis swarm", - agents=agents, - pulsar_url="pulsar+ssl://localhost:6651", - subscription_name="secure_financial_subscription", - topic_name="secure_financial_tasks", - security_config=security_config, - max_workers=5, - retry_attempts=3, - task_timeout=300, - metrics_port=8000, - ) as swarm: - # Example task - task = Task( - task_id=secrets.token_urlsafe(16), - description="Analyze Q4 financial reports", - output_type="json", - priority="high", - metadata={ - "department": "finance", - "requester": "john.doe@company.com", - }, - ) - - # Run the swarm - swarm.publish_task(task) - asyncio.run(swarm.consume_tasks()) +# if __name__ == "__main__": +# # Example usage with security configuration +# security_config = SecurityConfig( +# encryption_key=secrets.token_urlsafe(32), +# tls_cert_path="/path/to/cert.pem", +# tls_key_path="/path/to/key.pem", +# auth_token="your-auth-token", +# max_message_size=1048576, +# rate_limit=100, +# ) + +# # Agent factory function +# def create_financial_agent() -> Agent: +# """Factory function to create a financial analysis agent.""" +# return Agent( +# agent_name="Financial-Analysis-Agent", +# system_prompt=FINANCIAL_AGENT_SYS_PROMPT, +# model_name="gpt-4o-mini", +# max_loops=1, +# autosave=True, +# dashboard=False, +# verbose=True, +# dynamic_temperature_enabled=True, +# saved_state_path="finance_agent.json", +# user_name="swarms_corp", +# retry_attempts=1, +# context_length=200000, +# return_step_meta=False, +# output_type="string", +# streaming_on=False, +# ) + +# # Initialize agents (implementation not shown) +# agents = [create_financial_agent() for _ in range(3)] + +# # Initialize the secure swarm +# with SecurePulsarSwarm( +# name="Secure Financial Swarm", +# description="Production-grade financial analysis swarm", +# agents=agents, +# pulsar_url="pulsar+ssl://localhost:6651", +# subscription_name="secure_financial_subscription", +# topic_name="secure_financial_tasks", +# security_config=security_config, +# max_workers=5, +# retry_attempts=3, +# task_timeout=300, +# metrics_port=8000, +# ) as swarm: +# # Example task +# task = Task( +# task_id=secrets.token_urlsafe(16), +# description="Analyze Q4 financial reports", +# output_type="json", +# priority="high", +# metadata={ +# "department": "finance", +# "requester": "john.doe@company.com", +# }, +# ) + +# # Run the swarm +# swarm.publish_task(task) +# asyncio.run(swarm.consume_tasks())