Spaces:
Sleeping
Sleeping
Merge pull request #6 from harshalmore31/main
Browse filesRefactor README and module documentation; and ensured type safe methods
- .gitignore +1 -0
- README.md +71 -25
- mai_dx/__init__.py +69 -2
- mai_dx/main.py +8 -8
.gitignore
CHANGED
@@ -2,6 +2,7 @@
|
|
2 |
__pycache__/
|
3 |
*.py[cod]
|
4 |
*$py.class
|
|
|
5 |
|
6 |
# C extensions
|
7 |
*.so
|
|
|
2 |
__pycache__/
|
3 |
*.py[cod]
|
4 |
*$py.class
|
5 |
+
agent_workspace
|
6 |
|
7 |
# C extensions
|
8 |
*.so
|
README.md
CHANGED
@@ -1,72 +1,118 @@
|
|
1 |
-
#
|
2 |
|
3 |
-
> **An open-source implementation of
|
|
|
|
|
4 |
|
5 |
[](https://arxiv.org/abs/2506.22405)
|
6 |
[](LICENSE)
|
7 |
[](https://python.org)
|
8 |
|
9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
|
11 |
## 🚀 Quick Start
|
12 |
|
|
|
|
|
|
|
|
|
13 |
```bash
|
14 |
-
# Install the package
|
15 |
pip install mai-dx
|
|
|
|
|
|
|
16 |
|
17 |
-
|
18 |
git clone https://github.com/The-Swarm-Corporation/Open-MAI-Dx-Orchestrator.git
|
19 |
cd Open-MAI-Dx-Orchestrator
|
20 |
-
pip install -
|
21 |
```
|
22 |
|
23 |
-
|
24 |
|
25 |
-
|
26 |
-
Configure your api keys and environment variables like the following.
|
27 |
|
28 |
```txt
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
ANTHROPIC_API_KEY=""
|
33 |
```
|
34 |
|
35 |
-
|
36 |
-
|
37 |
-
## Example
|
38 |
|
39 |
```python
|
40 |
from mai_dx import MaiDxOrchestrator
|
41 |
|
42 |
-
# Create orchestrator
|
43 |
-
orchestrator = MaiDxOrchestrator(
|
44 |
|
45 |
-
# Run diagnosis
|
46 |
result = orchestrator.run(
|
47 |
initial_case_info="29-year-old woman with sore throat and peritonsillar swelling...",
|
48 |
full_case_details="Patient: 29-year-old female. History: Onset of sore throat...",
|
49 |
ground_truth_diagnosis="Embryonal rhabdomyosarcoma of the pharynx"
|
50 |
)
|
51 |
|
52 |
-
|
|
|
53 |
print(f"Accuracy: {result.accuracy_score}/5.0")
|
54 |
-
print(f"Cost: ${result.total_cost
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
55 |
```
|
56 |
|
57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
58 |
|
59 |
## Documentation
|
60 |
|
61 |
Learn more about this repository [with the docs](DOCS.md)
|
62 |
|
|
|
|
|
|
|
|
|
63 |
## 📄 License
|
64 |
|
65 |
-
This project is licensed under the MIT License
|
66 |
|
67 |
## 📚 Citation
|
68 |
|
69 |
-
If you use this
|
70 |
|
71 |
```bibtex
|
72 |
@misc{nori2025sequentialdiagnosislanguagemodels,
|
@@ -103,4 +149,4 @@ If you use this implementation in your research, please cite both the original p
|
|
103 |
|
104 |
<p align="center">
|
105 |
<strong>Built with Swarms for advancing AI-powered medical diagnosis</strong>
|
106 |
-
</p>
|
|
|
1 |
+
# MAI Diagnostic Orchestrator (MAI-DxO)
|
2 |
|
3 |
+
> **An open-source implementation of Microsoft Research's "Sequential Diagnosis with Language Models" paper, built with the Swarms AI framework.**
|
4 |
+
|
5 |
+
MAI-DxO (MAI Diagnostic Orchestrator) is a sophisticated AI-powered diagnostic system that simulates a virtual panel of physician-agents to perform iterative medical diagnosis with cost-effectiveness optimization. This implementation faithfully reproduces the methodology described in the Microsoft Research paper while providing additional features and flexibility.
|
6 |
|
7 |
[](https://arxiv.org/abs/2506.22405)
|
8 |
[](LICENSE)
|
9 |
[](https://python.org)
|
10 |
|
11 |
+
## ✨ Key Features
|
12 |
+
|
13 |
+
- **8 AI Physician Agents**: Specialized roles for comprehensive diagnosis.
|
14 |
+
- **5 Operational Modes**: Instant, question-only, budgeted, no-budget, and ensemble modes.
|
15 |
+
- **Cost Tracking**: Real-time budget monitoring with costs for 25+ medical tests.
|
16 |
+
- **Clinical Evaluation**: 5-point accuracy scoring with detailed feedback.
|
17 |
+
- **Model Agnostic**: Works with GPT, Gemini, Claude, and other leading LLMs.
|
18 |
|
19 |
## 🚀 Quick Start
|
20 |
|
21 |
+
### 1. Installation
|
22 |
+
|
23 |
+
Install the package directly via pip:
|
24 |
+
|
25 |
```bash
|
|
|
26 |
pip install mai-dx
|
27 |
+
```
|
28 |
+
|
29 |
+
Or, for development, clone the repository and install the requirements:
|
30 |
|
31 |
+
```bash
|
32 |
git clone https://github.com/The-Swarm-Corporation/Open-MAI-Dx-Orchestrator.git
|
33 |
cd Open-MAI-Dx-Orchestrator
|
34 |
+
pip install -r requirements.txt
|
35 |
```
|
36 |
|
37 |
+
### 2. Environment Setup
|
38 |
|
39 |
+
Create a `.env` file in your project root and add your API keys:
|
|
|
40 |
|
41 |
```txt
|
42 |
+
OPENAI_API_KEY="Your OpenAI API key"
|
43 |
+
GEMINI_API_KEY="Your Gemini API key"
|
44 |
+
ANTHROPIC_API_KEY="Your Anthropic API key"
|
|
|
45 |
```
|
46 |
|
47 |
+
### 3. Basic Usage
|
|
|
|
|
48 |
|
49 |
```python
|
50 |
from mai_dx import MaiDxOrchestrator
|
51 |
|
52 |
+
# Create the orchestrator (defaults to a capable model)
|
53 |
+
orchestrator = MaiDxOrchestrator()
|
54 |
|
55 |
+
# Run a diagnosis
|
56 |
result = orchestrator.run(
|
57 |
initial_case_info="29-year-old woman with sore throat and peritonsillar swelling...",
|
58 |
full_case_details="Patient: 29-year-old female. History: Onset of sore throat...",
|
59 |
ground_truth_diagnosis="Embryonal rhabdomyosarcoma of the pharynx"
|
60 |
)
|
61 |
|
62 |
+
# Print the results
|
63 |
+
print(f"Final Diagnosis: {result.final_diagnosis}")
|
64 |
print(f"Accuracy: {result.accuracy_score}/5.0")
|
65 |
+
print(f"Total Cost: ${result.total_cost:,.2f}")
|
66 |
+
```
|
67 |
+
|
68 |
+
## ⚙️ Advanced Usage & Configuration
|
69 |
+
|
70 |
+
Customize the orchestrator's model, budget, and operational mode.
|
71 |
+
|
72 |
+
```python
|
73 |
+
from mai_dx import MaiDxOrchestrator
|
74 |
+
|
75 |
+
# Configure with a specific model and budget
|
76 |
+
orchestrator = MaiDxOrchestrator(
|
77 |
+
model_name="gemini/gemini-2.5-flash", # or "gpt-4", "claude-3-5-sonnet"
|
78 |
+
max_iterations=10,
|
79 |
+
initial_budget=3000,
|
80 |
+
mode="budgeted" # Other modes: "instant", "question_only", "no_budget"
|
81 |
+
)
|
82 |
+
|
83 |
+
# Run the diagnosis
|
84 |
+
# ...
|
85 |
```
|
86 |
|
87 |
+
## 🏥 How It Works: The Virtual Physician Panel
|
88 |
+
|
89 |
+
MAI-DxO employs a multi-agent system where each agent has a specific role:
|
90 |
+
|
91 |
+
- **🧠 Dr. Hypothesis**: Maintains the differential diagnosis.
|
92 |
+
- **🔬 Dr. Test-Chooser**: Selects the most cost-effective diagnostic tests.
|
93 |
+
- **🤔 Dr. Challenger**: Prevents cognitive biases and diagnostic errors.
|
94 |
+
- **💰 Dr. Stewardship**: Ensures cost-effective care.
|
95 |
+
- **✅ Dr. Checklist**: Performs quality control checks.
|
96 |
+
- **🤝 Consensus Coordinator**: Synthesizes panel decisions.
|
97 |
+
- **🔑 Gatekeeper**: Acts as the clinical information oracle.
|
98 |
+
- **⚖️ Judge**: Evaluates the final diagnostic accuracy.
|
99 |
+
|
100 |
|
101 |
## Documentation
|
102 |
|
103 |
Learn more about this repository [with the docs](DOCS.md)
|
104 |
|
105 |
+
## 🤝 Contributing
|
106 |
+
|
107 |
+
We welcome contributions! Please feel free to open an issue or submit a pull request.
|
108 |
+
|
109 |
## 📄 License
|
110 |
|
111 |
+
This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.
|
112 |
|
113 |
## 📚 Citation
|
114 |
|
115 |
+
If you use this work in your research, please cite both the original paper and this software implementation.
|
116 |
|
117 |
```bibtex
|
118 |
@misc{nori2025sequentialdiagnosislanguagemodels,
|
|
|
149 |
|
150 |
<p align="center">
|
151 |
<strong>Built with Swarms for advancing AI-powered medical diagnosis</strong>
|
152 |
+
</p>
|
mai_dx/__init__.py
CHANGED
@@ -1,3 +1,70 @@
|
|
1 |
-
from
|
2 |
|
3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from typing import Any, Optional
|
2 |
|
3 |
+
from mai_dx.main import (
|
4 |
+
MaiDxOrchestrator,
|
5 |
+
DiagnosisResult,
|
6 |
+
Action,
|
7 |
+
AgentRole,
|
8 |
+
run_mai_dxo_demo
|
9 |
+
)
|
10 |
+
|
11 |
+
__version__ = "1.0.0"
|
12 |
+
__author__ = "The Swarm Corporation"
|
13 |
+
__description__ = "AI-powered diagnostic system with virtual physician panels"
|
14 |
+
|
15 |
+
# Main exports
|
16 |
+
__all__ = [
|
17 |
+
"MaiDxOrchestrator",
|
18 |
+
"DiagnosisResult",
|
19 |
+
"Action",
|
20 |
+
"AgentRole",
|
21 |
+
"run_mai_dxo_demo"
|
22 |
+
]
|
23 |
+
|
24 |
+
# Convenience imports for common usage patterns
|
25 |
+
def create_orchestrator(
|
26 |
+
model_name: str = "gemini/gemini-2.5-flash",
|
27 |
+
mode: str = "no_budget",
|
28 |
+
**kwargs: Any
|
29 |
+
) -> MaiDxOrchestrator:
|
30 |
+
"""
|
31 |
+
Convenience function to create a MAI-DxO orchestrator.
|
32 |
+
|
33 |
+
Args:
|
34 |
+
model_name: Language model to use
|
35 |
+
mode: Operational mode
|
36 |
+
**kwargs: Additional configuration parameters
|
37 |
+
|
38 |
+
Returns:
|
39 |
+
Configured MaiDxOrchestrator instance
|
40 |
+
"""
|
41 |
+
if mode in ["instant", "question_only", "budgeted", "no_budget", "ensemble"]:
|
42 |
+
return MaiDxOrchestrator.create_variant(mode, model_name=model_name, **kwargs)
|
43 |
+
else:
|
44 |
+
return MaiDxOrchestrator(model_name=model_name, mode=mode, **kwargs)
|
45 |
+
|
46 |
+
|
47 |
+
def quick_diagnosis(
|
48 |
+
case_info: str,
|
49 |
+
case_details: str,
|
50 |
+
ground_truth: Optional[str] = None,
|
51 |
+
model_name: str = "gemini/gemini-2.5-flash"
|
52 |
+
) -> DiagnosisResult:
|
53 |
+
"""
|
54 |
+
Convenience function for quick diagnosis without configuration.
|
55 |
+
|
56 |
+
Args:
|
57 |
+
case_info: Initial case presentation
|
58 |
+
case_details: Complete case information
|
59 |
+
ground_truth: Correct diagnosis (optional)
|
60 |
+
model_name: Model to use
|
61 |
+
|
62 |
+
Returns:
|
63 |
+
DiagnosisResult with diagnosis and evaluation
|
64 |
+
"""
|
65 |
+
orchestrator = MaiDxOrchestrator(model_name=model_name, max_iterations=5)
|
66 |
+
return orchestrator.run(
|
67 |
+
initial_case_info=case_info,
|
68 |
+
full_case_details=case_details,
|
69 |
+
ground_truth_diagnosis=ground_truth or "Unknown"
|
70 |
+
)
|
mai_dx/main.py
CHANGED
@@ -18,7 +18,7 @@ Key Features:
|
|
18 |
|
19 |
Example Usage:
|
20 |
# Standard MAI-DxO usage
|
21 |
-
orchestrator = MaiDxOrchestrator(model_name="
|
22 |
result = orchestrator.run(initial_case_info, full_case_details, ground_truth)
|
23 |
|
24 |
# Budget-constrained variant
|
@@ -131,7 +131,7 @@ class MaiDxOrchestrator:
|
|
131 |
|
132 |
def __init__(
|
133 |
self,
|
134 |
-
model_name: str = "
|
135 |
max_iterations: int = 10,
|
136 |
initial_budget: int = 10000,
|
137 |
mode: str = "no_budget", # "instant", "question_only", "budgeted", "no_budget", "ensemble"
|
@@ -198,7 +198,7 @@ class MaiDxOrchestrator:
|
|
198 |
f"🏥 MAI Diagnostic Orchestrator initialized successfully in '{mode}' mode with budget ${initial_budget:,}"
|
199 |
)
|
200 |
|
201 |
-
def _init_agents(self):
|
202 |
"""Initializes all required agents with their specific roles and prompts."""
|
203 |
self.agents = {
|
204 |
role: Agent(
|
@@ -1387,11 +1387,11 @@ def run_mai_dxo_demo(
|
|
1387 |
orchestrator = MaiDxOrchestrator.create_variant(
|
1388 |
variant,
|
1389 |
budget=3000,
|
1390 |
-
model_name="
|
1391 |
)
|
1392 |
else:
|
1393 |
orchestrator = MaiDxOrchestrator.create_variant(
|
1394 |
-
variant, model_name="
|
1395 |
)
|
1396 |
|
1397 |
result = orchestrator.run(
|
@@ -1466,13 +1466,13 @@ def run_mai_dxo_demo(
|
|
1466 |
# orchestrator = MaiDxOrchestrator.create_variant(
|
1467 |
# variant_name,
|
1468 |
# budget=3000,
|
1469 |
-
# model_name="
|
1470 |
# max_iterations=5,
|
1471 |
# )
|
1472 |
# else:
|
1473 |
# orchestrator = MaiDxOrchestrator.create_variant(
|
1474 |
# variant_name,
|
1475 |
-
# model_name="
|
1476 |
# max_iterations=5,
|
1477 |
# )
|
1478 |
|
@@ -1504,7 +1504,7 @@ def run_mai_dxo_demo(
|
|
1504 |
|
1505 |
# ensemble_orchestrator = MaiDxOrchestrator.create_variant(
|
1506 |
# "ensemble",
|
1507 |
-
# model_name="
|
1508 |
# max_iterations=3, # Shorter iterations for ensemble
|
1509 |
# )
|
1510 |
|
|
|
18 |
|
19 |
Example Usage:
|
20 |
# Standard MAI-DxO usage
|
21 |
+
orchestrator = MaiDxOrchestrator(model_name="gpt-4.1")
|
22 |
result = orchestrator.run(initial_case_info, full_case_details, ground_truth)
|
23 |
|
24 |
# Budget-constrained variant
|
|
|
131 |
|
132 |
def __init__(
|
133 |
self,
|
134 |
+
model_name: str = "gpt-4.1",
|
135 |
max_iterations: int = 10,
|
136 |
initial_budget: int = 10000,
|
137 |
mode: str = "no_budget", # "instant", "question_only", "budgeted", "no_budget", "ensemble"
|
|
|
198 |
f"🏥 MAI Diagnostic Orchestrator initialized successfully in '{mode}' mode with budget ${initial_budget:,}"
|
199 |
)
|
200 |
|
201 |
+
def _init_agents(self) -> None:
|
202 |
"""Initializes all required agents with their specific roles and prompts."""
|
203 |
self.agents = {
|
204 |
role: Agent(
|
|
|
1387 |
orchestrator = MaiDxOrchestrator.create_variant(
|
1388 |
variant,
|
1389 |
budget=3000,
|
1390 |
+
model_name="gpt-4.1",
|
1391 |
)
|
1392 |
else:
|
1393 |
orchestrator = MaiDxOrchestrator.create_variant(
|
1394 |
+
variant, model_name="gpt-4.1"
|
1395 |
)
|
1396 |
|
1397 |
result = orchestrator.run(
|
|
|
1466 |
# orchestrator = MaiDxOrchestrator.create_variant(
|
1467 |
# variant_name,
|
1468 |
# budget=3000,
|
1469 |
+
# model_name="gpt-4.1",
|
1470 |
# max_iterations=5,
|
1471 |
# )
|
1472 |
# else:
|
1473 |
# orchestrator = MaiDxOrchestrator.create_variant(
|
1474 |
# variant_name,
|
1475 |
+
# model_name="gpt-4.1",
|
1476 |
# max_iterations=5,
|
1477 |
# )
|
1478 |
|
|
|
1504 |
|
1505 |
# ensemble_orchestrator = MaiDxOrchestrator.create_variant(
|
1506 |
# "ensemble",
|
1507 |
+
# model_name="gpt-4.1",
|
1508 |
# max_iterations=3, # Shorter iterations for ensemble
|
1509 |
# )
|
1510 |
|