Skip to main content

DSPy extension for STCC medical triage with specialized nurses

Project description

STCC Triage Agent - DSPy Extension (v2.0.0)

Python 3.11+ License: MIT DSPy

Professional medical triage system powered by DSPy and DeepSeek, with 10 specialized nurse agents.

โš ๏ธ IMPORTANT: Educational and research use only. NOT approved for clinical use.


Demo

Demo

Interactive triage system with 10 specialized nurse agents. Watch full video


What's New in v2.0.0

This is now a professional DSPy extension that users can install locally:

  • ๐Ÿ“ฆ PyPI package: pip install dspy-stcc-homecare
  • ๐Ÿš€ CLI commands: stcc-ui, stcc-optimize, stcc-api
  • ๐Ÿ—๏ธ Clean structure: All code in stcc_triage/ package
  • ๐Ÿ“š Bundled data: 225 STCC protocols pre-parsed and included
  • ๐Ÿ’พ Zero setup: Just install and run - no manual configuration
  • ๐Ÿ”„ Breaking changes: Clean v2.0.0 release (no backward compatibility)

Installation

Quick Start (PyPI)

# Install the package
pip install dspy-stcc-homecare

# Configure API key
export DEEPSEEK_API_KEY="your-key-here"

# Launch UI
stcc-ui

Development Setup

# Clone repository
git clone https://github.com/chenhaodev/dspy-stcc-homecare.git
cd dspy-stcc-homecare

# Install in editable mode
pip install -e .

# Or use uv (faster)
uv pip install -e .

# Install with API support
pip install -e ".[api]"

Configure API Key

# Option 1: Environment variable
export DEEPSEEK_API_KEY="your-key-here"

# Option 2: .env file (for development)
cp .env.example .env
# Edit .env and add: DEEPSEEK_API_KEY=your_key_here

Quick Start

Launch Web UI

stcc-ui
# Opens at http://localhost:8501

Features:

  • ๐Ÿฅ Select from 10 specialized nurses
  • ๐Ÿ’ฌ Interactive chat interface
  • ๐ŸŽจ Color-coded triage levels
  • ๐Ÿ” View reasoning steps
  • ๐Ÿ”ง Check optimization status

Python API

from stcc_triage import STCCTriageAgent
from stcc_triage.nurses import WoundCareNurse

# Use baseline agent
agent = STCCTriageAgent()
result = agent.triage("55-year-old with severe chest pain and shortness of breath")

print(f"Triage Level: {result.triage_level}")
print(f"Justification: {result.clinical_justification}")

# Use specialized nurse (requires compilation first)
nurse = WoundCareNurse()
result = nurse.triage("deep laceration with bleeding")

Specialized Nurses

Instead of one generic agent, this system provides 10 specialized nurses, each optimized for their domain:

Nurse Specialization Protocol Count Example Cases
Wound Care Trauma, burns, bleeding 24 Deep laceration, severe burn
OB/Maternal Pregnancy, labor 13 Contractions at 32 weeks, bleeding
Pediatric Children/infants 12 Infant fever, child vomiting
Neuro Stroke, seizure 7 Sudden weakness, worst headache
GI Abdominal, digestive 6 Severe abdominal pain, GI bleeding
Respiratory Breathing, asthma 6 COPD exacerbation, wheezing
Mental Health Behavioral crises 5 Suicide risk, panic attack
CHF Heart failure 4 Dyspnea, edema, chest pain
ED Emergency/acute All Multi-trauma, critical
PreOp Pre-surgical 2 Surgical clearance

Each nurse is optimized with domain-specific training data using DSPy's BootstrapFewShot.


Optimize Nurses

Optimize a Specific Nurse

stcc-optimize --role wound_care_nurse

# Other roles: ob_nurse, pediatric_nurse, neuro_nurse, gi_nurse,
# respiratory_nurse, mental_health_nurse, chf_nurse, ed_nurse, preop_nurse

Output:

Compiling Specialized Agent: Wound Care Nurse
Training set: 16 specialized cases
Distribution:
  emergency: 4 cases
  home_care: 4 cases
  moderate: 4 cases
  urgent: 4 cases

Optimizing for Wound Care Nurse...
This may take 5-10 minutes...

โœ“ Compiled Wound Care Nurse agent saved to:
  user_data/compiled/compiled_wound_care_nurse_agent.json

Optimize All Nurses

stcc-optimize

This compiles all 10 specialized nurses (takes ~1 hour).


Launch API Server

# Launch FastAPI server
stcc-api

# With auto-reload for development
stcc-api --reload

# Custom host/port
stcc-api --host 127.0.0.1 --port 8080

Visit http://localhost:8000/docs for interactive API documentation.

Example API Usage:

# Basic triage
curl -X POST "http://localhost:8000/triage" \
  -H "Content-Type: application/json" \
  -d '{"symptoms": "severe chest pain for 30 minutes"}'

# Specialized nurse triage
curl -X POST "http://localhost:8000/triage/specialized" \
  -H "Content-Type: application/json" \
  -d '{"symptoms": "deep laceration with active bleeding", "nurse_role": "wound_care_nurse"}'

Architecture

Package Structure

dspy-stcc-homecare/
โ”œโ”€โ”€ stcc_triage/              # Main package
โ”‚   โ”œโ”€โ”€ core/                 # Core triage logic
โ”‚   โ”‚   โ”œโ”€โ”€ agent.py          # STCCTriageAgent
โ”‚   โ”‚   โ”œโ”€โ”€ signatures.py     # DSPy signatures
โ”‚   โ”‚   โ”œโ”€โ”€ settings.py       # DeepSeek config
โ”‚   โ”‚   โ””โ”€โ”€ paths.py          # Path management
โ”‚   โ”‚
โ”‚   โ”œโ”€โ”€ nurses/               # Specialized nurses
โ”‚   โ”‚   โ”œโ”€โ”€ roles.py          # NurseRole enum
โ”‚   โ”‚   โ””โ”€โ”€ specialized.py    # WoundCareNurse, OBNurse, etc.
โ”‚   โ”‚
โ”‚   โ”œโ”€โ”€ datasets/             # Dataset generation
โ”‚   โ”‚   โ”œโ”€โ”€ schema.py         # PatientCase schema
โ”‚   โ”‚   โ”œโ”€โ”€ generator.py      # Dataset generator
โ”‚   โ”‚   โ””โ”€โ”€ cases/            # Case definitions
โ”‚   โ”‚
โ”‚   โ”œโ”€โ”€ optimizers/           # Optimization logic
โ”‚   โ”‚   โ”œโ”€โ”€ metric.py         # Safety metrics
โ”‚   โ”‚   โ”œโ”€โ”€ optimizer.py      # BootstrapFewShot config
โ”‚   โ”‚   โ””โ”€โ”€ compiler.py       # Compilation logic
โ”‚   โ”‚
โ”‚   โ”œโ”€โ”€ protocols/            # Protocol handling
โ”‚   โ”‚   โ”œโ”€โ”€ parser.py         # Protocol parser
โ”‚   โ”‚   โ””โ”€โ”€ context.py        # Context enrichment
โ”‚   โ”‚
โ”‚   โ”œโ”€โ”€ ui/                   # Streamlit UI
โ”‚   โ”‚   โ”œโ”€โ”€ app.py            # Main app
โ”‚   โ”‚   โ””โ”€โ”€ components/       # UI components
โ”‚   โ”‚
โ”‚   โ”œโ”€โ”€ api/                  # FastAPI deployment
โ”‚   โ”‚   โ”œโ”€โ”€ app.py            # FastAPI app
โ”‚   โ”‚   โ””โ”€โ”€ models.py         # API models
โ”‚   โ”‚
โ”‚   โ”œโ”€โ”€ cli/                  # CLI commands
โ”‚   โ”‚   โ”œโ”€โ”€ ui.py             # stcc-ui
โ”‚   โ”‚   โ”œโ”€โ”€ optimize.py       # stcc-optimize
โ”‚   โ”‚   โ”œโ”€โ”€ api.py            # stcc-api
โ”‚   โ”‚   โ””โ”€โ”€ parse.py          # stcc-parse-protocols
โ”‚   โ”‚
โ”‚   โ””โ”€โ”€ data/                 # Bundled data
โ”‚       โ””โ”€โ”€ protocols/        # STCC protocols (~2MB)
โ”‚
โ”œโ”€โ”€ user_data/                # User-generated (gitignored)
โ”‚   โ”œโ”€โ”€ compiled/             # Compiled nurses
โ”‚   โ””โ”€โ”€ datasets/             # Generated datasets
โ”‚
โ””โ”€โ”€ protocols/                # Generated protocols.json

How It Works

  1. Protocol Context: Enriches patient symptoms with relevant STCC protocol guidelines
  2. DSPy ChainOfThought: Structured reasoning with transparent decision-making
  3. DeepSeek R1: Advanced reasoning engine with medical knowledge
  4. BootstrapFewShot: Automatic optimization with domain-specific examples
  5. Safety Metrics: Prevents under-triage (missing emergencies = score 0.0)

CLI Reference

# Launch Web UI
stcc-ui

# Optimize nurses
stcc-optimize                           # All nurses
stcc-optimize --role wound_care_nurse   # Specific nurse
stcc-optimize --regenerate-data         # Force regenerate training data

# Launch API server
stcc-api                                # Default: 0.0.0.0:8000
stcc-api --host 127.0.0.1 --port 8080   # Custom host/port
stcc-api --reload                       # Auto-reload for dev

# Parse protocols
stcc-parse-protocols                    # (Optional) Re-parse STCC markdown files

Python API Reference

# Core imports
from stcc_triage import STCCTriageAgent, TriageSignature, FollowUpSignature

# Specialized nurses
from stcc_triage.nurses import (
    WoundCareNurse,
    OBNurse,
    PediatricNurse,
    NeuroNurse,
    GINurse,
    RespiratoryNurse,
    MentalHealthNurse,
    CHFNurse,
    EDNurse,
    PreOpNurse,
    GeneralNurse,
    NurseRole,
)

# Optimization
from stcc_triage.optimizers import optimize_nurse, load_compiled_nurse

# Dataset generation
from stcc_triage.datasets import generate_all_specialized_datasets

# Protocol parsing
from stcc_triage.protocols import parse_all_protocols

Development

Run Tests

pytest tests/

Code Quality

# Format code
ruff format .

# Lint code
ruff check .

# Type checking
mypy stcc_triage/

Troubleshooting

"protocols.json not found"

This should only happen in development. The protocols are pre-bundled when installing from PyPI.

# For development only - regenerate protocols
stcc-parse-protocols

"Compiled agent not found"

stcc-optimize --role wound_care_nurse

Import errors after installation

pip install -e .  # Reinstall in editable mode

License

MIT License - See LICENSE


Contributing

Contributions welcome! This is an educational project demonstrating DSPy optimization for domain-specific agents.


Citation

If you use this project in research:

@software{stcc_triage_agent,
  title = {STCC Triage Agent: DSPy Extension for Medical Triage},
  author = {STCC Triage Agent Contributors},
  year = {2025},
  url = {https://github.com/chenhaodev/dspy-stcc-homecare}
}

Acknowledgments

  • STCC Protocols: Schmitt-Thompson Clinical Content (225 protocols)
  • DSPy: Stanford NLP's programming framework for LMs
  • DeepSeek: Advanced reasoning engine with medical knowledge

Built with DSPy โ€ข Optimized for Safety โ€ข Educational Use Only

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dspy_stcc_homecare-2.0.1.tar.gz (7.4 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

dspy_stcc_homecare-2.0.1-py3-none-any.whl (491.4 kB view details)

Uploaded Python 3

File details

Details for the file dspy_stcc_homecare-2.0.1.tar.gz.

File metadata

  • Download URL: dspy_stcc_homecare-2.0.1.tar.gz
  • Upload date:
  • Size: 7.4 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.8

File hashes

Hashes for dspy_stcc_homecare-2.0.1.tar.gz
Algorithm Hash digest
SHA256 c375555a30642c64edb8d36480d1fd1cf5a52676b623010189669fa6ce5e05e1
MD5 14616fe7d477427dc7605b12fa71dbc7
BLAKE2b-256 aca42bcb13e36a67c832cd72fdce03b48ca4b4ed12e8ac3b51f5c1e9d7889ae5

See more details on using hashes here.

File details

Details for the file dspy_stcc_homecare-2.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for dspy_stcc_homecare-2.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 b9efe679378306376eb27a5e95bff870253427c68ccd053884dec84e6bf33612
MD5 d5958ae93cab9a368c8b5b150ff8052a
BLAKE2b-256 88e8331b1fdff18fdf50e711f6a4261e34f7e13fbb92427e001681207fd81bcd

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page