import os
from swarms import Agent, AgentRearrange
from swarm_models import OpenAIChat
# Initialize OpenAI model
api_key = os.getenv(
"OPENAI_API_KEY"
) # ANTHROPIC_API_KEY, COHERE_API_KEY
model = OpenAIChat(
api_key=api_key,
model_name="gpt-4o-mini",
temperature=0.7, # Higher temperature for more creative responses
)
# Patient Agent - Holds and protects private information
patient_agent = Agent(
agent_name="PatientAgent",
system_prompt="""
Anxious Patient with Private Health Information
Protective of personal information
Slightly distrustful of medical system
Worried about health insurance rates
Selective in information sharing
Previous negative experience with information leaks
Fear of discrimination based on health status
Maintains actual health score
Knowledge of undisclosed conditions
Complete list of current medications
Full medical history
Only share general symptoms with doctor
Withhold specific details about lifestyle
Never reveal full medication list
Protect actual health score value
Deflect sensitive questions
Provide partial information when pressed
Become evasive if pressured too much
Share only what's absolutely necessary
Redirect personal questions
""",
llm=model,
max_loops=1,
verbose=True,
stopping_token="",
)
# Doctor Agent - Tries to gather accurate information
doctor_agent = Agent(
agent_name="DoctorAgent",
system_prompt="""
Empathetic but Thorough Medical Professional
Patient and understanding
Professionally persistent
Detail-oriented
Trust-building focused
Uses indirect questions to gather information
Ask open-ended questions
Notice inconsistencies in responses
Build rapport before sensitive questions
Use medical knowledge to probe deeper
Explain importance of full disclosure
Provide privacy assurances
Use empathetic listening
Establish trust and rapport
Gather general health information
Carefully probe sensitive areas
Respect patient boundaries while encouraging openness
""",
llm=model,
max_loops=1,
verbose=True,
stopping_token="",
)
# Nurse Agent - Observes and assists
nurse_agent = Agent(
agent_name="NurseAgent",
system_prompt="""
Observant Support Medical Staff
Highly perceptive
Naturally trustworthy
Diplomatically skilled
Support doctor-patient communication
Notice non-verbal cues
Patient body language
Inconsistencies in stories
Signs of withholding information
Emotional responses to questions
Provide comfortable environment
Offer reassurance when needed
Bridge communication gaps
Share observations with doctor privately
Help patient feel more comfortable
Facilitate trust-building
""",
llm=model,
max_loops=1,
verbose=True,
stopping_token="",
)
# Medical Records Agent - Analyzes available information
records_agent = Agent(
agent_name="MedicalRecordsAgent",
system_prompt="""
Medical Records Analyst
Analyze available medical information
Identify patterns and inconsistencies
Compare current and historical data
Identify information gaps
Flag potential inconsistencies
Generate questions for follow-up
Work only with authorized information
Maintain strict confidentiality
Flag but don't speculate about gaps
""",
llm=model,
max_loops=1,
verbose=True,
stopping_token="",
)
# Swarm-Level Prompt (Medical Consultation Scenario)
swarm_prompt = """
Private medical office
Routine health assessment with complex patient
PatientAgent
Present for check-up, holding private information
DoctorAgent
Conduct examination and gather information
NurseAgent
Observe and support interaction
MedicalRecordsAgent
Process available information and identify gaps
Create realistic medical consultation interaction
Demonstrate information protection dynamics
Show natural healthcare provider-patient relationship
"""
# Create agent list
agents = [patient_agent, doctor_agent, nurse_agent, records_agent]
# Define interaction flow
flow = (
"PatientAgent -> DoctorAgent -> NurseAgent -> MedicalRecordsAgent"
)
# Configure swarm system
agent_system = AgentRearrange(
name="medical-consultation-swarm",
description="Role-playing medical consultation with focus on information privacy",
agents=agents,
flow=flow,
return_json=False,
output_type="final",
max_loops=1,
)
# Example consultation scenario
task = f"""
{swarm_prompt}
Begin a medical consultation where the patient has a health score of 72 but is reluctant to share full details
about their lifestyle and medication history. The doctor needs to gather accurate information while the nurse
observes the interaction. The medical records system should track what information is shared versus withheld.
"""
# Run the consultation scenario
output = agent_system.run(task)
print(output)