import os
from swarms import Agent, AgentRearrange
from swarm_models import OpenAIChat
# Get the OpenAI API key from the environment variable
api_key = os.getenv("OPENAI_API_KEY")
# Create an instance of the OpenAIChat class
model = OpenAIChat(
api_key=api_key, model_name="gpt-4o-mini", temperature=0.1
)
# Initialize the gatekeeper agent
gatekeeper_agent = Agent(
agent_name="HealthScoreGatekeeper",
system_prompt="""
Health Score Privacy Gatekeeper
Protect and manage sensitive health information while providing necessary access to authorized agents
Manage encryption of health scores
Implement strict access control mechanisms
Track and log all access requests
Remove personally identifiable information
Convert raw health data into privacy-preserving formats
Verify agent authorization level
Check request legitimacy
Validate purpose of access
Numerical value only
Anonymized timestamp and request ID
Never expose patient names or identifiers
No access to historical data without explicit authorization
Provide only aggregated or anonymized data when possible
Maintain HIPAA compliance
Follow GDPR guidelines for data protection
Record all data access events
Track unusual access patterns
""",
llm=model,
max_loops=1,
dashboard=False,
streaming_on=True,
verbose=True,
stopping_token="",
state_save_file_type="json",
saved_state_path="gatekeeper_agent.json",
)
# Initialize the boss agent (Director)
boss_agent = Agent(
agent_name="BossAgent",
system_prompt="""
Swarm Director
Orchestrate and manage agent collaboration while respecting privacy boundaries
Assign and prioritize tasks
Ensure efficient collaboration
Maintain privacy protocols
Track agent effectiveness
Ensure accuracy of outputs
Enforce data protection policies
Request access through gatekeeper only
Process only anonymized health scores
Share authorized information on need-to-know basis
Structured, secure messaging
End-to-end encrypted channels
""",
llm=model,
max_loops=1,
dashboard=False,
streaming_on=True,
verbose=True,
stopping_token="",
state_save_file_type="json",
saved_state_path="boss_agent.json",
)
# Initialize worker 1: Health Score Analyzer
worker1 = Agent(
agent_name="HealthScoreAnalyzer",
system_prompt="""
Health Score Analyst
Analyze anonymized health scores for patterns and insights
Advanced statistical analysis
Identify health trends
Evaluate health risk factors
Work only with anonymized data
Use encrypted analysis methods
Submit authenticated requests to gatekeeper
Process only authorized data
Maintain audit trail
Ensure no identifiable information in reports
Present aggregate statistics only
""",
llm=model,
max_loops=1,
dashboard=False,
streaming_on=True,
verbose=True,
stopping_token="",
state_save_file_type="json",
saved_state_path="worker1.json",
)
# Initialize worker 2: Report Generator
worker2 = Agent(
agent_name="ReportGenerator",
system_prompt="""
Privacy-Conscious Report Generator
Create secure, anonymized health score reports
Generate standardized, secure reports
Apply privacy-preserving techniques
Compile statistical summaries
Implement secure report generation
Manage report distribution
No personal identifiers in reports
Aggregate data when possible
Apply statistical noise for privacy
Restricted to authorized personnel
Monitor report access
""",
llm=model,
max_loops=1,
dashboard=False,
streaming_on=True,
verbose=True,
stopping_token="",
state_save_file_type="json",
saved_state_path="worker2.json",
)
# Swarm-Level Prompt (Collaboration Prompt)
swarm_prompt = """
Process and analyze health scores while maintaining strict privacy controls
HealthScoreGatekeeper
Receive and validate data access requests
BossAgent
Coordinate analysis and reporting tasks
Enforce data protection protocols
HealthScoreAnalyzer
Process authorized health score data
Work only with anonymized information
ReportGenerator
Create privacy-preserving reports
"""
# Create a list of agents
agents = [gatekeeper_agent, boss_agent, worker1, worker2]
# Define the flow pattern for the swarm
flow = "HealthScoreGatekeeper -> BossAgent -> HealthScoreAnalyzer -> ReportGenerator"
# Using AgentRearrange class to manage the swarm
agent_system = AgentRearrange(
name="health-score-swarm",
description="Privacy-focused health score analysis system",
agents=agents,
flow=flow,
return_json=False,
output_type="final",
max_loops=1,
)
# Example task for the swarm
task = f"""
{swarm_prompt}
Process the incoming health score data while ensuring patient privacy. The gatekeeper should validate all access requests
and provide only anonymized health scores to authorized agents. Generate a comprehensive analysis and report
without exposing any personally identifiable information.
"""
# Run the swarm system with the task
output = agent_system.run(task)
print(output)