A comprehensive medical analysis system with CLI and REST API interfaces, featuring anti-hallucination detection, provider review workflows, and AgentDB-powered pattern learning.
- Confidence Scoring: Multi-factor confidence assessment for all analyses
- Citation Verification: Automatic verification of medical literature citations
- Knowledge Base Validation: Cross-reference with trusted medical sources
- Contradiction Detection: Identify contradictory statements and recommendations
- Provider Approval: Automatic routing to healthcare providers for low-confidence results
# Analyze medical condition
medai analyze "diabetes" -s "increased thirst" "frequent urination" --age 45
# Interactive mode
medai analyze --interactive
# Verify analysis confidence
medai verify <analysisId> --detailed
# Provider review
medai provider review <analysisId> --decision approved --comments "Diagnosis confirmed"
# Notify provider
medai provider notify <analysisId> --urgent
# Configuration
medai config show
medai config set minimumConfidence 0.75POST /api/analyze - Submit medical query
{
"condition": "Type 2 Diabetes",
"symptoms": ["increased thirst", "frequent urination"],
"patientContext": {
"age": 45,
"gender": "male",
"medicalHistory": ["hypertension"],
"currentMedications": ["lisinopril"]
},
"options": {
"requireHighConfidence": true,
"includeEmergencyCheck": true
}
}GET /api/analysis/:id - Get analysis results
POST /api/provider/review - Submit provider review
{
"analysisId": "123e4567-e89b-12d3-a456-426614174000",
"decision": "approved",
"comments": "Diagnosis confirmed through lab results"
}POST /api/provider/notify - Notify provider
{
"analysisId": "123e4567-e89b-12d3-a456-426614174000",
"urgent": true
}GET /api/metrics - Get learning metrics
Real-time updates for analysis progress:
const ws = new WebSocket('ws://localhost:3000');
ws.on('message', (data) => {
const message = JSON.parse(data);
if (message.type === 'analysis_update') {
console.log(`Progress: ${message.payload.progress}%`);
console.log(`Status: ${message.payload.status}`);
}
});- Pattern Recognition: Learn from successful analyses
- Skill Library: Build reusable diagnostic skills
- Reflexion Learning: Improve from provider feedback
- Embedding Search: Find similar historical cases
src/
├── cli/ # CLI interface
│ └── index.ts # Command definitions
├── api/ # REST API
│ └── index.ts # Express server & WebSocket
├── services/ # Business logic
│ ├── medical-analysis.service.ts
│ ├── anti-hallucination.service.ts
│ ├── agentdb-learning.service.ts
│ └── provider.service.ts
├── middleware/ # Express middleware
│ ├── auth.middleware.ts
│ └── logging.middleware.ts
├── types/ # TypeScript types
│ └── medical.types.ts
└── utils/ # Utilities
└── config.ts
# Install dependencies
npm install
# Build
npm run build
# Run API server
npm start
# Run CLI
npm run cli -- analyze "condition" -s "symptom1" "symptom2"PORT=3000
AGENTDB_PATH=./data/medical-learning.db
MIN_CONFIDENCE=0.70
NODE_ENV=production
ALLOWED_ORIGINS=http://localhost:3000,https://app.example.com{
"antiHallucination": {
"minimumConfidence": 0.70,
"requireProviderReviewThreshold": 0.75,
"autoApproveThreshold": 0.90,
"enableContradictionDetection": true,
"enableCitationVerification": true
},
"providers": {
"notificationEnabled": true,
"autoAssignProvider": true,
"requiredForHighRisk": true
},
"api": {
"port": 3000,
"enableWebSocket": true,
"rateLimitPerMinute": 100
},
"learning": {
"enablePatternLearning": true,
"agentDbPath": "./data/medical-learning.db",
"minimumPatternFrequency": 3
}
}- Analysis Generation - AI generates medical analysis
- Confidence Scoring - Multi-factor confidence calculation
- Diagnosis confidence (0.3 weight)
- Citation verification (0.25 weight)
- Knowledge base validation (0.2 weight)
- Contradiction detection (0.15 weight)
- Provider alignment (0.1 weight)
- Warning Generation - Identify potential issues
- Low confidence
- Hallucination detection
- Contradictions
- Emergency situations
- Provider Review - Route to provider if needed
- Learning Update - Update AgentDB with outcomes
- Authentication: API key and Bearer token support
- Rate Limiting: 100 requests/minute per IP
- CORS: Configurable origin whitelist
- Helmet: Security headers
- Input Validation: Request validation
- Logging: Comprehensive request/response logging
- Analysis Triggers Review - Low confidence or high-risk
- Provider Notification - Email, SMS, or push notification
- Provider Reviews - Approve, reject, or modify
- Learning Update - System learns from provider decisions
- Pattern Recognition - Apply learnings to future cases
# Run in development mode
npm run dev
# Type checking
npm run typecheck
# Linting
npm run lint
# Tests
npm run testimport { MedicalAnalysisAPI } from './src/api';
const api = new MedicalAnalysisAPI(3000);
api.start();import { exec } from 'child_process';
exec('medai analyze "diabetes" -s "thirst" --output json', (error, stdout) => {
const result = JSON.parse(stdout);
console.log(result);
});import WebSocket from 'ws';
const ws = new WebSocket('ws://localhost:3000');
ws.on('open', () => {
ws.send(JSON.stringify({ type: 'subscribe', analysisId: '123' }));
});
ws.on('message', (data) => {
const message = JSON.parse(data.toString());
console.log('Update:', message);
});MIT
Built with the SPARC methodology and AgentDB integration.
