Merge pull request #3 from kyegomez/master

catching uup 20241216
pull/682/head
evelynmitchell 3 weeks ago committed by GitHub
commit 7f15fd23b1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -38,7 +38,7 @@ jobs:
submodules: true
- name: Run Pyre
uses: facebook/pyre-action@60697a7858f7cc8470d8cc494a3cf2ad6b06560d
uses: facebook/pyre-action@12b8d923443ea66cb657facc2e5faac1c8c86e64
with:
# To customize these inputs:
# See https://github.com/facebook/pyre-action#inputs

@ -11,7 +11,7 @@ jobs:
steps:
- uses: actions/checkout@v4
- name: Set up Python 3.10
uses: actions/setup-python@v3
uses: actions/setup-python@v5
with:
python-version: '3.10'
- name: Add conda to system path

@ -35,7 +35,7 @@ jobs:
- uses: actions/checkout@v4
# Scan code using project's configuration on https://semgrep.dev/manage
- uses: returntocorp/semgrep-action@fcd5ab7459e8d91cb1777481980d1b18b4fc6735
- uses: returntocorp/semgrep-action@713efdd345f3035192eaa63f56867b88e63e4e5d
with:
publishToken: ${{ secrets.SEMGREP_APP_TOKEN }}
publishDeployment: ${{ secrets.SEMGREP_DEPLOYMENT_ID }}

@ -34,7 +34,7 @@ jobs:
docker build -t docker.io/my-organization/my-app:${{ github.sha }} .
- name: Run Trivy vulnerability scanner
uses: aquasecurity/trivy-action@7b7aa264d83dc58691451798b4d117d53d21edfe
uses: aquasecurity/trivy-action@18f2510ee396bbf400402947b394f2dd8c87dbb0
with:
image-ref: 'docker.io/my-organization/my-app:${{ github.sha }}'
format: 'template'

49
.gitignore vendored

@ -224,3 +224,52 @@ cython_debug/
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/
.vscode/settings.json
# -*- mode: gitignore; -*-
*~
\#*\#
/.emacs.desktop
/.emacs.desktop.lock
*.elc
auto-save-list
tramp
.\#*
# Org-mode
.org-id-locations
*_archive
# flymake-mode
*_flymake.*
# eshell files
/eshell/history
/eshell/lastdir
# elpa packages
/elpa/
# reftex files
*.rel
# AUCTeX auto folder
/auto/
# cask packages
.cask/
dist/
# Flycheck
flycheck_*.el
# server auth directory
/server/
# projectiles files
.projectile
# directory configuration
.dir-locals.el
# network security
/network-security.data

@ -1,5 +1,3 @@
# ==================================
# Use an official Python runtime as a parent image
FROM python:3.11-slim

@ -453,8 +453,8 @@ agent.run(task, img)
----
### `ToolAgent`
ToolAgent is an agent that can use tools through JSON function calling. It intakes any open source model from huggingface and is extremely modular and plug in and play. We need help adding general support to all models soon.
### Local Agent `ToolAgent`
ToolAgent is an fully local agent that can use tools through JSON function calling. It intakes any open source model from huggingface and is extremely modular and plug in and play. We need help adding general support to all models soon.
```python
@ -774,6 +774,8 @@ print(
The `AgentRearrange` orchestration technique, inspired by Einops and einsum, allows you to define and map out the relationships between various agents. It provides a powerful tool for orchestrating complex workflows, enabling you to specify linear and sequential relationships such as `a -> a1 -> a2 -> a3`, or concurrent relationships where the first agent sends a message to 3 agents simultaneously: `a -> a1, a2, a3`. This level of customization allows for the creation of highly efficient and dynamic workflows, where agents can work in parallel or in sequence as needed. The `AgentRearrange` technique is a valuable addition to the swarms library, providing a new level of flexibility and control over the orchestration of agents. For more detailed information and examples, please refer to the [official documentation](https://docs.swarms.world/en/latest/swarms/structs/agent_rearrange/).
[Check out my video on agent rearrange!](https://youtu.be/Rq8wWQ073mg)
### Methods
@ -799,68 +801,184 @@ The `run` method returns the final output after all agents have processed the in
```python
from swarms import Agent, AgentRearrange
from datetime import datetime
# Initialize the director agent
from swarms import Agent, AgentRearrange, create_file_in_folder
director = Agent(
agent_name="Director",
system_prompt="Directs the tasks for the workers",
model_name="claude-2",
chief_medical_officer = Agent(
agent_name="Chief Medical Officer",
system_prompt="""You are the Chief Medical Officer coordinating a team of medical specialists for viral disease diagnosis.
Your responsibilities include:
- Gathering initial patient symptoms and medical history
- Coordinating with specialists to form differential diagnoses
- Synthesizing different specialist opinions into a cohesive diagnosis
- Ensuring all relevant symptoms and test results are considered
- Making final diagnostic recommendations
- Suggesting treatment plans based on team input
- Identifying when additional specialists need to be consulted
Guidelines:
1. Always start with a comprehensive patient history
2. Consider both common and rare viral conditions
3. Factor in patient demographics and risk factors
4. Document your reasoning process clearly
5. Highlight any critical or emergency symptoms
6. Note any limitations or uncertainties in the diagnosis
Format all responses with clear sections for:
- Initial Assessment
- Differential Diagnoses
- Specialist Consultations Needed
- Recommended Next Steps""",
model_name="gpt-4o", # Models from litellm -> claude-2
max_loops=1,
dashboard=False,
streaming_on=True,
verbose=True,
stopping_token="<DONE>",
state_save_file_type="json",
saved_state_path="director.json",
)
# Viral Disease Specialist
virologist = Agent(
agent_name="Virologist",
system_prompt="""You are a specialist in viral diseases with expertise in:
- Respiratory viruses (Influenza, Coronavirus, RSV)
- Systemic viral infections (EBV, CMV, HIV)
- Childhood viral diseases (Measles, Mumps, Rubella)
- Emerging viral threats
Your role involves:
1. Analyzing symptoms specific to viral infections
2. Distinguishing between different viral pathogens
3. Assessing viral infection patterns and progression
4. Recommending specific viral tests
5. Evaluating epidemiological factors
For each case, consider:
- Incubation periods
- Transmission patterns
- Seasonal factors
- Geographic prevalence
- Patient immune status
- Current viral outbreaks
Provide detailed analysis of:
- Characteristic viral symptoms
- Disease progression timeline
- Risk factors for severe disease
- Potential complications""",
model_name="gpt-4o",
max_loops=1,
)
# Initialize worker 1
# Internal Medicine Specialist
internist = Agent(
agent_name="Internist",
system_prompt="""You are an Internal Medicine specialist responsible for:
- Comprehensive system-based evaluation
- Integration of symptoms across organ systems
- Identification of systemic manifestations
- Assessment of comorbidities
For each case, analyze:
1. Vital signs and their implications
2. System-by-system review (cardiovascular, respiratory, etc.)
3. Impact of existing medical conditions
4. Medication interactions and contraindications
5. Risk stratification
Consider these aspects:
- Age-related factors
- Chronic disease impact
- Medication history
- Social and environmental factors
Document:
- Physical examination findings
- System-specific symptoms
- Relevant lab abnormalities
- Risk factors for complications""",
model_name="gpt-4o",
max_loops=1,
)
worker1 = Agent(
agent_name="Worker1",
system_prompt="Generates a transcript for a youtube video on what swarms are",
model_name="claude-2",
# Diagnostic Synthesizer
synthesizer = Agent(
agent_name="Diagnostic Synthesizer",
system_prompt="""You are responsible for synthesizing all specialist inputs to create a final diagnostic assessment:
Core responsibilities:
1. Integrate findings from all specialists
2. Identify patterns and correlations
3. Resolve conflicting opinions
4. Generate probability-ranked differential diagnoses
5. Recommend additional testing if needed
Analysis framework:
- Weight evidence based on reliability and specificity
- Consider epidemiological factors
- Evaluate diagnostic certainty
- Account for test limitations
Provide structured output including:
1. Primary diagnosis with confidence level
2. Supporting evidence summary
3. Alternative diagnoses to consider
4. Recommended confirmatory tests
5. Red flags or warning signs
6. Follow-up recommendations
Documentation requirements:
- Clear reasoning chain
- Evidence quality assessment
- Confidence levels for each diagnosis
- Knowledge gaps identified
- Risk assessment""",
model_name="gpt-4o",
max_loops=1,
dashboard=False,
streaming_on=True,
verbose=True,
stopping_token="<DONE>",
state_save_file_type="json",
saved_state_path="worker1.json",
)
# Create agent list
agents = [chief_medical_officer, virologist, internist, synthesizer]
# Define diagnostic flow
flow = f"""{chief_medical_officer.agent_name} -> {virologist.agent_name} -> {internist.agent_name} -> {synthesizer.agent_name}"""
# Initialize worker 2
worker2 = Agent(
agent_name="Worker2",
system_prompt="Summarizes the transcript generated by Worker1",
model_name="claude-2",
# Create the swarm system
diagnosis_system = AgentRearrange(
name="Medical-nlp-diagnosis-swarm",
description="natural language symptions to diagnosis report",
agents=agents,
flow=flow,
max_loops=1,
dashboard=False,
streaming_on=True,
verbose=True,
stopping_token="<DONE>",
state_save_file_type="json",
saved_state_path="worker2.json",
output_type="all",
)
# Create a list of agents
agents = [director, worker1, worker2]
# Example usage
if __name__ == "__main__":
# Example patient case
patient_case = """
Patient: 45-year-old female
Presenting symptoms:
- Fever (101.5°F) for 3 days
- Dry cough
- Fatigue
- Mild shortness of breath
Medical history:
- Controlled hypertension
- No recent travel
- Fully vaccinated for COVID-19
- No known sick contacts
"""
# Define the flow pattern
flow = "Director -> Worker1 -> Worker2"
# Add timestamp to the patient case
case_info = f"Timestamp: {datetime.now()}\nPatient Information: {patient_case}"
# Run the diagnostic process
diagnosis = diagnosis_system.run(case_info)
# Create a folder and file called reports
create_file_in_folder(
"reports", "medical_analysis_agent_rearrange.md", diagnosis
)
# Using AgentRearrange class
agent_system = AgentRearrange(agents=agents, flow=flow)
output = agent_system.run(
"Create a format to express and communicate swarms of llms in a structured manner for youtube"
)
print(output)
```

@ -1,107 +1,320 @@
import requests
from loguru import logger
import time
# Configure loguru
logger.add(
"api_tests_{time}.log",
rotation="100 MB",
level="DEBUG",
format="{time} {level} {message}",
)
from typing import Dict, Optional, Tuple
from uuid import UUID
import sys
BASE_URL = "http://localhost:8000/v1"
def test_create_agent():
def check_api_server() -> bool:
"""Check if the API server is running and accessible."""
try:
response = requests.get(f"{BASE_URL}/docs")
return response.status_code == 200
except requests.exceptions.ConnectionError:
logger.error("API server is not running at {BASE_URL}")
logger.error("Please start the API server first with:")
logger.error(" python main.py")
return False
except Exception as e:
logger.error(f"Error checking API server: {str(e)}")
return False
class TestSession:
"""Manages test session state and authentication."""
def __init__(self):
self.user_id: Optional[UUID] = None
self.api_key: Optional[str] = None
self.test_agents: list[UUID] = []
@property
def headers(self) -> Dict[str, str]:
"""Get headers with authentication."""
return {"api-key": self.api_key} if self.api_key else {}
def create_test_user(session: TestSession) -> Tuple[bool, str]:
"""Create a test user and store credentials in session."""
logger.info("Creating test user")
try:
response = requests.post(
f"{BASE_URL}/users",
json={"username": f"test_user_{int(time.time())}"},
)
if response.status_code == 200:
data = response.json()
session.user_id = data["user_id"]
session.api_key = data["api_key"]
logger.success(f"Created user with ID: {session.user_id}")
return True, "Success"
else:
logger.error(f"Failed to create user: {response.text}")
return False, response.text
except Exception as e:
logger.exception("Exception during user creation")
return False, str(e)
def create_additional_api_key(
session: TestSession,
) -> Tuple[bool, str]:
"""Test creating an additional API key."""
logger.info("Creating additional API key")
try:
response = requests.post(
f"{BASE_URL}/users/{session.user_id}/api-keys",
headers=session.headers,
json={"name": "Test Key"},
)
if response.status_code == 200:
logger.success("Created additional API key")
return True, response.json()["key"]
else:
logger.error(f"Failed to create API key: {response.text}")
return False, response.text
except Exception as e:
logger.exception("Exception during API key creation")
return False, str(e)
def test_create_agent(
session: TestSession,
) -> Tuple[bool, Optional[UUID]]:
"""Test creating a new agent."""
logger.info("Testing agent creation")
payload = {
"agent_name": "Test Agent",
"agent_name": f"Test Agent {int(time.time())}",
"system_prompt": "You are a helpful assistant",
"model_name": "gpt-4",
"description": "Test agent",
"tags": ["test"],
"tags": ["test", "automated"],
}
response = requests.post(f"{BASE_URL}/agent", json=payload)
logger.debug(f"Create response: {response.json()}")
try:
response = requests.post(
f"{BASE_URL}/agent", headers=session.headers, json=payload
)
if response.status_code == 200:
agent_id = response.json()["agent_id"]
session.test_agents.append(agent_id)
logger.success(f"Created agent with ID: {agent_id}")
return True, agent_id
else:
logger.error(f"Failed to create agent: {response.text}")
return False, None
except Exception:
logger.exception("Exception during agent creation")
return False, None
def test_list_user_agents(session: TestSession) -> bool:
"""Test listing user's agents."""
logger.info("Testing user agent listing")
try:
response = requests.get(
f"{BASE_URL}/users/me/agents", headers=session.headers
)
if response.status_code == 200:
agents = response.json()
logger.success(f"Found {len(agents)} user agents")
return True
else:
logger.error(
f"Failed to list user agents: {response.text}"
)
return False
except Exception:
logger.exception("Exception during agent listing")
return False
if response.status_code == 200:
logger.success("Successfully created agent")
return response.json()["agent_id"]
else:
logger.error(f"Failed to create agent: {response.text}")
return None
def test_agent_operations(
session: TestSession, agent_id: UUID
) -> bool:
"""Test various operations on an agent."""
logger.info(f"Testing operations for agent {agent_id}")
def test_list_agents():
"""Test listing all agents."""
logger.info("Testing agent listing")
# Test update
try:
update_response = requests.patch(
f"{BASE_URL}/agent/{agent_id}",
headers=session.headers,
json={
"description": "Updated description",
"tags": ["test", "updated"],
},
)
if update_response.status_code != 200:
logger.error(
f"Failed to update agent: {update_response.text}"
)
return False
response = requests.get(f"{BASE_URL}/agents")
logger.debug(f"List response: {response.json()}")
# Test metrics
metrics_response = requests.get(
f"{BASE_URL}/agent/{agent_id}/metrics",
headers=session.headers,
)
if metrics_response.status_code != 200:
logger.error(
f"Failed to get agent metrics: {metrics_response.text}"
)
return False
if response.status_code == 200:
logger.success(f"Found {len(response.json())} agents")
else:
logger.error(f"Failed to list agents: {response.text}")
logger.success("Successfully performed agent operations")
return True
except Exception:
logger.exception("Exception during agent operations")
return False
def test_completion(agent_id):
def test_completion(session: TestSession, agent_id: UUID) -> bool:
"""Test running a completion."""
logger.info("Testing completion")
payload = {
"prompt": "What is the weather like today?",
"agent_id": agent_id,
"max_tokens": 100,
}
response = requests.post(
f"{BASE_URL}/agent/completions", json=payload
)
logger.debug(f"Completion response: {response.json()}")
try:
response = requests.post(
f"{BASE_URL}/agent/completions",
headers=session.headers,
json=payload,
)
if response.status_code == 200:
logger.success("Successfully got completion")
else:
logger.error(f"Failed to get completion: {response.text}")
if response.status_code == 200:
completion_data = response.json()
logger.success(
f"Got completion, used {completion_data['token_usage']['total_tokens']} tokens"
)
return True
else:
logger.error(f"Failed to get completion: {response.text}")
return False
except Exception:
logger.exception("Exception during completion")
return False
def test_delete_agent(agent_id):
"""Test deleting an agent."""
logger.info("Testing agent deletion")
def cleanup_test_resources(session: TestSession):
"""Clean up all test resources."""
logger.info("Cleaning up test resources")
response = requests.delete(f"{BASE_URL}/agent/{agent_id}")
logger.debug(f"Delete response: {response.json()}")
# Delete test agents
for agent_id in session.test_agents:
try:
response = requests.delete(
f"{BASE_URL}/agent/{agent_id}",
headers=session.headers,
)
if response.status_code == 200:
logger.debug(f"Deleted agent {agent_id}")
else:
logger.warning(
f"Failed to delete agent {agent_id}: {response.text}"
)
except Exception:
logger.exception(f"Exception deleting agent {agent_id}")
if response.status_code == 200:
logger.success("Successfully deleted agent")
else:
logger.error(f"Failed to delete agent: {response.text}")
# Revoke API keys
if session.user_id:
try:
response = requests.get(
f"{BASE_URL}/users/{session.user_id}/api-keys",
headers=session.headers,
)
if response.status_code == 200:
for key in response.json():
try:
revoke_response = requests.delete(
f"{BASE_URL}/users/{session.user_id}/api-keys/{key['key']}",
headers=session.headers,
)
if revoke_response.status_code == 200:
logger.debug(
f"Revoked API key {key['name']}"
)
else:
logger.warning(
f"Failed to revoke API key {key['name']}"
)
except Exception:
logger.exception(
f"Exception revoking API key {key['name']}"
)
except Exception:
logger.exception("Exception getting API keys for cleanup")
def run_tests():
"""Run all tests in sequence."""
def run_test_workflow():
"""Run complete test workflow."""
logger.info("Starting API tests")
# Create agent and get ID
agent_id = test_create_agent()
if not agent_id:
logger.error("Cannot continue tests without agent ID")
return
# Check if API server is running first
if not check_api_server():
return False
session = TestSession()
try:
# Create user
user_success, message = create_test_user(session)
if not user_success:
logger.error(f"User creation failed: {message}")
return False
# Create additional API key
key_success, key = create_additional_api_key(session)
if not key_success:
logger.error(f"API key creation failed: {key}")
return False
# Create agent
agent_success, agent_id = test_create_agent(session)
if not agent_success or not agent_id:
logger.error("Agent creation failed")
return False
# Test user agent listing
if not test_list_user_agents(session):
logger.error("Agent listing failed")
return False
# Test agent operations
if not test_agent_operations(session, agent_id):
logger.error("Agent operations failed")
return False
# Wait a bit for agent to be ready
time.sleep(1)
# Test completion
if not test_completion(session, agent_id):
logger.error("Completion test failed")
return False
# Run other tests
test_list_agents()
test_completion(agent_id)
test_delete_agent(agent_id)
logger.success("All tests completed successfully")
return True
logger.info("Tests completed")
except Exception:
logger.exception("Exception during test workflow")
return False
finally:
cleanup_test_resources(session)
if __name__ == "__main__":
run_tests()
success = run_test_workflow()
sys.exit(0 if success else 1)

@ -1,40 +1,34 @@
import os
import secrets
import traceback
from concurrent.futures import ThreadPoolExecutor
from datetime import datetime, timedelta
from enum import Enum
from pathlib import Path
from typing import Any, Dict, List, Optional
from uuid import UUID, uuid4
import uvicorn
from dotenv import load_dotenv
from fastapi import (
BackgroundTasks,
Depends,
FastAPI,
Header,
HTTPException,
status,
Query,
BackgroundTasks,
Request,
status,
)
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel, Field
from typing import Optional, Dict, Any, List
from loguru import logger
import uvicorn
from datetime import datetime, timedelta
from uuid import UUID, uuid4
from enum import Enum
from pathlib import Path
from concurrent.futures import ThreadPoolExecutor
import traceback
from pydantic import BaseModel, Field
from swarms import Agent
from dotenv import load_dotenv
# Load environment variables
load_dotenv()
# Configure Loguru
logger.add(
"logs/api_{time}.log",
rotation="500 MB",
retention="10 days",
level="INFO",
format="{time} {level} {message}",
backtrace=True,
diagnose=True,
)
class AgentStatus(str, Enum):
"""Enum for agent status."""
@ -45,6 +39,30 @@ class AgentStatus(str, Enum):
MAINTENANCE = "maintenance"
# Security configurations
API_KEY_LENGTH = 32 # Length of generated API keys
class APIKey(BaseModel):
key: str
name: str
created_at: datetime
last_used: datetime
is_active: bool = True
class APIKeyCreate(BaseModel):
name: str # A friendly name for the API key
class User(BaseModel):
id: UUID
username: str
is_active: bool = True
is_admin: bool = False
api_keys: Dict[str, APIKey] = {} # key -> APIKey object
class AgentConfig(BaseModel):
"""Configuration model for creating a new agent."""
@ -172,6 +190,11 @@ class AgentStore:
def __init__(self):
self.agents: Dict[UUID, Agent] = {}
self.agent_metadata: Dict[UUID, Dict[str, Any]] = {}
self.users: Dict[UUID, User] = {} # user_id -> User
self.api_keys: Dict[str, UUID] = {} # api_key -> user_id
self.user_agents: Dict[UUID, List[UUID]] = (
{}
) # user_id -> [agent_ids]
self.executor = ThreadPoolExecutor(max_workers=4)
self._ensure_directories()
@ -180,7 +203,59 @@ class AgentStore:
Path("logs").mkdir(exist_ok=True)
Path("states").mkdir(exist_ok=True)
async def create_agent(self, config: AgentConfig) -> UUID:
def create_api_key(self, user_id: UUID, key_name: str) -> APIKey:
"""Create a new API key for a user."""
if user_id not in self.users:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="User not found",
)
# Generate a secure random API key
api_key = secrets.token_urlsafe(API_KEY_LENGTH)
# Create the API key object
key_object = APIKey(
key=api_key,
name=key_name,
created_at=datetime.utcnow(),
last_used=datetime.utcnow(),
)
# Store the API key
self.users[user_id].api_keys[api_key] = key_object
self.api_keys[api_key] = user_id
return key_object
async def verify_agent_access(
self, agent_id: UUID, user_id: UUID
) -> bool:
"""Verify if a user has access to an agent."""
if agent_id not in self.agents:
return False
return (
self.agent_metadata[agent_id]["owner_id"] == user_id
or self.users[user_id].is_admin
)
def validate_api_key(self, api_key: str) -> Optional[UUID]:
"""Validate an API key and return the associated user ID."""
user_id = self.api_keys.get(api_key)
if not user_id or api_key not in self.users[user_id].api_keys:
return None
key_object = self.users[user_id].api_keys[api_key]
if not key_object.is_active:
return None
# Update last used timestamp
key_object.last_used = datetime.utcnow()
return user_id
async def create_agent(
self, config: AgentConfig, user_id: UUID
) -> UUID:
"""Create a new agent with the given configuration."""
try:
@ -219,7 +294,11 @@ class AgentStore:
"successful_completions": 0,
}
logger.info(f"Created agent with ID: {agent_id}")
# Add to user's agents list
if user_id not in self.user_agents:
self.user_agents[user_id] = []
self.user_agents[user_id].append(agent_id)
return agent_id
except Exception as e:
@ -465,6 +544,40 @@ class AgentStore:
metadata["status"] = AgentStatus.IDLE
class StoreManager:
_instance = None
@classmethod
def get_instance(cls) -> "AgentStore":
if cls._instance is None:
cls._instance = AgentStore()
return cls._instance
# Modify the dependency function
def get_store() -> AgentStore:
"""Dependency to get the AgentStore instance."""
return StoreManager.get_instance()
# Security utility function using the new dependency
async def get_current_user(
api_key: str = Header(
..., description="API key for authentication"
),
store: AgentStore = Depends(get_store),
) -> User:
"""Validate API key and return current user."""
user_id = store.validate_api_key(api_key)
if not user_id:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Invalid or expired API key",
headers={"WWW-Authenticate": "ApiKey"},
)
return store.users[user_id]
class SwarmsAPI:
"""Enhanced API class for Swarms agent integration."""
@ -476,7 +589,9 @@ class SwarmsAPI:
docs_url="/v1/docs",
redoc_url="/v1/redoc",
)
self.store = AgentStore()
# Initialize the store using the singleton manager
self.store = StoreManager.get_instance()
# Configure CORS
self.app.add_middleware(
CORSMiddleware,
@ -493,10 +608,130 @@ class SwarmsAPI:
def _setup_routes(self):
"""Set up API routes."""
# In your API code
@self.app.post("/v1/users", response_model=Dict[str, Any])
async def create_user(request: Request):
"""Create a new user and initial API key."""
try:
body = await request.json()
username = body.get("username")
if not username or len(username) < 3:
raise HTTPException(
status_code=400, detail="Invalid username"
)
user_id = uuid4()
user = User(id=user_id, username=username)
self.store.users[user_id] = user
initial_key = self.store.create_api_key(
user_id, "Initial Key"
)
return {
"user_id": user_id,
"api_key": initial_key.key,
}
except Exception as e:
logger.error(f"Error creating user: {str(e)}")
raise HTTPException(status_code=400, detail=str(e))
@self.app.post(
"/v1/users/{user_id}/api-keys", response_model=APIKey
)
async def create_api_key(
user_id: UUID,
key_create: APIKeyCreate,
current_user: User = Depends(get_current_user),
):
"""Create a new API key for a user."""
if (
current_user.id != user_id
and not current_user.is_admin
):
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
detail="Not authorized to create API keys for this user",
)
return self.store.create_api_key(user_id, key_create.name)
@self.app.get(
"/v1/users/{user_id}/api-keys",
response_model=List[APIKey],
)
async def list_api_keys(
user_id: UUID,
current_user: User = Depends(get_current_user),
):
"""List all API keys for a user."""
if (
current_user.id != user_id
and not current_user.is_admin
):
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
detail="Not authorized to view API keys for this user",
)
return list(self.store.users[user_id].api_keys.values())
@self.app.delete("/v1/users/{user_id}/api-keys/{key}")
async def revoke_api_key(
user_id: UUID,
key: str,
current_user: User = Depends(get_current_user),
):
"""Revoke an API key."""
if (
current_user.id != user_id
and not current_user.is_admin
):
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
detail="Not authorized to revoke API keys for this user",
)
if key in self.store.users[user_id].api_keys:
self.store.users[user_id].api_keys[
key
].is_active = False
del self.store.api_keys[key]
return {"status": "API key revoked"}
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="API key not found",
)
@self.app.get(
"/v1/users/me/agents", response_model=List[AgentSummary]
)
async def list_user_agents(
current_user: User = Depends(get_current_user),
tags: Optional[List[str]] = Query(None),
status: Optional[AgentStatus] = None,
):
"""List all agents owned by the current user."""
user_agents = self.store.user_agents.get(
current_user.id, []
)
return [
agent
for agent in await self.store.list_agents(
tags, status
)
if agent.agent_id in user_agents
]
# Modify existing routes to use API key authentication
@self.app.post("/v1/agent", response_model=Dict[str, UUID])
async def create_agent(config: AgentConfig):
async def create_agent(
config: AgentConfig,
current_user: User = Depends(get_current_user),
):
"""Create a new agent with the specified configuration."""
agent_id = await self.store.create_agent(config)
agent_id = await self.store.create_agent(
config, current_user.id
)
return {"agent_id": agent_id}
@self.app.get("/v1/agents", response_model=List[AgentSummary])
@ -613,17 +848,26 @@ class SwarmsAPI:
def create_app() -> FastAPI:
"""Create and configure the FastAPI application."""
logger.info("Creating FastAPI application")
api = SwarmsAPI()
return api.app
app = api.app
logger.info("FastAPI application created successfully")
return app
app = create_app()
if __name__ == "__main__":
# Configure uvicorn logging
logger.info("API Starting")
uvicorn.run(
"main:create_app",
host="0.0.0.0",
port=8000,
reload=True,
workers=4,
)
try:
logger.info("Starting API server...")
print("Starting API server on http://0.0.0.0:8000")
uvicorn.run(
app, # Pass the app instance directly
host="0.0.0.0",
port=8000,
log_level="info",
)
except Exception as e:
logger.error(f"Failed to start API: {str(e)}")
print(f"Error starting server: {str(e)}")

@ -0,0 +1,6 @@
fastapi
uvicorn
pydantic
loguru
python-dotenv
swarms # Specify the version or source if it's not on PyPI

@ -0,0 +1,37 @@
service:
readiness_probe:
path: /docs
initial_delay_seconds: 300
timeout_seconds: 30
replica_policy:
min_replicas: 1
max_replicas: 50
target_qps_per_replica: 5
upscale_delay_seconds: 180
downscale_delay_seconds: 600
resources:
ports: 8000 # FastAPI default port
cpus: 16
memory: 64
disk_size: 100
use_spot: true
workdir: /app
setup: |
git clone https://github.com/kyegomez/swarms.git
cd swarms/api
pip install -r requirements.txt
pip install swarms
run: |
cd swarms/api
uvicorn main:app --host 0.0.0.0 --port 8000 --workers 4
# env:
# PYTHONPATH: /app/swarms
# LOG_LEVEL: "INFO"
# # MAX_WORKERS: "4"

@ -0,0 +1,112 @@
import requests
import json
from time import sleep
BASE_URL = "http://api.swarms.ai:8000"
def make_request(method, endpoint, data=None):
"""Helper function to make requests with error handling"""
url = f"{BASE_URL}{endpoint}"
try:
if method == "GET":
response = requests.get(url)
elif method == "POST":
response = requests.post(url, json=data)
elif method == "DELETE":
response = requests.delete(url)
response.raise_for_status()
return response.json()
except requests.exceptions.RequestException as e:
print(
f"Error making {method} request to {endpoint}: {str(e)}"
)
if hasattr(e.response, "text"):
print(f"Response text: {e.response.text}")
return None
def create_agent():
"""Create a test agent"""
data = {
"agent_name": "test_agent",
"model_name": "gpt-4",
"system_prompt": "You are a helpful assistant",
"description": "Test agent",
"temperature": 0.7,
"max_loops": 1,
"tags": ["test"],
}
return make_request("POST", "/v1/agent", data)
def list_agents():
"""List all agents"""
return make_request("GET", "/v1/agents")
def test_completion(agent_id):
"""Test a completion with the agent"""
data = {
"prompt": "Say hello!",
"agent_id": agent_id,
"max_tokens": 100,
}
return make_request("POST", "/v1/agent/completions", data)
def get_agent_metrics(agent_id):
"""Get metrics for an agent"""
return make_request("GET", f"/v1/agent/{agent_id}/metrics")
def delete_agent(agent_id):
"""Delete an agent"""
return make_request("DELETE", f"/v1/agent/{agent_id}")
def run_tests():
print("Starting API tests...")
# Create an agent
print("\n1. Creating agent...")
agent_response = create_agent()
if not agent_response:
print("Failed to create agent")
return
agent_id = agent_response.get("agent_id")
print(f"Created agent with ID: {agent_id}")
# Give the server a moment to process
sleep(2)
# List agents
print("\n2. Listing agents...")
agents = list_agents()
print(f"Found {len(agents)} agents")
# Test completion
if agent_id:
print("\n3. Testing completion...")
completion = test_completion(agent_id)
if completion:
print(
f"Completion response: {completion.get('response')}"
)
print("\n4. Getting agent metrics...")
metrics = get_agent_metrics(agent_id)
if metrics:
print(f"Agent metrics: {json.dumps(metrics, indent=2)}")
# Clean up
# print("\n5. Cleaning up - deleting agent...")
# delete_result = delete_agent(agent_id)
# if delete_result:
# print("Successfully deleted agent")
if __name__ == "__main__":
run_tests()

@ -1,898 +0,0 @@
from enum import Enum
from typing import Union, Optional
import io
from PIL import Image
import numpy as np
import torch
import struct
from enum import auto
from typing import List, Dict, Tuple
import wave
from dataclasses import dataclass
import torch.nn as nn
import torch.nn.functional as F
from loguru import logger
from einops import rearrange
from torch import Tensor
@dataclass
class ModelConfig:
"""Configuration for the enhanced BytePredictor model."""
vocab_size: int = 256 # Standard byte range
hidden_size: int = 1024
num_layers: int = 12
num_key_value_heads: int = 8 # For multi-query attention
num_query_heads: int = 32 # More query heads than kv heads
dropout: float = 0.1
max_sequence_length: int = 8192
rope_theta: float = 10000.0
layer_norm_eps: float = 1e-5
vocab_parallel: bool = False
qk_norm: bool = True
qk_norm_scale: float = None
attention_bias: bool = False
class MultiQueryAttention(nn.Module):
"""Fixed Multi-Query Attention implementation."""
def __init__(self, config: ModelConfig):
super().__init__()
self.hidden_size = config.hidden_size
self.num_query_heads = config.num_query_heads
self.num_key_value_heads = config.num_key_value_heads
self.head_dim = config.hidden_size // config.num_query_heads
self.qk_scale = config.qk_norm_scale or (self.head_dim**-0.5)
self.q_proj = nn.Linear(
config.hidden_size, config.num_query_heads * self.head_dim
)
self.k_proj = nn.Linear(
config.hidden_size,
config.num_key_value_heads * self.head_dim,
)
self.v_proj = nn.Linear(
config.hidden_size,
config.num_key_value_heads * self.head_dim,
)
self.o_proj = nn.Linear(
config.num_query_heads * self.head_dim, config.hidden_size
)
self.qk_norm = config.qk_norm
if self.qk_norm:
self.q_norm = nn.LayerNorm(self.head_dim)
self.k_norm = nn.LayerNorm(self.head_dim)
def forward(
self,
hidden_states: torch.Tensor,
attention_mask: Optional[torch.Tensor] = None,
) -> torch.Tensor:
batch_size, seq_length, _ = hidden_states.shape
# Project and reshape
q = self.q_proj(hidden_states)
k = self.k_proj(hidden_states)
v = self.v_proj(hidden_states)
# Reshape to [seq_len, batch, heads, head_dim]
q = q.view(
batch_size,
seq_length,
self.num_query_heads,
self.head_dim,
).permute(1, 0, 2, 3)
k = k.view(
batch_size,
seq_length,
self.num_key_value_heads,
self.head_dim,
).permute(1, 0, 2, 3)
v = v.view(
batch_size,
seq_length,
self.num_key_value_heads,
self.head_dim,
).permute(1, 0, 2, 3)
# Apply rotary embeddings
# q, k = self.rotary(q, k, seq_length)
# Apply QK normalization if enabled
if self.qk_norm:
q = self.q_norm(q)
k = self.k_norm(k)
# Handle MQA head expansion
if self.num_key_value_heads != self.num_query_heads:
k = k.repeat_interleave(
self.num_query_heads // self.num_key_value_heads,
dim=2,
)
v = v.repeat_interleave(
self.num_query_heads // self.num_key_value_heads,
dim=2,
)
# Compute attention
# Reshape for matmul: [batch, heads, seq_length, head_dim]
q = q.permute(1, 2, 0, 3)
k = k.permute(1, 2, 0, 3)
v = v.permute(1, 2, 0, 3)
attn_weights = (
torch.matmul(q, k.transpose(-2, -1)) * self.qk_scale
)
if attention_mask is not None:
attn_weights = attn_weights + attention_mask
attn_weights = F.softmax(attn_weights, dim=-1)
output = torch.matmul(attn_weights, v)
# Reshape back to [batch, seq_length, hidden_size]
output = (
output.transpose(1, 2)
.contiguous()
.view(batch_size, seq_length, -1)
)
output = self.o_proj(output)
return output
class EnhancedBytePredictor(nn.Module):
"""Enhanced byte prediction model with state-of-the-art features."""
def __init__(self, config: ModelConfig):
super().__init__()
self.config = config
# Token embeddings
self.tok_embeddings = nn.Embedding(
config.vocab_size, config.hidden_size
)
# Transformer layers
self.layers = nn.ModuleList(
[
nn.ModuleDict(
{
"attention": MultiQueryAttention(config),
"attention_norm": nn.LayerNorm(
config.hidden_size,
eps=config.layer_norm_eps,
),
"feed_forward": nn.Sequential(
nn.Linear(
config.hidden_size,
4 * config.hidden_size,
),
nn.GELU(),
nn.Linear(
4 * config.hidden_size,
config.hidden_size,
),
),
"feed_forward_norm": nn.LayerNorm(
config.hidden_size,
eps=config.layer_norm_eps,
),
}
)
for _ in range(config.num_layers)
]
)
self.norm = nn.LayerNorm(
config.hidden_size, eps=config.layer_norm_eps
)
self.output = nn.Linear(
config.hidden_size, config.vocab_size, bias=False
)
# Initialize weights
self.apply(self._init_weights)
def _init_weights(self, module: nn.Module) -> None:
"""Initialize weights with scaled normal distribution."""
if isinstance(module, nn.Linear):
torch.nn.init.normal_(module.weight, mean=0.0, std=0.02)
if module.bias is not None:
torch.nn.init.zeros_(module.bias)
elif isinstance(module, nn.Embedding):
torch.nn.init.normal_(module.weight, mean=0.0, std=0.02)
def forward(
self,
input_ids: torch.Tensor,
attention_mask: Optional[torch.Tensor] = None,
) -> torch.Tensor:
"""
Forward pass of the model.
Args:
input_ids: Tensor of shape (batch_size, sequence_length)
attention_mask: Optional attention mask
Returns:
Tensor of logits with shape (batch_size, sequence_length, vocab_size)
"""
hidden_states = self.tok_embeddings(input_ids)
# Create causal mask if needed
if attention_mask is None:
attention_mask = torch.triu(
torch.ones(
(input_ids.size(1), input_ids.size(1)),
device=input_ids.device,
dtype=torch.bool,
),
diagonal=1,
)
attention_mask = attention_mask.masked_fill(
attention_mask == 1, float("-inf")
)
# Apply transformer layers
for layer in self.layers:
# Attention block
hidden_states = hidden_states + layer["attention"](
layer["attention_norm"](hidden_states), attention_mask
)
# Feed-forward block
hidden_states = hidden_states + layer["feed_forward"](
layer["feed_forward_norm"](hidden_states)
)
hidden_states = self.norm(hidden_states)
logits = self.output(hidden_states)
return logits
def compute_loss(
self,
input_ids: torch.Tensor,
target_ids: torch.Tensor,
attention_mask: Optional[torch.Tensor] = None,
) -> torch.Tensor:
"""
Compute cross entropy loss.
Args:
input_ids: Input token ids
target_ids: Target token ids
attention_mask: Optional attention mask
Returns:
Loss value
"""
logits = self(input_ids, attention_mask)
loss = F.cross_entropy(
rearrange(logits, "b s v -> (b s) v"),
rearrange(target_ids, "b s -> (b s)"),
)
return loss
@torch.no_grad()
def _generate(
self,
input_ids: torch.Tensor,
max_new_tokens: int = 100,
temperature: float = 1.0,
top_k: Optional[int] = None,
top_p: Optional[float] = None,
repetition_penalty: float = 1.0,
) -> torch.Tensor:
"""
Generate new tokens autoregressively.
Args:
input_ids: Starting sequence
max_new_tokens: Number of tokens to generate
temperature: Sampling temperature
top_k: K for top-k sampling
top_p: P for nucleus sampling
repetition_penalty: Penalty for repeating tokens
Returns:
Generated sequence
"""
batch_size, seq_length = input_ids.shape
generated = input_ids.clone()
for _ in range(max_new_tokens):
if generated.size(1) >= self.config.max_sequence_length:
break
# Forward pass
logits = self(generated)[:, -1, :]
# Apply temperature
logits = logits / temperature
# Apply repetition penalty
if repetition_penalty != 1.0:
for i in range(batch_size):
for token_id in set(generated[i].tolist()):
logits[i, token_id] /= repetition_penalty
# Apply top-k sampling
if top_k is not None:
indices_to_remove = (
logits
< torch.topk(logits, top_k)[0][..., -1, None]
)
logits[indices_to_remove] = float("-inf")
# Apply nucleus (top-p) sampling
if top_p is not None:
sorted_logits, sorted_indices = torch.sort(
logits, descending=True
)
cumulative_probs = torch.cumsum(
F.softmax(sorted_logits, dim=-1), dim=-1
)
# Remove tokens with cumulative probability above the threshold
sorted_indices_to_remove = cumulative_probs > top_p
sorted_indices_to_remove[..., 1:] = (
sorted_indices_to_remove[..., :-1].clone()
)
sorted_indices_to_remove[..., 0] = 0
indices_to_remove = torch.zeros_like(
logits, dtype=torch.bool
)
indices_to_remove.scatter_(
1, sorted_indices, sorted_indices_to_remove
)
logits[indices_to_remove] = float("-inf")
# Sample next token
probs = F.softmax(logits, dim=-1)
next_token = torch.multinomial(probs, num_samples=1)
# Append to sequence
generated = torch.cat([generated, next_token], dim=1)
return generated
def generate(
self,
input_ids: torch.Tensor,
max_new_tokens: int = 100,
temperature: float = 1.0,
top_k: Optional[int] = None,
top_p: Optional[float] = None,
repetition_penalty: float = 1.0,
):
tensor_data = self._generate(
input_ids=input_ids,
max_new_tokens=max_new_tokens,
temperature=temperature,
top_k=top_k,
top_p=top_p,
repetition_penalty=repetition_penalty,
)
return tensor_to_data(tensor_data)
# import torch
# from typing import Optional
class DataType(Enum):
TEXT = "text"
IMAGE = "image"
AUDIO = "audio"
VIDEO = "video"
BINARY = "binary"
class ByteDetokenizer:
"""Utility class for converting model output bytes back to original data formats."""
@staticmethod
def tensor_to_bytes(tensor: torch.Tensor) -> bytes:
"""Convert model output tensor to bytes."""
# Convert logits/probabilities to byte values
if tensor.dim() > 1:
# If we have logits, convert to byte indices
byte_indices = tensor.argmax(dim=-1)
else:
byte_indices = tensor
# Convert to Python bytes
return bytes(
byte_indices.cpu().numpy().astype(np.uint8).tolist()
)
@staticmethod
def decode_text(byte_sequence: bytes) -> str:
"""Convert bytes to text."""
try:
return byte_sequence.decode("utf-8")
except UnicodeDecodeError:
# Try with error handling
return byte_sequence.decode("utf-8", errors="replace")
@staticmethod
def decode_image(
byte_sequence: bytes,
mode: str = "RGB",
size: Optional[tuple] = None,
) -> Image.Image:
"""Convert bytes to image.
Args:
byte_sequence: Raw image bytes
mode: Image mode (RGB, RGBA, L, etc.)
size: Optional tuple of (width, height)
"""
try:
# Try to load as-is first (for standard image formats)
img = Image.open(io.BytesIO(byte_sequence))
if size:
img = img.resize(size)
return img
except:
# If failed, assume raw pixel data
if not size:
# Try to determine size from byte count
pixel_count = len(byte_sequence) // len(mode)
size = (
int(np.sqrt(pixel_count)),
int(np.sqrt(pixel_count)),
)
# Convert raw bytes to pixel array
pixels = np.frombuffer(byte_sequence, dtype=np.uint8)
pixels = pixels.reshape((*size, len(mode)))
return Image.fromarray(pixels, mode=mode)
@staticmethod
def decode_audio(
byte_sequence: bytes,
sample_rate: int = 44100,
channels: int = 2,
sample_width: int = 2,
) -> np.ndarray:
"""Convert bytes to audio samples.
Args:
byte_sequence: Raw audio bytes
sample_rate: Audio sample rate in Hz
channels: Number of audio channels
sample_width: Bytes per sample (1, 2, or 4)
"""
# Determine format string based on sample width
format_str = {
1: "b", # signed char
2: "h", # short
4: "i", # int
}[sample_width]
# Unpack bytes to samples
sample_count = len(byte_sequence) // (channels * sample_width)
samples = struct.unpack(
f"<{sample_count * channels}{format_str}", byte_sequence
)
# Reshape to [samples, channels]
return np.array(samples).reshape(-1, channels)
def decode_data(
self,
model_output: Union[torch.Tensor, bytes],
data_type: DataType,
**kwargs,
) -> Union[str, Image.Image, np.ndarray, bytes]:
"""Main method to decode model output to desired format.
Args:
model_output: Either tensor from model or raw bytes
data_type: Type of data to decode to
**kwargs: Additional parameters for specific decoders
Returns:
Decoded data in specified format
"""
# Convert tensor to bytes if needed
if isinstance(model_output, torch.Tensor):
byte_sequence = self.tensor_to_bytes(model_output)
else:
byte_sequence = model_output
# Decode based on type
if data_type == DataType.TEXT:
return self.decode_text(byte_sequence)
elif data_type == DataType.IMAGE:
return self.decode_image(byte_sequence, **kwargs)
elif data_type == DataType.AUDIO:
return self.decode_audio(byte_sequence, **kwargs)
elif data_type == DataType.VIDEO:
raise NotImplementedError(
"Video decoding not yet implemented"
)
else: # BINARY
return byte_sequence
# Usage example
class Modality(Enum):
TEXT = auto()
IMAGE = auto()
AUDIO = auto()
VIDEO = auto()
BINARY = auto()
MULTIMODAL = auto()
@dataclass
class ModalityInfo:
"""Information about detected modality."""
modality: Modality
confidence: float
metadata: Dict[str, any]
sub_modalities: Optional[List["ModalityInfo"]] = None
class ModalityDetector:
"""Detects data modalities from byte sequences."""
# Common file signatures (magic numbers)
SIGNATURES = {
# Images
b"\xFF\xD8\xFF": "JPEG",
b"\x89PNG\r\n\x1a\n": "PNG",
b"GIF87a": "GIF",
b"GIF89a": "GIF",
b"RIFF": "WEBP",
# Audio
b"RIFF....WAVE": "WAV",
b"ID3": "MP3",
b"\xFF\xFB": "MP3",
b"OggS": "OGG",
# Video
b"\x00\x00\x00\x18ftypmp42": "MP4",
b"\x00\x00\x00\x1Cftypav01": "MP4",
b"\x1A\x45\xDF\xA3": "WEBM",
}
def __init__(self):
self.magic = magic.Magic(mime=True)
def _check_text_probability(self, data: bytes) -> float:
"""Estimate probability that data is text."""
# Check if data is valid UTF-8
try:
data.decode("utf-8")
# Count printable ASCII characters
printable = sum(1 for b in data if 32 <= b <= 126)
return printable / len(data)
except UnicodeDecodeError:
return 0.0
def _check_image_validity(self, data: bytes) -> Tuple[bool, Dict]:
"""Check if data is a valid image and extract metadata."""
try:
with io.BytesIO(data) as bio:
img = Image.open(bio)
return True, {
"format": img.format,
"size": img.size,
"mode": img.mode,
}
except:
return False, {}
def _check_audio_validity(self, data: bytes) -> Tuple[bool, Dict]:
"""Check if data is valid audio and extract metadata."""
try:
with io.BytesIO(data) as bio:
# Try to parse as WAV
with wave.open(bio) as wav:
return True, {
"channels": wav.getnchannels(),
"sample_width": wav.getsampwidth(),
"framerate": wav.getframerate(),
"frames": wav.getnframes(),
}
except:
# Check for other audio signatures
for sig in [b"ID3", b"\xFF\xFB", b"OggS"]:
if data.startswith(sig):
return True, {"format": "compressed_audio"}
return False, {}
def _detect_boundaries(
self, data: bytes
) -> List[Tuple[int, int, Modality]]:
"""Detect boundaries between different modalities in the data."""
boundaries = []
current_pos = 0
while current_pos < len(data):
# Look for known signatures
for sig, format_type in self.SIGNATURES.items():
if data[current_pos:].startswith(sig):
# Found a signature, determine its length
if format_type in ["JPEG", "PNG", "GIF"]:
# Find image end
try:
with io.BytesIO(
data[current_pos:]
) as bio:
img = Image.open(bio)
img.verify()
size = bio.tell()
boundaries.append(
(
current_pos,
current_pos + size,
Modality.IMAGE,
)
)
current_pos += size
continue
except:
pass
# Check for text sections
text_prob = self._check_text_probability(
data[current_pos : current_pos + 1024]
)
if text_prob > 0.8:
# Look for end of text section
end_pos = current_pos + 1
while end_pos < len(data):
if (
self._check_text_probability(
data[end_pos : end_pos + 32]
)
< 0.5
):
break
end_pos += 1
boundaries.append(
(current_pos, end_pos, Modality.TEXT)
)
current_pos = end_pos
continue
current_pos += 1
return boundaries
def detect_modality(self, data: bytes) -> ModalityInfo:
"""Detect modality of byte sequence."""
# First check for single modality
mime_type = self.magic.from_buffer(data)
# Check text
text_prob = self._check_text_probability(data)
if text_prob > 0.9:
return ModalityInfo(
modality=Modality.TEXT,
confidence=text_prob,
metadata={"mime_type": mime_type},
)
# Check image
is_image, image_meta = self._check_image_validity(data)
if is_image:
return ModalityInfo(
modality=Modality.IMAGE,
confidence=1.0,
metadata={**image_meta, "mime_type": mime_type},
)
# Check audio
is_audio, audio_meta = self._check_audio_validity(data)
if is_audio:
return ModalityInfo(
modality=Modality.AUDIO,
confidence=1.0,
metadata={**audio_meta, "mime_type": mime_type},
)
# Check for multimodal content
boundaries = self._detect_boundaries(data)
if len(boundaries) > 1:
sub_modalities = []
for start, end, modality in boundaries:
chunk_data = data[start:end]
sub_info = self.detect_modality(chunk_data)
if sub_info.modality != Modality.BINARY:
sub_modalities.append(sub_info)
if sub_modalities:
return ModalityInfo(
modality=Modality.MULTIMODAL,
confidence=0.8,
metadata={"mime_type": "multipart/mixed"},
sub_modalities=sub_modalities,
)
# Default to binary
return ModalityInfo(
modality=Modality.BINARY,
confidence=0.5,
metadata={"mime_type": mime_type},
)
def split_modalities(
self, data: bytes
) -> List[Tuple[Modality, bytes, Dict]]:
"""Split multimodal data into separate modalities."""
boundaries = self._detect_boundaries(data)
result = []
for start, end, modality in boundaries:
chunk = data[start:end]
info = self.detect_modality(chunk)
result.append((modality, chunk, info.metadata))
return result
class AutoDetectBytesDecoder:
"""Decoder that automatically detects and decodes different modalities."""
def __init__(self):
self.detector = ModalityDetector()
self.text_decoder = ByteDetokenizer() # From previous example
def decode(
self, data: bytes
) -> Union[str, Image.Image, np.ndarray, List[any]]:
"""Automatically detect and decode byte sequence."""
info = self.detector.detect_modality(data)
if info.modality == Modality.MULTIMODAL:
# Handle multimodal content
parts = self.detector.split_modalities(data)
return [
self.decode(chunk) for modality, chunk, _ in parts
]
if info.modality == Modality.TEXT:
return self.text_decoder.decode_text(data)
elif info.modality == Modality.IMAGE:
return self.text_decoder.decode_image(data)
elif info.modality == Modality.AUDIO:
return self.text_decoder.decode_audio(data)
else:
return data
# # Example usage
# def demo_auto_detection():
# """Demonstrate auto modality detection."""
# # Create mixed content
# text = "Hello, World!".encode('utf-8')
# # Create a small test image
# img = Image.new('RGB', (100, 100), color='red')
# img_bytes = io.BytesIO()
# img.save(img_bytes, format='PNG')
# # Combine into multimodal content
# mixed_content = text + img_bytes.getvalue()
# # Initialize decoder
# decoder = AutoDetectBytesDecoder()
# # Decode
# result = decoder.decode(mixed_content)
# if isinstance(result, list):
# print("Detected multimodal content:")
# for i, part in enumerate(result):
# print(f"Part {i+1}: {type(part)}")
# if __name__ == "__main__":
# demo_auto_detection()
def tensor_to_data(tensor: Tensor):
byte_sequence = ByteDetokenizer.tensor_to_bytes(tensor)
# Initialize auto-detector
decoder = AutoDetectBytesDecoder()
# Decode with automatic detection
result = decoder.decode(byte_sequence)
return result
def demo_byte_predictor():
"""Demo with smaller dimensions to test."""
# Initialize model configuration with adjusted dimensions
config = ModelConfig(
vocab_size=256,
hidden_size=128, # Smaller for testing
num_layers=2, # Fewer layers for testing
num_key_value_heads=2,
num_query_heads=4,
dropout=0.1,
max_sequence_length=1024,
)
# Initialize model
model = EnhancedBytePredictor(config)
logger.info("Model initialized")
# Move to GPU if available
device = torch.device(
"cuda" if torch.cuda.is_available() else "cpu"
)
model = model.to(device)
logger.info(f"Using device: {device}")
# Create sample input data
batch_size = 2
seq_length = 16 # Shorter sequence for testing
input_ids = torch.randint(
0, config.vocab_size, (batch_size, seq_length), device=device
)
logger.info(f"Created input tensor of shape: {input_ids.shape}")
# Test forward pass
try:
logits = model(input_ids)
logger.info(
f"Forward pass successful! Output shape: {logits.shape}"
)
# Test loss computation
target_ids = torch.randint(
0,
config.vocab_size,
(batch_size, seq_length),
device=device,
)
loss = model.compute_loss(input_ids, target_ids)
logger.info(
f"Loss computation successful! Loss value: {loss.item():.4f}"
)
# Test generation
prompt = torch.randint(
0,
config.vocab_size,
(1, 4), # Very short prompt for testing
device=device,
)
generated = model.generate(
prompt, max_new_tokens=8, temperature=0.8, top_k=50
)
logger.info(
f"Generation successful! Generated shape: {generated.shape}"
)
except Exception as e:
logger.error(f"Error during execution: {str(e)}")
raise
if __name__ == "__main__":
# Set up logging
# logger.remove() # Remove default handler
# logger.add(sys.stderr, format="<green>{time:HH:mm:ss}</green> | {level} | {message}")
demo_byte_predictor()

@ -0,0 +1,61 @@
# Swarms Bounty Program
## Overview
The Swarms Bounty Program is an initiative designed to incentivize contributors to help us improve and expand the Swarms framework. With an impressive $150,000 allocated for bounties, contributors have the unique opportunity to earn generous rewards while gaining prestigious recognition in the Swarms community of over 9,000 agent engineers. This program offers more than just financial benefits; it allows contributors to play a pivotal role in advancing the field of multi-agent collaboration and AI automation, while also growing their professional skills and network. By joining the Swarms Bounty Program, you become part of an innovative movement shaping the future of technology.
## Why Contribute?
1. **Generous Rewards**: The bounty pool totals $150,000, ensuring that contributors are fairly compensated for their valuable work on successfully completed tasks. Each task comes with its own reward, reflecting its complexity and impact.
2. **Community Status**: Gain coveted recognition as a valued and active contributor within the thriving Swarms community. This status not only highlights your contributions but also builds your reputation among a network of AI engineers.
3. **Skill Development**: Collaborate on cutting-edge AI projects, hone your expertise in agent engineering, and learn practical skills that can be applied to real-world challenges in the AI domain.
4. **Networking Opportunities**: Work side-by-side with over 9,000 agent engineers in our active and supportive community. This network fosters collaboration, knowledge sharing, and mentorship opportunities that can significantly boost your career.
## How It Works
1. **Explore Issues and Tasks**:
- Visit the [Swarms GitHub Issues](https://github.com/kyegomez/swarms/issues) to find a comprehensive list of open tasks requiring attention. These issues range from coding challenges to documentation improvements, offering opportunities for contributors with various skill sets.
- Check the [Swarms Project Board](https://github.com/users/kyegomez/projects/1) for prioritized tasks and ongoing milestones. This board provides a clear view of project priorities and helps contributors align their efforts with the project's immediate goals.
2. **Claim a Bounty**:
- Identify a task that aligns with your interests and expertise.
- Comment on the issue to indicate your intent to work on it and describe your approach if necessary.
- Await approval from the Swarms team before commencing work. Approval ensures clarity and avoids duplication of efforts by other contributors.
3. **Submit Your Work**:
- Complete the task as per the outlined requirements in the issue description. Pay close attention to details to ensure your submission meets the expectations.
- Submit your pull request (PR) on GitHub with all the required elements, including documentation, test cases, or any relevant files that demonstrate your work.
- Engage with reviewers to refine your submission if requested.
4. **Earn Rewards**:
- Once your PR is reviewed, accepted, and merged into the main project, you will receive the bounty payment associated with the task.
- Your contributor status in the Swarms community will be updated, showcasing your involvement and accomplishments.
## Contribution Guidelines
To ensure high-quality contributions and streamline the process, please adhere to the following guidelines:
- Familiarize yourself with the [Swarms Contribution Guidelines](https://github.com/kyegomez/swarms/blob/main/CONTRIBUTING.md). These guidelines outline coding standards, best practices, and procedures for contributing effectively.
- Ensure your code is clean, modular, and well-documented. Contributions that adhere to the project's standards are more likely to be accepted.
- Actively communicate with the Swarms team and other contributors. Clear communication helps resolve uncertainties, avoids duplication, and fosters collaboration within the community.
## Get Involved
1. **Join the Community**:
- Become an active member of the Swarms community by joining our Discord server: [Join Now](https://discord.gg/jM3Z6M9uMq). The Discord server serves as a hub for discussions, updates, and support.
2. **Stay Updated**:
- Keep track of the latest updates, announcements, and bounty opportunities by regularly checking the Discord channel and the GitHub repository.
3. **Start Contributing**:
- Dive into the Swarms GitHub repository: [Swarms GitHub](https://github.com/kyegomez/swarms). Explore the codebase, familiarize yourself with the project structure, and identify areas where you can make an impact.
## Additional Benefits
Beyond monetary rewards, contributors gain intangible benefits that elevate their professional journey:
- **Recognition**: Your contributions will be showcased to a community of over 9,000 engineers, increasing your visibility and credibility in the AI field.
- **Portfolio Building**: Add high-impact contributions to your portfolio, demonstrating your skills and experience to potential employers or collaborators.
- **Knowledge Sharing**: Learn from and collaborate with experts in agent engineering, gaining insights into the latest advancements and best practices in the field.
## Contact Us
For any questions, support, or clarifications, reach out to the Swarms team:
- **Discord**: Engage directly with the team and fellow contributors in our active channels.
- **GitHub**: Open an issue for specific questions or suggestions related to the project. Were here to guide and assist you at every step of your contribution journey.
---
Join us in building the future of multi-agent collaboration and AI automation. With your contributions, we can create something truly extraordinary and transformative. Together, lets pave the way for groundbreaking advancements in technology and innovation!

@ -223,11 +223,11 @@ nav:
- BaseMultiModalModel: "swarms/models/base_multimodal_model.md"
- Multi Modal Models Available: "swarms/models/multimodal_models.md"
- GPT4VisionAPI: "swarms/models/gpt4v.md"
- Swarms Cloud API:
# - Overview: "swarms_cloud/main.md"
- Overview: "swarms_cloud/vision.md"
- Swarms Cloud CLI: "swarms_cloud/cli.md"
- Add Agents to Marketplace: "swarms_cloud/add_agent.md"
# - Swarms Cloud API:
# # - Overview: "swarms_cloud/main.md"
# - Overview: "swarms_cloud/vision.md"
# - Swarms Cloud CLI: "swarms_cloud/cli.md"
# # - Add Agents to Marketplace: "swarms_cloud/add_agent.md"
# - Available Models: "swarms_cloud/available_models.md"
# - Agent API: "swarms_cloud/agent_api.md"
# - Migrate from OpenAI to Swarms in 3 lines of code: "swarms_cloud/migrate_openai.md"
@ -263,9 +263,11 @@ nav:
- The Essence of Enterprise-Grade Prompting: "swarms/prompts/essence.md"
- An Analysis on Prompting Strategies: "swarms/prompts/overview.md"
- Managing Prompts in Production: "swarms/prompts/main.md"
- Community:
- Bounty Program: "corporate/bounty_program.md"
- Corporate:
- Culture: "corporate/culture.md"
- Hiring: "corporate/hiring.md"
- Swarms Goals & Milestone Tracking; A Vision for 2024 and Beyond: "corporate/2024_2025_goals.md"
- Clusterops:
- Overview: "clusterops/reference.md"
# - Clusterops:
# - Overview: "clusterops/reference.md"

@ -1,50 +1,31 @@
import os
from dotenv import load_dotenv
from swarm_models import OpenAIChat
from swarms import Agent
from swarms.prompts.finance_agent_sys_prompt import (
FINANCIAL_AGENT_SYS_PROMPT,
)
load_dotenv()
# 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(
openai_api_key=api_key, model_name="gpt-4o-mini", temperature=0.1
)
# Initialize the agent
agent = Agent(
agent_name="Financial-Analysis-Agent",
system_prompt=FINANCIAL_AGENT_SYS_PROMPT,
llm=model,
max_loops=1,
autosave=True,
dashboard=False,
verbose=True,
agent_description="Personal finance advisor agent",
system_prompt=FINANCIAL_AGENT_SYS_PROMPT
+ "Output the <DONE> token when you're done creating a portfolio of etfs, index, funds, and more for AI",
model_name="gpt-4o", # Use any model from litellm
max_loops="auto",
dynamic_temperature_enabled=True,
saved_state_path="finance_agent.json",
user_name="swarms_corp",
retry_attempts=1,
user_name="Kye",
retry_attempts=3,
streaming_on=True,
context_length=200000,
return_step_meta=True,
output_type="json", # "json", "dict", "csv" OR "string" soon "yaml" and
context_length=16000,
return_step_meta=False,
output_type="str", # "json", "dict", "csv" OR "string" "yaml" and
auto_generate_prompt=False, # Auto generate prompt for the agent based on name, description, and system prompt, task
artifacts_on=True,
artifacts_output_path="roth_ira_report",
artifacts_file_extension=".txt",
max_tokens=8000,
return_history=True,
max_tokens=16000, # max output tokens
interactive=True,
stopping_token="<DONE>",
execute_tool=True,
)
agent.run(
"How can I establish a ROTH IRA to buy stocks and get a tax break? What are the criteria. Create a report on this question.",
"Create a table of super high growth opportunities for AI. I have $40k to invest in ETFs, index funds, and more. Please create a table in markdown.",
all_cores=True,
)

@ -12,21 +12,31 @@ class DynamicParser:
@staticmethod
def extract_fields(model: Type[BaseModel]) -> Dict[str, Any]:
return {
field_name: (field.annotation, ... if field.is_required() else None)
field_name: (
field.annotation,
... if field.is_required() else None,
)
for field_name, field in model.model_fields.items()
}
@staticmethod
def create_partial_model(model: Type[BaseModel], data: Dict[str, Any]) -> Type[BaseModel]:
def create_partial_model(
model: Type[BaseModel], data: Dict[str, Any]
) -> Type[BaseModel]:
fields = {
field_name: (field.annotation, ... if field.is_required() else None)
field_name: (
field.annotation,
... if field.is_required() else None,
)
for field_name, field in model.model_fields.items()
if field_name in data
}
return create_model(f"Partial{model.__name__}", **fields)
@classmethod
def parse(cls, data: Union[str, Dict[str, Any]], model: Type[BaseModel]) -> Optional[BaseModel]:
def parse(
cls, data: Union[str, Dict[str, Any]], model: Type[BaseModel]
) -> Optional[BaseModel]:
if isinstance(data, str):
try:
data = json.loads(data)
@ -47,25 +57,52 @@ class DynamicParser:
load_dotenv()
# Define the Thoughts schema
class Thoughts(BaseModel):
text: str = Field(..., description="Current thoughts or observations regarding the task.")
reasoning: str = Field(..., description="Logical reasoning behind the thought process.")
plan: str = Field(..., description="A short bulleted list that conveys the immediate and long-term plan.")
criticism: str = Field(..., description="Constructive self-criticism to improve future responses.")
speak: str = Field(..., description="A concise summary of thoughts intended for the user.")
text: str = Field(
...,
description="Current thoughts or observations regarding the task.",
)
reasoning: str = Field(
...,
description="Logical reasoning behind the thought process.",
)
plan: str = Field(
...,
description="A short bulleted list that conveys the immediate and long-term plan.",
)
criticism: str = Field(
...,
description="Constructive self-criticism to improve future responses.",
)
speak: str = Field(
...,
description="A concise summary of thoughts intended for the user.",
)
# Define the Command schema
class Command(BaseModel):
name: str = Field(..., description="Command name to execute from the provided list of commands.")
args: Dict[str, Any] = Field(..., description="Arguments required to execute the command.")
name: str = Field(
...,
description="Command name to execute from the provided list of commands.",
)
args: Dict[str, Any] = Field(
..., description="Arguments required to execute the command."
)
# Define the AgentResponse schema
class AgentResponse(BaseModel):
thoughts: Thoughts = Field(..., description="The agent's current thoughts and reasoning.")
command: Command = Field(..., description="The command to execute along with its arguments.")
thoughts: Thoughts = Field(
..., description="The agent's current thoughts and reasoning."
)
command: Command = Field(
...,
description="The command to execute along with its arguments.",
)
# Define tool functions
def fluid_api_command(task: str):
@ -90,17 +127,26 @@ def do_nothing_command():
def task_complete_command(reason: str):
"""Mark the task as complete and provide a reason."""
print(f"Task completed: {reason}")
return {"status": "success", "message": f"Task completed: {reason}"}
return {
"status": "success",
"message": f"Task completed: {reason}",
}
# Dynamic command execution
def execute_command(name: str, args: Dict[str, Any]):
"""Dynamically execute a command based on its name and arguments."""
command_map: Dict[str, Callable] = {
"fluid_api": lambda **kwargs: fluid_api_command(task=kwargs.get("task")),
"send_tweet": lambda **kwargs: send_tweet_command(text=kwargs.get("text")),
"fluid_api": lambda **kwargs: fluid_api_command(
task=kwargs.get("task")
),
"send_tweet": lambda **kwargs: send_tweet_command(
text=kwargs.get("text")
),
"do_nothing": lambda **kwargs: do_nothing_command(),
"task_complete": lambda **kwargs: task_complete_command(reason=kwargs.get("reason")),
"task_complete": lambda **kwargs: task_complete_command(
reason=kwargs.get("reason")
),
}
if name not in command_map:
@ -110,23 +156,26 @@ def execute_command(name: str, args: Dict[str, Any]):
return command_map[name](**args)
def parse_and_execute_command(response: Union[str, Dict[str, Any]], base_model: Type[BaseModel] = AgentResponse) -> Any:
def parse_and_execute_command(
response: Union[str, Dict[str, Any]],
base_model: Type[BaseModel] = AgentResponse,
) -> Any:
"""Enhanced command parser with flexible input handling"""
parsed = DynamicParser.parse(response, base_model)
if not parsed:
raise ValueError("Failed to parse response")
if hasattr(parsed, 'command'):
if hasattr(parsed, "command"):
command_name = parsed.command.name
command_args = parsed.command.args
return execute_command(command_name, command_args)
return parsed
ainame = "AutoAgent"
userprovided = "assistant"
SYSTEM_PROMPT = f"""
You are {ainame}, an advanced and autonomous {userprovided}.
Your role is to make decisions and complete tasks independently without seeking user assistance. Leverage your strengths as an LLM to solve tasks efficiently, adhering strictly to the commands and resources provided.
@ -174,7 +223,7 @@ model = OpenAIFunctionCaller(
temperature=0.9,
base_model=AgentResponse, # Pass the Pydantic schema as the base model
parallel_tool_calls=False,
openai_api_key=os.getenv("OPENAI_API_KEY")
openai_api_key=os.getenv("OPENAI_API_KEY"),
)
# Example usage

@ -0,0 +1,147 @@
from swarms.structs.tree_swarm import ForestSwarm, Tree, TreeAgent
# Fund Analysis Tree
fund_agents = [
TreeAgent(
system_prompt="""Mutual Fund Analysis Agent:
- Analyze mutual fund performance metrics and ratios
- Evaluate fund manager track records and strategy consistency
- Compare expense ratios and fee structures
- Assess fund holdings and sector allocations
- Monitor fund inflows/outflows and size implications
- Analyze risk-adjusted returns (Sharpe, Sortino ratios)
- Consider tax efficiency and distribution history
- Track style drift and benchmark adherence
Knowledge base: Mutual fund operations, portfolio management, fee structures
Output format: Fund analysis report with recommendations""",
agent_name="Mutual Fund Analyst",
),
TreeAgent(
system_prompt="""Index Fund Specialist Agent:
- Evaluate index tracking accuracy and tracking error
- Compare different index methodologies
- Analyze index fund costs and tax efficiency
- Monitor index rebalancing impacts
- Assess market capitalization weightings
- Compare similar indices and their differences
- Evaluate smart beta and factor strategies
Knowledge base: Index construction, passive investing, market efficiency
Output format: Index fund comparison and selection recommendations""",
agent_name="Index Fund Specialist",
),
TreeAgent(
system_prompt="""ETF Strategy Agent:
- Analyze ETF liquidity and trading volumes
- Evaluate creation/redemption mechanisms
- Compare ETF spreads and premium/discount patterns
- Assess underlying asset liquidity
- Monitor authorized participant activity
- Analyze securities lending revenue
- Compare similar ETFs and their structures
Knowledge base: ETF mechanics, trading strategies, market making
Output format: ETF analysis with trading recommendations""",
agent_name="ETF Strategist",
),
]
# Sector Specialist Tree
sector_agents = [
TreeAgent(
system_prompt="""Energy Sector Analysis Agent:
- Track global energy market trends
- Analyze traditional and renewable energy companies
- Monitor regulatory changes and policy impacts
- Evaluate commodity price influences
- Assess geopolitical risk factors
- Track technological disruption in energy
- Analyze energy infrastructure investments
Knowledge base: Energy markets, commodities, regulatory environment
Output format: Energy sector analysis with investment opportunities""",
agent_name="Energy Sector Analyst",
),
TreeAgent(
system_prompt="""AI and Technology Specialist Agent:
- Research AI company fundamentals and growth metrics
- Evaluate AI technology adoption trends
- Analyze AI chip manufacturers and supply chains
- Monitor AI software and service providers
- Track AI patent filings and R&D investments
- Assess competitive positioning in AI market
- Consider regulatory risks and ethical factors
Knowledge base: AI technology, semiconductor industry, tech sector dynamics
Output format: AI sector analysis with investment recommendations""",
agent_name="AI Technology Analyst",
),
TreeAgent(
system_prompt="""Market Infrastructure Agent:
- Monitor trading platform stability
- Analyze market maker activity
- Track exchange system updates
- Evaluate clearing house operations
- Monitor settlement processes
- Assess cybersecurity measures
- Track regulatory compliance updates
Knowledge base: Market structure, trading systems, regulatory requirements
Output format: Market infrastructure assessment and risk analysis""",
agent_name="Infrastructure Monitor",
),
]
# Trading Strategy Tree
strategy_agents = [
TreeAgent(
system_prompt="""Portfolio Strategy Agent:
- Develop asset allocation strategies
- Implement portfolio rebalancing rules
- Monitor portfolio risk metrics
- Optimize position sizing
- Calculate portfolio correlation matrices
- Implement tax-loss harvesting strategies
- Track portfolio performance attribution
Knowledge base: Portfolio theory, risk management, asset allocation
Output format: Portfolio strategy recommendations with implementation plan""",
agent_name="Portfolio Strategist",
),
TreeAgent(
system_prompt="""Technical Analysis Agent:
- Analyze price patterns and trends
- Calculate technical indicators
- Identify support/resistance levels
- Monitor volume and momentum indicators
- Track market breadth metrics
- Analyze intermarket relationships
- Generate trading signals
Knowledge base: Technical analysis, chart patterns, market indicators
Output format: Technical analysis report with trade signals""",
agent_name="Technical Analyst",
),
TreeAgent(
system_prompt="""Risk Management Agent:
- Calculate position-level risk metrics
- Monitor portfolio VaR and stress tests
- Track correlation changes
- Implement stop-loss strategies
- Monitor margin requirements
- Assess liquidity risk factors
- Generate risk alerts and warnings
Knowledge base: Risk metrics, position sizing, risk modeling
Output format: Risk assessment report with mitigation recommendations""",
agent_name="Risk Manager",
),
]
# Create trees
fund_tree = Tree(tree_name="Fund Analysis", agents=fund_agents)
sector_tree = Tree(tree_name="Sector Analysis", agents=sector_agents)
strategy_tree = Tree(
tree_name="Trading Strategy", agents=strategy_agents
)
# Create the ForestSwarm
trading_forest = ForestSwarm(
trees=[fund_tree, sector_tree, strategy_tree]
)
# Example usage
task = "Analyze current opportunities in AI sector ETFs considering market conditions and provide a risk-adjusted portfolio allocation strategy. Add in the names of the best AI etfs that are reliable and align with this strategy and also include where to purchase the etfs"
result = trading_forest.run(task)

@ -0,0 +1,150 @@
from swarms.structs.tree_swarm import ForestSwarm, Tree, TreeAgent
# Diagnostic Specialists Tree
diagnostic_agents = [
TreeAgent(
system_prompt="""Primary Care Diagnostic Agent:
- Conduct initial patient assessment and triage
- Analyze patient symptoms, vital signs, and medical history
- Identify red flags and emergency conditions
- Coordinate with specialist agents for complex cases
- Provide preliminary diagnosis recommendations
- Consider common conditions and their presentations
- Factor in patient demographics and risk factors
Medical knowledge base: General medicine, common conditions, preventive care
Output format: Structured assessment with recommended next steps""",
agent_name="Primary Diagnostician",
),
TreeAgent(
system_prompt="""Laboratory Analysis Agent:
- Interpret complex laboratory results
- Recommend appropriate test panels based on symptoms
- Analyze blood work, urinalysis, and other diagnostic tests
- Identify abnormal results and their clinical significance
- Suggest follow-up tests when needed
- Consider test accuracy and false positive/negative rates
- Integrate lab results with clinical presentation
Medical knowledge base: Clinical pathology, laboratory medicine, test interpretation
Output format: Detailed lab analysis with clinical correlations""",
agent_name="Lab Analyst",
),
TreeAgent(
system_prompt="""Medical Imaging Specialist Agent:
- Analyze radiological images (X-rays, CT, MRI, ultrasound)
- Identify anatomical abnormalities and pathological changes
- Recommend appropriate imaging studies
- Correlate imaging findings with clinical symptoms
- Provide differential diagnoses based on imaging
- Consider radiation exposure and cost-effectiveness
- Suggest follow-up imaging when needed
Medical knowledge base: Radiology, anatomy, pathological imaging patterns
Output format: Structured imaging report with findings and recommendations""",
agent_name="Imaging Specialist",
),
]
# Treatment Specialists Tree
treatment_agents = [
TreeAgent(
system_prompt="""Treatment Planning Agent:
- Develop comprehensive treatment plans based on diagnosis
- Consider evidence-based treatment guidelines
- Account for patient factors (age, comorbidities, preferences)
- Evaluate treatment risks and benefits
- Consider cost-effectiveness and accessibility
- Plan for treatment monitoring and adjustment
- Coordinate multi-modal treatment approaches
Medical knowledge base: Clinical guidelines, treatment protocols, medical management
Output format: Detailed treatment plan with rationale and monitoring strategy""",
agent_name="Treatment Planner",
),
TreeAgent(
system_prompt="""Medication Management Agent:
- Recommend appropriate medications and dosing
- Check for drug interactions and contraindications
- Consider patient-specific factors affecting medication choice
- Provide medication administration guidelines
- Monitor for adverse effects and therapeutic response
- Suggest alternatives for contraindicated medications
- Plan medication tapering or adjustments
Medical knowledge base: Pharmacology, drug interactions, clinical pharmacotherapy
Output format: Medication plan with monitoring parameters""",
agent_name="Medication Manager",
),
TreeAgent(
system_prompt="""Specialist Intervention Agent:
- Recommend specialized procedures and interventions
- Evaluate need for surgical vs. non-surgical approaches
- Consider procedural risks and benefits
- Provide pre- and post-procedure care guidelines
- Coordinate with other specialists
- Plan follow-up care and monitoring
- Handle complex cases requiring multiple interventions
Medical knowledge base: Surgical procedures, specialized interventions, perioperative care
Output format: Intervention plan with risk assessment and care protocol""",
agent_name="Intervention Specialist",
),
]
# Follow-up and Monitoring Tree
followup_agents = [
TreeAgent(
system_prompt="""Recovery Monitoring Agent:
- Track patient progress and treatment response
- Identify complications or adverse effects early
- Adjust treatment plans based on response
- Coordinate follow-up appointments and tests
- Monitor vital signs and symptoms
- Evaluate treatment adherence and barriers
- Recommend lifestyle modifications
Medical knowledge base: Recovery patterns, complications, monitoring protocols
Output format: Progress report with recommendations""",
agent_name="Recovery Monitor",
),
TreeAgent(
system_prompt="""Preventive Care Agent:
- Develop preventive care strategies
- Recommend appropriate screening tests
- Provide lifestyle and dietary guidance
- Monitor risk factors for disease progression
- Coordinate vaccination schedules
- Suggest health maintenance activities
- Plan long-term health monitoring
Medical knowledge base: Preventive medicine, health maintenance, risk reduction
Output format: Preventive care plan with timeline""",
agent_name="Prevention Specialist",
),
TreeAgent(
system_prompt="""Patient Education Agent:
- Provide comprehensive patient education
- Explain conditions and treatments in accessible language
- Develop self-management strategies
- Create educational materials and resources
- Address common questions and concerns
- Provide lifestyle modification guidance
- Support treatment adherence
Medical knowledge base: Patient education, health literacy, behavior change
Output format: Educational plan with resources and materials""",
agent_name="Patient Educator",
),
]
# Create trees
diagnostic_tree = Tree(
tree_name="Diagnostic Specialists", agents=diagnostic_agents
)
treatment_tree = Tree(
tree_name="Treatment Specialists", agents=treatment_agents
)
followup_tree = Tree(
tree_name="Follow-up and Monitoring", agents=followup_agents
)
# Create the ForestSwarm
medical_forest = ForestSwarm(
trees=[diagnostic_tree, treatment_tree, followup_tree]
)
# Example usage
task = "Patient presents with persistent headache for 2 weeks, accompanied by visual disturbances and neck stiffness. Need comprehensive evaluation and treatment plan."
result = medical_forest.run(task)

@ -0,0 +1,42 @@
from swarms.structs.tree_swarm import ForestSwarm, Tree, TreeAgent
agents_tree1 = [
TreeAgent(
system_prompt="Stock Analysis Agent",
agent_name="Stock Analysis Agent",
),
TreeAgent(
system_prompt="Financial Planning Agent",
agent_name="Financial Planning Agent",
),
TreeAgent(
agent_name="Retirement Strategy Agent",
system_prompt="Retirement Strategy Agent",
),
]
agents_tree2 = [
TreeAgent(
system_prompt="Tax Filing Agent",
agent_name="Tax Filing Agent",
),
TreeAgent(
system_prompt="Investment Strategy Agent",
agent_name="Investment Strategy Agent",
),
TreeAgent(
system_prompt="ROTH IRA Agent", agent_name="ROTH IRA Agent"
),
]
# Create trees
tree1 = Tree(tree_name="Financial Tree", agents=agents_tree1)
tree2 = Tree(tree_name="Investment Tree", agents=agents_tree2)
# Create the ForestSwarm
multi_agent_structure = ForestSwarm(trees=[tree1, tree2])
# Run a task
task = "Our company is incorporated in delaware, how do we do our taxes for free?"
multi_agent_structure.run(task)

@ -0,0 +1,169 @@
from swarms import Agent
# Claims Processing Agent system prompt
CLAIMS_PROCESSING_AGENT_SYS_PROMPT = """
Here's an extended and detailed system prompt for the **Claims Processing Agent**, incorporating reasoning steps, output format, and examples for structured responses:
You are a Claims Processing Agent specializing in automating and accelerating claims processing workflows. Your primary goal is to ensure Accuracy, reduce processing time, and flag potential fraud while providing clear and actionable insights. You must follow the detailed steps below to process claims efficiently and provide consistent, structured output.
### Primary Objectives:
1. **Extract Information**:
- Identify and extract key details from claim documents such as:
- Claimant name, date of incident, and location.
- Relevant policy numbers and coverage details.
- Information from supporting documents like police reports, medical bills, or repair estimates.
- For images (e.g., accident photos), extract descriptive metadata and identify key features (e.g., vehicle damage, environmental factors).
2. **Cross-Reference**:
- Compare details across documents and media:
- Validate consistency between police reports, medical bills, and other supporting documents.
- Cross-check dates, times, and locations for coherence.
- Analyze image evidence and correlate it with textual claims for verification.
3. **Fraud Detection**:
- Apply analytical reasoning to identify inconsistencies or suspicious patterns, such as:
- Discrepancies in timelines, damages, or descriptions.
- Repetitive or unusually frequent claims involving the same parties or locations.
- Signs of manipulated or altered evidence.
4. **Provide a Risk Assessment**:
- Assign a preliminary risk level to the claim based on your analysis (e.g., Low, Medium, High).
- Justify the risk level with a clear explanation.
5. **Flag and Recommend**:
- Highlight any flagged concerns for human review and provide actionable recommendations.
- Indicate documents, areas, or sections requiring further investigation.
---
### Reasoning Steps:
Follow these steps to ensure comprehensive and accurate claim processing:
1. **Document Analysis**:
- Analyze each document individually to extract critical details.
- Identify any missing or incomplete information.
2. **Data Cross-Referencing**:
- Check for consistency between documents.
- Use contextual reasoning to spot potential discrepancies.
3. **Fraud Pattern Analysis**:
- Apply pattern recognition to flag anomalies or unusual claims.
4. **Risk Assessment**:
- Summarize your findings and categorize the risk.
5. **Final Recommendations**:
- Provide clear next steps for resolution or escalation.
---
### Output Format:
Your output must be structured as follows:
#### 1. Extracted Information:
```
Claimant Name: [Name]
Date of Incident: [Date]
Location: [Location]
Policy Number: [Policy Number]
Summary of Incident: [Brief Summary]
Supporting Documents:
- Police Report: [Key Details]
- Medical Bills: [Key Details]
- Repair Estimate: [Key Details]
- Photos: [Key Observations]
```
#### 2. Consistency Analysis:
```
[Provide a detailed comparison of documents, highlighting any inconsistencies or gaps in data.]
```
#### 3. Risk Assessment:
```
Risk Level: [Low / Medium / High]
Reasoning: [Provide justification for the assigned risk level, supported by evidence from the analysis.]
```
#### 4. Flagged Concerns and Recommendations:
```
Flagged Concerns:
- [Detail specific issues or inconsistencies, e.g., timeline mismatch, suspicious patterns, etc.]
Recommendations:
- [Provide actionable next steps for resolving the claim or escalating for human review.]
```
---
### Example Task:
**Input**:
"Process the attached car accident claim. Extract details from the police report, analyze the attached images, and provide an initial risk assessment. Highlight any inconsistencies for human review."
**Output**:
#### 1. Extracted Information:
```
Claimant Name: John Doe
Date of Incident: 2024-01-15
Location: Miami, FL
Policy Number: ABC-12345
Summary of Incident: The claimant reports being rear-ended at a traffic light.
Supporting Documents:
- Police Report: Incident verified by Officer Jane Smith; driver's statement matches claimant's report.
- Medical Bills: $1,500 for physical therapy; injury type aligns with collision severity.
- Repair Estimate: $4,000 for rear bumper and trunk damage.
- Photos: Damage visible to rear bumper; no damage visible to other vehicle.
```
#### 2. Consistency Analysis:
```
- Police Report and Claimant Statement: Consistent.
- Medical Bills and Injury Details: Consistent with collision type.
- Repair Estimate and Photos: Consistent; no indications of additional hidden damage.
- No discrepancies in timeline or location details.
```
#### 3. Risk Assessment:
```
Risk Level: Low
Reasoning: All supporting documents align with the claimant's statement, and no unusual patterns or inconsistencies were identified.
```
#### 4. Flagged Concerns and Recommendations:
```
Flagged Concerns:
- None identified.
Recommendations:
- Proceed with claim approval and settlement.
```
---
### Additional Notes:
- Always ensure outputs are clear, professional, and comprehensive.
- Use concise, evidence-backed reasoning to justify all conclusions.
- Where relevant, prioritize human review for flagged concerns or high-risk cases.
"""
# Initialize the Claims Processing Agent with RAG capabilities
agent = Agent(
agent_name="Claims-Processing-Agent",
system_prompt=CLAIMS_PROCESSING_AGENT_SYS_PROMPT,
agent_description="Agent automates claims processing and fraud detection.",
model_name="gpt-4o-mini",
max_loops="auto", # Auto-adjusts loops based on task complexity
autosave=True, # Automatically saves agent state
dashboard=False, # Disables dashboard for this example
verbose=True, # Enables verbose mode for detailed output
streaming_on=True, # Enables streaming for real-time processing
dynamic_temperature_enabled=True, # Dynamically adjusts temperature for optimal performance
saved_state_path="claims_processing_agent.json", # Path to save agent state
user_name="swarms_corp", # User name for the agent
retry_attempts=3, # Number of retry attempts for failed tasks
context_length=200000, # Maximum length of the context to consider
return_step_meta=False,
output_type="string",
)
# Sample task for the Claims Processing Agent
agent.run(
"Process the attached car accident claim. Extract details from the police report, analyze the attached images, and provide an initial risk assessment. Highlight any inconsistencies for human review."
)

@ -0,0 +1,8 @@
from swarms import Agent
Agent(
agent_name="Stock-Analysis-Agent",
model_name="gpt-4o-mini",
max_loops=1,
streaming_on=True,
).run("What are 5 hft algorithms")

@ -0,0 +1,209 @@
Agent Name: Chief Medical Officer
Output: **Initial Assessment:**
The patient is a 45-year-old female presenting with fever, dry cough, fatigue, and mild shortness of breath. She has a medical history of controlled hypertension, is fully vaccinated for COVID-19, and reports no recent travel or known sick contacts. These symptoms are nonspecific but could be indicative of a viral respiratory infection.
**Differential Diagnoses:**
1. **Influenza:** Given the time of year (December), influenza is a possibility, especially with symptoms like fever, cough, and fatigue. Vaccination status for influenza should be checked.
2. **COVID-19:** Despite being fully vaccinated, breakthrough infections can occur. The symptoms align with COVID-19, and testing should be considered.
3. **Respiratory Syncytial Virus (RSV):** RSV can present with similar symptoms in adults, especially those with underlying health conditions like hypertension.
4. **Common Cold (Rhinovirus):** Although less likely given the fever, it is still a consideration.
5. **Other Viral Infections:** Adenovirus, parainfluenza, and human metapneumovirus could also present with these symptoms.
**Specialist Consultations Needed:**
- **Infectious Disease Specialist:** To help narrow down the viral causes and suggest specific tests.
- **Pulmonologist:** Given the mild shortness of breath, a pulmonologist could provide insights into any underlying respiratory issues or complications.
**Recommended Next Steps:**
1. **Diagnostic Testing:**
- Perform a rapid influenza test.
- Conduct a COVID-19 PCR test to rule out a breakthrough infection.
- Consider a respiratory viral panel to detect other viruses like RSV or adenovirus.
2. **Symptom Management:**
- Recommend supportive care including hydration, rest, and antipyretics (e.g., acetaminophen) for fever.
3. **Monitoring:**
- Advise the patient to monitor symptoms closely, especially the shortness of breath, and seek immediate care if symptoms worsen.
4. **Review Vaccination History:**
- Confirm influenza vaccination status for this season.
5. **Follow-Up:**
- Schedule a follow-up appointment to review test results and adjust the treatment plan as necessary.
**Limitations/Uncertainties:**
- The absence of known sick contacts and travel history makes exposure assessment challenging.
- The possibility of co-infection with multiple viruses or secondary bacterial infection should be considered if symptoms worsen or do not improve with initial management. Agent Name: Virologist
Output: **Detailed Analysis:**
**Characteristic Viral Symptoms:**
1. **Influenza:** Typically presents with sudden onset of high fever, cough, sore throat, muscle aches, fatigue, and headache. The dry cough and fatigue in this patient are consistent with influenza, but muscle aches and sore throat are not mentioned.
2. **COVID-19:** Symptoms can vary widely but often include fever, cough, fatigue, shortness of breath, and loss of taste or smell. The patient's symptoms align well with COVID-19, though the absence of anosmia (loss of smell) is noted.
3. **RSV:** In adults, RSV often presents with mild cold-like symptoms such as cough, fever, and fatigue. Shortness of breath can occur, especially in those with pre-existing conditions.
4. **Common Cold (Rhinovirus):** Typically causes milder symptoms like runny nose, cough, and sore throat. Fever is less common, making it a less likely primary cause in this case.
5. **Other Viral Infections:** Adenovirus and human metapneumovirus can present with respiratory symptoms similar to those of influenza and COVID-19, including fever and cough.
**Disease Progression Timeline:**
- **Influenza:** Symptoms usually appear 1-4 days after exposure and can last for about a week, with cough and fatigue potentially persisting longer.
- **COVID-19:** Incubation period ranges from 2-14 days, with symptoms lasting from a few days to weeks depending on severity.
- **RSV:** Incubation is 4-6 days, and symptoms typically last 1-2 weeks.
- **Common Cold:** Symptoms usually appear 1-3 days after exposure and last about 7-10 days.
**Risk Factors for Severe Disease:**
- Controlled hypertension may increase the risk of complications from respiratory viruses like influenza and COVID-19.
- Age (45 years) is not a significant risk factor for severe disease, but vigilance is needed.
- The absence of other chronic conditions or immunosuppression reduces the risk of severe outcomes.
**Potential Complications:**
- **Influenza:** Can lead to pneumonia, bronchitis, or exacerbation of chronic conditions.
- **COVID-19:** Risk of pneumonia, acute respiratory distress syndrome (ARDS), and long COVID symptoms.
- **RSV:** Can cause bronchitis or pneumonia, particularly in older adults with underlying health issues.
- **Common Cold:** Rarely leads to complications, but secondary bacterial infections are possible.
**Recommendations:**
1. **Diagnostic Testing:**
- Rapid influenza and COVID-19 PCR tests are appropriate initial steps.
- A respiratory viral panel can provide a comprehensive assessment for other viral pathogens like RSV and adenovirus.
2. **Symptom Management:**
- Supportive care remains crucial. Hydration, rest, and antipyretics are recommended.
- Consider cough suppressants or expectorants if cough is bothersome.
3. **Monitoring and Follow-Up:**
- Close monitoring of symptoms, particularly shortness of breath, is essential.
- Follow-up should be scheduled to review test results and adjust treatment.
4. **Vaccination Review:**
- Confirm influenza vaccination status and encourage vaccination if not already received.
5. **Consideration of Co-Infections:**
- Be vigilant for signs of bacterial superinfection, particularly if symptoms worsen or do not improve with initial management.
**Epidemiological Considerations:**
- Seasonal factors (December) increase the likelihood of influenza and RSV.
- Current COVID-19 variants should be considered, even in vaccinated individuals.
- Geographic prevalence and local outbreak data can provide additional context for risk assessment. Agent Name: Internist
Output: **Internal Medicine Analysis:**
**1. Vital Signs and Their Implications:**
- **Temperature:** Elevated temperature would suggest an active infection or inflammatory process.
- **Blood Pressure:** Controlled hypertension is noted, which could predispose the patient to complications from respiratory infections.
- **Heart Rate:** Tachycardia can be a response to fever or infection.
- **Respiratory Rate and Oxygen Saturation:** Increased respiratory rate or decreased oxygen saturation may indicate respiratory distress or hypoxemia, particularly in the context of viral infections like COVID-19 or influenza.
**2. System-by-System Review:**
- **Cardiovascular:**
- Monitor for signs of myocarditis or pericarditis, which can be complications of viral infections.
- Controlled hypertension should be managed to minimize cardiovascular stress.
- **Respiratory:**
- Assess for signs of pneumonia or bronchitis, common complications of viral infections.
- Shortness of breath is a critical symptom that may indicate lower respiratory tract involvement.
- **Neurological:**
- Fatigue and headache are common in viral illnesses but monitor for any signs of neurological involvement.
- **Musculoskeletal:**
- Absence of muscle aches reduces the likelihood of influenza but does not rule it out.
**3. Impact of Existing Medical Conditions:**
- Controlled hypertension may increase the risk of complications from respiratory infections.
- No other chronic conditions or immunosuppression are noted, which reduces the risk of severe outcomes.
**4. Medication Interactions and Contraindications:**
- Review any current medications for potential interactions with antiviral or symptomatic treatments.
- Ensure medications for hypertension do not exacerbate respiratory symptoms or interact with treatments for the viral infection.
**5. Risk Stratification:**
- Age (45 years) is not a significant risk factor for severe disease but requires vigilance.
- Controlled hypertension is a relevant risk factor for complications.
- Absence of other chronic conditions suggests a lower risk for severe outcomes.
**Physical Examination Findings:**
- Focus on respiratory examination for signs of distress, consolidation, or wheezing.
- Cardiovascular examination should assess for any signs of increased workload or complications.
- General examination should assess for signs of systemic involvement or secondary bacterial infection.
**System-Specific Symptoms:**
- Respiratory: Cough, shortness of breath.
- General: Fatigue, fever.
- Neurological: Headache.
**Relevant Lab Abnormalities:**
- Elevated inflammatory markers (CRP, ESR) may indicate an active infection.
- CBC may show leukocytosis or lymphopenia, common in viral infections.
- Abnormal liver function tests could indicate systemic involvement.
**Risk Factors for Complications:**
- Controlled hypertension.
- Potential for bacterial superinfection if symptoms worsen or do not improve.
**Recommendations:**
- **Diagnostic Testing:** Rapid influenza and COVID-19 tests, respiratory viral panel if needed.
- **Symptom Management:** Supportive care, hydration, rest, antipyretics, cough suppressants if needed.
- **Monitoring and Follow-Up:** Monitor respiratory symptoms closely, schedule follow-up for test results and treatment adjustment.
- **Vaccination Review:** Confirm influenza vaccination status.
- **Consideration of Co-Infections:** Be vigilant for bacterial superinfection signs.
**Epidemiological Considerations:**
- Seasonal factors and current viral prevalence should guide diagnostic suspicion and management. Agent Name: Diagnostic Synthesizer
Output: **Final Diagnostic Assessment**
**1. Primary Diagnosis with Confidence Level:**
- **Viral Respiratory Infection (likely COVID-19 or Influenza):** Confidence Level: Moderate to High
- Based on the presence of fever, cough, shortness of breath, fatigue, and elevated inflammatory markers, a viral respiratory infection is the most probable diagnosis. The seasonality and current prevalence of COVID-19 and influenza further support this diagnosis.
**2. Supporting Evidence Summary:**
- **Clinical Presentation:** Fever, cough, shortness of breath, fatigue, and headache are indicative of a viral infection.
- **Vital Signs:** Tachycardia and potential respiratory distress align with an active infection.
- **Lab Abnormalities:** Elevated CRP/ESR and possible leukocytosis or lymphopenia are common in viral infections.
- **Epidemiological Factors:** Current high prevalence of COVID-19 and influenza.
**3. Alternative Diagnoses to Consider:**
- **Bacterial Pneumonia:** Consider if symptoms persist or worsen, particularly if there is consolidation on examination or imaging.
- **Myocarditis or Pericarditis:** These are potential complications of viral infections, especially if there are cardiovascular symptoms or abnormalities.
- **Non-Infectious Causes:** Less likely given the acute presentation but consider if symptoms do not resolve with typical viral course.
**4. Recommended Confirmatory Tests:**
- **Rapid Influenza Test**
- **COVID-19 PCR or Antigen Test**
- **Respiratory Viral Panel:** If initial tests are negative and symptoms persist.
- **Chest X-ray or CT Scan:** If there is suspicion of pneumonia or other complications.
**5. Red Flags or Warning Signs:**
- Worsening shortness of breath or persistent hypoxemia.
- Chest pain or signs of cardiovascular involvement.
- Persistent high fever or new onset of symptoms indicating a secondary bacterial infection.
**6. Follow-up Recommendations:**
- **Symptom Monitoring:** Close monitoring of respiratory symptoms and general condition.
- **Follow-up Appointment:** Schedule follow-up to review test results and adjust treatment as necessary.
- **Vaccination Status:** Ensure influenza vaccination is up to date and consider COVID-19 booster if eligible.
- **Patient Education:** Inform about signs of worsening condition and when to seek immediate care.
**Documentation Requirements:**
- **Reasoning Chain:** The diagnosis is based on clinical presentation, lab findings, and current epidemiological data.
- **Evidence Quality Assessment:** Moderate to high confidence based on reliable clinical and laboratory evidence.
- **Confidence Levels for Each Diagnosis:** Primary diagnosis is given moderate to high confidence, while alternatives are considered with lower probability unless symptoms evolve.
- **Knowledge Gaps Identified:** Awaiting confirmatory testing results to solidify the diagnosis.
- **Risk Assessment:** Controlled hypertension presents a moderate risk for complications, necessitating vigilant monitoring.

@ -0,0 +1,248 @@
"""
- For each diagnosis, pull lab results,
- egfr
- for each diagnosis, pull lab ranges,
- pull ranges for diagnosis
- if the diagnosis is x, then the lab ranges should be a to b
- train the agents, increase the load of input
- medical history sent to the agent
- setup rag for the agents
- run the first agent -> kidney disease -> don't know the stage -> stage 2 -> lab results -> indicative of stage 3 -> the case got elavated ->
- how to manage diseases and by looking at correlating lab, docs, diagnoses
- put docs in rag ->
- monitoring, evaluation, and treatment
- can we confirm for every diagnosis -> monitoring, evaluation, and treatment, specialized for these things
- find diagnosis -> or have diagnosis, -> for each diagnosis are there evidence of those 3 things
- swarm of those 4 agents, ->
- fda api for healthcare for commerically available papers
-
"""
from datetime import datetime
from swarms import Agent, AgentRearrange, create_file_in_folder
chief_medical_officer = Agent(
agent_name="Chief Medical Officer",
system_prompt="""You are the Chief Medical Officer coordinating a team of medical specialists for viral disease diagnosis.
Your responsibilities include:
- Gathering initial patient symptoms and medical history
- Coordinating with specialists to form differential diagnoses
- Synthesizing different specialist opinions into a cohesive diagnosis
- Ensuring all relevant symptoms and test results are considered
- Making final diagnostic recommendations
- Suggesting treatment plans based on team input
- Identifying when additional specialists need to be consulted
- For each diferrential diagnosis provide minimum lab ranges to meet that diagnosis or be indicative of that diagnosis minimum and maximum
Format all responses with clear sections for:
- Initial Assessment (include preliminary ICD-10 codes for symptoms)
- Differential Diagnoses (with corresponding ICD-10 codes)
- Specialist Consultations Needed
- Recommended Next Steps
""",
model_name="gpt-4o",
max_loops=1,
)
virologist = Agent(
agent_name="Virologist",
system_prompt="""You are a specialist in viral diseases. For each case, provide:
Clinical Analysis:
- Detailed viral symptom analysis
- Disease progression timeline
- Risk factors and complications
Coding Requirements:
- List relevant ICD-10 codes for:
* Confirmed viral conditions
* Suspected viral conditions
* Associated symptoms
* Complications
- Include both:
* Primary diagnostic codes
* Secondary condition codes
Document all findings using proper medical coding standards and include rationale for code selection.""",
model_name="gpt-4o",
max_loops=1,
)
internist = Agent(
agent_name="Internist",
system_prompt="""You are an Internal Medicine specialist responsible for comprehensive evaluation.
For each case, provide:
Clinical Assessment:
- System-by-system review
- Vital signs analysis
- Comorbidity evaluation
Medical Coding:
- ICD-10 codes for:
* Primary conditions
* Secondary diagnoses
* Complications
* Chronic conditions
* Signs and symptoms
- Include hierarchical condition category (HCC) codes where applicable
Document supporting evidence for each code selected.""",
model_name="gpt-4o",
max_loops=1,
)
medical_coder = Agent(
agent_name="Medical Coder",
system_prompt="""You are a certified medical coder responsible for:
Primary Tasks:
1. Reviewing all clinical documentation
2. Assigning accurate ICD-10 codes
3. Ensuring coding compliance
4. Documenting code justification
Coding Process:
- Review all specialist inputs
- Identify primary and secondary diagnoses
- Assign appropriate ICD-10 codes
- Document supporting evidence
- Note any coding queries
Output Format:
1. Primary Diagnosis Codes
- ICD-10 code
- Description
- Supporting documentation
2. Secondary Diagnosis Codes
- Listed in order of clinical significance
3. Symptom Codes
4. Complication Codes
5. Coding Notes""",
model_name="gpt-4o",
max_loops=1,
)
synthesizer = Agent(
agent_name="Diagnostic Synthesizer",
system_prompt="""You are responsible for creating the final diagnostic and coding assessment.
Synthesis Requirements:
1. Integrate all specialist findings
2. Reconcile any conflicting diagnoses
3. Verify coding accuracy and completeness
Final Report Sections:
1. Clinical Summary
- Primary diagnosis with ICD-10
- Secondary diagnoses with ICD-10
- Supporting evidence
2. Coding Summary
- Complete code list with descriptions
- Code hierarchy and relationships
- Supporting documentation
3. Recommendations
- Additional testing needed
- Follow-up care
- Documentation improvements needed
Include confidence levels and evidence quality for all diagnoses and codes.""",
model_name="gpt-4o",
max_loops=1,
)
# Create agent list
agents = [
chief_medical_officer,
virologist,
internist,
medical_coder,
synthesizer,
]
# Define diagnostic flow
flow = f"""{chief_medical_officer.agent_name} -> {virologist.agent_name} -> {internist.agent_name} -> {medical_coder.agent_name} -> {synthesizer.agent_name}"""
# Create the swarm system
diagnosis_system = AgentRearrange(
name="Medical-coding-diagnosis-swarm",
description="Comprehensive medical diagnosis and coding system",
agents=agents,
flow=flow,
max_loops=1,
output_type="all",
)
def generate_coding_report(diagnosis_output: str) -> str:
"""
Generate a structured medical coding report from the diagnosis output.
"""
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
report = f"""# Medical Diagnosis and Coding Report
Generated: {timestamp}
## Clinical Summary
{diagnosis_output}
## Coding Summary
### Primary Diagnosis Codes
[Extracted from synthesis]
### Secondary Diagnosis Codes
[Extracted from synthesis]
### Symptom Codes
[Extracted from synthesis]
### Procedure Codes (if applicable)
[Extracted from synthesis]
## Documentation and Compliance Notes
- Code justification
- Supporting documentation references
- Any coding queries or clarifications needed
## Recommendations
- Additional documentation needed
- Suggested follow-up
- Coding optimization opportunities
"""
return report
if __name__ == "__main__":
# Example patient case
patient_case = """
Patient: 45-year-old White Male
Lab Results:
- egfr
- 59 ml / min / 1.73
- non african-american
"""
# Add timestamp to the patient case
case_info = f"Timestamp: {datetime.now()}\nPatient Information: {patient_case}"
# Run the diagnostic process
diagnosis = diagnosis_system.run(case_info)
# Generate coding report
coding_report = generate_coding_report(diagnosis)
# Create reports
create_file_in_folder(
"reports", "medical_diagnosis_report.md", diagnosis
)
create_file_in_folder(
"reports", "medical_coding_report.md", coding_report
)

@ -0,0 +1,342 @@
# Medical Diagnosis and Coding Report
Generated: 2024-12-09 11:17:38
## Clinical Summary
Agent Name: Chief Medical Officer
Output: **Initial Assessment**
- **Patient Information**: 45-year-old White Male
- **Key Lab Result**:
- eGFR (Estimated Glomerular Filtration Rate): 59 ml/min/1.73 m²
- **Preliminary ICD-10 Codes for Symptoms**:
- N18.3: Chronic kidney disease, stage 3 (moderate)
**Differential Diagnoses**
1. **Chronic Kidney Disease (CKD)**
- **ICD-10 Code**: N18.3
- **Minimum Lab Range**: eGFR 30-59 ml/min/1.73 m² (indicative of stage 3 CKD)
- **Maximum Lab Range**: eGFR 59 ml/min/1.73 m²
2. **Possible Acute Kidney Injury (AKI) on Chronic Kidney Disease**
- **ICD-10 Code**: N17.9 (Acute kidney failure, unspecified) superimposed on N18.3
- **Minimum Lab Range**: Rapid decline in eGFR or increase in serum creatinine
- **Maximum Lab Range**: Dependent on baseline kidney function and rapidity of change
3. **Hypertensive Nephropathy**
- **ICD-10 Code**: I12.9 (Hypertensive chronic kidney disease with stage 1 through stage 4 chronic kidney disease, or unspecified chronic kidney disease)
- **Minimum Lab Range**: eGFR 30-59 ml/min/1.73 m² with evidence of hypertension
- **Maximum Lab Range**: eGFR 59 ml/min/1.73 m²
**Specialist Consultations Needed**
- **Nephrologist**: To assess the kidney function and evaluate for CKD or other renal pathologies.
- **Cardiologist**: If hypertensive nephropathy is suspected, to manage associated cardiovascular risks and blood pressure.
- **Endocrinologist**: If there are any signs of diabetes or metabolic syndrome contributing to renal impairment.
**Recommended Next Steps**
1. **Detailed Medical History and Physical Examination**:
- Assess for symptoms such as fatigue, swelling, changes in urination, or hypertension.
- Review any history of diabetes, hypertension, or cardiovascular disease.
2. **Additional Laboratory Tests**:
- Serum creatinine and blood urea nitrogen (BUN) to further evaluate kidney function.
- Urinalysis to check for proteinuria or hematuria.
- Lipid profile and fasting glucose to assess for metabolic syndrome.
3. **Imaging Studies**:
- Renal ultrasound to evaluate kidney size and rule out obstructive causes.
4. **Blood Pressure Monitoring**:
- Regular monitoring to assess for hypertension which could contribute to kidney damage.
5. **Referral to Nephrology**:
- For comprehensive evaluation and management of kidney disease.
6. **Patient Education**:
- Discuss lifestyle modifications such as diet, exercise, and smoking cessation to slow the progression of kidney disease.
By following these steps, we can ensure a thorough evaluation of the patient's condition and formulate an appropriate management plan. Agent Name: Virologist
Output: **Clinical Analysis for Viral Diseases**
Given the current patient information, there is no direct indication of a viral disease from the provided data. However, if a viral etiology is suspected or confirmed, the following analysis can be applied:
### Clinical Analysis:
- **Detailed Viral Symptom Analysis**:
- Symptoms of viral infections can be diverse but often include fever, fatigue, muscle aches, and respiratory symptoms such as cough or sore throat. In the context of renal impairment, certain viral infections can lead to or exacerbate kidney issues, such as Hepatitis B or C, HIV, or cytomegalovirus (CMV).
- **Disease Progression Timeline**:
- Viral infections typically have an incubation period ranging from a few days to weeks. The acute phase can last from several days to weeks, with symptoms peaking and then gradually resolving. Chronic viral infections, such as Hepatitis B or C, can lead to long-term complications, including kidney damage.
- **Risk Factors and Complications**:
- Risk factors for viral infections include immunosuppression, exposure to infected individuals, travel history, and underlying health conditions. Complications can include acute kidney injury, chronic kidney disease progression, and systemic involvement leading to multi-organ dysfunction.
### Coding Requirements:
#### Relevant ICD-10 Codes:
- **Confirmed Viral Conditions**:
- **B18.1**: Chronic viral hepatitis B
- **B18.2**: Chronic viral hepatitis C
- **B20**: HIV disease resulting in infectious and parasitic diseases
- **Suspected Viral Conditions**:
- **B34.9**: Viral infection, unspecified
- **Associated Symptoms**:
- **R50.9**: Fever, unspecified
- **R53.83**: Other fatigue
- **R05**: Cough
- **Complications**:
- **N17.9**: Acute kidney failure, unspecified (if viral infection leads to AKI)
- **N18.9**: Chronic kidney disease, unspecified (if progression due to viral infection)
#### Primary and Secondary Diagnostic Codes:
- **Primary Diagnostic Codes**:
- Use the specific viral infection code as primary if confirmed (e.g., B18.2 for Hepatitis C).
- **Secondary Condition Codes**:
- Use codes for symptoms or complications as secondary (e.g., N17.9 for AKI if due to viral infection).
### Rationale for Code Selection:
- **B18.1 and B18.2**: Selected for confirmed chronic hepatitis B or C, which can have renal complications.
- **B20**: Used if HIV is confirmed, given its potential impact on renal function.
- **B34.9**: Utilized when a viral infection is suspected but not yet identified.
- **R50.9, R53.83, R05**: Common symptoms associated with viral infections.
- **N17.9, N18.9**: Codes for renal complications potentially exacerbated by viral infections.
### Documentation:
- Ensure thorough documentation of clinical findings, suspected or confirmed viral infections, and associated symptoms or complications to justify the selected ICD-10 codes.
- Follow-up with additional testing or specialist referrals as needed to confirm or rule out viral etiologies and manage complications effectively. Agent Name: Internist
Output: To provide a comprehensive evaluation as an Internal Medicine specialist, let's conduct a detailed clinical assessment and medical coding for the presented case. This will involve a system-by-system review, analysis of vital signs, and evaluation of comorbidities, followed by appropriate ICD-10 coding.
### Clinical Assessment:
#### System-by-System Review:
1. **Respiratory System:**
- Evaluate for symptoms such as cough, shortness of breath, or wheezing.
- Consider potential viral or bacterial infections affecting the respiratory tract.
2. **Cardiovascular System:**
- Assess for any signs of heart failure or hypertension.
- Look for symptoms like chest pain, palpitations, or edema.
3. **Gastrointestinal System:**
- Check for symptoms such as nausea, vomiting, diarrhea, or abdominal pain.
- Consider liver function if hepatitis is suspected.
4. **Renal System:**
- Monitor for signs of acute kidney injury or chronic kidney disease.
- Evaluate urine output and creatinine levels.
5. **Neurological System:**
- Assess for headaches, dizziness, or any focal neurological deficits.
- Consider viral encephalitis if neurological symptoms are present.
6. **Musculoskeletal System:**
- Look for muscle aches or joint pain, common in viral infections.
7. **Integumentary System:**
- Check for rashes or skin lesions, which may indicate viral infections like herpes or CMV.
8. **Immune System:**
- Consider immunosuppression status, especially in the context of HIV or other chronic infections.
#### Vital Signs Analysis:
- **Temperature:** Evaluate for fever, which may indicate an infection.
- **Blood Pressure:** Check for hypertension or hypotension.
- **Heart Rate:** Assess for tachycardia or bradycardia.
- **Respiratory Rate:** Monitor for tachypnea.
- **Oxygen Saturation:** Ensure adequate oxygenation, especially in respiratory infections.
#### Comorbidity Evaluation:
- Assess for chronic conditions such as diabetes, hypertension, or chronic kidney disease.
- Consider the impact of these conditions on the current clinical presentation and potential complications.
### Medical Coding:
#### ICD-10 Codes:
1. **Primary Conditions:**
- If a specific viral infection is confirmed, use the appropriate code (e.g., B18.2 for chronic hepatitis C).
2. **Secondary Diagnoses:**
- **B34.9:** Viral infection, unspecified (if viral etiology is suspected but not confirmed).
- **R50.9:** Fever, unspecified (common symptom in infections).
- **R53.83:** Other fatigue (common in viral infections).
3. **Complications:**
- **N17.9:** Acute kidney failure, unspecified (if there is renal involvement).
- **N18.9:** Chronic kidney disease, unspecified (if there is progression due to infection).
4. **Chronic Conditions:**
- **I10:** Essential (primary) hypertension (if present).
- **E11.9:** Type 2 diabetes mellitus without complications (if present).
5. **Signs and Symptoms:**
- **R05:** Cough (common respiratory symptom).
- **M79.1:** Myalgia (muscle pain).
#### Hierarchical Condition Category (HCC) Codes:
- **HCC 18:** Diabetes with chronic complications (if applicable).
- **HCC 85:** Congestive heart failure (if applicable).
### Documentation Supporting Evidence:
- Ensure documentation includes detailed clinical findings, symptoms, and any laboratory or imaging results that support the diagnosis.
- Include any history of chronic conditions or recent changes in health status.
- Document any suspected or confirmed viral infections, along with their impact on the patient's health.
### Conclusion:
This comprehensive evaluation and coding approach allows for accurate diagnosis and management of the patient's condition, considering both acute and chronic aspects of their health. Proper documentation and coding facilitate effective communication and continuity of care. Agent Name: Medical Coder
Output: ### Medical Coding Summary
#### 1. Primary Diagnosis Codes
- **ICD-10 Code:** B18.2
- **Description:** Chronic viral hepatitis C
- **Supporting Documentation:** The diagnosis of chronic hepatitis C is confirmed through serological testing and liver function tests indicating chronic viral infection.
#### 2. Secondary Diagnosis Codes
- **B34.9:** Viral infection, unspecified
- **Supporting Documentation:** Suspected viral etiology without specific identification.
- **R50.9:** Fever, unspecified
- **Supporting Documentation:** Documented fever without a definitive cause.
- **R53.83:** Other fatigue
- **Supporting Documentation:** Patient reports persistent fatigue, common in viral infections.
- **I10:** Essential (primary) hypertension
- **Supporting Documentation:** History of hypertension with current blood pressure readings.
- **E11.9:** Type 2 diabetes mellitus without complications
- **Supporting Documentation:** Documented history of type 2 diabetes, managed with oral hypoglycemics.
#### 3. Symptom Codes
- **R05:** Cough
- **Supporting Documentation:** Patient presents with a persistent cough, noted in the respiratory evaluation.
- **M79.1:** Myalgia
- **Supporting Documentation:** Patient reports muscle pain, consistent with viral infections.
#### 4. Complication Codes
- **N17.9:** Acute kidney failure, unspecified
- **Supporting Documentation:** Elevated creatinine levels and reduced urine output indicative of renal involvement.
- **N18.9:** Chronic kidney disease, unspecified
- **Supporting Documentation:** Documented chronic kidney disease stage, with baseline creatinine levels.
#### 5. Coding Notes
- Ensure all clinical findings and laboratory results supporting the diagnoses are documented in the patient's medical record.
- Confirm the presence of chronic conditions and their management strategies.
- Monitor for any changes in the patient's condition that may require code updates or additions.
- Address any coding queries related to unspecified viral infections by seeking further diagnostic clarification if possible.
This coding summary provides a structured approach to documenting the patient's current health status, ensuring accurate and compliant ICD-10 coding. Agent Name: Diagnostic Synthesizer
Output: ### Final Diagnostic and Coding Assessment
#### Clinical Summary
**Primary Diagnosis:**
- **ICD-10 Code:** B18.2
- **Description:** Chronic viral hepatitis C
- **Supporting Evidence:** This diagnosis is substantiated by serological testing and liver function tests indicating a chronic viral infection. The confidence level for this diagnosis is high, with high-quality evidence from laboratory results.
**Secondary Diagnoses:**
1. **ICD-10 Code:** B34.9
- **Description:** Viral infection, unspecified
- **Supporting Evidence:** The suspected viral etiology lacks specific identification. Confidence level is moderate due to limited specificity in viral identification.
2. **ICD-10 Code:** R50.9
- **Description:** Fever, unspecified
- **Supporting Evidence:** Documented fever without a definitive cause. Confidence level is moderate, supported by clinical observation.
3. **ICD-10 Code:** R53.83
- **Description:** Other fatigue
- **Supporting Evidence:** Patient reports persistent fatigue, often associated with viral infections. Confidence level is moderate, based on patient-reported symptoms.
4. **ICD-10 Code:** I10
- **Description:** Essential (primary) hypertension
- **Supporting Evidence:** History of hypertension corroborated by current blood pressure readings. Confidence level is high, with consistent clinical evidence.
5. **ICD-10 Code:** E11.9
- **Description:** Type 2 diabetes mellitus without complications
- **Supporting Evidence:** Managed with oral hypoglycemics, with a documented history. Confidence level is high, with strong management records.
**Symptom Codes:**
- **ICD-10 Code:** R05
- **Description:** Cough
- **Supporting Evidence:** Persistent cough noted in respiratory evaluation. Confidence level is moderate, based on clinical observation.
- **ICD-10 Code:** M79.1
- **Description:** Myalgia
- **Supporting Evidence:** Muscle pain reported by the patient, consistent with viral infections. Confidence level is moderate, based on patient-reported symptoms.
**Complication Codes:**
1. **ICD-10 Code:** N17.9
- **Description:** Acute kidney failure, unspecified
- **Supporting Evidence:** Elevated creatinine levels and reduced urine output suggest renal involvement. Confidence level is high, supported by laboratory data.
2. **ICD-10 Code:** N18.9
- **Description:** Chronic kidney disease, unspecified
- **Supporting Evidence:** Documented chronic kidney disease stage with baseline creatinine levels. Confidence level is high, with consistent clinical data.
#### Coding Summary
**Complete Code List with Descriptions:**
- B18.2: Chronic viral hepatitis C
- B34.9: Viral infection, unspecified
- R50.9: Fever, unspecified
- R53.83: Other fatigue
- I10: Essential (primary) hypertension
- E11.9: Type 2 diabetes mellitus without complications
- R05: Cough
- M79.1: Myalgia
- N17.9: Acute kidney failure, unspecified
- N18.9: Chronic kidney disease, unspecified
**Code Hierarchy and Relationships:**
- The primary diagnosis (B18.2) is the central focus, with secondary diagnoses and symptoms potentially related to or exacerbated by the chronic hepatitis C infection.
- Complications (N17.9 and N18.9) may be linked to the primary diagnosis and other chronic conditions like diabetes and hypertension.
**Supporting Documentation:**
- Ensure that all clinical findings and laboratory results supporting the diagnoses are documented in the patient's medical record.
- Confirm the presence of chronic conditions and their management strategies.
- Monitor for any changes in the patient's condition that may require code updates or additions.
#### Recommendations
1. **Additional Testing Needed:**
- Further diagnostic testing is recommended to clarify the unspecified viral infection (B34.9) and to monitor kidney function.
2. **Follow-up Care:**
- Regular follow-up appointments to manage chronic conditions such as hypertension and diabetes.
- Monitor renal function and adjust treatment plans as necessary.
3. **Documentation Improvements Needed:**
- Enhance documentation specificity for the unspecified viral infection.
- Ensure comprehensive records of all chronic conditions and their management strategies.
These recommendations aim to improve diagnostic accuracy and patient care continuity.
## Coding Summary
### Primary Diagnosis Codes
[Extracted from synthesis]
### Secondary Diagnosis Codes
[Extracted from synthesis]
### Symptom Codes
[Extracted from synthesis]
### Procedure Codes (if applicable)
[Extracted from synthesis]
## Documentation and Compliance Notes
- Code justification
- Supporting documentation references
- Any coding queries or clarifications needed
## Recommendations
- Additional documentation needed
- Suggested follow-up
- Coding optimization opportunities

@ -0,0 +1,314 @@
Agent Name: Chief Medical Officer
Output: **Initial Assessment**
- **Patient Information**: 45-year-old White Male
- **Key Lab Result**:
- eGFR (Estimated Glomerular Filtration Rate): 59 ml/min/1.73 m²
- **Preliminary ICD-10 Codes for Symptoms**:
- N18.3: Chronic kidney disease, stage 3 (moderate)
**Differential Diagnoses**
1. **Chronic Kidney Disease (CKD)**
- **ICD-10 Code**: N18.3
- **Minimum Lab Range**: eGFR 30-59 ml/min/1.73 m² (indicative of stage 3 CKD)
- **Maximum Lab Range**: eGFR 59 ml/min/1.73 m²
2. **Possible Acute Kidney Injury (AKI) on Chronic Kidney Disease**
- **ICD-10 Code**: N17.9 (Acute kidney failure, unspecified) superimposed on N18.3
- **Minimum Lab Range**: Rapid decline in eGFR or increase in serum creatinine
- **Maximum Lab Range**: Dependent on baseline kidney function and rapidity of change
3. **Hypertensive Nephropathy**
- **ICD-10 Code**: I12.9 (Hypertensive chronic kidney disease with stage 1 through stage 4 chronic kidney disease, or unspecified chronic kidney disease)
- **Minimum Lab Range**: eGFR 30-59 ml/min/1.73 m² with evidence of hypertension
- **Maximum Lab Range**: eGFR 59 ml/min/1.73 m²
**Specialist Consultations Needed**
- **Nephrologist**: To assess the kidney function and evaluate for CKD or other renal pathologies.
- **Cardiologist**: If hypertensive nephropathy is suspected, to manage associated cardiovascular risks and blood pressure.
- **Endocrinologist**: If there are any signs of diabetes or metabolic syndrome contributing to renal impairment.
**Recommended Next Steps**
1. **Detailed Medical History and Physical Examination**:
- Assess for symptoms such as fatigue, swelling, changes in urination, or hypertension.
- Review any history of diabetes, hypertension, or cardiovascular disease.
2. **Additional Laboratory Tests**:
- Serum creatinine and blood urea nitrogen (BUN) to further evaluate kidney function.
- Urinalysis to check for proteinuria or hematuria.
- Lipid profile and fasting glucose to assess for metabolic syndrome.
3. **Imaging Studies**:
- Renal ultrasound to evaluate kidney size and rule out obstructive causes.
4. **Blood Pressure Monitoring**:
- Regular monitoring to assess for hypertension which could contribute to kidney damage.
5. **Referral to Nephrology**:
- For comprehensive evaluation and management of kidney disease.
6. **Patient Education**:
- Discuss lifestyle modifications such as diet, exercise, and smoking cessation to slow the progression of kidney disease.
By following these steps, we can ensure a thorough evaluation of the patient's condition and formulate an appropriate management plan. Agent Name: Virologist
Output: **Clinical Analysis for Viral Diseases**
Given the current patient information, there is no direct indication of a viral disease from the provided data. However, if a viral etiology is suspected or confirmed, the following analysis can be applied:
### Clinical Analysis:
- **Detailed Viral Symptom Analysis**:
- Symptoms of viral infections can be diverse but often include fever, fatigue, muscle aches, and respiratory symptoms such as cough or sore throat. In the context of renal impairment, certain viral infections can lead to or exacerbate kidney issues, such as Hepatitis B or C, HIV, or cytomegalovirus (CMV).
- **Disease Progression Timeline**:
- Viral infections typically have an incubation period ranging from a few days to weeks. The acute phase can last from several days to weeks, with symptoms peaking and then gradually resolving. Chronic viral infections, such as Hepatitis B or C, can lead to long-term complications, including kidney damage.
- **Risk Factors and Complications**:
- Risk factors for viral infections include immunosuppression, exposure to infected individuals, travel history, and underlying health conditions. Complications can include acute kidney injury, chronic kidney disease progression, and systemic involvement leading to multi-organ dysfunction.
### Coding Requirements:
#### Relevant ICD-10 Codes:
- **Confirmed Viral Conditions**:
- **B18.1**: Chronic viral hepatitis B
- **B18.2**: Chronic viral hepatitis C
- **B20**: HIV disease resulting in infectious and parasitic diseases
- **Suspected Viral Conditions**:
- **B34.9**: Viral infection, unspecified
- **Associated Symptoms**:
- **R50.9**: Fever, unspecified
- **R53.83**: Other fatigue
- **R05**: Cough
- **Complications**:
- **N17.9**: Acute kidney failure, unspecified (if viral infection leads to AKI)
- **N18.9**: Chronic kidney disease, unspecified (if progression due to viral infection)
#### Primary and Secondary Diagnostic Codes:
- **Primary Diagnostic Codes**:
- Use the specific viral infection code as primary if confirmed (e.g., B18.2 for Hepatitis C).
- **Secondary Condition Codes**:
- Use codes for symptoms or complications as secondary (e.g., N17.9 for AKI if due to viral infection).
### Rationale for Code Selection:
- **B18.1 and B18.2**: Selected for confirmed chronic hepatitis B or C, which can have renal complications.
- **B20**: Used if HIV is confirmed, given its potential impact on renal function.
- **B34.9**: Utilized when a viral infection is suspected but not yet identified.
- **R50.9, R53.83, R05**: Common symptoms associated with viral infections.
- **N17.9, N18.9**: Codes for renal complications potentially exacerbated by viral infections.
### Documentation:
- Ensure thorough documentation of clinical findings, suspected or confirmed viral infections, and associated symptoms or complications to justify the selected ICD-10 codes.
- Follow-up with additional testing or specialist referrals as needed to confirm or rule out viral etiologies and manage complications effectively. Agent Name: Internist
Output: To provide a comprehensive evaluation as an Internal Medicine specialist, let's conduct a detailed clinical assessment and medical coding for the presented case. This will involve a system-by-system review, analysis of vital signs, and evaluation of comorbidities, followed by appropriate ICD-10 coding.
### Clinical Assessment:
#### System-by-System Review:
1. **Respiratory System:**
- Evaluate for symptoms such as cough, shortness of breath, or wheezing.
- Consider potential viral or bacterial infections affecting the respiratory tract.
2. **Cardiovascular System:**
- Assess for any signs of heart failure or hypertension.
- Look for symptoms like chest pain, palpitations, or edema.
3. **Gastrointestinal System:**
- Check for symptoms such as nausea, vomiting, diarrhea, or abdominal pain.
- Consider liver function if hepatitis is suspected.
4. **Renal System:**
- Monitor for signs of acute kidney injury or chronic kidney disease.
- Evaluate urine output and creatinine levels.
5. **Neurological System:**
- Assess for headaches, dizziness, or any focal neurological deficits.
- Consider viral encephalitis if neurological symptoms are present.
6. **Musculoskeletal System:**
- Look for muscle aches or joint pain, common in viral infections.
7. **Integumentary System:**
- Check for rashes or skin lesions, which may indicate viral infections like herpes or CMV.
8. **Immune System:**
- Consider immunosuppression status, especially in the context of HIV or other chronic infections.
#### Vital Signs Analysis:
- **Temperature:** Evaluate for fever, which may indicate an infection.
- **Blood Pressure:** Check for hypertension or hypotension.
- **Heart Rate:** Assess for tachycardia or bradycardia.
- **Respiratory Rate:** Monitor for tachypnea.
- **Oxygen Saturation:** Ensure adequate oxygenation, especially in respiratory infections.
#### Comorbidity Evaluation:
- Assess for chronic conditions such as diabetes, hypertension, or chronic kidney disease.
- Consider the impact of these conditions on the current clinical presentation and potential complications.
### Medical Coding:
#### ICD-10 Codes:
1. **Primary Conditions:**
- If a specific viral infection is confirmed, use the appropriate code (e.g., B18.2 for chronic hepatitis C).
2. **Secondary Diagnoses:**
- **B34.9:** Viral infection, unspecified (if viral etiology is suspected but not confirmed).
- **R50.9:** Fever, unspecified (common symptom in infections).
- **R53.83:** Other fatigue (common in viral infections).
3. **Complications:**
- **N17.9:** Acute kidney failure, unspecified (if there is renal involvement).
- **N18.9:** Chronic kidney disease, unspecified (if there is progression due to infection).
4. **Chronic Conditions:**
- **I10:** Essential (primary) hypertension (if present).
- **E11.9:** Type 2 diabetes mellitus without complications (if present).
5. **Signs and Symptoms:**
- **R05:** Cough (common respiratory symptom).
- **M79.1:** Myalgia (muscle pain).
#### Hierarchical Condition Category (HCC) Codes:
- **HCC 18:** Diabetes with chronic complications (if applicable).
- **HCC 85:** Congestive heart failure (if applicable).
### Documentation Supporting Evidence:
- Ensure documentation includes detailed clinical findings, symptoms, and any laboratory or imaging results that support the diagnosis.
- Include any history of chronic conditions or recent changes in health status.
- Document any suspected or confirmed viral infections, along with their impact on the patient's health.
### Conclusion:
This comprehensive evaluation and coding approach allows for accurate diagnosis and management of the patient's condition, considering both acute and chronic aspects of their health. Proper documentation and coding facilitate effective communication and continuity of care. Agent Name: Medical Coder
Output: ### Medical Coding Summary
#### 1. Primary Diagnosis Codes
- **ICD-10 Code:** B18.2
- **Description:** Chronic viral hepatitis C
- **Supporting Documentation:** The diagnosis of chronic hepatitis C is confirmed through serological testing and liver function tests indicating chronic viral infection.
#### 2. Secondary Diagnosis Codes
- **B34.9:** Viral infection, unspecified
- **Supporting Documentation:** Suspected viral etiology without specific identification.
- **R50.9:** Fever, unspecified
- **Supporting Documentation:** Documented fever without a definitive cause.
- **R53.83:** Other fatigue
- **Supporting Documentation:** Patient reports persistent fatigue, common in viral infections.
- **I10:** Essential (primary) hypertension
- **Supporting Documentation:** History of hypertension with current blood pressure readings.
- **E11.9:** Type 2 diabetes mellitus without complications
- **Supporting Documentation:** Documented history of type 2 diabetes, managed with oral hypoglycemics.
#### 3. Symptom Codes
- **R05:** Cough
- **Supporting Documentation:** Patient presents with a persistent cough, noted in the respiratory evaluation.
- **M79.1:** Myalgia
- **Supporting Documentation:** Patient reports muscle pain, consistent with viral infections.
#### 4. Complication Codes
- **N17.9:** Acute kidney failure, unspecified
- **Supporting Documentation:** Elevated creatinine levels and reduced urine output indicative of renal involvement.
- **N18.9:** Chronic kidney disease, unspecified
- **Supporting Documentation:** Documented chronic kidney disease stage, with baseline creatinine levels.
#### 5. Coding Notes
- Ensure all clinical findings and laboratory results supporting the diagnoses are documented in the patient's medical record.
- Confirm the presence of chronic conditions and their management strategies.
- Monitor for any changes in the patient's condition that may require code updates or additions.
- Address any coding queries related to unspecified viral infections by seeking further diagnostic clarification if possible.
This coding summary provides a structured approach to documenting the patient's current health status, ensuring accurate and compliant ICD-10 coding. Agent Name: Diagnostic Synthesizer
Output: ### Final Diagnostic and Coding Assessment
#### Clinical Summary
**Primary Diagnosis:**
- **ICD-10 Code:** B18.2
- **Description:** Chronic viral hepatitis C
- **Supporting Evidence:** This diagnosis is substantiated by serological testing and liver function tests indicating a chronic viral infection. The confidence level for this diagnosis is high, with high-quality evidence from laboratory results.
**Secondary Diagnoses:**
1. **ICD-10 Code:** B34.9
- **Description:** Viral infection, unspecified
- **Supporting Evidence:** The suspected viral etiology lacks specific identification. Confidence level is moderate due to limited specificity in viral identification.
2. **ICD-10 Code:** R50.9
- **Description:** Fever, unspecified
- **Supporting Evidence:** Documented fever without a definitive cause. Confidence level is moderate, supported by clinical observation.
3. **ICD-10 Code:** R53.83
- **Description:** Other fatigue
- **Supporting Evidence:** Patient reports persistent fatigue, often associated with viral infections. Confidence level is moderate, based on patient-reported symptoms.
4. **ICD-10 Code:** I10
- **Description:** Essential (primary) hypertension
- **Supporting Evidence:** History of hypertension corroborated by current blood pressure readings. Confidence level is high, with consistent clinical evidence.
5. **ICD-10 Code:** E11.9
- **Description:** Type 2 diabetes mellitus without complications
- **Supporting Evidence:** Managed with oral hypoglycemics, with a documented history. Confidence level is high, with strong management records.
**Symptom Codes:**
- **ICD-10 Code:** R05
- **Description:** Cough
- **Supporting Evidence:** Persistent cough noted in respiratory evaluation. Confidence level is moderate, based on clinical observation.
- **ICD-10 Code:** M79.1
- **Description:** Myalgia
- **Supporting Evidence:** Muscle pain reported by the patient, consistent with viral infections. Confidence level is moderate, based on patient-reported symptoms.
**Complication Codes:**
1. **ICD-10 Code:** N17.9
- **Description:** Acute kidney failure, unspecified
- **Supporting Evidence:** Elevated creatinine levels and reduced urine output suggest renal involvement. Confidence level is high, supported by laboratory data.
2. **ICD-10 Code:** N18.9
- **Description:** Chronic kidney disease, unspecified
- **Supporting Evidence:** Documented chronic kidney disease stage with baseline creatinine levels. Confidence level is high, with consistent clinical data.
#### Coding Summary
**Complete Code List with Descriptions:**
- B18.2: Chronic viral hepatitis C
- B34.9: Viral infection, unspecified
- R50.9: Fever, unspecified
- R53.83: Other fatigue
- I10: Essential (primary) hypertension
- E11.9: Type 2 diabetes mellitus without complications
- R05: Cough
- M79.1: Myalgia
- N17.9: Acute kidney failure, unspecified
- N18.9: Chronic kidney disease, unspecified
**Code Hierarchy and Relationships:**
- The primary diagnosis (B18.2) is the central focus, with secondary diagnoses and symptoms potentially related to or exacerbated by the chronic hepatitis C infection.
- Complications (N17.9 and N18.9) may be linked to the primary diagnosis and other chronic conditions like diabetes and hypertension.
**Supporting Documentation:**
- Ensure that all clinical findings and laboratory results supporting the diagnoses are documented in the patient's medical record.
- Confirm the presence of chronic conditions and their management strategies.
- Monitor for any changes in the patient's condition that may require code updates or additions.
#### Recommendations
1. **Additional Testing Needed:**
- Further diagnostic testing is recommended to clarify the unspecified viral infection (B34.9) and to monitor kidney function.
2. **Follow-up Care:**
- Regular follow-up appointments to manage chronic conditions such as hypertension and diabetes.
- Monitor renal function and adjust treatment plans as necessary.
3. **Documentation Improvements Needed:**
- Enhance documentation specificity for the unspecified viral infection.
- Ensure comprehensive records of all chronic conditions and their management strategies.
These recommendations aim to improve diagnostic accuracy and patient care continuity.

@ -0,0 +1,177 @@
from datetime import datetime
from swarms import Agent, AgentRearrange, create_file_in_folder
chief_medical_officer = Agent(
agent_name="Chief Medical Officer",
system_prompt="""You are the Chief Medical Officer coordinating a team of medical specialists for viral disease diagnosis.
Your responsibilities include:
- Gathering initial patient symptoms and medical history
- Coordinating with specialists to form differential diagnoses
- Synthesizing different specialist opinions into a cohesive diagnosis
- Ensuring all relevant symptoms and test results are considered
- Making final diagnostic recommendations
- Suggesting treatment plans based on team input
- Identifying when additional specialists need to be consulted
Guidelines:
1. Always start with a comprehensive patient history
2. Consider both common and rare viral conditions
3. Factor in patient demographics and risk factors
4. Document your reasoning process clearly
5. Highlight any critical or emergency symptoms
6. Note any limitations or uncertainties in the diagnosis
Format all responses with clear sections for:
- Initial Assessment
- Differential Diagnoses
- Specialist Consultations Needed
- Recommended Next Steps""",
model_name="gpt-4o", # Models from litellm -> claude-2
max_loops=1,
)
# Viral Disease Specialist
virologist = Agent(
agent_name="Virologist",
system_prompt="""You are a specialist in viral diseases with expertise in:
- Respiratory viruses (Influenza, Coronavirus, RSV)
- Systemic viral infections (EBV, CMV, HIV)
- Childhood viral diseases (Measles, Mumps, Rubella)
- Emerging viral threats
Your role involves:
1. Analyzing symptoms specific to viral infections
2. Distinguishing between different viral pathogens
3. Assessing viral infection patterns and progression
4. Recommending specific viral tests
5. Evaluating epidemiological factors
For each case, consider:
- Incubation periods
- Transmission patterns
- Seasonal factors
- Geographic prevalence
- Patient immune status
- Current viral outbreaks
Provide detailed analysis of:
- Characteristic viral symptoms
- Disease progression timeline
- Risk factors for severe disease
- Potential complications""",
model_name="gpt-4o",
max_loops=1,
)
# Internal Medicine Specialist
internist = Agent(
agent_name="Internist",
system_prompt="""You are an Internal Medicine specialist responsible for:
- Comprehensive system-based evaluation
- Integration of symptoms across organ systems
- Identification of systemic manifestations
- Assessment of comorbidities
For each case, analyze:
1. Vital signs and their implications
2. System-by-system review (cardiovascular, respiratory, etc.)
3. Impact of existing medical conditions
4. Medication interactions and contraindications
5. Risk stratification
Consider these aspects:
- Age-related factors
- Chronic disease impact
- Medication history
- Social and environmental factors
Document:
- Physical examination findings
- System-specific symptoms
- Relevant lab abnormalities
- Risk factors for complications""",
model_name="gpt-4o",
max_loops=1,
)
# Diagnostic Synthesizer
synthesizer = Agent(
agent_name="Diagnostic Synthesizer",
system_prompt="""You are responsible for synthesizing all specialist inputs to create a final diagnostic assessment:
Core responsibilities:
1. Integrate findings from all specialists
2. Identify patterns and correlations
3. Resolve conflicting opinions
4. Generate probability-ranked differential diagnoses
5. Recommend additional testing if needed
Analysis framework:
- Weight evidence based on reliability and specificity
- Consider epidemiological factors
- Evaluate diagnostic certainty
- Account for test limitations
Provide structured output including:
1. Primary diagnosis with confidence level
2. Supporting evidence summary
3. Alternative diagnoses to consider
4. Recommended confirmatory tests
5. Red flags or warning signs
6. Follow-up recommendations
Documentation requirements:
- Clear reasoning chain
- Evidence quality assessment
- Confidence levels for each diagnosis
- Knowledge gaps identified
- Risk assessment""",
model_name="gpt-4o",
max_loops=1,
)
# Create agent list
agents = [chief_medical_officer, virologist, internist, synthesizer]
# Define diagnostic flow
flow = f"""{chief_medical_officer.agent_name} -> {virologist.agent_name} -> {internist.agent_name} -> {synthesizer.agent_name}"""
# Create the swarm system
diagnosis_system = AgentRearrange(
name="Medical-nlp-diagnosis-swarm",
description="natural language symptions to diagnosis report",
agents=agents,
flow=flow,
max_loops=1,
output_type="all",
)
# Example usage
if __name__ == "__main__":
# Example patient case
patient_case = """
Patient: 45-year-old female
Presenting symptoms:
- Fever (101.5°F) for 3 days
- Dry cough
- Fatigue
- Mild shortness of breath
Medical history:
- Controlled hypertension
- No recent travel
- Fully vaccinated for COVID-19
- No known sick contacts
"""
# Add timestamp to the patient case
case_info = f"Timestamp: {datetime.now()}\nPatient Information: {patient_case}"
# Run the diagnostic process
diagnosis = diagnosis_system.run(case_info)
# Create a folder and file called reports
create_file_in_folder(
"reports", "medical_analysis_agent_rearrange.md", diagnosis
)

@ -0,0 +1,173 @@
**Initial Assessment:**
The patient is a 45-year-old female presenting with a fever, dry cough, fatigue, and mild shortness of breath. Her medical history includes controlled hypertension. She has not traveled recently and has no known sick contacts. Additionally, she is fully vaccinated for COVID-19.
**Differential Diagnoses:**
1. **Influenza:** Given the season and symptoms, influenza is a strong possibility. The patients symptoms align well with typical flu presentations, which include fever, cough, and fatigue.
2. **COVID-19:** Despite being fully vaccinated, breakthrough infections can occur, especially with new variants. Symptoms such as fever, cough, and shortness of breath are consistent with COVID-19.
3. **Respiratory Syncytial Virus (RSV):** RSV can cause symptoms similar to those of the flu and COVID-19, including cough and shortness of breath, particularly in adults with underlying conditions.
4. **Viral Pneumonia:** This could be a complication of an initial viral infection, presenting with fever, cough, and shortness of breath.
5. **Other Viral Infections:** Other respiratory viruses, such as adenovirus or parainfluenza, could also be considered, though less common.
**Specialist Consultations Needed:**
1. **Infectious Disease Specialist:** To evaluate and prioritize testing for specific viral pathogens and to provide input on potential treatment plans.
2. **Pulmonologist:** Given the mild shortness of breath and history of hypertension, a pulmonologist could assess the need for further respiratory evaluation or intervention.
**Recommended Next Steps:**
1. **Diagnostic Testing:**
- Perform a rapid influenza test and a COVID-19 PCR test to rule out these common viral infections.
- Consider a respiratory viral panel if initial tests are negative to identify other potential viral causes.
2. **Symptomatic Treatment:**
- Recommend antipyretics for fever management.
- Encourage rest and hydration to help manage fatigue and overall symptoms.
3. **Monitoring and Follow-Up:**
- Monitor respiratory symptoms closely, given the mild shortness of breath, and advise the patient to seek immediate care if symptoms worsen.
- Schedule a follow-up appointment to reassess symptoms and review test results.
4. **Consideration of Antiviral Treatment:**
- If influenza is confirmed, consider antiviral treatment with oseltamivir, especially given the patient's age and comorbidities.
**Limitations or Uncertainties:**
- There is uncertainty regarding the exact viral cause without specific test results.
- The potential for atypical presentations or co-infections should be considered, particularly if initial tests are inconclusive.
By following these steps, we aim to determine the underlying cause of the patient's symptoms and provide appropriate care. **Detailed Analysis:**
1. **Characteristic Viral Symptoms:**
- **Influenza:** Typically presents with sudden onset of fever, chills, cough, sore throat, muscle or body aches, headaches, and fatigue. Shortness of breath can occur, especially if there is a progression to viral pneumonia.
- **COVID-19:** Symptoms can vary widely but often include fever, cough, fatigue, and shortness of breath. Loss of taste or smell, sore throat, and gastrointestinal symptoms may also occur.
- **RSV:** In adults, RSV can cause symptoms similar to a mild cold, but in some cases, it can lead to more severe respiratory symptoms, especially in those with underlying conditions.
- **Viral Pneumonia:** Often presents with persistent cough, fever, shortness of breath, and fatigue. It can be a complication of other respiratory viral infections.
- **Other Respiratory Viruses (e.g., Adenovirus, Parainfluenza):** These can cause a range of symptoms similar to the common cold or flu, including fever, cough, and congestion.
2. **Disease Progression Timeline:**
- **Influenza:** Symptoms usually appear 1-4 days after exposure and can last for about a week, although cough and fatigue may persist longer.
- **COVID-19:** Symptoms typically appear 2-14 days after exposure, with a median of 5 days. The course can vary significantly, from mild to severe.
- **RSV:** Symptoms generally appear 4-6 days after exposure and can last 1-2 weeks.
- **Viral Pneumonia:** Can develop as a complication of a primary viral infection, often within a few days of the initial symptoms.
3. **Risk Factors for Severe Disease:**
- **Influenza and COVID-19:** Age over 50, hypertension, and other comorbidities can increase the risk of severe disease.
- **RSV:** More severe in adults with chronic heart or lung disease or weakened immune systems.
- **Viral Pneumonia:** More likely in individuals with weakened immune systems or pre-existing respiratory conditions.
4. **Potential Complications:**
- **Influenza:** Can lead to pneumonia, exacerbation of chronic medical conditions, and secondary bacterial infections.
- **COVID-19:** Complications can include pneumonia, acute respiratory distress syndrome (ARDS), organ failure, and long COVID.
- **RSV:** Can result in bronchiolitis or pneumonia, particularly in vulnerable populations.
- **Viral Pneumonia:** Can lead to respiratory failure and secondary bacterial infections.
**Considerations for Testing and Monitoring:**
- Given the overlapping symptoms, initial testing for influenza and COVID-19 is crucial.
- A comprehensive respiratory viral panel can help identify less common viral pathogens if initial tests are negative.
- Monitoring for worsening respiratory symptoms is essential, given the patient's mild shortness of breath and history of hypertension.
**Recommendations for Care:**
- Symptomatic treatment should focus on fever and symptom relief while awaiting test results.
- In the case of confirmed influenza, antiviral treatment with oseltamivir is advisable, especially due to the patient's age and hypertension.
- Close follow-up is necessary to reassess symptoms and ensure timely intervention if the patient's condition deteriorates.
**Final Note:**
- Stay updated on current viral outbreaks and emerging variants, as these can influence the likelihood of specific viral infections and guide testing priorities. To proceed with a comprehensive internal medicine evaluation based on the virologist's analysis, we will assess the case systematically:
1. **Vital Signs and Their Implications:**
- **Temperature:** Evaluate for fever, which can indicate an ongoing infection or inflammatory process.
- **Respiratory Rate:** Increased rate may suggest respiratory distress or compensation for hypoxemia.
- **Heart Rate and Blood Pressure:** Tachycardia or hypertension may indicate systemic stress or a response to fever/infection.
- **Oxygen Saturation:** Important to assess for hypoxemia, especially in respiratory infections.
2. **System-by-System Review:**
- **Cardiovascular:** Consider the impact of viral infections on the cardiovascular system, such as myocarditis or exacerbation of heart failure, especially in patients with hypertension or other comorbidities.
- **Respiratory:** Assess for signs of pneumonia or bronchitis. Auscultation may reveal crackles or wheezes. Consider chest imaging if indicated.
- **Gastrointestinal:** Evaluate for symptoms like nausea, vomiting, or diarrhea, which can occur with COVID-19 or other viral infections.
- **Neurological:** Monitor for headache, confusion, or loss of taste/smell, which can be associated with viral infections like COVID-19.
- **Musculoskeletal:** Assess for myalgias or arthralgias, common in influenza.
3. **Impact of Existing Medical Conditions:**
- **Hypertension:** Monitor blood pressure closely, as viral infections can exacerbate hypertension.
- **Age-related Factors:** Older age increases the risk of severe disease and complications from viral infections.
- **Chronic Diseases:** Consider the impact of other chronic conditions, such as diabetes or COPD, which may complicate the clinical course.
4. **Medication Interactions and Contraindications:**
- Review current medications for interactions with potential antiviral treatments, such as oseltamivir for influenza.
- Consider contraindications for specific treatments based on the patient's comorbidities.
5. **Risk Stratification:**
- Assess the patient's risk for severe disease based on age, comorbidities, and current symptoms.
- Identify patients who may need more intensive monitoring or early intervention.
**Documentation:**
- **Physical Examination Findings:**
- Document vital signs, respiratory effort, and any abnormal findings on auscultation or other systems.
- **System-Specific Symptoms:**
- Record symptoms such as cough, fever, fatigue, and any gastrointestinal or neurological symptoms.
- **Relevant Lab Abnormalities:**
- Note any significant lab findings, such as elevated inflammatory markers or abnormal CBC.
- **Risk Factors for Complications:**
- Highlight factors such as age, hypertension, and any other relevant comorbid conditions.
**Plan:**
- Initiate appropriate symptomatic treatment while awaiting test results.
- Consider antiviral therapy if influenza is confirmed, particularly given the patient's age and hypertension.
- Ensure close follow-up to monitor for any deterioration in the patient's condition, and adjust the management plan as needed.
- Educate the patient on signs of worsening symptoms and when to seek further medical attention.
By integrating these considerations, we can provide a holistic approach to the management of viral infections in the context of internal medicine. **Final Diagnostic Assessment**
1. **Primary Diagnosis: Viral Respiratory Infection (e.g., Influenza or COVID-19)**
- **Confidence Level:** Moderate to High
- **Supporting Evidence Summary:**
- Presence of fever, cough, and respiratory symptoms.
- Possible gastrointestinal symptoms (nausea, vomiting, diarrhea).
- Neurological symptoms such as headache and potential anosmia.
- Elevated inflammatory markers and potential CBC abnormalities.
- Older age and hypertension increase risk for severe disease.
2. **Alternative Diagnoses to Consider:**
- **Bacterial Pneumonia:** Consider if symptoms worsen or if there is a lack of improvement with antiviral treatment.
- **Heart Failure Exacerbation:** Especially if there are cardiovascular symptoms like edema or worsening dyspnea.
- **Other Viral Infections:** Such as RSV or adenovirus, particularly if COVID-19 and influenza tests are negative.
3. **Recommended Confirmatory Tests:**
- PCR testing for COVID-19 and Influenza.
- Chest X-ray or CT scan if pneumonia is suspected.
- Blood cultures if bacterial infection is a concern.
- Complete blood count (CBC) and inflammatory markers for further assessment.
4. **Red Flags or Warning Signs:**
- Rapid deterioration in respiratory status (e.g., increased work of breathing, hypoxemia).
- Signs of cardiovascular compromise (e.g., chest pain, severe hypertension).
- Neurological changes (e.g., confusion, severe headache).
- Persistent high fever despite treatment.
5. **Follow-up Recommendations:**
- Close monitoring of vital signs and symptom progression.
- Re-evaluation within 48-72 hours or sooner if symptoms worsen.
- Adjust treatment plan based on test results and clinical response.
- Patient education on recognizing signs of complications and when to seek urgent care.
**Documentation Requirements:**
- **Clear Reasoning Chain:** The diagnosis is based on the synthesis of clinical symptoms, lab findings, and risk factors.
- **Evidence Quality Assessment:** Moderate quality; relies on clinical presentation and initial lab results.
- **Confidence Levels for Each Diagnosis:** Primary diagnosis (Viral Respiratory Infection) is moderate to high; alternative diagnoses are lower.
- **Knowledge Gaps Identified:** Awaiting confirmatory test results for specific viral or bacterial pathogens.
- **Risk Assessment:** High risk for complications due to age and hypertension; requires vigilant monitoring and timely intervention.
By following this structured diagnostic framework, we ensure a comprehensive and patient-centered approach to managing the suspected viral respiratory infection while being prepared for alternative diagnoses and potential complications.

@ -0,0 +1,291 @@
**Executive Summary:**
The document under review is a SAFE (Simple Agreement for Future Equity) agreement, which provides the investor with rights to convert their investment into equity under specified conditions. The key financial terms include a valuation cap of $10,000,000 and a discount rate of 20%. The investment amount is $500,000, with provisions for automatic and optional conversion, as well as pro-rata rights for future investment rounds.
**Key Terms Analysis:**
1. **Valuation Cap:** $10,000,000 - This sets the maximum valuation at which the investment will convert into equity.
2. **Discount Rate:** 20% - This provides the investor with a discount on the price per share during conversion.
3. **Investment Amount:** $500,000 - The amount invested under this agreement.
4. **Conversion Provisions:**
- **Automatic Conversion:** Triggers upon an equity financing round of at least $1,000,000.
- **Optional Conversion:** Available upon a liquidity event.
- **Most Favored Nation (MFN):** Ensures the investor receives terms no less favorable than those offered to subsequent investors.
5. **Pro-rata Rights:** Allows the investor to maintain their percentage ownership in future financing rounds.
**Risk Factors:**
1. **Valuation Cap Risk:** If the company's valuation exceeds $10,000,000 during conversion, the investor benefits from a lower conversion price, but if the valuation is below, the cap may not provide a significant advantage.
2. **Conversion Timing:** The automatic conversion depends on a future equity financing event, which introduces timing and market risk.
3. **MFN Clause Complexity:** The inclusion of an MFN clause can complicate future financing negotiations and may deter other investors.
**Negotiation Points:**
1. **Valuation Cap Adjustment:** Consider negotiating a lower valuation cap to provide better upside protection.
2. **Discount Rate Increase:** Explore increasing the discount rate to improve conversion terms.
3. **Clarification of MFN Terms:** Ensure clarity on the MFN provision to avoid potential disputes in future rounds.
4. **Pro-rata Rights Specification:** Detail the conditions under which pro-rata rights can be exercised, including any limitations or exceptions.
**Recommended Actions:**
1. **Review Market Comparables:** Assess current market conditions to ensure valuation cap and discount rate align with industry standards.
2. **Legal Review of MFN Clause:** Engage legal counsel to review the MFN provision for potential issues.
3. **Scenario Analysis:** Conduct a scenario analysis to understand the impact of various conversion events on equity ownership.
**Areas Requiring Specialist Review:**
1. **Legal Review:** A legal specialist should review the MFN provision and conversion clauses for enforceability and potential conflicts.
2. **Financial Modeling:** A financial analyst should model different conversion scenarios to assess potential outcomes and returns.
3. **Market Analysis:** A market specialist should evaluate the competitiveness of the valuation cap and discount rate based on current trends. **Detailed Analysis of SAFE Agreement**
**1. Term-by-Term Analysis:**
- **Valuation Cap ($10,000,000):**
- Sets a ceiling on the companys valuation for conversion purposes. This cap provides the investor with protection in case the company's valuation at the time of conversion is higher than $10M, ensuring a more favorable conversion price.
- **Valuation Implications:** If the company's pre-money valuation is above $10M in a future financing round, the investor benefits from a lower effective price per share, potentially increasing their ownership percentage.
- **Recommendation:** Consider negotiating a lower cap if market conditions suggest the company might achieve a higher valuation soon.
- **Discount Rate (20%):**
- Provides a reduction on the price per share during conversion, giving the investor a benefit compared to new investors in the subsequent round.
- **Valuation Implications:** Acts as a hedge against high valuations by ensuring a discount on the conversion price.
- **Recommendation:** Assess market standards to determine if a higher discount rate is achievable.
- **Investment Amount ($500,000):**
- The principal amount invested, which will convert into equity under the agreed terms.
- **Future Round Impacts:** This amount will affect the company's cap table upon conversion, diluting existing shareholders.
- **Recommendation:** Ensure this aligns with the companys capital needs and strategic goals.
- **Conversion Provisions:**
- **Automatic Conversion:** Triggers upon an equity financing round of at least $1,000,000.
- **Conversion Mechanics:** The investment converts into equity automatically, based on the valuation cap or discount rate, whichever is more favorable.
- **Recommendation:** Ensure the threshold aligns with realistic fundraising expectations.
- **Optional Conversion:** Available upon a liquidity event, such as an acquisition or IPO.
- **Conversion Mechanics:** Provides flexibility for the investor to convert under favorable terms during liquidity events.
- **Recommendation:** Clearly define what constitutes a liquidity event to avoid ambiguity.
- **Most Favored Nation (MFN) Provision:**
- **Investor Rights and Limitations:** Ensures the investor receives terms no less favorable than those offered to future investors.
- **Potential Conflicts:** This can complicate future rounds as new investors might demand the same or better terms.
- **Recommendation:** Clarify the scope and limitations of the MFN clause to avoid deterring future investors.
- **Pro-rata Rights:**
- **Investor Rights:** Allows the investor to maintain their ownership percentage in future financing rounds by purchasing additional shares.
- **Recommendation:** Specify the conditions and limitations under which these rights can be exercised to avoid potential disputes.
**2. Conversion Scenarios Modeling:**
- **Scenario Analysis:** Model various conversion scenarios based on different company valuations and financing events to understand potential outcomes for both the investor and company.
- **Impact on Cap Table:** Analyze how different conversion scenarios will affect the company's equity distribution and the dilution of existing shareholders.
**3. Rights and Preferences Evaluation:**
- **Investor Protections:** Evaluate the effectiveness of the valuation cap, discount rate, and MFN provisions in protecting investor interests.
- **Future Round Implications:** Consider how these rights might influence future fundraising efforts and investor relations.
**4. Standard vs. Non-standard Terms Identification:**
- **Market Comparisons:** Compare the agreement's terms against industry standards to identify any non-standard provisions that may require negotiation or adjustment.
- **Risk Assessment:** Assess the risk associated with any non-standard terms, particularly in relation to future financing and investor relations.
**5. Post-money vs. Pre-money SAFE Analysis:**
- **Valuation Implications:** Understand the difference between post-money and pre-money SAFE agreements, as this affects the calculation of ownership percentages and dilution.
- **Recommendation:** Ensure clarity on whether the SAFE is structured as pre-money or post-money to accurately assess its impact on the cap table.
**Recommendations for Negotiations:**
- **Valuation Cap and Discount Rate:** Consider negotiating these terms to ensure they align with market conditions and provide adequate investor protection.
- **MFN Clause:** Engage legal counsel to review and potentially simplify the MFN provision to facilitate future financing rounds.
- **Pro-rata Rights:** Clearly define the exercise conditions to avoid future disputes and ensure alignment with company growth strategies.
- **Legal and Financial Review:** Engage specialists to review the agreement for enforceability and to model potential financial outcomes. **Detailed Analysis of Venture Capital Term Sheet**
**1. Economic Terms Analysis:**
- **Pre/Post-money Valuation:**
- **Pre-money Valuation:** The company's valuation before the investment is made. It's crucial for determining the price per share and the percentage of the company the investor will own post-investment.
- **Post-money Valuation:** The valuation after the investment is made, calculated as pre-money valuation plus the new investment amount.
- **Market Standard Comparison:** Typically, early-stage startups have lower pre-money valuations, ranging from $3M to $10M, depending on the sector and traction.
- **Founder Impact Analysis:** A higher pre-money valuation minimizes dilution for founders but might set a high bar for future rounds.
- **Share Price Calculation:**
- Calculated by dividing the pre-money valuation by the total number of pre-investment shares.
- **Market Standard Comparison:** Ensure the share price aligns with industry norms for similar stage companies.
- **Founder Impact Analysis:** Affects the dilution founders face; higher share prices generally mean less dilution.
- **Capitalization Analysis:**
- Involves understanding the fully diluted capitalization, including all shares, options, warrants, and convertible securities.
- **Founder Impact Analysis:** Critical for understanding potential dilution and control implications.
- **Option Pool Sizing:**
- Typically set aside 10-20% of post-money shares for future employee grants.
- **Market Standard Comparison:** 15% is common for early-stage rounds.
- **Founder Impact Analysis:** Larger pools increase pre-money dilution for founders.
**2. Control Provisions Review:**
- **Board Composition:**
- Defines the number of seats and who appoints them. Investors often seek at least one seat.
- **Market Standard Comparison:** A 3-5 member board is common, with one investor seat.
- **Founder Impact Analysis:** More investor seats can reduce founder control.
- **Voting Rights:**
- Typically, investors want voting rights proportional to their ownership.
- **Market Standard Comparison:** Standard practice is one vote per share.
- **Founder Impact Analysis:** High investor voting power can limit founder decision-making.
- **Protective Provisions:**
- Rights that give investors veto power over key decisions (e.g., sale of the company, issuance of new shares).
- **Market Standard Comparison:** Common for significant matters.
- **Founder Impact Analysis:** Can constrain founder flexibility in strategic decisions.
- **Information Rights:**
- Entail regular financial and operational updates.
- **Market Standard Comparison:** Quarterly reports are typical.
- **Founder Impact Analysis:** Ensures transparency but can increase administrative burden.
**3. Investor Rights Assessment:**
- **Pro-rata Rights:**
- Allow investors to maintain their ownership percentage in future rounds.
- **Market Standard Comparison:** Common for early-stage investors.
- **Founder Impact Analysis:** Can limit the amount of shares available for new investors.
- **Anti-dilution Protection:**
- Protects investors from dilution in down rounds, with full ratchet or weighted average being common methods.
- **Market Standard Comparison:** Weighted average is more founder-friendly.
- **Founder Impact Analysis:** Full ratchet can lead to significant founder dilution.
- **Registration Rights:**
- Rights to register shares for public sale.
- **Market Standard Comparison:** Demand and piggyback rights are standard.
- **Founder Impact Analysis:** Typically, no immediate impact but relevant for IPO considerations.
- **Right of First Refusal:**
- Allows investors to purchase shares before they are sold to a third party.
- **Market Standard Comparison:** Common to protect investor ownership.
- **Founder Impact Analysis:** Can complicate secondary sales.
**4. Governance Structures:**
- **Market Standard Comparison:** Early-stage companies often have a simple governance structure with founders retaining significant control.
- **Founder Impact Analysis:** Complex structures can dilute founder control and complicate decision-making.
**5. Exit and Liquidity Provisions:**
- **Liquidation Preferences:**
- Determine the order and amount investors receive before common shareholders in a liquidity event.
- **Market Standard Comparison:** 1x non-participating is common.
- **Founder Impact Analysis:** Participating preferences can significantly reduce returns for common shareholders.
- **Drag-along Rights:**
- Allow majority shareholders to force minority shareholders to sell their shares in an acquisition.
- **Market Standard Comparison:** Common to facilitate exits.
- **Founder Impact Analysis:** Ensures alignment but reduces minority shareholder control.
**Recommendations for Negotiations:**
- **Valuation and Option Pool:** Negotiate a valuation that reflects company potential and a reasonable option pool to minimize founder dilution.
- **Board Composition and Protective Provisions:** Balance investor oversight with founder control.
- **Anti-dilution and Liquidation Preferences:** Opt for weighted average anti-dilution and non-participating liquidation preferences to protect founder interests.
- **Legal and Financial Review:** Engage professionals to ensure terms align with strategic goals and industry standards. **Compliance Checklist**
1. **Regulatory Compliance:**
- Ensure adherence to relevant securities regulations, such as the Securities Act of 1933 and the Securities Exchange Act of 1934.
- Confirm that disclosure requirements are met, including financial statements and material risks.
- Evaluate if the company qualifies as an investment company under the Investment Company Act of 1940.
- Verify compliance with blue sky laws in applicable states for the offer and sale of securities.
2. **Documentation Review:**
- Check for accuracy in legal definitions used within the term sheet.
- Assess the enforceability of key provisions, such as protective provisions and anti-dilution rights.
- Identify jurisdiction concerns, ensuring governing law clauses align with strategic needs.
- Review amendment provisions to ensure they allow for necessary flexibility and protection.
3. **Risk Assessment:**
- Analyze legal precedents related to similar term sheet provisions.
- Assess regulatory exposure, particularly concerning investor rights and control provisions.
- Evaluate enforcement mechanisms for protective provisions and dispute resolution clauses.
- Review dispute resolution provisions, ensuring they are comprehensive and efficient.
**Risk Assessment Summary**
- **Securities Law Compliance Risk:** Moderate risk if disclosure and registration requirements are not fully met.
- **Corporate Governance Risk:** High risk if board composition and protective provisions overly favor investors, potentially leading to founder disenfranchisement.
- **Regulatory Exposure:** Low to moderate, depending on the company's industry and geographical reach.
- **Dispute Resolution Risk:** Low if arbitration or mediation clauses are included and jurisdiction is favorable.
**Required Disclosures List**
- Financial statements and projections.
- Material risks associated with the business and the investment.
- Information on existing and potential legal proceedings.
- Details on capitalization, including option pool and convertible securities.
**Recommended Legal Modifications**
- Adjust board composition to ensure a balanced representation of founders and investors.
- Opt for a weighted average anti-dilution provision to protect founder interests.
- Specify a clear and favorable jurisdiction for dispute resolution.
- Consider simplifying governance structures to maintain founder control while providing necessary investor oversight.
**Jurisdiction-Specific Concerns**
- Ensure compliance with state-specific blue sky laws for the offer and sale of securities.
- Consider the implications of governing law clauses, particularly if the company operates in multiple jurisdictions.
- Review state-specific requirements for board composition and shareholder rights to ensure compliance.
This comprehensive analysis ensures that the venture capital term sheet aligns with legal compliance requirements while balancing the interests of both founders and investors. **Market Positioning Summary**
The current venture capital term sheet aligns moderately well with market standards, but there are areas where competitiveness can be improved. The terms reflect a balance between investor control and founder protection, with room for adjustments to enhance founder-friendliness without compromising investor interests.
**Comparative Analysis**
1. **Stage-Appropriate Terms:**
- The term sheet is suitable for early-stage investments, offering standard protective provisions and anti-dilution rights.
- Board composition and control rights are more aligned with later-stage investments, which could be adjusted for early-stage flexibility.
2. **Industry-Standard Provisions:**
- The inclusion of a weighted average anti-dilution provision is consistent with industry standards.
- Protective provisions are standard but may need fine-tuning to prevent founder disenfranchisement.
3. **Geographic Variations:**
- Compliance with blue sky laws and state-specific regulations is critical. The term sheet adequately addresses these concerns but should be reviewed for any recent changes in state laws.
4. **Recent Trend Analysis:**
- There's a growing trend towards more founder-friendly terms, including increased control over board decisions and simplified governance structures.
**Trend Implications**
- **Emerging Terms and Conditions:**
- Increased focus on founder-friendly provisions, such as enhanced voting rights and simplified governance.
- **Shifting Market Standards:**
- A trend towards balancing investor protection with founder autonomy is evident, with more emphasis on collaborative governance.
- **Industry-Specific Adaptations:**
- Tech and biotech sectors are seeing more flexible terms to accommodate rapid innovation and scaling needs.
- **Regional Variations:**
- U.S. markets are increasingly adopting terms that offer founders more control, especially in tech hubs like Silicon Valley.
**Negotiation Leverage Points**
- Emphasize the inclusion of industry-standard anti-dilution provisions as a protective measure for investors.
- Highlight the potential for improved founder control to attract high-quality entrepreneurial talent.
- Use geographic compliance as a selling point for investors concerned with regulatory exposure.
**Recommended Modifications**
1. **Board Composition:**
- Adjust to ensure a balanced representation of founders and investors, possibly introducing independent board members.
2. **Anti-Dilution Provisions:**
- Maintain the weighted average provision but clarify terms to protect founder interests further.
3. **Governance Structure:**
- Simplify governance to enhance founder control while maintaining necessary investor oversight.
4. **Dispute Resolution:**
- Specify a clear and favorable jurisdiction for dispute resolution to minimize legal uncertainties.
By implementing these modifications, the term sheet can better align with current market trends and improve its competitiveness in attracting both investors and founders.

@ -0,0 +1,243 @@
from datetime import datetime
from swarms import Agent, AgentRearrange, create_file_in_folder
# Lead Investment Analyst
lead_analyst = Agent(
agent_name="Lead Investment Analyst",
system_prompt="""You are the Lead Investment Analyst coordinating document analysis for venture capital investments.
Core responsibilities:
- Coordinating overall document review process
- Identifying key terms and conditions
- Flagging potential risks and concerns
- Synthesizing specialist inputs into actionable insights
- Recommending negotiation points
Document Analysis Framework:
1. Initial document classification and overview
2. Key terms identification
3. Risk assessment
4. Market context evaluation
5. Recommendation formulation
Output Format Requirements:
- Executive Summary
- Key Terms Analysis
- Risk Factors
- Negotiation Points
- Recommended Actions
- Areas Requiring Specialist Review""",
model_name="gpt-4o",
max_loops=1,
)
# SAFE Agreement Specialist
safe_specialist = Agent(
agent_name="SAFE Specialist",
system_prompt="""You are a specialist in SAFE (Simple Agreement for Future Equity) agreements with expertise in:
Technical Analysis Areas:
- Valuation caps and discount rates
- Conversion mechanisms and triggers
- Pro-rata rights
- Most Favored Nation (MFN) provisions
- Dilution and anti-dilution provisions
Required Assessments:
1. Cap table impact analysis
2. Conversion scenarios modeling
3. Rights and preferences evaluation
4. Standard vs. non-standard terms identification
5. Post-money vs. pre-money SAFE analysis
Consider and Document:
- Valuation implications
- Future round impacts
- Investor rights and limitations
- Comparative market terms
- Potential conflicts with other securities
Output Requirements:
- Term-by-term analysis
- Conversion mechanics explanation
- Risk assessment for non-standard terms
- Recommendations for negotiations""",
model_name="gpt-4o",
max_loops=1,
)
# Term Sheet Analyst
term_sheet_analyst = Agent(
agent_name="Term Sheet Analyst",
system_prompt="""You are a Term Sheet Analyst specialized in venture capital financing documents.
Core Analysis Areas:
- Economic terms (valuation, option pool, etc.)
- Control provisions
- Investor rights and protections
- Governance structures
- Exit and liquidity provisions
Detailed Review Requirements:
1. Economic Terms Analysis:
- Pre/post-money valuation
- Share price calculation
- Capitalization analysis
- Option pool sizing
2. Control Provisions Review:
- Board composition
- Voting rights
- Protective provisions
- Information rights
3. Investor Rights Assessment:
- Pro-rata rights
- Anti-dilution protection
- Registration rights
- Right of first refusal
Output Format:
- Term-by-term breakdown
- Market standard comparison
- Founder impact analysis
- Investor rights summary
- Governance implications""",
model_name="gpt-4o",
max_loops=1,
)
# Legal Compliance Analyst
legal_analyst = Agent(
agent_name="Legal Compliance Analyst",
system_prompt="""You are a Legal Compliance Analyst for venture capital documentation.
Primary Focus Areas:
- Securities law compliance
- Corporate governance requirements
- Regulatory restrictions
- Standard market practices
- Legal risk assessment
Analysis Framework:
1. Regulatory Compliance:
- Securities regulations
- Disclosure requirements
- Investment company considerations
- Blue sky laws
2. Documentation Review:
- Legal definitions accuracy
- Enforceability concerns
- Jurisdiction issues
- Amendment provisions
3. Risk Assessment:
- Legal precedent analysis
- Regulatory exposure
- Enforcement mechanisms
- Dispute resolution provisions
Output Requirements:
- Compliance checklist
- Risk assessment summary
- Required disclosures list
- Recommended legal modifications
- Jurisdiction-specific concerns""",
model_name="gpt-4o",
max_loops=1,
)
# Market Comparison Analyst
market_analyst = Agent(
agent_name="Market Comparison Analyst",
system_prompt="""You are a Market Comparison Analyst for venture capital terms and conditions.
Core Responsibilities:
- Benchmark terms against market standards
- Identify industry-specific variations
- Track emerging market trends
- Assess term competitiveness
Analysis Framework:
1. Market Comparison:
- Stage-appropriate terms
- Industry-standard provisions
- Geographic variations
- Recent trend analysis
2. Competitive Assessment:
- Investor-friendliness rating
- Founder-friendliness rating
- Term flexibility analysis
- Market positioning
3. Trend Analysis:
- Emerging terms and conditions
- Shifting market standards
- Industry-specific adaptations
- Regional variations
Output Format:
- Market positioning summary
- Comparative analysis
- Trend implications
- Negotiation leverage points
- Recommended modifications""",
model_name="gpt-4o",
max_loops=1,
)
# Create agent list
agents = [
lead_analyst,
safe_specialist,
term_sheet_analyst,
legal_analyst,
market_analyst,
]
# Define analysis flow
flow = f"""{lead_analyst.agent_name} -> {safe_specialist.agent_name} -> {term_sheet_analyst.agent_name} -> {legal_analyst.agent_name} -> {market_analyst.agent_name}"""
# Create the swarm system
vc_analysis_system = AgentRearrange(
name="VC-Document-Analysis-Swarm",
description="SAFE and Term Sheet document analysis and Q&A system",
agents=agents,
flow=flow,
max_loops=1,
output_type="all",
)
# Example usage
if __name__ == "__main__":
try:
# Example document for analysis
document_text = """
SAFE AGREEMENT
Valuation Cap: $10,000,000
Discount Rate: 20%
Investment Amount: $500,000
Conversion Provisions:
- Automatic conversion upon Equity Financing of at least $1,000,000
- Optional conversion upon Liquidity Event
- Most Favored Nation provision included
Pro-rata Rights: Included for future rounds
"""
# Add timestamp to the analysis
analysis_request = f"Timestamp: {datetime.now()}\nDocument for Analysis: {document_text}"
# Run the analysis
analysis = vc_analysis_system.run(analysis_request)
# Create analysis report
create_file_in_folder(
"reports", "vc_document_analysis.md", analysis
)
except Exception as e:
print(f"An error occurred: {e}")

@ -0,0 +1,252 @@
"""
- For each diagnosis, pull lab results,
- egfr
- for each diagnosis, pull lab ranges,
- pull ranges for diagnosis
- if the diagnosis is x, then the lab ranges should be a to b
- train the agents, increase the load of input
- medical history sent to the agent
- setup rag for the agents
- run the first agent -> kidney disease -> don't know the stage -> stage 2 -> lab results -> indicative of stage 3 -> the case got elavated ->
- how to manage diseases and by looking at correlating lab, docs, diagnoses
- put docs in rag ->
- monitoring, evaluation, and treatment
- can we confirm for every diagnosis -> monitoring, evaluation, and treatment, specialized for these things
- find diagnosis -> or have diagnosis, -> for each diagnosis are there evidence of those 3 things
- swarm of those 4 agents, ->
- fda api for healthcare for commerically available papers
-
"""
from datetime import datetime
from swarms import Agent, AgentRearrange, create_file_in_folder
from swarm_models import OllamaModel
model = OllamaModel(model_name="llama3.2")
chief_medical_officer = Agent(
agent_name="Chief Medical Officer",
system_prompt="""You are the Chief Medical Officer coordinating a team of medical specialists for viral disease diagnosis.
Your responsibilities include:
- Gathering initial patient symptoms and medical history
- Coordinating with specialists to form differential diagnoses
- Synthesizing different specialist opinions into a cohesive diagnosis
- Ensuring all relevant symptoms and test results are considered
- Making final diagnostic recommendations
- Suggesting treatment plans based on team input
- Identifying when additional specialists need to be consulted
- For each diferrential diagnosis provide minimum lab ranges to meet that diagnosis or be indicative of that diagnosis minimum and maximum
Format all responses with clear sections for:
- Initial Assessment (include preliminary ICD-10 codes for symptoms)
- Differential Diagnoses (with corresponding ICD-10 codes)
- Specialist Consultations Needed
- Recommended Next Steps
""",
llm=model,
max_loops=1,
)
virologist = Agent(
agent_name="Virologist",
system_prompt="""You are a specialist in viral diseases. For each case, provide:
Clinical Analysis:
- Detailed viral symptom analysis
- Disease progression timeline
- Risk factors and complications
Coding Requirements:
- List relevant ICD-10 codes for:
* Confirmed viral conditions
* Suspected viral conditions
* Associated symptoms
* Complications
- Include both:
* Primary diagnostic codes
* Secondary condition codes
Document all findings using proper medical coding standards and include rationale for code selection.""",
llm=model,
max_loops=1,
)
internist = Agent(
agent_name="Internist",
system_prompt="""You are an Internal Medicine specialist responsible for comprehensive evaluation.
For each case, provide:
Clinical Assessment:
- System-by-system review
- Vital signs analysis
- Comorbidity evaluation
Medical Coding:
- ICD-10 codes for:
* Primary conditions
* Secondary diagnoses
* Complications
* Chronic conditions
* Signs and symptoms
- Include hierarchical condition category (HCC) codes where applicable
Document supporting evidence for each code selected.""",
llm=model,
max_loops=1,
)
medical_coder = Agent(
agent_name="Medical Coder",
system_prompt="""You are a certified medical coder responsible for:
Primary Tasks:
1. Reviewing all clinical documentation
2. Assigning accurate ICD-10 codes
3. Ensuring coding compliance
4. Documenting code justification
Coding Process:
- Review all specialist inputs
- Identify primary and secondary diagnoses
- Assign appropriate ICD-10 codes
- Document supporting evidence
- Note any coding queries
Output Format:
1. Primary Diagnosis Codes
- ICD-10 code
- Description
- Supporting documentation
2. Secondary Diagnosis Codes
- Listed in order of clinical significance
3. Symptom Codes
4. Complication Codes
5. Coding Notes""",
llm=model,
max_loops=1,
)
synthesizer = Agent(
agent_name="Diagnostic Synthesizer",
system_prompt="""You are responsible for creating the final diagnostic and coding assessment.
Synthesis Requirements:
1. Integrate all specialist findings
2. Reconcile any conflicting diagnoses
3. Verify coding accuracy and completeness
Final Report Sections:
1. Clinical Summary
- Primary diagnosis with ICD-10
- Secondary diagnoses with ICD-10
- Supporting evidence
2. Coding Summary
- Complete code list with descriptions
- Code hierarchy and relationships
- Supporting documentation
3. Recommendations
- Additional testing needed
- Follow-up care
- Documentation improvements needed
Include confidence levels and evidence quality for all diagnoses and codes.""",
llm=model,
max_loops=1,
)
# Create agent list
agents = [
chief_medical_officer,
virologist,
internist,
medical_coder,
synthesizer,
]
# Define diagnostic flow
flow = f"""{chief_medical_officer.agent_name} -> {virologist.agent_name} -> {internist.agent_name} -> {medical_coder.agent_name} -> {synthesizer.agent_name}"""
# Create the swarm system
diagnosis_system = AgentRearrange(
name="Medical-coding-diagnosis-swarm",
description="Comprehensive medical diagnosis and coding system",
agents=agents,
flow=flow,
max_loops=1,
output_type="all",
)
def generate_coding_report(diagnosis_output: str) -> str:
"""
Generate a structured medical coding report from the diagnosis output.
"""
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
report = f"""# Medical Diagnosis and Coding Report
Generated: {timestamp}
## Clinical Summary
{diagnosis_output}
## Coding Summary
### Primary Diagnosis Codes
[Extracted from synthesis]
### Secondary Diagnosis Codes
[Extracted from synthesis]
### Symptom Codes
[Extracted from synthesis]
### Procedure Codes (if applicable)
[Extracted from synthesis]
## Documentation and Compliance Notes
- Code justification
- Supporting documentation references
- Any coding queries or clarifications needed
## Recommendations
- Additional documentation needed
- Suggested follow-up
- Coding optimization opportunities
"""
return report
if __name__ == "__main__":
# Example patient case
patient_case = """
Patient: 45-year-old White Male
Lab Results:
- egfr
- 59 ml / min / 1.73
- non african-american
"""
# Add timestamp to the patient case
case_info = f"Timestamp: {datetime.now()}\nPatient Information: {patient_case}"
# Run the diagnostic process
diagnosis = diagnosis_system.run(case_info)
# Generate coding report
coding_report = generate_coding_report(diagnosis)
# Create reports
create_file_in_folder(
"reports", "medical_diagnosis_report.md", diagnosis
)
create_file_in_folder(
"reports", "medical_coding_report.md", coding_report
)

@ -0,0 +1,354 @@
from dataclasses import dataclass
from typing import List, Optional, Dict, Any
from datetime import datetime
import asyncio
from loguru import logger
import json
import base58
from decimal import Decimal
# Swarms imports
from swarms import Agent
# Solana imports
from solders.rpc.responses import GetTransactionResp
from solders.transaction import Transaction
from anchorpy import Provider, Wallet
from solders.keypair import Keypair
import aiohttp
# Specialized Solana Analysis System Prompt
SOLANA_ANALYSIS_PROMPT = """You are a specialized Solana blockchain analyst agent. Your role is to:
1. Analyze real-time Solana transactions for patterns and anomalies
2. Identify potential market-moving transactions and whale movements
3. Detect important DeFi interactions across major protocols
4. Monitor program interactions for suspicious or notable activity
5. Track token movements across significant protocols like:
- Serum DEX
- Raydium
- Orca
- Marinade
- Jupiter
- Other major Solana protocols
When analyzing transactions, consider:
- Transaction size relative to protocol norms
- Historical patterns for involved addresses
- Impact on protocol liquidity
- Relationship to known market events
- Potential wash trading or suspicious patterns
- MEV opportunities and arbitrage patterns
- Program interaction sequences
Provide analysis in the following format:
{
"analysis_type": "[whale_movement|program_interaction|defi_trade|suspicious_activity]",
"severity": "[high|medium|low]",
"details": {
"transaction_context": "...",
"market_impact": "...",
"recommended_actions": "...",
"related_patterns": "..."
}
}
Focus on actionable insights that could affect:
1. Market movements
2. Protocol stability
3. Trading opportunities
4. Risk management
"""
@dataclass
class TransactionData:
"""Data structure for parsed Solana transaction information"""
signature: str
block_time: datetime
slot: int
fee: int
lamports: int
from_address: str
to_address: str
program_id: str
instruction_data: Optional[str] = None
program_logs: List[str] = None
@property
def sol_amount(self) -> Decimal:
"""Convert lamports to SOL"""
return Decimal(self.lamports) / Decimal(1e9)
def to_dict(self) -> Dict[str, Any]:
"""Convert transaction data to dictionary for agent analysis"""
return {
"signature": self.signature,
"timestamp": self.block_time.isoformat(),
"slot": self.slot,
"fee": self.fee,
"amount_sol": str(self.sol_amount),
"from_address": self.from_address,
"to_address": self.to_address,
"program_id": self.program_id,
"instruction_data": self.instruction_data,
"program_logs": self.program_logs,
}
class SolanaSwarmAgent:
"""Intelligent agent for analyzing Solana transactions using swarms"""
def __init__(
self,
agent_name: str = "Solana-Analysis-Agent",
model_name: str = "gpt-4",
):
self.agent = Agent(
agent_name=agent_name,
system_prompt=SOLANA_ANALYSIS_PROMPT,
model_name=model_name,
max_loops=1,
autosave=True,
dashboard=False,
verbose=True,
dynamic_temperature_enabled=True,
saved_state_path="solana_agent.json",
user_name="solana_analyzer",
retry_attempts=3,
context_length=4000,
)
# Initialize known patterns database
self.known_patterns = {
"whale_addresses": set(),
"program_interactions": {},
"recent_transactions": [],
}
logger.info(
f"Initialized {agent_name} with specialized Solana analysis capabilities"
)
async def analyze_transaction(
self, tx_data: TransactionData
) -> Dict[str, Any]:
"""Analyze a transaction using the specialized agent"""
try:
# Update recent transactions for pattern analysis
self.known_patterns["recent_transactions"].append(
tx_data.signature
)
if len(self.known_patterns["recent_transactions"]) > 1000:
self.known_patterns["recent_transactions"].pop(0)
# Prepare context for agent
context = {
"transaction": tx_data.to_dict(),
"known_patterns": {
"recent_similar_transactions": [
tx
for tx in self.known_patterns[
"recent_transactions"
][-5:]
if abs(
TransactionData(tx).sol_amount
- tx_data.sol_amount
)
< 1
],
"program_statistics": self.known_patterns[
"program_interactions"
].get(tx_data.program_id, {}),
},
}
# Get analysis from agent
analysis = await self.agent.run_async(
f"Analyze the following Solana transaction and provide insights: {json.dumps(context, indent=2)}"
)
# Update pattern database
if tx_data.sol_amount > 1000: # Track whale addresses
self.known_patterns["whale_addresses"].add(
tx_data.from_address
)
# Update program interaction statistics
if (
tx_data.program_id
not in self.known_patterns["program_interactions"]
):
self.known_patterns["program_interactions"][
tx_data.program_id
] = {"total_interactions": 0, "total_volume": 0}
self.known_patterns["program_interactions"][
tx_data.program_id
]["total_interactions"] += 1
self.known_patterns["program_interactions"][
tx_data.program_id
]["total_volume"] += float(tx_data.sol_amount)
return json.loads(analysis)
except Exception as e:
logger.error(f"Error in agent analysis: {str(e)}")
return {
"analysis_type": "error",
"severity": "low",
"details": {
"error": str(e),
"transaction": tx_data.signature,
},
}
class SolanaTransactionMonitor:
"""Main class for monitoring and analyzing Solana transactions"""
def __init__(
self,
rpc_url: str,
swarm_agent: SolanaSwarmAgent,
min_sol_threshold: Decimal = Decimal("100"),
):
self.rpc_url = rpc_url
self.swarm_agent = swarm_agent
self.min_sol_threshold = min_sol_threshold
self.wallet = Wallet(Keypair())
self.provider = Provider(rpc_url, self.wallet)
logger.info("Initialized Solana transaction monitor")
async def parse_transaction(
self, tx_resp: GetTransactionResp
) -> Optional[TransactionData]:
"""Parse transaction response into TransactionData object"""
try:
if not tx_resp.value:
return None
tx_value = tx_resp.value
meta = tx_value.transaction.meta
if not meta:
return None
tx: Transaction = tx_value.transaction.transaction
# Extract transaction details
from_pubkey = str(tx.message.account_keys[0])
to_pubkey = str(tx.message.account_keys[1])
program_id = str(tx.message.account_keys[-1])
# Calculate amount from balance changes
amount = abs(meta.post_balances[0] - meta.pre_balances[0])
return TransactionData(
signature=str(tx_value.transaction.signatures[0]),
block_time=datetime.fromtimestamp(
tx_value.block_time or 0
),
slot=tx_value.slot,
fee=meta.fee,
lamports=amount,
from_address=from_pubkey,
to_address=to_pubkey,
program_id=program_id,
program_logs=(
meta.log_messages if meta.log_messages else []
),
)
except Exception as e:
logger.error(f"Failed to parse transaction: {str(e)}")
return None
async def start_monitoring(self):
"""Start monitoring for new transactions"""
logger.info(
"Starting transaction monitoring with swarm agent analysis"
)
async with aiohttp.ClientSession() as session:
async with session.ws_connect(self.rpc_url) as ws:
await ws.send_json(
{
"jsonrpc": "2.0",
"id": 1,
"method": "transactionSubscribe",
"params": [
{"commitment": "finalized"},
{
"encoding": "jsonParsed",
"commitment": "finalized",
},
],
}
)
async for msg in ws:
if msg.type == aiohttp.WSMsgType.TEXT:
try:
data = json.loads(msg.data)
if "params" in data:
signature = data["params"]["result"][
"value"
]["signature"]
# Fetch full transaction data
tx_response = await self.provider.connection.get_transaction(
base58.b58decode(signature)
)
if tx_response:
tx_data = (
await self.parse_transaction(
tx_response
)
)
if (
tx_data
and tx_data.sol_amount
>= self.min_sol_threshold
):
# Get agent analysis
analysis = await self.swarm_agent.analyze_transaction(
tx_data
)
logger.info(
f"Transaction Analysis:\n"
f"Signature: {tx_data.signature}\n"
f"Amount: {tx_data.sol_amount} SOL\n"
f"Analysis: {json.dumps(analysis, indent=2)}"
)
except Exception as e:
logger.error(
f"Error processing message: {str(e)}"
)
continue
async def main():
"""Example usage"""
# Start monitoring
try:
# Initialize swarm agent
swarm_agent = SolanaSwarmAgent(
agent_name="Solana-Whale-Detector", model_name="gpt-4"
)
# Initialize monitor
monitor = SolanaTransactionMonitor(
rpc_url="wss://api.mainnet-beta.solana.com",
swarm_agent=swarm_agent,
min_sol_threshold=Decimal("100"),
)
await monitor.start_monitoring()
except KeyboardInterrupt:
logger.info("Shutting down gracefully...")
if __name__ == "__main__":
asyncio.run(main())

@ -0,0 +1,164 @@
from swarms import Agent, SwarmRouter
# Portfolio Analysis Specialist
portfolio_analyzer = Agent(
agent_name="Portfolio-Analysis-Specialist",
system_prompt="""You are an expert portfolio analyst specializing in fund analysis and selection. Your core competencies include:
- Comprehensive analysis of mutual funds, ETFs, and index funds
- Evaluation of fund performance metrics (expense ratios, tracking error, Sharpe ratio)
- Assessment of fund composition and strategy alignment
- Risk-adjusted return analysis
- Tax efficiency considerations
For each portfolio analysis:
1. Evaluate fund characteristics and performance metrics
2. Analyze expense ratios and fee structures
3. Assess historical performance and volatility
4. Compare funds within same category
5. Consider tax implications
6. Review fund manager track record and strategy consistency
Maintain focus on cost-efficiency and alignment with investment objectives.""",
model_name="gpt-4o",
max_loops=1,
saved_state_path="portfolio_analyzer.json",
user_name="investment_team",
retry_attempts=2,
context_length=200000,
output_type="string",
)
# Asset Allocation Strategist
allocation_strategist = Agent(
agent_name="Asset-Allocation-Strategist",
system_prompt="""You are a specialized asset allocation strategist focused on portfolio construction and optimization. Your expertise includes:
- Strategic and tactical asset allocation
- Risk tolerance assessment and portfolio matching
- Geographic and sector diversification
- Rebalancing strategy development
- Portfolio optimization using modern portfolio theory
For each allocation:
1. Analyze investor risk tolerance and objectives
2. Develop appropriate asset class weights
3. Select optimal fund combinations
4. Design rebalancing triggers and schedules
5. Consider tax-efficient fund placement
6. Account for correlation between assets
Focus on creating well-diversified portfolios aligned with client goals and risk tolerance.""",
model_name="gpt-4o",
max_loops=1,
saved_state_path="allocation_strategist.json",
user_name="investment_team",
retry_attempts=2,
context_length=200000,
output_type="string",
)
# Risk Management Specialist
risk_manager = Agent(
agent_name="Risk-Management-Specialist",
system_prompt="""You are a risk management specialist focused on portfolio risk assessment and mitigation. Your expertise covers:
- Portfolio risk metrics analysis
- Downside protection strategies
- Correlation analysis between funds
- Stress testing and scenario analysis
- Market condition impact assessment
For each portfolio:
1. Calculate key risk metrics (Beta, Standard Deviation, etc.)
2. Analyze correlation matrices
3. Perform stress tests under various scenarios
4. Evaluate liquidity risks
5. Assess concentration risks
6. Monitor factor exposures
Focus on maintaining appropriate risk levels while maximizing risk-adjusted returns.""",
model_name="gpt-4o",
max_loops=1,
saved_state_path="risk_manager.json",
user_name="investment_team",
retry_attempts=2,
context_length=200000,
output_type="string",
)
# Portfolio Implementation Specialist
implementation_specialist = Agent(
agent_name="Portfolio-Implementation-Specialist",
system_prompt="""You are a portfolio implementation specialist focused on efficient execution and maintenance. Your responsibilities include:
- Fund selection for specific asset class exposure
- Tax-efficient implementation strategies
- Portfolio rebalancing execution
- Trading cost analysis
- Cash flow management
For each implementation:
1. Select most efficient funds for desired exposure
2. Plan tax-efficient transitions
3. Design rebalancing schedule
4. Optimize trade execution
5. Manage cash positions
6. Monitor tracking error
Maintain focus on minimizing costs and maximizing tax efficiency during implementation.""",
model_name="gpt-4o",
max_loops=1,
saved_state_path="implementation_specialist.json",
user_name="investment_team",
retry_attempts=2,
context_length=200000,
output_type="string",
)
# Portfolio Monitoring Specialist
monitoring_specialist = Agent(
agent_name="Portfolio-Monitoring-Specialist",
system_prompt="""You are a portfolio monitoring specialist focused on ongoing portfolio oversight and optimization. Your expertise includes:
- Regular portfolio performance review
- Drift monitoring and rebalancing triggers
- Fund changes and replacements
- Tax loss harvesting opportunities
- Performance attribution analysis
For each review:
1. Track portfolio drift from targets
2. Monitor fund performance and changes
3. Identify tax loss harvesting opportunities
4. Analyze tracking error and expenses
5. Review risk metrics evolution
6. Generate performance attribution reports
Ensure continuous alignment with investment objectives while maintaining optimal portfolio efficiency.""",
model_name="gpt-4o",
max_loops=1,
saved_state_path="monitoring_specialist.json",
user_name="investment_team",
retry_attempts=2,
context_length=200000,
output_type="string",
)
# List of all agents for portfolio management
portfolio_agents = [
portfolio_analyzer,
allocation_strategist,
risk_manager,
implementation_specialist,
monitoring_specialist,
]
# Router
router = SwarmRouter(
name="etf-portfolio-management-swarm",
description="Creates and suggests an optimal portfolio",
agents=portfolio_agents,
swarm_type="SequentialWorkflow", # ConcurrentWorkflow
max_loops=1,
)
router.run(
task="I have 10,000$ and I want to create a porfolio based on energy, ai, and datacenter companies. high growth."
)

@ -5,8 +5,8 @@ build-backend = "poetry.core.masonry.api"
[tool.poetry]
name = "swarms"
version = "6.4.7"
description = "Swarms - Pytorch"
version = "6.6.9"
description = "Swarms - TGSC"
license = "MIT"
authors = ["Kye Gomez <kye@apac.ai>"]
homepage = "https://github.com/kyegomez/swarms"
@ -57,28 +57,53 @@ classifiers = [
[tool.poetry.dependencies]
python = ">=3.10,<4.0"
torch = ">=2.1.1,<3.0"
transformers = ">= 4.39.0, <5.0.0"
# torch = ">=2.1.1,<3.0"
# transformers = ">= 4.39.0, <5.0.0"
asyncio = ">=3.4.3,<4.0"
toml = "*"
pypdf = "5.1.0"
loguru = "*"
pydantic = "2.8.2"
pydantic = "*"
tenacity = "*"
psutil = "*"
sentry-sdk = {version = "*", extras = ["http"]} # Updated here
sentry-sdk = "*"
python-dotenv = "*"
PyYAML = "*"
docstring_parser = "0.16"
tiktoken = "*"
networkx = "*"
aiofiles = "*"
swarm-models = "*"
clusterops = "*"
chromadb = "*"
# chromadb = "*"
reportlab = "*"
doc-master = "*"
rich = "*"
# sentence-transformers = "*"
swarm-models = "*"
# [tool.poetry.extras]
# # Extra for NLP-related functionalities
# nlp = [
# "torch>=2.1.1,<3.0",
# "transformers>=4.39.0,<5.0.0",
# "sentence-transformers",
# "swarm-models",
# ]
# # Extra for database-related functionalities
# db = ["chromadb"]
# # All optional dependencies for convenience
# all = [
# "torch>=2.1.1,<3.0",
# "transformers>=4.39.0,<5.0.0",
# "sentence-transformers",
# "chromadb",
# "swarm-models"
# ]
[tool.poetry.scripts]
swarms = "swarms.cli.main:main"
@ -86,7 +111,7 @@ swarms = "swarms.cli.main:main"
[tool.poetry.group.lint.dependencies]
black = ">=23.1,<25.0"
ruff = ">=0.5.1,<0.8.2"
ruff = ">=0.5.1,<0.8.3"
types-toml = "^0.10.8.1"
types-pytz = ">=2023.3,<2025.0"
types-chardet = "^5.0.4.6"

@ -3,7 +3,7 @@ torch>=2.1.1,<3.0
transformers>=4.39.0,<5.0.0
asyncio>=3.4.3,<4.0
toml
pypdf==4.3.1
pypdf==5.1.0
ratelimit==2.2.1
loguru
pydantic==2.8.2
@ -24,7 +24,6 @@ pytest>=8.1.1
pandas>=2.2.2
networkx
aiofiles
swarm-models
clusterops
reportlab
doc-master

@ -1,121 +0,0 @@
import json
import os
from difflib import SequenceMatcher
import requests
from dotenv import load_dotenv
from loguru import logger
from supabase import Client, create_client
load_dotenv()
# Initialize Supabase client
SUPABASE_URL = os.getenv("SUPABASE_URL")
SUPABASE_KEY = os.getenv("SUPABASE_KEY")
supabase: Client = create_client(SUPABASE_URL, SUPABASE_KEY)
# Swarms API URL and headers
SWARMS_API_URL = "https://swarms.world/api/add-prompt"
SWARMS_API_KEY = os.getenv("SWARMS_API_KEY")
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {SWARMS_API_KEY}",
}
# Configure logger
logger.add(
"fetch_and_publish_prompts.log", rotation="1 MB"
) # Log file with rotation
def fetch_and_publish_prompts():
logger.info("Starting to fetch and publish prompts.")
# Fetch data from Supabase
try:
response = (
supabase.table("swarms_framework_schema")
.select("*")
.execute()
)
rows = response.data
logger.info(f"Fetched {len(rows)} rows from Supabase.")
except Exception as e:
logger.error(f"Failed to fetch data from Supabase: {e}")
return
# Track published prompts to avoid duplicates
published_prompts = set()
for row in rows:
# Extract agent_name and system_prompt
data = row.get("data", {})
agent_name = data.get("agent_name")
system_prompt = data.get("system_prompt")
# Skip if either is missing or duplicate
if not agent_name or not system_prompt:
logger.warning(
f"Skipping row due to missing agent_name or system_prompt: {row}"
)
continue
if is_duplicate(system_prompt, published_prompts):
logger.info(
f"Skipping duplicate prompt for agent: {agent_name}"
)
continue
# Create the data payload for the marketplace
prompt_data = {
"name": f"{agent_name} - System Prompt",
"prompt": system_prompt,
"description": f"System prompt for agent {agent_name}.",
"useCases": extract_use_cases(system_prompt),
"tags": "agent, system-prompt",
}
# Publish to the marketplace
try:
response = requests.post(
SWARMS_API_URL,
headers=headers,
data=json.dumps(prompt_data),
)
if response.status_code == 200:
logger.info(
f"Successfully published prompt for agent: {agent_name}"
)
published_prompts.add(system_prompt)
else:
logger.error(
f"Failed to publish prompt for agent: {agent_name}. Response: {response.text}"
)
except Exception as e:
logger.error(
f"Exception occurred while publishing prompt for agent: {agent_name}. Error: {e}"
)
def is_duplicate(new_prompt, published_prompts):
"""Check if the prompt is a duplicate using semantic similarity."""
for prompt in published_prompts:
similarity = SequenceMatcher(None, new_prompt, prompt).ratio()
if (
similarity > 0.9
): # Threshold for considering prompts as duplicates
return True
return False
def extract_use_cases(prompt):
"""Extract use cases from the prompt by chunking it into meaningful segments."""
# This is a simple placeholder; you can use a more advanced method to extract use cases
chunks = [prompt[i : i + 50] for i in range(0, len(prompt), 50)]
return [
{"title": f"Use case {idx+1}", "description": chunk}
for idx, chunk in enumerate(chunks)
]
# Main execution
fetch_and_publish_prompts()

@ -1,121 +0,0 @@
import json
import os
from difflib import SequenceMatcher
import requests
from dotenv import load_dotenv
from loguru import logger
from supabase import Client, create_client
load_dotenv()
# Initialize Supabase client
SUPABASE_URL = os.getenv("SUPABASE_URL")
SUPABASE_KEY = os.getenv("SUPABASE_KEY")
supabase: Client = create_client(SUPABASE_URL, SUPABASE_KEY)
# Swarms API URL and headers
SWARMS_API_URL = "https://swarms.world/api/add-prompt"
SWARMS_API_KEY = os.getenv("SWARMS_API_KEY")
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {SWARMS_API_KEY}",
}
# Configure logger
logger.add(
"fetch_and_publish_prompts.log", rotation="1 MB"
) # Log file with rotation
def fetch_and_publish_prompts():
logger.info("Starting to fetch and publish prompts.")
# Fetch data from Supabase
try:
response = (
supabase.table("swarms_framework_schema")
.select("*")
.execute()
)
rows = response.data
logger.info(f"Fetched {len(rows)} rows from Supabase.")
except Exception as e:
logger.error(f"Failed to fetch data from Supabase: {e}")
return
# Track published prompts to avoid duplicates
published_prompts = set()
for row in rows:
# Extract agent_name and system_prompt
data = row.get("data", {})
agent_name = data.get("agent_name")
system_prompt = data.get("system_prompt")
# Skip if either is missing or duplicate
if not agent_name or not system_prompt:
logger.warning(
f"Skipping row due to missing agent_name or system_prompt: {row}"
)
continue
if is_duplicate(system_prompt, published_prompts):
logger.info(
f"Skipping duplicate prompt for agent: {agent_name}"
)
continue
# Create the data payload for the marketplace
prompt_data = {
"name": f"{agent_name} - System Prompt",
"prompt": system_prompt,
"description": f"System prompt for agent {agent_name}.",
"useCases": extract_use_cases(system_prompt),
"tags": "agent, system-prompt",
}
# Publish to the marketplace
try:
response = requests.post(
SWARMS_API_URL,
headers=headers,
data=json.dumps(prompt_data),
)
if response.status_code == 200:
logger.info(
f"Successfully published prompt for agent: {agent_name}"
)
published_prompts.add(system_prompt)
else:
logger.error(
f"Failed to publish prompt for agent: {agent_name}. Response: {response.text}"
)
except Exception as e:
logger.error(
f"Exception occurred while publishing prompt for agent: {agent_name}. Error: {e}"
)
def is_duplicate(new_prompt, published_prompts):
"""Check if the prompt is a duplicate using semantic similarity."""
for prompt in published_prompts:
similarity = SequenceMatcher(None, new_prompt, prompt).ratio()
if (
similarity > 0.9
): # Threshold for considering prompts as duplicates
return True
return False
def extract_use_cases(prompt):
"""Extract use cases from the prompt by chunking it into meaningful segments."""
# This is a simple placeholder; you can use a more advanced method to extract use cases
chunks = [prompt[i : i + 50] for i in range(0, len(prompt), 50)]
return [
{"title": f"Use case {idx+1}", "description": chunk}
for idx, chunk in enumerate(chunks)
]
# Main execution
fetch_and_publish_prompts()

@ -4,4 +4,6 @@ Agent(
agent_name="Stock-Analysis-Agent",
model_name="gpt-4o-mini",
max_loops=1,
interactive=False,
streaming_on=True,
).run("What are 5 hft algorithms")

@ -0,0 +1,333 @@
import os
from typing import List, Optional
from datetime import datetime
from pydantic import BaseModel, Field
from pydantic.v1 import validator
from loguru import logger
from tenacity import (
retry,
stop_after_attempt,
wait_exponential,
)
from swarm_models import OpenAIFunctionCaller, OpenAIChat
from swarms.structs.agent import Agent
from swarms.structs.swarm_router import SwarmRouter
from swarms.structs.agents_available import showcase_available_agents
BOSS_SYSTEM_PROMPT = """
Manage a swarm of worker agents to efficiently serve the user by deciding whether to create new agents or delegate tasks. Ensure operations are efficient and effective.
### Instructions:
1. **Task Assignment**:
- Analyze available worker agents when a task is presented.
- Delegate tasks to existing agents with clear, direct, and actionable instructions if an appropriate agent is available.
- If no suitable agent exists, create a new agent with a fitting system prompt to handle the task.
2. **Agent Creation**:
- Name agents according to the task they are intended to perform (e.g., "Twitter Marketing Agent").
- Provide each new agent with a concise and clear system prompt that includes its role, objectives, and any tools it can utilize.
3. **Efficiency**:
- Minimize redundancy and maximize task completion speed.
- Avoid unnecessary agent creation if an existing agent can fulfill the task.
4. **Communication**:
- Be explicit in task delegation instructions to avoid ambiguity and ensure effective task execution.
- Require agents to report back on task completion or encountered issues.
5. **Reasoning and Decisions**:
- Offer brief reasoning when selecting or creating agents to maintain transparency.
- Avoid using an agent if unnecessary, with a clear explanation if no agents are suitable for a task.
# Output Format
Present your plan in clear, bullet-point format or short concise paragraphs, outlining task assignment, agent creation, efficiency strategies, and communication protocols.
# Notes
- Preserve transparency by always providing reasoning for task-agent assignments and creation.
- Ensure instructions to agents are unambiguous to minimize error.
"""
class AgentConfig(BaseModel):
"""Configuration for an individual agent in a swarm"""
name: str = Field(
description="The name of the agent", example="Research-Agent"
)
description: str = Field(
description="A description of the agent's purpose and capabilities",
example="Agent responsible for researching and gathering information",
)
system_prompt: str = Field(
description="The system prompt that defines the agent's behavior",
example="You are a research agent. Your role is to gather and analyze information...",
)
@validator("name")
def validate_name(cls, v):
if not v.strip():
raise ValueError("Agent name cannot be empty")
return v.strip()
@validator("system_prompt")
def validate_system_prompt(cls, v):
if not v.strip():
raise ValueError("System prompt cannot be empty")
return v.strip()
class SwarmConfig(BaseModel):
"""Configuration for a swarm of cooperative agents"""
name: str = Field(
description="The name of the swarm",
example="Research-Writing-Swarm",
)
description: str = Field(
description="The description of the swarm's purpose and capabilities",
example="A swarm of agents that work together to research topics and write articles",
)
agents: List[AgentConfig] = Field(
description="The list of agents that make up the swarm",
min_items=1,
)
@validator("agents")
def validate_agents(cls, v):
if not v:
raise ValueError("Swarm must have at least one agent")
return v
class AutoSwarmBuilder:
"""A class that automatically builds and manages swarms of AI agents with enhanced error handling."""
def __init__(
self,
name: Optional[str] = None,
description: Optional[str] = None,
verbose: bool = True,
api_key: Optional[str] = None,
model_name: str = "gpt-4",
):
self.name = name or "DefaultSwarm"
self.description = description or "Generic AI Agent Swarm"
self.verbose = verbose
self.agents_pool = []
self.api_key = api_key or os.getenv("OPENAI_API_KEY")
self.model_name = model_name
if not self.api_key:
raise ValueError(
"OpenAI API key must be provided either through initialization or environment variable"
)
logger.info(
"Initialized AutoSwarmBuilder",
extra={
"swarm_name": self.name,
"description": self.description,
"model": self.model_name,
},
)
# Initialize OpenAI chat model
try:
self.chat_model = OpenAIChat(
openai_api_key=self.api_key,
model_name=self.model_name,
temperature=0.1,
)
except Exception as e:
logger.error(
f"Failed to initialize OpenAI chat model: {str(e)}"
)
raise
@retry(
stop=stop_after_attempt(3),
wait=wait_exponential(multiplier=1, min=4, max=10),
)
def run(self, task: str, image_url: Optional[str] = None) -> str:
"""Run the swarm on a given task with error handling and retries."""
if not task or not task.strip():
raise ValueError("Task cannot be empty")
logger.info("Starting swarm execution", extra={"task": task})
try:
# Create agents for the task
agents = self._create_agents(task, image_url)
if not agents:
raise ValueError(
"No agents were created for the task"
)
# Execute the task through the swarm router
logger.info(
"Routing task through swarm",
extra={"num_agents": len(agents)},
)
output = self.swarm_router(agents, task, image_url)
logger.info("Swarm execution completed successfully")
return output
except Exception as e:
logger.error(
f"Error during swarm execution: {str(e)}",
exc_info=True,
)
raise
def _create_agents(
self, task: str, image_url: Optional[str] = None
) -> List[Agent]:
"""Create the necessary agents for a task with enhanced error handling."""
logger.info("Creating agents for task", extra={"task": task})
try:
model = OpenAIFunctionCaller(
system_prompt=BOSS_SYSTEM_PROMPT,
api_key=self.api_key,
temperature=0.1,
base_model=SwarmConfig,
)
agents_config = model.run(task)
print(f"{agents_config}")
if isinstance(agents_config, dict):
agents_config = SwarmConfig(**agents_config)
# Update swarm configuration
self.name = agents_config.name
self.description = agents_config.description
# Create agents from configuration
agents = []
for agent_config in agents_config.agents:
if isinstance(agent_config, dict):
agent_config = AgentConfig(**agent_config)
agent = self.build_agent(
agent_name=agent_config.name,
agent_description=agent_config.description,
agent_system_prompt=agent_config.system_prompt,
)
agents.append(agent)
# Add available agents showcase to system prompts
agents_available = showcase_available_agents(
name=self.name,
description=self.description,
agents=agents,
)
for agent in agents:
agent.system_prompt += "\n" + agents_available
logger.info(
"Successfully created agents",
extra={"num_agents": len(agents)},
)
return agents
except Exception as e:
logger.error(
f"Error creating agents: {str(e)}", exc_info=True
)
raise
def build_agent(
self,
agent_name: str,
agent_description: str,
agent_system_prompt: str,
) -> Agent:
"""Build a single agent with enhanced error handling."""
logger.info(
"Building agent", extra={"agent_name": agent_name}
)
try:
agent = Agent(
agent_name=agent_name,
description=agent_description,
system_prompt=agent_system_prompt,
llm=self.chat_model,
autosave=True,
dashboard=False,
verbose=self.verbose,
dynamic_temperature_enabled=True,
saved_state_path=f"states/{agent_name}_{datetime.now().strftime('%Y%m%d_%H%M%S')}.json",
user_name="swarms_corp",
retry_attempts=3,
context_length=200000,
return_step_meta=False,
output_type="str",
streaming_on=False,
auto_generate_prompt=True,
)
return agent
except Exception as e:
logger.error(
f"Error building agent: {str(e)}", exc_info=True
)
raise
@retry(
stop=stop_after_attempt(3),
wait=wait_exponential(multiplier=1, min=4, max=10),
)
def swarm_router(
self,
agents: List[Agent],
task: str,
image_url: Optional[str] = None,
) -> str:
"""Route tasks between agents in the swarm with error handling and retries."""
logger.info(
"Initializing swarm router",
extra={"num_agents": len(agents)},
)
try:
swarm_router_instance = SwarmRouter(
name=self.name,
description=self.description,
agents=agents,
swarm_type="auto",
)
formatted_task = f"{self.name} {self.description} {task}"
result = swarm_router_instance.run(formatted_task)
logger.info("Successfully completed swarm routing")
return result
except Exception as e:
logger.error(
f"Error in swarm router: {str(e)}", exc_info=True
)
raise
swarm = AutoSwarmBuilder(
name="ChipDesign-Swarm",
description="A swarm of specialized AI agents for chip design",
api_key="your-api-key", # Optional if set in environment
model_name="gpt-4", # Optional, defaults to gpt-4
)
result = swarm.run(
"Design a new AI accelerator chip optimized for transformer model inference..."
)

@ -1,4 +1,4 @@
from swarms.agents.stopping_conditions import (
from swarms.structs.stopping_conditions import (
check_cancelled,
check_complete,
check_done,

@ -8,7 +8,7 @@ from swarms.agents.create_agents_from_yaml import (
create_agents_from_yaml,
)
from swarms.utils.formatter import formatter
from swarms.utils.litellm import LiteLLM
from swarms.utils.litellm_wrapper import LiteLLM
load_dotenv()

@ -16,7 +16,7 @@ from pydantic import (
from swarms.utils.loguru_logger import initialize_logger
from swarms.structs.agent import Agent
from swarms.structs.swarm_router import SwarmRouter
from swarms.utils.litellm import LiteLLM
from swarms.utils.litellm_wrapper import LiteLLM
logger = initialize_logger(log_folder="create_agents_from_yaml")

@ -1,10 +1,12 @@
from typing import Any, Optional, Callable
from swarms.tools.json_former import Jsonformer
from swarms.utils.loguru_logger import initialize_logger
from swarms.utils.lazy_loader import lazy_import_decorator
logger = initialize_logger(log_folder="tool_agent")
@lazy_import_decorator
class ToolAgent:
"""
Represents a tool agent that performs a specific task using a model and tokenizer.

@ -2,7 +2,8 @@ import time
import os
import json
from typing import List, Union, Dict, Any
from pydantic import BaseModel, Field, validator
from pydantic import BaseModel, Field
from pydantic.v1 import validator
from datetime import datetime
from swarms.utils.file_processing import create_file_in_folder
from swarms.utils.loguru_logger import initialize_logger

@ -1,84 +1,43 @@
import os
from swarms.structs.agent import Agent
from swarm_models.popular_llms import OpenAIChat
from swarms.structs.agent_registry import AgentRegistry
# 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
)
# Registry of agents
agent_registry = AgentRegistry(
name="Swarms CLI",
description="A registry of agents for the Swarms CLI",
)
def create_agent(name: str, system_prompt: str, max_loops: int = 1):
# Run the agents in the registry
def run_agent_by_name(
name: str,
system_prompt: str,
model_name: str,
max_loops: int,
task: str,
img: str,
*args,
**kwargs,
):
"""
Create and initialize an agent with the given parameters.
This function creates an Agent instance and runs a task on it.
Args:
name (str): The name of the agent.
system_prompt (str): The system prompt for the agent.
max_loops (int, optional): The maximum number of loops the agent can perform. Defaults to 1.
model_name (str): The name of the model used by the agent.
max_loops (int): The maximum number of loops the agent can run.
task (str): The task to be run by the agent.
*args: Variable length arguments.
**kwargs: Keyword arguments.
Returns:
Agent: The initialized agent.
The output of the task run by the agent.
"""
# Initialize the agent
agent = Agent(
agent_name=name,
system_prompt=system_prompt,
llm=model,
max_loops=max_loops,
autosave=True,
dashboard=False,
verbose=True,
dynamic_temperature_enabled=True,
saved_state_path=f"{name}.json",
user_name="swarms_corp",
retry_attempts=1,
context_length=200000,
# return_step_meta=True,
# disable_print_every_step=True,
# output_type="json",
interactive=True,
)
agent_registry.add(agent)
return agent
# Run the agents in the registry
def run_agent_by_name(name: str, task: str, *args, **kwargs):
"""
Run an agent by its name and perform a specified task.
Parameters:
- name (str): The name of the agent.
- task (str): The task to be performed by the agent.
- *args: Variable length argument list.
- **kwargs: Arbitrary keyword arguments.
Returns:
- output: The output of the agent's task.
"""
agent = agent_registry.get_agent_by_name(name)
output = agent.run(task, *args, **kwargs)
return output
# # Test
# out = create_agent("Accountant1", "Prepares financial statements")
# print(out)
try:
agent = Agent(
agent_name=name,
system_prompt=system_prompt,
model_name=model_name,
max_loops=max_loops,
)
output = agent.run(task=task, img=img, *args, **kwargs)
return output
except Exception as e:
print(f"An error occurred: {str(e)}")
return None

@ -1,6 +1,5 @@
import argparse
import os
import subprocess
import time
import webbrowser
@ -227,45 +226,6 @@ def run_autoswarm(task: str, model: str):
)
def check_and_upgrade_version():
"""Check for updates with visual progress."""
def check_update():
result = subprocess.run(
["pip", "list", "--outdated", "--format=freeze"],
capture_output=True,
text=True,
)
return result.stdout.splitlines()
outdated = execute_with_spinner(
check_update, "Checking for updates..."
)
for package in outdated:
if package.startswith("swarms=="):
console.print(
f"[{COLORS['warning']}]↑ Update available![/{COLORS['warning']}]"
)
with create_spinner("Upgrading Swarms...") as progress:
task = progress.add_task(
"Installing latest version..."
)
subprocess.run(
["pip", "install", "--upgrade", "swarms"],
check=True,
)
progress.remove_task(task)
console.print(
f"[{COLORS['success']}]✓ Swarms upgraded successfully![/{COLORS['success']}]"
)
return
console.print(
f"[{COLORS['success']}]✓ Swarms is up to date![/{COLORS['success']}]"
)
def main():
try:
@ -319,8 +279,6 @@ def main():
create_agents_from_yaml(
yaml_file=args.yaml_file, return_type="tasks"
)
elif args.command == "auto-upgrade":
check_and_upgrade_version()
elif args.command == "book-call":
webbrowser.open(
"https://cal.com/swarms/swarms-strategy-session"

@ -19,7 +19,6 @@ from swarms.structs.majority_voting import (
most_frequent,
parse_code_completion,
)
from swarms.structs.message import Message
from swarms.structs.mixture_of_agents import MixtureOfAgents
from swarms.structs.multi_agent_collab import MultiAgentCollaboration
from swarms.structs.multi_agent_exec import (
@ -39,7 +38,6 @@ from swarms.structs.round_robin import RoundRobinSwarm
from swarms.structs.sequential_workflow import SequentialWorkflow
from swarms.structs.spreadsheet_swarm import SpreadSheetSwarm
from swarms.structs.swarm_arange import SwarmRearrange
from swarms.structs.swarm_net import SwarmNetwork
from swarms.structs.swarm_router import (
SwarmRouter,
SwarmType,
@ -108,9 +106,7 @@ __all__ = [
"majority_voting",
"most_frequent",
"parse_code_completion",
"Message",
"MultiAgentCollaboration",
"SwarmNetwork",
"AgentRearrange",
"rearrange",
"RoundRobinSwarm",

@ -598,7 +598,7 @@ class Agent:
def llm_handling(self):
if self.llm is None:
from swarms.utils.litellm import LiteLLM
from swarms.utils.litellm_wrapper import LiteLLM
if self.llm_args is not None:
self.llm = LiteLLM(
@ -2388,36 +2388,42 @@ class Agent:
) -> None:
"""Handle creating and saving artifacts with error handling."""
try:
logger.info(
f"Creating artifact for file: {file_output_path}"
)
# Ensure file_extension starts with a dot
if not file_extension.startswith('.'):
file_extension = '.' + file_extension
# If file_output_path doesn't have an extension, treat it as a directory
# and create a default filename based on timestamp
if not os.path.splitext(file_output_path)[1]:
timestamp = time.strftime("%Y%m%d_%H%M%S")
filename = f"artifact_{timestamp}{file_extension}"
full_path = os.path.join(file_output_path, filename)
else:
full_path = file_output_path
# Create the directory if it doesn't exist
os.makedirs(os.path.dirname(full_path), exist_ok=True)
logger.info(f"Creating artifact for file: {full_path}")
artifact = Artifact(
file_path=file_output_path,
file_path=full_path,
file_type=file_extension,
contents=text,
edit_count=0,
)
logger.info(
f"Saving artifact with extension: {file_extension}"
)
logger.info(f"Saving artifact with extension: {file_extension}")
artifact.save_as(file_extension)
logger.success(
f"Successfully saved artifact to {file_output_path}"
)
logger.success(f"Successfully saved artifact to {full_path}")
except ValueError as e:
logger.error(
f"Invalid input values for artifact: {str(e)}"
)
logger.error(f"Invalid input values for artifact: {str(e)}")
raise
except IOError as e:
logger.error(f"Error saving artifact to file: {str(e)}")
raise
except Exception as e:
logger.error(
f"Unexpected error handling artifact: {str(e)}"
)
logger.error(f"Unexpected error handling artifact: {str(e)}")
raise
def showcase_config(self):

@ -1,14 +1,19 @@
from typing import List, Optional
import chromadb
from tenacity import retry, stop_after_attempt, wait_exponential
from typing import Union, Callable, Any
from swarms import Agent
from swarms.utils.loguru_logger import initialize_logger
from swarms.utils.lazy_loader import lazy_import_decorator
from swarms.utils.auto_download_check_packages import (
auto_check_and_download_package,
)
logger = initialize_logger(log_folder="agent_router")
@lazy_import_decorator
class AgentRouter:
"""
Initialize the AgentRouter.
@ -29,6 +34,14 @@ class AgentRouter:
*args,
**kwargs,
):
try:
import chromadb
except ImportError:
auto_check_and_download_package(
"chromadb", package_manager="pip", upgrade=True
)
import chromadb
self.collection_name = collection_name
self.n_agents = n_agents
self.persist_directory = persist_directory

@ -50,13 +50,11 @@ class SwarmConfig(BaseModel):
name="Research-Agent",
description="Gathers information",
system_prompt="You are a research agent...",
max_loops=2,
),
AgentConfig(
name="Writing-Agent",
description="Writes content",
system_prompt="You are a writing agent...",
max_loops=1,
),
],
)
@ -195,7 +193,7 @@ class AutoSwarmBuilder:
self.name = agents_dictionary.name
self.description = agents_dictionary.description
self.max_loops = getattr(
agents_dictionary, "max_loops", 1
agents_dictionary
) # Default to 1 if not set
logger.info(
@ -213,7 +211,6 @@ class AutoSwarmBuilder:
agent_name=agent_config.name,
agent_description=agent_config.description,
agent_system_prompt=agent_config.system_prompt,
# max_loops=agent_config.max_loops,
)
agents.append(agent)

@ -16,7 +16,6 @@ from typing import (
import yaml
from swarms_memory import BaseVectorDatabase
from swarms.structs.agent import Agent
from swarms.structs.conversation import Conversation
from swarms.structs.omni_agent_types import AgentType
@ -98,9 +97,7 @@ class BaseSwarm(ABC):
agentops_on: Optional[bool] = False,
speaker_selection_func: Optional[Callable] = None,
rules: Optional[str] = None,
collective_memory_system: Optional[
BaseVectorDatabase
] = False,
collective_memory_system: Optional[Any] = False,
agent_ops_on: bool = False,
output_schema: Optional[BaseModel] = None,
*args,

@ -1,10 +1,3 @@
"""
GraphSwarm: A production-grade framework for orchestrating swarms of agents
Author: Claude
License: MIT
Version: 2.0.0
"""
import asyncio
import json
import time
@ -12,13 +5,13 @@ from concurrent.futures import ThreadPoolExecutor
from datetime import datetime
from typing import Any, Dict, List, Optional, Tuple, Union
import chromadb
import networkx as nx
from loguru import logger
from pydantic import BaseModel, Field
from swarms import Agent
from swarms.utils.auto_download_check_packages import (
auto_check_and_download_package,
)
from swarms.structs.agent import Agent
# Configure logging
logger.add(
@ -57,6 +50,15 @@ class SwarmMemory:
def __init__(self, collection_name: str = "swarm_memories"):
"""Initialize SwarmMemory with ChromaDB."""
try:
import chromadb
except ImportError:
auto_check_and_download_package(
"chromadb", package_manager="pip", upgrade=True
)
import chromadb
self.client = chromadb.Client()
# Get or create collection

@ -3,7 +3,6 @@ import asyncio
from pydantic import BaseModel, Field
from typing import List, Dict, Any
from swarms import Agent
from swarm_models import OpenAIChat
from dotenv import load_dotenv
from swarms.utils.formatter import formatter
@ -181,64 +180,64 @@ class GroupChat:
]
# Example Usage
if __name__ == "__main__":
load_dotenv()
# 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(
openai_api_key=api_key,
model_name="gpt-4o-mini",
temperature=0.1,
)
# Example agents
agent1 = Agent(
agent_name="Financial-Analysis-Agent",
system_prompt="You are a financial analyst specializing in investment strategies.",
llm=model,
max_loops=1,
autosave=False,
dashboard=False,
verbose=True,
dynamic_temperature_enabled=True,
user_name="swarms_corp",
retry_attempts=1,
context_length=200000,
output_type="string",
streaming_on=False,
)
agent2 = Agent(
agent_name="Tax-Adviser-Agent",
system_prompt="You are a tax adviser who provides clear and concise guidance on tax-related queries.",
llm=model,
max_loops=1,
autosave=False,
dashboard=False,
verbose=True,
dynamic_temperature_enabled=True,
user_name="swarms_corp",
retry_attempts=1,
context_length=200000,
output_type="string",
streaming_on=False,
)
# Create group chat
group_chat = GroupChat(
name="Financial Discussion",
description="A group chat for financial analysis and tax advice.",
agents=[agent1, agent2],
)
# Run the group chat
asyncio.run(
group_chat.run(
"How can I establish a ROTH IRA to buy stocks and get a tax break? What are the criteria? What do you guys think?"
)
)
# # Example Usage
# if __name__ == "__main__":
# load_dotenv()
# # 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(
# openai_api_key=api_key,
# model_name="gpt-4o-mini",
# temperature=0.1,
# )
# # Example agents
# agent1 = Agent(
# agent_name="Financial-Analysis-Agent",
# system_prompt="You are a financial analyst specializing in investment strategies.",
# llm=model,
# max_loops=1,
# autosave=False,
# dashboard=False,
# verbose=True,
# dynamic_temperature_enabled=True,
# user_name="swarms_corp",
# retry_attempts=1,
# context_length=200000,
# output_type="string",
# streaming_on=False,
# )
# agent2 = Agent(
# agent_name="Tax-Adviser-Agent",
# system_prompt="You are a tax adviser who provides clear and concise guidance on tax-related queries.",
# llm=model,
# max_loops=1,
# autosave=False,
# dashboard=False,
# verbose=True,
# dynamic_temperature_enabled=True,
# user_name="swarms_corp",
# retry_attempts=1,
# context_length=200000,
# output_type="string",
# streaming_on=False,
# )
# # Create group chat
# group_chat = GroupChat(
# name="Financial Discussion",
# description="A group chat for financial analysis and tax advice.",
# agents=[agent1, agent2],
# )
# # Run the group chat
# asyncio.run(
# group_chat.run(
# "How can I establish a ROTH IRA to buy stocks and get a tax break? What are the criteria? What do you guys think?"
# )
# )

@ -1,29 +0,0 @@
from typing import Dict, Optional
from datetime import datetime
from pydantic import BaseModel, Field
class Message(BaseModel):
"""
Represents a message with timestamp and optional metadata.
Usage
--------------
mes = Message(
sender = "Kye",
content = "message"
)
print(mes)
"""
timestamp: datetime = Field(default_factory=datetime.now)
sender: str
content: str
metadata: Optional[Dict[str, str]] = {}
def __repr__(self) -> str:
"""
__repr__ means...
"""
return f"{self.timestamp} - {self.sender}: {self.content}"

@ -3,7 +3,7 @@ from concurrent.futures import ThreadPoolExecutor
import psutil
from dataclasses import dataclass
import threading
from typing import List, Union, Any, Callable
from typing import List, Any
from multiprocessing import cpu_count
import os
@ -12,9 +12,7 @@ from swarms.utils.wrapper_clusterop import (
exec_callable_with_clusterops,
)
# Type definitions
AgentType = Union[Agent, Callable]
from swarms.structs.omni_agent_types import AgentType
def run_single_agent(agent: AgentType, task: str) -> Any:

@ -0,0 +1,15 @@
from typing import Literal
# Literal of output types
# Literal of output types
OutputType = Literal[
"all",
"final",
"list",
"dict",
".json",
".md",
".txt",
".yaml",
".toml",
]

@ -3,15 +3,15 @@ import traceback
import uuid
from concurrent.futures import ThreadPoolExecutor
from datetime import datetime
from typing import Callable, Dict, List, Literal, Optional
from typing import Any, Callable, Dict, List, Optional
from pydantic import BaseModel, Field
from swarms_memory import BaseVectorDatabase
from swarms.schemas.agent_step_schemas import ManySteps
from swarms.structs.agent import Agent
from swarms.structs.agents_available import showcase_available_agents
from swarms.structs.base_swarm import BaseSwarm
from swarms.structs.output_types import OutputType
from swarms.utils.add_docs_to_agents import handle_input_docs
from swarms.utils.loguru_logger import initialize_logger
from swarms.utils.wrapper_clusterop import (
@ -20,19 +20,6 @@ from swarms.utils.wrapper_clusterop import (
logger = initialize_logger(log_folder="rearrange")
# Literal of output types
OutputType = Literal[
"all",
"final",
"list",
"dict",
".json",
".md",
".txt",
".yaml",
".toml",
]
def swarm_id():
return uuid.uuid4().hex
@ -54,7 +41,10 @@ class AgentRearrangeInput(BaseModel):
class AgentRearrangeOutput(BaseModel):
Input: Optional[AgentRearrangeInput] = None
output_id: str = Field(
default=swarm_id(), description="Output-UUID"
)
input: Optional[AgentRearrangeInput] = None
outputs: Optional[List[ManySteps]] = None
time: str = Field(
default_factory=lambda: datetime.now().strftime(
@ -112,13 +102,13 @@ class AgentRearrange(BaseSwarm):
flow: str = None,
max_loops: int = 1,
verbose: bool = True,
memory_system: BaseVectorDatabase = None,
memory_system: Any = None,
human_in_the_loop: bool = False,
custom_human_in_the_loop: Optional[
Callable[[str], str]
] = None,
return_json: bool = False,
output_type: OutputType = "final",
output_type: OutputType = "all",
docs: List[str] = None,
doc_folder: str = None,
device: str = "cpu",
@ -154,6 +144,17 @@ class AgentRearrange(BaseSwarm):
self.all_gpus = all_gpus
self.no_use_clusterops = no_use_clusterops
self.output_schema = AgentRearrangeOutput(
input=AgentRearrangeInput(
swarm_id=id,
name=name,
description=description,
flow=flow,
max_loops=max_loops,
),
outputs=[],
)
def showcase_agents(self):
# Get formatted agent info once
agents_available = showcase_available_agents(
@ -461,7 +462,9 @@ class AgentRearrange(BaseSwarm):
f"Agent {agent_name} output: {current_task}"
)
all_responses.append(current_task)
all_responses.append(
f"Agent Name: {agent.agent_name} \n Output: {current_task} "
)
previous_agent = agent_name
loop_count += 1
@ -698,6 +701,7 @@ def rearrange(
agents: List[Agent] = None,
flow: str = None,
task: str = None,
img: str = None,
*args,
**kwargs,
):
@ -723,4 +727,4 @@ def rearrange(
agent_system = AgentRearrange(
agents=agents, flow=flow, *args, **kwargs
)
return agent_system.run(task, *args, **kwargs)
return agent_system.run(task, img=img, *args, **kwargs)

@ -1,6 +1,7 @@
from typing import List, Optional
from swarms.structs.agent import Agent
from swarms.structs.rearrange import AgentRearrange, OutputType
from swarms.structs.rearrange import AgentRearrange
from swarms.structs.output_types import OutputType
from concurrent.futures import ThreadPoolExecutor, as_completed
from swarms.utils.loguru_logger import initialize_logger

@ -1,11 +1,14 @@
from typing import List, Tuple, Optional
import numpy as np
import torch
from transformers import AutoTokenizer, AutoModel
from swarms.utils.lazy_loader import lazy_import_decorator
from pydantic import BaseModel, Field
import json
from tenacity import retry, stop_after_attempt, wait_exponential
from swarms.utils.loguru_logger import initialize_logger
from swarms.utils.auto_download_check_packages import (
auto_check_and_download_package,
)
logger = initialize_logger(log_folder="swarm_matcher")
@ -25,6 +28,7 @@ class SwarmMatcherConfig(BaseModel):
)
@lazy_import_decorator
class SwarmMatcher:
"""
A class for matching tasks to swarm types based on their descriptions.
@ -41,12 +45,34 @@ class SwarmMatcher:
"""
logger.add("swarm_matcher_debug.log", level="DEBUG")
logger.debug("Initializing SwarmMatcher")
try:
import torch
except ImportError:
auto_check_and_download_package(
"torch", package_manager="pip", upgrade=True
)
import torch
try:
import transformers
except ImportError:
auto_check_and_download_package(
"transformers", package_manager="pip", upgrade=True
)
import transformers
self.torch = torch
try:
self.config = config
self.tokenizer = AutoTokenizer.from_pretrained(
self.tokenizer = (
transformers.AutoTokenizer.from_pretrained(
config.model_name
)
)
self.model = transformers.AutoModel.from_pretrained(
config.model_name
)
self.model = AutoModel.from_pretrained(config.model_name)
self.swarm_types: List[SwarmType] = []
logger.debug("SwarmMatcher initialized successfully")
except Exception as e:
@ -76,7 +102,7 @@ class SwarmMatcher:
truncation=True,
max_length=512,
)
with torch.no_grad():
with self.torch.no_grad():
outputs = self.model(**inputs)
embedding = (
outputs.last_hidden_state.mean(dim=1)
@ -244,6 +270,7 @@ def initialize_swarm_types(matcher: SwarmMatcher):
logger.debug("Swarm types initialized")
@lazy_import_decorator
def swarm_matcher(task: str, *args, **kwargs):
"""
Runs the SwarmMatcher example with predefined tasks and swarm types.

@ -1,511 +0,0 @@
"""
Todo
- [ ] Test the new api feature
- [ ] Add the agent schema for every agent -- following OpenAI assistaants schema
- [ ] then add the swarm schema for the swarm url: /v1/swarms/{swarm_name}/agents/{agent_id}
- [ ] Add the agent schema for the agent url: /v1/swarms/{swarm_name}/agents/{agent_id}
"""
import asyncio
import multiprocessing
import queue
import threading
from typing import List, Optional
import tenacity
# from fastapi import FastAPI
from pydantic import BaseModel
from swarms.structs.agent import Agent
from swarms.structs.base_swarm import BaseSwarm
from swarms.utils.loguru_logger import initialize_logger
logger = initialize_logger("swarm-network")
# Pydantic models
class TaskRequest(BaseModel):
task: str
# Pydantic models
class TaskResponse(BaseModel):
result: str
class AgentInfo(BaseModel):
agent_name: str
agent_description: str
class SwarmInfo(BaseModel):
swarm_name: str
swarm_description: str
agents: List[AgentInfo]
# Helper function to get the number of workers
def get_number_of_workers():
return multiprocessing.cpu_count()
# [TODO] Add the agent schema for every agent -- following OpenAI assistaants schema
class SwarmNetwork(BaseSwarm):
"""
SwarmNetwork class
The SwarmNetwork class is responsible for managing the agents pool
and the task queue. It also monitors the health of the agents and
scales the pool up or down based on the number of pending tasks
and the current load of the agents.
For example, if the number of pending tasks is greater than the
number of agents in the pool, the SwarmNetwork will scale up the
pool by adding new agents. If the number of pending tasks is less
than the number of agents in the pool, the SwarmNetwork will scale
down the pool by removing agents.
The SwarmNetwork class also provides a simple API for interacting
with the agents pool. The API is implemented using the Flask
framework and is enabled by default. The API can be disabled by
setting the `api_enabled` parameter to False.
Features:
- Agent pool management
- Task queue management
- Agent health monitoring
- Agent pool scaling
- Simple API for interacting with the agent pool
- Simple API for interacting with the task queue
- Simple API for interacting with the agent health monitor
- Simple API for interacting with the agent pool scaler
- Create APIs for each agent in the pool (optional)
- Run each agent on it's own thread
- Run each agent on it's own process
- Run each agent on it's own container
- Run each agent on it's own machine
- Run each agent on it's own cluster
Attributes:
task_queue (queue.Queue): A queue for storing tasks.
idle_threshold (float): The idle threshold for the agents.
busy_threshold (float): The busy threshold for the agents.
agents (List[Agent]): A list of agents in the pool.
api_enabled (bool): A flag to enable/disable the API.
logging_enabled (bool): A flag to enable/disable logging.
Example:
>>> from swarms.structs.agent import Agent
>>> from swarms.structs.swarm_net import SwarmNetwork
>>> agent = Agent()
>>> swarm = SwarmNetwork(agents=[agent])
>>> swarm.add_task("task")
>>> swarm.run()
"""
def __init__(
self,
name: str = None,
description: str = None,
agents: List[Agent] = None,
idle_threshold: float = 0.2,
busy_threshold: float = 0.7,
api_enabled: Optional[bool] = False,
logging_enabled: Optional[bool] = False,
api_on: Optional[bool] = False,
host: str = "0.0.0.0",
port: int = 8000,
swarm_callable: Optional[callable] = None,
*args,
**kwargs,
):
super().__init__(agents=agents, *args, **kwargs)
self.name = name
self.description = description
self.agents = agents
self.task_queue = queue.Queue()
self.idle_threshold = idle_threshold
self.busy_threshold = busy_threshold
self.lock = threading.Lock()
self.api_enabled = api_enabled
self.logging_enabled = logging_enabled
self.host = host
self.port = port
self.swarm_callable = swarm_callable
# Ensure that the agents list is not empty
if not agents:
raise ValueError("The agents list cannot be empty")
# Create a dictionary of agents for easy access
self.agent_dict = {agent.id: agent for agent in agents}
# # Create the FastAPI instance
# if api_on is True:
# logger.info("Creating FastAPI instance")
# self.app = FastAPI(debug=True, *args, **kwargs)
# self.app.add_middleware(
# CORSMiddleware,
# allow_origins=["*"],
# allow_credentials=True,
# allow_methods=["*"],
# allow_headers=["*"],
# )
# logger.info("Routes set for creation")
# self._create_routes()
def add_task(self, task):
"""Add task to the task queue
Args:
task (_type_): _description_
Example:
>>> from swarms.structs.agent import Agent
>>> from swarms.structs.swarm_net import SwarmNetwork
>>> agent = Agent()
>>> swarm = SwarmNetwork(agents=[agent])
>>> swarm.add_task("task")
"""
self.logger.info(f"Adding task {task} to queue")
try:
self.task_queue.put(task)
self.logger.info(f"Task {task} added to queue")
except Exception as error:
print(
f"Error adding task to queue: {error} try again with"
" a new task"
)
raise error
async def async_add_task(self, task):
"""Add task to the task queue
Args:
task (_type_): _description_
Example:
>>> from swarms.structs.agent import Agent
>>> from swarms.structs.swarm_net import SwarmNetwork
>>> agent = Agent()
>>> swarm = SwarmNetwork(agents=[agent])
>>> swarm.add_task("task")
"""
self.logger.info(
f"Adding task {task} to queue asynchronously"
)
try:
# Add task to queue asynchronously with asyncio
loop = asyncio.get_running_loop()
await loop.run_in_executor(
None, self.task_queue.put, task
)
self.logger.info(f"Task {task} added to queue")
except Exception as error:
print(
f"Error adding task to queue: {error} try again with"
" a new task"
)
raise error
# def _create_routes(self) -> None:
# """
# Creates the routes for the API.
# """
# # Extensive logginbg
# logger.info("Creating routes for the API")
# # Routes available
# logger.info(
# "Routes available: /v1/swarms, /v1/health, /v1/swarms/{swarm_name}/agents/{agent_id}, /v1/swarms/{swarm_name}/run"
# )
# @self.app.get("/v1/swarms", response_model=SwarmInfo)
# async def get_swarms() -> SwarmInfo:
# try:
# logger.info("Getting swarm information")
# return SwarmInfo(
# swarm_name=self.swarm_name,
# swarm_description=self.swarm_description,
# agents=[
# AgentInfo(
# agent_name=agent.agent_name,
# agent_description=agent.agent_description,
# )
# for agent in self.agents
# ],
# )
# except Exception as e:
# logger.error(f"Error getting swarm information: {str(e)}")
# raise HTTPException(
# status_code=500, detail="Internal Server Error"
# )
# @self.app.get("/v1/health")
# async def get_health() -> Dict[str, str]:
# try:
# logger.info("Checking health status")
# return {"status": "healthy"}
# except Exception as e:
# logger.error(f"Error checking health status: {str(e)}")
# raise HTTPException(
# status_code=500, detail="Internal Server Error"
# )
# @self.app.get(f"/v1/swarms/{self.swarm_name}/agents/{{agent_id}}")
# async def get_agent_info(agent_id: str) -> AgentInfo:
# try:
# logger.info(f"Getting information for agent {agent_id}")
# agent = self.agent_dict.get(agent_id)
# if not agent:
# raise HTTPException(
# status_code=404, detail="Agent not found"
# )
# return AgentInfo(
# agent_name=agent.agent_name,
# agent_description=agent.agent_description,
# )
# except Exception as e:
# logger.error(f"Error getting agent information: {str(e)}")
# raise HTTPException(
# status_code=500, detail="Internal Server Error"
# )
# @self.app.post(
# f"/v1/swarms/{self.swarm_name}/agents/{{agent_id}}/run",
# response_model=TaskResponse,
# )
# async def run_agent_task(
# task_request: TaskRequest,
# ) -> TaskResponse:
# try:
# logger.info("Running agent task")
# # Assuming only one agent in the swarm for this example
# agent = self.agents[0]
# logger.info(f"Running agent task: {task_request.task}")
# result = agent.run(task_request.task)
# return TaskResponse(result=result)
# except Exception as e:
# logger.error(f"Error running agent task: {str(e)}")
# raise HTTPException(
# status_code=500, detail="Internal Server Error"
# )
# def get_app(self) -> FastAPI:
# """
# Returns the FastAPI instance.
# Returns:
# FastAPI: The FastAPI instance.
# """
# return self.app
def run_single_agent(
self, agent_id, task: Optional[str], *args, **kwargs
):
"""Run agent the task on the agent id
Args:
agent_id (_type_): _description_
task (str, optional): _description_. Defaults to None.
Raises:
ValueError: _description_
Returns:
_type_: _description_
"""
self.logger.info(f"Running task {task} on agent {agent_id}")
try:
for agent in self.agents:
if agent.id == agent_id:
out = agent.run(task, *args, **kwargs)
return out
except Exception as error:
self.logger.error(f"Error running task on agent: {error}")
raise error
def run_many_agents(
self, task: Optional[str] = None, *args, **kwargs
) -> List:
"""Run the task on all agents
Args:
task (str, optional): _description_. Defaults to None.
Returns:
List: _description_
"""
self.logger.info(f"Running task {task} on all agents")
try:
return [
agent.run(task, *args, **kwargs)
for agent in self.agents
]
except Exception as error:
logger.error(f"Error running task on agents: {error}")
raise error
def list_agents(self):
"""List all agents."""
self.logger.info("[Listing all active agents]")
try:
# Assuming self.agents is a list of agent objects
for agent in self.agents:
self.logger.info(
f"[Agent] [ID: {agent.id}] [Name:"
f" {agent.agent_name}] [Description:"
f" {agent.agent_description}] [Status: Running]"
)
except Exception as error:
self.logger.error(f"Error listing agents: {error}")
raise
def get_agent(self, agent_id):
"""Get agent by id
Args:
agent_id (_type_): _description_
Returns:
_type_: _description_
"""
self.logger.info(f"Getting agent {agent_id}")
try:
for agent in self.agents:
if agent.id == agent_id:
return agent
raise ValueError(f"No agent found with ID {agent_id}")
except Exception as error:
self.logger.error(f"Error getting agent: {error}")
raise error
def add_agent(self, agent: Agent):
"""Add agent to the agent pool
Args:
agent (_type_): _description_
"""
self.logger.info(f"Adding agent {agent} to pool")
try:
self.agents.append(agent)
except Exception as error:
print(f"Error adding agent to pool: {error}")
raise error
def remove_agent(self, agent_id):
"""Remove agent from the agent pool
Args:
agent_id (_type_): _description_
"""
self.logger.info(f"Removing agent {agent_id} from pool")
try:
for agent in self.agents:
if agent.id == agent_id:
self.agents.remove(agent)
return
raise ValueError(f"No agent found with ID {agent_id}")
except Exception as error:
print(f"Error removing agent from pool: {error}")
raise error
async def async_remove_agent(self, agent_id):
"""Remove agent from the agent pool
Args:
agent_id (_type_): _description_
"""
self.logger.info(f"Removing agent {agent_id} from pool")
try:
# Remove agent from pool asynchronously with asyncio
loop = asyncio.get_running_loop()
await loop.run_in_executor(
None, self.remove_agent, agent_id
)
except Exception as error:
print(f"Error removing agent from pool: {error}")
raise error
def scale_up(self, num_agents: int = 1):
"""Scale up the agent pool
Args:
num_agents (int, optional): _description_. Defaults to 1.
"""
self.logger.info(f"Scaling up agent pool by {num_agents}")
try:
for _ in range(num_agents):
self.agents.append(Agent())
except Exception as error:
print(f"Error scaling up agent pool: {error}")
raise error
def scale_down(self, num_agents: int = 1):
"""Scale down the agent pool
Args:
num_agents (int, optional): _description_. Defaults to 1.
"""
for _ in range(num_agents):
self.agents.pop()
@tenacity.retry(
wait=tenacity.wait_fixed(1),
stop=tenacity.stop_after_attempt(3),
retry=tenacity.retry_if_exception_type(Exception),
)
def run(self, *args, **kwargs):
"""run the swarm network"""
app = self.get_app()
try:
import uvicorn
logger.info(
f"Running the swarm network with {len(self.agents)} on {self.host}:{self.port}"
)
uvicorn.run(
app,
host=self.host,
port=self.port,
# workers=get_number_of_workers(),
*args,
**kwargs,
)
return app
except Exception as error:
logger.error(f"Error running the swarm network: {error}")
raise error
# # # Example usage
# if __name__ == "__main__":
# agent1 = Agent(
# agent_name="Covid-19-Chat",
# agent_description="This agent provides information about COVID-19 symptoms.",
# llm=OpenAIChat(),
# max_loops="auto",
# autosave=True,
# verbose=True,
# stopping_condition="finish",
# )
# agents = [agent1] # Add more agents as needed
# swarm_name = "HealthSwarm"
# swarm_description = (
# "A swarm of agents providing health-related information."
# )
# agent_api = SwarmNetwork(swarm_name, swarm_description, agents)
# agent_api.run()

@ -137,6 +137,7 @@ class SwarmRouter:
rules: str = None,
documents: List[str] = [], # A list of docs file paths
output_type: str = "string", # Md, PDF, Txt, csv
no_cluster_ops: bool = False,
*args,
**kwargs,
):
@ -153,6 +154,7 @@ class SwarmRouter:
self.rules = rules
self.documents = documents
self.output_type = output_type
self.no_cluster_ops = no_cluster_ops
self.logs = []
self.reliability_check()
@ -176,6 +178,12 @@ class SwarmRouter:
# if self.documents is not None:
# self.handle_docs()
# let's make a function that checks the agents parameter and disables clusterops
def deactivate_clusterops(self):
for agent in self.agents:
agent.do_not_use_cluster_ops = True
def handle_docs(self):
# Process all documents in parallel using list comprehension
data = "".join(
@ -279,6 +287,9 @@ class SwarmRouter:
self._create_swarm(self.swarm_type)
if self.no_cluster_ops:
self.deactivate_clusterops()
elif self.swarm_type == "AgentRearrange":
return AgentRearrange(
name=self.name,

@ -4,17 +4,15 @@ from datetime import datetime
from typing import Any, List, Optional
from pydantic import BaseModel, Field
from sentence_transformers import SentenceTransformer, util
from swarms.structs.agent import Agent
from swarms.utils.loguru_logger import initialize_logger
from swarms.utils.auto_download_check_packages import (
auto_check_and_download_package,
)
from swarms.structs.conversation import Conversation
logger = initialize_logger(log_folder="tree_swarm")
# Pretrained model for embeddings
embedding_model = SentenceTransformer(
"all-MiniLM-L6-v2"
) # A small, fast model for embedding
logger = initialize_logger(log_folder="tree_swarm")
# Pydantic Models for Logging
@ -68,7 +66,7 @@ class TreeAgent(Agent):
name: str = None,
description: str = None,
system_prompt: str = None,
llm: callable = None,
model_name: str = "gpt-4o",
agent_name: Optional[str] = None,
*args,
**kwargs,
@ -78,12 +76,29 @@ class TreeAgent(Agent):
name=name,
description=description,
system_prompt=system_prompt,
llm=llm,
model_name=model_name,
agent_name=agent_name,
*args,
**kwargs,
)
self.system_prompt_embedding = embedding_model.encode(
try:
import sentence_transformers
except ImportError:
auto_check_and_download_package(
"sentence-transformers", package_manager="pip"
)
import sentence_transformers
self.sentence_transformers = sentence_transformers
# Pretrained model for embeddings
self.embedding_model = (
sentence_transformers.SentenceTransformer(
"all-MiniLM-L6-v2"
)
)
self.system_prompt_embedding = self.embedding_model.encode(
system_prompt, convert_to_tensor=True
)
@ -103,7 +118,7 @@ class TreeAgent(Agent):
Returns:
float: Distance score between 0 and 1, with 0 being close and 1 being far.
"""
similarity = util.pytorch_cos_sim(
similarity = self.sentence_transformers.util.pytorch_cos_sim(
self.system_prompt_embedding,
other_agent.system_prompt_embedding,
).item()
@ -112,7 +127,9 @@ class TreeAgent(Agent):
) # Closer agents have a smaller distance
return distance
def run_task(self, task: str) -> Any:
def run_task(
self, task: str, img: str = None, *args, **kwargs
) -> Any:
input_log = AgentLogInput(
agent_name=self.agent_name,
task=task,
@ -121,7 +138,7 @@ class TreeAgent(Agent):
logger.info(f"Running task on {self.agent_name}: {task}")
logger.debug(f"Input Log: {input_log.json()}")
result = self.run(task)
result = self.run(task=task, img=img, *args, **kwargs)
output_log = AgentLogOutput(
agent_name=self.agent_name,
@ -154,12 +171,14 @@ class TreeAgent(Agent):
# Perform embedding similarity match if keyword match is not found
if not keyword_match:
task_embedding = embedding_model.encode(
task_embedding = self.embedding_model.encode(
task, convert_to_tensor=True
)
similarity = util.pytorch_cos_sim(
self.system_prompt_embedding, task_embedding
).item()
similarity = (
self.sentence_transformers.util.pytorch_cos_sim(
self.system_prompt_embedding, task_embedding
).item()
)
logger.info(
f"Semantic similarity between task and {self.agent_name}: {similarity:.2f}"
)
@ -241,16 +260,33 @@ class Tree:
class ForestSwarm:
def __init__(self, trees: List[Tree], *args, **kwargs):
def __init__(
self,
name: str = "default-forest-swarm",
description: str = "Standard forest swarm",
trees: List[Tree] = [],
shared_memory: Any = None,
rules: str = None,
*args,
**kwargs,
):
"""
Initializes the structure with multiple trees of agents.
Args:
trees (List[Tree]): A list of trees in the structure.
"""
self.name = name
self.description = description
self.trees = trees
# Add auto grouping based on trees.
# Add auto group agents
self.shared_memory = shared_memory
self.save_file_path = f"forest_swarm_{uuid.uuid4().hex}.json"
self.conversation = Conversation(
time_enabled=True,
auto_save=True,
save_filepath=self.save_file_path,
rules=rules,
)
def find_relevant_tree(self, task: str) -> Optional[Tree]:
"""
@ -271,7 +307,7 @@ class ForestSwarm:
logger.warning(f"No relevant tree found for task: {task}")
return None
def run(self, task: str) -> Any:
def run(self, task: str, img: str = None, *args, **kwargs) -> Any:
"""
Executes the given task by finding the most relevant tree and agent within that tree.
@ -281,21 +317,30 @@ class ForestSwarm:
Returns:
Any: The result of the task after it has been processed by the agents.
"""
logger.info(
f"Running task across MultiAgentTreeStructure: {task}"
)
relevant_tree = self.find_relevant_tree(task)
if relevant_tree:
agent = relevant_tree.find_relevant_agent(task)
if agent:
result = agent.run_task(task)
relevant_tree.log_tree_execution(task, agent, result)
return result
else:
try:
logger.info(
f"Running task across MultiAgentTreeStructure: {task}"
)
relevant_tree = self.find_relevant_tree(task)
if relevant_tree:
agent = relevant_tree.find_relevant_agent(task)
if agent:
result = agent.run_task(
task, img=img, *args, **kwargs
)
relevant_tree.log_tree_execution(
task, agent, result
)
return result
else:
logger.error(
"Task could not be completed: No relevant agent or tree found."
)
return "No relevant agent found to handle this task."
except Exception as error:
logger.error(
"Task could not be completed: No relevant agent or tree found."
f"Error detected in the ForestSwarm, check your inputs and try again ;) {error}"
)
return "No relevant agent found to handle this task."
# # Example Usage:

@ -1,40 +0,0 @@
import os
import subprocess
from swarms.utils.loguru_logger import initialize_logger
from swarms.telemetry.check_update import check_for_update
logger = initialize_logger(log_folder="auto_upgrade_swarms")
def auto_update():
"""auto update swarms"""
try:
# Check if auto-update is disabled
auto_update_enabled = os.getenv(
"SWARMS_AUTOUPDATE_ON", "false"
).lower()
if auto_update_enabled == "false":
logger.info(
"Auto-update is disabled via SWARMS_AUTOUPDATE_ON"
)
return
outcome = check_for_update()
if outcome is True:
logger.info(
"There is a new version of swarms available! Downloading..."
)
try:
subprocess.run(
["pip", "install", "-U", "swarms"], check=True
)
except subprocess.CalledProcessError:
logger.info("Attempting to install with pip3...")
subprocess.run(
["pip3", "install", "-U", "swarms"], check=True
)
else:
logger.info("swarms is up to date!")
except Exception as e:
logger.error(e)

@ -2,8 +2,6 @@ import os
import logging
import warnings
from concurrent.futures import ThreadPoolExecutor
from swarms.telemetry.auto_upgrade_swarms import auto_update
from swarms.utils.disable_logging import disable_logging
@ -22,9 +20,8 @@ def bootup():
warnings.filterwarnings("ignore", category=DeprecationWarning)
# Use ThreadPoolExecutor to run disable_logging and auto_update concurrently
with ThreadPoolExecutor(max_workers=2) as executor:
with ThreadPoolExecutor(max_workers=1) as executor:
executor.submit(disable_logging)
executor.submit(auto_update)
except Exception as e:
print(f"An error occurred: {str(e)}")
raise

@ -39,8 +39,7 @@ def capture_system_data() -> Dict[str, str]:
system_data["external_ip"] = requests.get(
"https://api.ipify.org"
).text
except Exception as e:
logger.warning("Failed to retrieve external IP: {}", e)
except Exception:
system_data["external_ip"] = "N/A"
return system_data
@ -49,9 +48,7 @@ def capture_system_data() -> Dict[str, str]:
return {}
def log_agent_data(
data_dict: dict, retry_attempts: int = 1
) -> dict | None:
def log_agent_data(data_dict: dict) -> dict | None:
"""
Logs agent data to the Swarms database with retry logic.
@ -76,25 +73,7 @@ def log_agent_data(
"Authorization": "Bearer sk-f24a13ed139f757d99cdd9cdcae710fccead92681606a97086d9711f69d44869",
}
try:
response = requests.post(
url, json=data_dict, headers=headers, timeout=10
)
response.raise_for_status()
result = response.json()
return result
except requests.exceptions.Timeout:
logger.warning("Request timed out")
except requests.exceptions.HTTPError as e:
logger.error(f"HTTP error occurred: {e}")
if response.status_code == 401:
logger.error("Authentication failed - check API key")
except requests.exceptions.RequestException as e:
logger.error(f"Error logging agent data: {e}")
requests.post(url, json=data_dict, headers=headers, timeout=10)
# response.raise_for_status()
logger.error("Failed to log agent data")
return None

@ -1,73 +0,0 @@
import importlib.util
import sys
import pkg_resources
import requests
from packaging import version
from swarms.utils.loguru_logger import initialize_logger
logger = initialize_logger("check-update")
# borrowed from: https://stackoverflow.com/a/1051266/656011
def check_for_package(package: str) -> bool:
"""
Checks if a package is installed and available for import.
Args:
package (str): The name of the package to check.
Returns:
bool: True if the package is installed and can be imported, False otherwise.
"""
if package in sys.modules:
return True
elif (spec := importlib.util.find_spec(package)) is not None:
try:
module = importlib.util.module_from_spec(spec)
sys.modules[package] = module
spec.loader.exec_module(module)
return True
except ImportError:
logger.error(f"Failed to import {package}")
return False
else:
logger.info(f"{package} not found")
return False
def check_for_update() -> bool:
"""
Checks if there is an update available for the swarms package.
Returns:
bool: True if an update is available, False otherwise.
"""
try:
# Fetch the latest version from the PyPI API
response = requests.get("https://pypi.org/pypi/swarms/json")
response.raise_for_status() # Raises an HTTPError if the response status code is 4XX/5XX
latest_version = response.json()["info"]["version"]
# Get the current version using pkg_resources
current_version = pkg_resources.get_distribution(
"swarms"
).version
if version.parse(latest_version) > version.parse(
current_version
):
logger.info(
f"Update available: {latest_version} > {current_version}"
)
return True
else:
logger.info(
f"No update available: {latest_version} <= {current_version}"
)
return False
except requests.exceptions.RequestException as e:
logger.error(f"Failed to check for update: {e}")
return False

@ -28,6 +28,7 @@ from swarms.tools.cohere_func_call_schema import (
ParameterDefinition,
)
from swarms.tools.tool_registry import ToolStorage, tool_registry
from swarms.tools.json_utils import base_model_to_json
__all__ = [
@ -51,4 +52,5 @@ __all__ = [
"ParameterDefinition",
"ToolStorage",
"tool_registry",
"base_model_to_json",
]

@ -387,6 +387,8 @@ class BaseTool(BaseModel):
"Converting tools into OpenAI function calling schema"
)
tool_schemas = []
for tool in self.tools:
# Transform the tool into a openai function calling schema
if self.check_func_if_have_docs(
@ -398,7 +400,7 @@ class BaseTool(BaseModel):
logger.info(
f"Converting tool: {name} into a OpenAI certified function calling schema. Add documentation and type hints."
)
tool_schema_list = (
tool_schema = (
get_openai_function_schema_from_func(
tool, name=name, description=description
)
@ -408,18 +410,21 @@ class BaseTool(BaseModel):
f"Tool {name} converted successfully into OpenAI schema"
)
# Transform the dictionary to a string
tool_schema_list = json.dumps(
tool_schema_list, indent=4
)
return tool_schema_list
tool_schemas.append(tool_schema)
else:
logger.error(
f"Tool {tool.__name__} does not have documentation or type hints, please add them to make the tool execution reliable."
)
return tool_schema_list
# Combine all tool schemas into a single schema
if tool_schemas:
combined_schema = {
"type": "function",
"functions": [schema["function"] for schema in tool_schemas]
}
return json.dumps(combined_schema, indent=4)
return None
def check_func_if_have_docs(self, func: callable):
if func.__doc__ is not None:

@ -1,7 +1,7 @@
import json
from typing import Any, Dict, List, Union
from transformers import PreTrainedModel, PreTrainedTokenizer
from swarms.utils.lazy_loader import lazy_import_decorator
from pydantic import BaseModel
from swarms.tools.logits_processor import (
NumberStoppingCriteria,
@ -9,10 +9,23 @@ from swarms.tools.logits_processor import (
StringStoppingCriteria,
)
from swarm_models.base_llm import BaseLLM
from swarms.utils.auto_download_check_packages import (
auto_check_and_download_package,
)
try:
import transformers
except ImportError:
auto_check_and_download_package(
"transformers", package_manager="pip"
)
import transformers
GENERATION_MARKER = "|GENERATION|"
@lazy_import_decorator
class Jsonformer:
"""
Initializes the FormatTools class.
@ -35,8 +48,8 @@ class Jsonformer:
def __init__(
self,
model: PreTrainedModel = None,
tokenizer: PreTrainedTokenizer = None,
model: transformers.PreTrainedModel = None, # type: ignore
tokenizer: transformers.PreTrainedTokenizer = None, # type: ignore
json_schema: Union[Dict[str, Any], BaseModel] = None,
schemas: List[Union[Dict[str, Any], BaseModel]] = [],
prompt: str = None,

@ -1,21 +1,35 @@
import torch
from transformers import (
LogitsWarper,
PreTrainedTokenizer,
StoppingCriteria,
from swarms.utils.auto_download_check_packages import (
auto_check_and_download_package,
)
class StringStoppingCriteria(StoppingCriteria):
try:
import torch
except ImportError:
auto_check_and_download_package(
"torch", package_manager="pip", upgrade=True
)
import torch
try:
import transformers
except ImportError:
auto_check_and_download_package(
"transformers", package_manager="pip", upgrade=True
)
import transformers
class StringStoppingCriteria(transformers.StoppingCriteria):
def __init__(
self, tokenizer: PreTrainedTokenizer, prompt_length: int
self, tokenizer: transformers.PreTrainedTokenizer, prompt_length: int # type: ignore
):
self.tokenizer = tokenizer
self.prompt_length = prompt_length
def __call__(
self,
input_ids: torch.LongTensor,
input_ids: torch.LongTensor, # type: ignore
_,
) -> bool:
if len(input_ids[0]) <= self.prompt_length:
@ -31,10 +45,10 @@ class StringStoppingCriteria(StoppingCriteria):
return result
class NumberStoppingCriteria(StoppingCriteria):
class NumberStoppingCriteria(transformers.StoppingCriteria):
def __init__(
self,
tokenizer: PreTrainedTokenizer,
tokenizer: transformers.PreTrainedTokenizer, # type: ignore
prompt_length: int,
precision: int = 3,
):
@ -44,8 +58,8 @@ class NumberStoppingCriteria(StoppingCriteria):
def __call__(
self,
input_ids: torch.LongTensor,
scores: torch.FloatTensor,
input_ids: torch.LongTensor, # type: ignore
scores: torch.FloatTensor, # type: ignore
) -> bool:
decoded = self.tokenizer.decode(
input_ids[0][self.prompt_length :],
@ -71,8 +85,8 @@ class NumberStoppingCriteria(StoppingCriteria):
return False
class OutputNumbersTokens(LogitsWarper):
def __init__(self, tokenizer: PreTrainedTokenizer, prompt: str):
class OutputNumbersTokens(transformers.LogitsWarper):
def __init__(self, tokenizer: transformers.PreTrainedTokenizer, prompt: str): # type: ignore
self.tokenizer = tokenizer
self.tokenized_prompt = tokenizer(prompt, return_tensors="pt")
vocab_size = len(tokenizer)

@ -0,0 +1,146 @@
"""
Package installation utility that checks for package existence and installs if needed.
Supports both pip and conda package managers.
"""
import importlib.util
import subprocess
import sys
from typing import Literal, Optional, Union
from swarms.utils.loguru_logger import initialize_logger
import pkg_resources
logger = initialize_logger("autocheckpackages")
def check_and_install_package(
package_name: str,
package_manager: Literal["pip", "conda"] = "pip",
version: Optional[str] = None,
upgrade: bool = False,
) -> bool:
"""
Check if a package is installed and install it if not found.
Args:
package_name: Name of the package to check/install
package_manager: Package manager to use ('pip' or 'conda')
version: Specific version to install (optional)
upgrade: Whether to upgrade the package if it exists
Returns:
bool: True if package is available after check/install, False if installation failed
Raises:
ValueError: If invalid package manager is specified
"""
try:
# Check if package exists
if package_manager == "pip":
try:
pkg_resources.get_distribution(package_name)
if not upgrade:
logger.info(
f"Package {package_name} is already installed"
)
return True
except pkg_resources.DistributionNotFound:
pass
# Construct installation command
cmd = [sys.executable, "-m", "pip", "install"]
if upgrade:
cmd.append("--upgrade")
if version:
cmd.append(f"{package_name}=={version}")
else:
cmd.append(package_name)
elif package_manager == "conda":
# Check if conda is available
try:
subprocess.run(
["conda", "--version"],
check=True,
capture_output=True,
)
except (subprocess.CalledProcessError, FileNotFoundError):
logger.error(
"Conda is not available. Please install conda first."
)
return False
# Construct conda command
cmd = ["conda", "install", "-y"]
if version:
cmd.append(f"{package_name}={version}")
else:
cmd.append(package_name)
else:
raise ValueError(
f"Invalid package manager: {package_manager}"
)
# Run installation
logger.info(f"Installing {package_name}...")
subprocess.run(
cmd, check=True, capture_output=True, text=True
)
# Verify installation
try:
importlib.import_module(package_name)
logger.info(f"Successfully installed {package_name}")
return True
except ImportError:
logger.error(
f"Package {package_name} was installed but cannot be imported"
)
return False
except subprocess.CalledProcessError as e:
logger.error(f"Failed to install {package_name}: {e.stderr}")
return False
except Exception as e:
logger.error(
f"Unexpected error while installing {package_name}: {str(e)}"
)
return False
def auto_check_and_download_package(
packages: Union[str, list[str]],
package_manager: Literal["pip", "conda"] = "pip",
upgrade: bool = False,
) -> bool:
"""
Ensure multiple packages are installed.
Args:
packages: Single package name or list of package names
package_manager: Package manager to use ('pip' or 'conda')
upgrade: Whether to upgrade existing packages
Returns:
bool: True if all packages are available, False if any installation failed
"""
if isinstance(packages, str):
packages = [packages]
success = True
for package in packages:
if ":" in package:
name, version = package.split(":")
if not check_and_install_package(
name, package_manager, version, upgrade
):
success = False
else:
if not check_and_install_package(
package, package_manager, upgrade=upgrade
):
success = False
return success

@ -0,0 +1,263 @@
"""
Lazy Package Loader
This module provides utilities for lazy loading Python packages to improve startup time
and reduce memory usage by only importing packages when they are actually used.
Features:
- Type-safe lazy loading of packages
- Support for nested module imports
- Auto-completion support in IDEs
- Thread-safe implementation
- Comprehensive test coverage
"""
from types import ModuleType
from typing import (
Optional,
Dict,
Any,
Callable,
Type,
TypeVar,
Union,
cast,
)
import importlib
import functools
import threading
from importlib.util import find_spec
from swarms.utils.auto_download_check_packages import (
auto_check_and_download_package,
)
T = TypeVar("T")
C = TypeVar("C")
class ImportError(Exception):
"""Raised when a lazy import fails."""
pass
class LazyLoader:
"""
A thread-safe lazy loader for Python packages that only imports them when accessed.
Attributes:
_module_name (str): The name of the module to be lazily loaded
_module (Optional[ModuleType]): The cached module instance once loaded
_lock (threading.Lock): Thread lock for safe concurrent access
Examples:
>>> np = LazyLoader('numpy')
>>> # numpy is not imported yet
>>> result = np.array([1, 2, 3])
>>> # numpy is imported only when first used
"""
def __init__(self, module_name: str) -> None:
"""
Initialize the lazy loader with a module name.
Args:
module_name: The fully qualified name of the module to lazily load
Raises:
ImportError: If the module cannot be found in sys.path
"""
self._module_name = module_name
self._module: Optional[ModuleType] = None
self._lock = threading.Lock()
auto_check_and_download_package(
module_name, package_manager="pip"
)
# Verify module exists without importing it
if find_spec(module_name) is None:
raise ImportError(
f"Module '{module_name}' not found in sys.path"
)
def _load_module(self) -> ModuleType:
"""
Thread-safe module loading.
Returns:
ModuleType: The loaded module
Raises:
ImportError: If module import fails
"""
if self._module is None:
with self._lock:
# Double-check pattern
if self._module is None:
try:
self._module = importlib.import_module(
self._module_name
)
except Exception as e:
raise ImportError(
f"Failed to import '{self._module_name}': {str(e)}"
)
return cast(ModuleType, self._module)
def __getattr__(self, name: str) -> Any:
"""
Intercepts attribute access to load the module if needed.
Args:
name: The attribute name being accessed
Returns:
Any: The requested attribute from the loaded module
Raises:
AttributeError: If the attribute doesn't exist in the module
"""
module = self._load_module()
try:
return getattr(module, name)
except AttributeError:
raise AttributeError(
f"Module '{self._module_name}' has no attribute '{name}'"
)
def __dir__(self) -> list[str]:
"""
Returns list of attributes for autocomplete support.
Returns:
List[str]: Available attributes in the module
"""
return dir(self._load_module())
def is_loaded(self) -> bool:
"""
Check if the module has been loaded.
Returns:
bool: True if module is loaded, False otherwise
"""
return self._module is not None
class LazyLoaderMetaclass(type):
"""Metaclass to handle lazy loading behavior"""
def __call__(cls, *args, **kwargs):
if hasattr(cls, "_lazy_loader"):
return super().__call__(*args, **kwargs)
return super().__call__(*args, **kwargs)
class LazyClassLoader:
"""
A descriptor that creates the actual class only when accessed,
with proper inheritance support.
"""
def __init__(
self, class_name: str, bases: tuple, namespace: Dict[str, Any]
):
self.class_name = class_name
self.bases = bases
self.namespace = namespace
self._real_class: Optional[Type] = None
self._lock = threading.Lock()
def _create_class(self) -> Type:
"""Creates the actual class if it hasn't been created yet."""
if self._real_class is None:
with self._lock:
if self._real_class is None:
# Update namespace to include metaclass
namespace = dict(self.namespace)
namespace["__metaclass__"] = LazyLoaderMetaclass
# Create the class with metaclass
new_class = LazyLoaderMetaclass(
self.class_name, self.bases, namespace
)
# Store reference to this loader
new_class._lazy_loader = self
self._real_class = new_class
return cast(Type, self._real_class)
def __call__(self, *args: Any, **kwargs: Any) -> Any:
"""Creates an instance of the lazy loaded class."""
real_class = self._create_class()
# Use the metaclass __call__ method
return real_class(*args, **kwargs)
def __instancecheck__(self, instance: Any) -> bool:
"""Support for isinstance() checks"""
real_class = self._create_class()
return isinstance(instance, real_class)
def __subclasscheck__(self, subclass: Type) -> bool:
"""Support for issubclass() checks"""
real_class = self._create_class()
return issubclass(subclass, real_class)
def lazy_import(*names: str) -> Dict[str, LazyLoader]:
"""
Create multiple lazy loaders at once.
Args:
*names: Module names to create lazy loaders for
Returns:
Dict[str, LazyLoader]: Dictionary mapping module names to their lazy loaders
Examples:
>>> modules = lazy_import('numpy', 'pandas', 'matplotlib.pyplot')
>>> np = modules['numpy']
>>> pd = modules['pandas']
>>> plt = modules['matplotlib.pyplot']
"""
return {name.split(".")[-1]: LazyLoader(name) for name in names}
def lazy_import_decorator(
target: Union[Callable[..., T], Type[C]]
) -> Union[Callable[..., T], Type[C], LazyClassLoader]:
"""
Enhanced decorator that supports both lazy imports and lazy class loading.
"""
if isinstance(target, type):
# Store the original class details
namespace = {
name: value
for name, value in target.__dict__.items()
if not name.startswith("__")
or name in ("__init__", "__new__")
}
# Create lazy loader
loader = LazyClassLoader(
target.__name__, target.__bases__, namespace
)
# Preserve class metadata
loader.__module__ = target.__module__
loader.__doc__ = target.__doc__
# Add reference to original class
loader._original_class = target
return loader
else:
# Handle function decoration
@functools.wraps(target)
def wrapper(*args: Any, **kwargs: Any) -> T:
return target(*args, **kwargs)
return wrapper

@ -8,6 +8,7 @@ except ImportError:
from litellm import completion
litellm.set_verbose = True
litellm.ssl_verify = False
class LiteLLM:
@ -23,6 +24,7 @@ class LiteLLM:
stream: bool = False,
temperature: float = 0.5,
max_tokens: int = 4000,
ssl_verify: bool = False,
):
"""
Initialize the LiteLLM with the given parameters.
@ -39,6 +41,7 @@ class LiteLLM:
self.stream = stream
self.temperature = temperature
self.max_tokens = max_tokens
self.ssl_verify = ssl_verify
def _prepare_messages(self, task: str) -> list:
"""
@ -73,22 +76,26 @@ class LiteLLM:
Returns:
str: The content of the response from the model.
"""
messages = self._prepare_messages(task)
response = completion(
model=self.model_name,
messages=messages,
stream=self.stream,
temperature=self.temperature,
# max_completion_tokens=self.max_tokens,
max_tokens=self.max_tokens,
*args,
**kwargs,
)
content = response.choices[
0
].message.content # Accessing the content
return content
try:
messages = self._prepare_messages(task)
response = completion(
model=self.model_name,
messages=messages,
stream=self.stream,
temperature=self.temperature,
# max_completion_tokens=self.max_tokens,
max_tokens=self.max_tokens,
*args,
**kwargs,
)
content = response.choices[
0
].message.content # Accessing the content
return content
except Exception as error:
print(error)
def __call__(self, task: str, *args, **kwargs):
"""

@ -34,4 +34,4 @@ def initialize_logger(log_folder: str = "logs"):
retention="10 days",
# compression="zip",
)
return logger
return logger

@ -1,73 +0,0 @@
import os
from loguru import logger
import pygame
import requests
import tempfile
from openai import OpenAI
class OpenAITTS:
"""
A class to interact with OpenAI API and play the generated audio with improved streaming capabilities.
"""
def __init__(self, *args, **kwargs):
self.client = OpenAI(
api_key=os.getenv("OPENAI_API_KEY"), *args, **kwargs
)
pygame.init()
def run(
self, task: str, play_sound: bool = True, *args, **kwargs
):
"""
Run a task with the OpenAI API and optionally play the generated audio with improved streaming.
Args:
task (str): The task to be executed.
play_sound (bool): If True, play the generated audio.
Returns:
None
"""
try:
response = self.client.audio.speech.create(
model="tts-1",
voice="nova",
input=task,
*args,
**kwargs,
)
audio_url = response["url"]
logger.info("Task completed successfully.")
if play_sound:
with tempfile.NamedTemporaryFile(
delete=False, suffix=".mp3"
) as tmp_file:
with requests.get(audio_url, stream=True) as r:
r.raise_for_status()
for chunk in r.iter_content(chunk_size=8192):
tmp_file.write(chunk)
pygame.mixer.music.load(tmp_file.name)
pygame.mixer.music.play()
while pygame.mixer.music.get_busy():
pygame.time.Clock().tick(10)
except Exception as e:
logger.error(f"Error during task execution: {str(e)}")
# client = OpenAITTS(api_key=os.getenv("OPENAI_API_KEY"))
# client.run("Hello world! This is a streaming test.", play_sound=True)
def text_to_speech(
task: str, play_sound: bool = True, *args, **kwargs
):
out = OpenAITTS().run(
task, play_sound=play_sound, *args, **kwargs
)
return out
# print(text_to_speech(task="hello"))
Loading…
Cancel
Save