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79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
# Docs


## โœจ Features

### ๐Ÿฅ Virtual Physician Panel
- **8 Specialized AI Agents**: Each with distinct medical expertise and decision-making roles
- **Iterative Deliberation**: Sequential consultation and consensus-building process
- **Bayesian Reasoning**: Probability-based differential diagnosis updates
- **Cognitive Bias Detection**: Built-in challenger agent to prevent diagnostic errors

### ๐Ÿ’ฐ Cost-Effectiveness Optimization
- **Comprehensive Cost Tracking**: Real-time budget monitoring with 25+ medical test costs
- **Resource Stewardship**: AI agent dedicated to cost-conscious care decisions
- **Budget Constraints**: Configurable spending limits with intelligent test prioritization
- **Value-Based Testing**: Information theory-driven test selection

### ๐ŸŽฏ Multiple Operational Modes
- **Instant**: Immediate diagnosis from initial presentation
- **Question-Only**: History-taking without diagnostic tests
- **Budgeted**: Cost-constrained diagnostic workup
- **No-Budget**: Full diagnostic capability
- **Ensemble**: Multiple independent panels with consensus aggregation

### ๐Ÿ“Š Advanced Evaluation
- **Clinical Accuracy Scoring**: 5-point Likert scale with detailed rubric
- **Management Impact Assessment**: Evaluation based on treatment implications
- **Diagnostic Reasoning Tracking**: Complete conversation history and decision trails
- **Ensemble Methods**: Multi-run consensus for improved accuracy

### ๐Ÿ”ง Technical Excellence
- **Model Agnostic**: Support for GPT, Gemini, Claude, and other LLMs
- **Robust Error Handling**: Comprehensive exception management and fallback mechanisms
- **Beautiful Logging**: Structured logging with Loguru for debugging and monitoring
- **Type Safety**: Full Pydantic models and type hints throughout
- **Token-Optimized Prompts**: Compact, role-specific directives minimize token usage while preserving diagnostic quality

## ๐Ÿ— Architecture

### Virtual Panel Composition

The MAI-DxO system consists of 8 specialized AI agents that work together to provide comprehensive medical diagnosis:

#### Core Diagnostic Panel

**๐Ÿง  Dr. Hypothesis**
- Maintains probability-ranked differential diagnosis (top 3 conditions)
- Updates probabilities using Bayesian reasoning after each finding
- Tracks evidence supporting and contradicting each hypothesis

**๐Ÿ”ฌ Dr. Test-Chooser**
- Selects up to 3 diagnostic tests per round for maximum information value
- Optimizes for discriminatory power between competing hypotheses
- Balances diagnostic yield with patient burden

**๐Ÿค” Dr. Challenger**
- Acts as devil's advocate to prevent cognitive biases
- Identifies contradictory evidence and alternative explanations
- Proposes falsifying tests and guards against premature closure

**๐Ÿ’ฐ Dr. Stewardship**
- Enforces cost-conscious, high-value care decisions
- Advocates for cheaper alternatives when diagnostically equivalent
- Evaluates test necessity and suggests cost-effective strategies

**โœ… Dr. Checklist**
- Performs quality control on panel deliberations
- Validates test names and maintains logical consistency
- Flags errors and ensures proper diagnostic methodology

#### Coordination and Evaluation

**๐Ÿค Consensus Coordinator**
- Synthesizes panel input into optimal next action
- Decides between asking questions, ordering tests, or diagnosing
- Balances accuracy, cost, efficiency, and thoroughness

**๐Ÿ”‘ Gatekeeper**
- Serves as clinical information oracle with complete case access
- Provides objective findings and realistic synthetic results
- Maintains clinical realism while preventing information leakage

**โš–๏ธ Judge**
- Evaluates final diagnoses against ground truth
- Uses rigorous 5-point clinical rubric
- Considers management implications and diagnostic completeness

### Decision Process Flow

```mermaid
graph TD
    A[Initial Case Information] --> B[Panel Deliberation]
    B --> C{Consensus Decision}
    C -->|Ask| D[Question to Gatekeeper]
    C -->|Test| E[Diagnostic Tests]
    C -->|Diagnose| F[Final Diagnosis]
    D --> G[Update Case Information]
    E --> G
    G --> H{Max Iterations or Budget?}
    H -->|No| B
    H -->|Yes| F
    F --> I[Judge Evaluation]
    I --> J[Diagnosis Result]
```

## ๐ŸŽฎ Usage

### Basic Usage

```python
from mai_dx import MaiDxOrchestrator

# Initialize orchestrator
orchestrator = MaiDxOrchestrator(
    model_name="gemini/gemini-2.5-flash",
    max_iterations=10,
    initial_budget=10000
)

# Define case information
initial_info = "A 45-year-old male presents with chest pain..."
full_case = "Patient: 45-year-old male. History: Acute onset chest pain..."
ground_truth = "Myocardial infarction"

# Run diagnosis
result = orchestrator.run(initial_info, full_case, ground_truth)

# Access results
print(f"Diagnosis: {result.final_diagnosis}")
print(f"Accuracy Score: {result.accuracy_score}/5.0")
print(f"Total Cost: ${result.total_cost:,}")
print(f"Iterations: {result.iterations}")
```

### Advanced Configuration

```python
# Custom orchestrator with specific settings
orchestrator = MaiDxOrchestrator(
    model_name="gpt-4",
    max_iterations=15,
    initial_budget=5000,
    mode="budgeted",
    physician_visit_cost=250,
    enable_budget_tracking=True
)

# Enable debug logging
import os
os.environ["MAIDX_DEBUG"] = "1"
```

## ๐Ÿ“‹ MAI-DxO Variants

The system supports five distinct operational variants, each optimized for different clinical scenarios:

### 1. Instant Answer
```python
orchestrator = MaiDxOrchestrator.create_variant("instant")
result = orchestrator.run(initial_info, full_case, ground_truth)
```
- **Use Case**: Emergency triage, rapid screening
- **Behavior**: Immediate diagnosis from initial presentation only
- **Cost**: Single physician visit ($300)

### 2. Question-Only
```python
orchestrator = MaiDxOrchestrator.create_variant("question_only")
result = orchestrator.run(initial_info, full_case, ground_truth)
```
- **Use Case**: Telemedicine, history-taking focused consultations
- **Behavior**: Detailed questioning without diagnostic tests
- **Cost**: Physician visit only

### 3. Budgeted
```python
orchestrator = MaiDxOrchestrator.create_variant("budgeted", budget=3000)
result = orchestrator.run(initial_info, full_case, ground_truth)
```
- **Use Case**: Resource-constrained settings, cost-conscious care
- **Behavior**: Full panel with strict budget enforcement
- **Cost**: Limited by specified budget

### 4. No-Budget
```python
orchestrator = MaiDxOrchestrator.create_variant("no_budget")
result = orchestrator.run(initial_info, full_case, ground_truth)
```
- **Use Case**: Academic medical centers, complex cases
- **Behavior**: Full diagnostic capability without cost constraints
- **Cost**: Unlimited (tracks for analysis)

### 5. Ensemble
```python
orchestrator = MaiDxOrchestrator.create_variant("ensemble")
result = orchestrator.run_ensemble(initial_info, full_case, ground_truth, num_runs=3)
```
- **Use Case**: Critical diagnoses, second opinion simulation
- **Behavior**: Multiple independent panels with consensus aggregation
- **Cost**: Sum of all panel costs

## โš™๏ธ Configuration

### Model Configuration

```python
# Supported models
models = [
    "gemini/gemini-2.5-flash",
    "gpt-4o",
    "gpt-4o-mini", 
    "claude-3-5-sonnet-20241022",
    "meta-llama/llama-3.1-8b-instruct"
]

orchestrator = MaiDxOrchestrator(model_name="gpt-4o")
```

### Cost Database Customization

```python
# Access and modify cost database
orchestrator = MaiDxOrchestrator()
orchestrator.test_cost_db.update({
    "custom_test": 450,
    "specialized_imaging": 2000
})
```

### Logging Configuration

```python
# Enable detailed debug logging
import os
os.environ["MAIDX_DEBUG"] = "1"

# Custom log levels and formats available
```

## ๐Ÿ“– Examples

### Example 1: Comprehensive Diagnostic Workup

```python
from mai_dx import MaiDxOrchestrator

# Complex case requiring multiple tests
case_info = """
A 29-year-old woman was admitted to the hospital because of sore throat 
and peritonsillar swelling and bleeding. Symptoms did not abate with 
antimicrobial therapy.
"""

case_details = """
Patient: 29-year-old female.
History: Onset of sore throat 7 weeks prior to admission. Worsening 
right-sided pain and swelling. No fevers, headaches, or GI symptoms.
Physical Exam: Right peritonsillar mass, displacing the uvula.
Initial Labs: FBC, clotting studies normal.
"""

ground_truth = "Embryonal rhabdomyosarcoma of the pharynx"

# Run with different variants
variants = ["question_only", "budgeted", "no_budget"]
results = {}

for variant in variants:
    if variant == "budgeted":
        orch = MaiDxOrchestrator.create_variant(variant, budget=3000)
    else:
        orch = MaiDxOrchestrator.create_variant(variant)
    
    results[variant] = orch.run(case_info, case_details, ground_truth)

# Compare results
for variant, result in results.items():
    print(f"{variant}: {result.final_diagnosis} (Score: {result.accuracy_score})")
```

### Example 2: Ensemble Diagnosis

```python
# High-stakes diagnosis with ensemble approach
ensemble_orchestrator = MaiDxOrchestrator.create_variant("ensemble")

ensemble_result = ensemble_orchestrator.run_ensemble(
    initial_case_info=case_info,
    full_case_details=case_details,
    ground_truth_diagnosis=ground_truth,
    num_runs=5  # 5 independent diagnostic panels
)

print(f"Ensemble Diagnosis: {ensemble_result.final_diagnosis}")
print(f"Confidence Score: {ensemble_result.accuracy_score}/5.0")
print(f"Total Cost: ${ensemble_result.total_cost:,}")
```

### Example 3: Custom Cost Analysis

```python
# Analyze cost-effectiveness across variants
import matplotlib.pyplot as plt

variants = ["instant", "question_only", "budgeted", "no_budget"]
costs = []
accuracies = []

for variant in variants:
    orch = MaiDxOrchestrator.create_variant(variant)
    result = orch.run(case_info, case_details, ground_truth)
    costs.append(result.total_cost)
    accuracies.append(result.accuracy_score)

# Plot cost vs accuracy
plt.scatter(costs, accuracies)
plt.xlabel('Total Cost ($)')
plt.ylabel('Accuracy Score')
plt.title('Cost vs Accuracy Trade-off')
for i, variant in enumerate(variants):
    plt.annotate(variant, (costs[i], accuracies[i]))
plt.show()
```

## ๐Ÿ” API Reference

### MaiDxOrchestrator Class

#### Constructor
```python
MaiDxOrchestrator(
    model_name: str = "gemini/gemini-2.5-flash",
    max_iterations: int = 10,
    initial_budget: int = 10000,
    mode: str = "no_budget",
    physician_visit_cost: int = 300,
    enable_budget_tracking: bool = False
)
```

#### Methods

**`run(initial_case_info, full_case_details, ground_truth_diagnosis)`**
- Executes the sequential diagnostic process
- Returns: `DiagnosisResult` object

**`run_ensemble(initial_case_info, full_case_details, ground_truth_diagnosis, num_runs=3)`**
- Runs multiple independent sessions with consensus aggregation
- Returns: `DiagnosisResult` object

**`create_variant(variant, **kwargs)` (Class Method)**
- Factory method for creating specialized variants
- Variants: "instant", "question_only", "budgeted", "no_budget", "ensemble"

### DiagnosisResult Class

```python
@dataclass
class DiagnosisResult:
    final_diagnosis: str
    ground_truth: str
    accuracy_score: float
    accuracy_reasoning: str
    total_cost: int
    iterations: int
    conversation_history: str
```

### Utility Functions

**`run_mai_dxo_demo(case_info=None, case_details=None, ground_truth=None)`**
- Convenience function for quick demonstrations
- Returns: Dictionary of results from multiple variants

## ๐Ÿงช Testing and Validation

### Running Tests
```bash
# Run the built-in demo
python -m mai_dx.main

# Run with custom cases
python -c "
from mai_dx import run_mai_dxo_demo
results = run_mai_dxo_demo()
print(results)
"
```

### Benchmarking
```python
import time
from mai_dx import MaiDxOrchestrator

# Performance benchmarking
start_time = time.time()
orchestrator = MaiDxOrchestrator()
result = orchestrator.run(case_info, case_details, ground_truth)
elapsed = time.time() - start_time

print(f"Diagnosis completed in {elapsed:.2f} seconds")
print(f"Accuracy: {result.accuracy_score}/5.0")
print(f"Cost efficiency: ${result.total_cost/result.accuracy_score:.0f} per accuracy point")
```

## ๐Ÿค Contributing

We welcome contributions! Please see our [Contributing Guidelines](CONTRIBUTING.md) for details.

### Development Setup
```bash
git clone https://github.com/your-org/Open-MAI-Dx-Orchestrator.git
cd Open-MAI-Dx-Orchestrator
pip install -e ".[dev]"
pre-commit install
```

### Code Style
- Follow PEP 8 guidelines
- Use type hints throughout
- Maintain comprehensive docstrings
- Add tests for new features