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:

  • ๐Ÿ“ฆ Installable package: pip install -e .
  • ๐Ÿš€ CLI commands: stcc-ui, stcc-optimize, stcc-api
  • ๐Ÿ—๏ธ Clean structure: All code in stcc_triage/ package
  • ๐Ÿ“š Bundled protocols: STCC-chinese protocols (~2MB) included
  • ๐Ÿ’พ User data separation: Compiled agents in user_data/ (gitignored)
  • ๐Ÿ”„ Breaking changes: Clean v2.0.0 release (no backward compatibility)

Installation

1. Clone the Repository

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

2. Install Dependencies

# Install with pip (editable mode)
pip install -e .

# Or install with uv (recommended - faster)
uv pip install -e .

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

3. Configure API Key

cp .env.example .env
# Edit .env and add: DEEPSEEK_API_KEY=your_key_here

4. Parse Protocols (First Time Only)

stcc-parse-protocols

This parses 225 STCC protocols from stcc_triage/data/protocols/STCC-chinese/ and generates protocols/protocols.json.


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                    # Parse STCC markdown to JSON

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"

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.0.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.0-py3-none-any.whl (491.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: dspy_stcc_homecare-2.0.0.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.0.tar.gz
Algorithm Hash digest
SHA256 b3ee0efc0d7da498888831818959fca8db4d78f8d4c6b61587fa61d114b0aac5
MD5 93566ab1a8ca7d8fcda344c450dc466b
BLAKE2b-256 19e5b99e8bbabced3d6e1dd10a078e758c8d0aee8f98652241d38e1202d96bf7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dspy_stcc_homecare-2.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 de4fb1c01b2d145a4c3382f0696daf456f6235764b5680d729c81b70da08fb1b
MD5 ca5c71397f2dd91022aa1d5218ea8150
BLAKE2b-256 36a5d470476c3bd5b5f02508c7e2b8d1041641c941427b9fe45785b029d8802d

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