File size: 8,694 Bytes
c4f5f25
6dc9d46
 
 
c4f5f25
6dc9d46
 
 
 
c4f5f25
 
6dc9d46
 
 
 
 
c4f5f25
 
6dc9d46
 
 
 
 
c4f5f25
6dc9d46
 
 
c4f5f25
6dc9d46
 
 
 
c4f5f25
 
 
 
 
6dc9d46
c4f5f25
 
 
6dc9d46
 
c4f5f25
6dc9d46
 
 
 
 
 
c4f5f25
 
6dc9d46
 
 
c4f5f25
aefac4f
c4f5f25
6dc9d46
 
c4f5f25
 
aefac4f
c4f5f25
 
aefac4f
c4f5f25
 
 
 
 
 
aefac4f
 
 
 
c4f5f25
aefac4f
c4f5f25
aefac4f
c4f5f25
aefac4f
 
c4f5f25
 
6dc9d46
c4f5f25
 
6dc9d46
 
c4f5f25
 
 
 
6dc9d46
 
c4f5f25
aefac4f
c4f5f25
6dc9d46
 
 
 
 
 
 
c4f5f25
6dc9d46
c4f5f25
 
 
 
 
 
 
 
6dc9d46
 
c4f5f25
 
 
 
 
 
6dc9d46
c4f5f25
 
 
 
 
6dc9d46
c4f5f25
 
 
 
 
 
 
6dc9d46
 
c4f5f25
 
 
 
 
6dc9d46
 
 
c4f5f25
6dc9d46
c4f5f25
6dc9d46
c4f5f25
 
 
6dc9d46
c4f5f25
 
 
 
 
 
aefac4f
6dc9d46
c4f5f25
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6dc9d46
c4f5f25
aefac4f
c4f5f25
 
 
6dc9d46
 
c4f5f25
6dc9d46
 
c4f5f25
 
 
 
 
 
 
 
 
 
6dc9d46
c4f5f25
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6dc9d46
c4f5f25
6dc9d46
 
c4f5f25
6dc9d46
 
c4f5f25
6dc9d46
 
c4f5f25
 
6dc9d46
 
 
c4f5f25
 
 
6dc9d46
c4f5f25
 
 
 
 
 
 
 
 
 
 
 
 
 
6dc9d46
c4f5f25
6dc9d46
c4f5f25
 
 
 
 
6dc9d46
 
c4f5f25
 
 
 
 
 
 
 
aefac4f
6dc9d46
 
 
c4f5f25
6dc9d46
 
c4f5f25
 
6dc9d46
c4f5f25
 
 
 
 
 
 
 
 
6dc9d46
c4f5f25
 
 
6dc9d46
 
 
c4f5f25
 
 
 
 
6dc9d46
 
 
c4f5f25
 
 
 
 
 
 
 
 
 
 
 
6dc9d46
 
 
c4f5f25
6dc9d46
c4f5f25
 
 
 
 
 
 
 
 
6dc9d46
 
 
 
c4f5f25
6dc9d46
c4f5f25
6dc9d46
c4f5f25
 
 
 
 
6dc9d46
c4f5f25
 
 
 
 
6dc9d46
 
c4f5f25
6dc9d46
 
c4f5f25
 
 
 
 
 
 
6dc9d46
c4f5f25
 
 
 
 
 
 
 
 
 
 
 
6dc9d46
 
 
 
 
c4f5f25
 
6dc9d46
 
c4f5f25
6dc9d46
 
c4f5f25
 
 
 
 
 
 
6dc9d46
c4f5f25
6dc9d46
c4f5f25
 
6dc9d46
c4f5f25
6dc9d46
c4f5f25
 
 
 
6dc9d46
 
c4f5f25
6dc9d46
c4f5f25
6dc9d46
c4f5f25
 
 
 
 
 
 
 
 
 
 
 
6dc9d46
c4f5f25
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6dc9d46
c4f5f25
6dc9d46
c4f5f25
 
 
 
 
6dc9d46
c4f5f25
 
 
 
6dc9d46
c4f5f25
 
 
 
6dc9d46
c4f5f25
6dc9d46
c4f5f25
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
# MediGuard AI REST API Documentation

## Overview

MediGuard AI provides a comprehensive RESTful API for integrating biomarker analysis and medical Q&A into applications, web services, and dashboards.

## Base URL

```
Development: http://localhost:8000
Production: https://api.mediguard-ai.com
```

## Quick Start

1. **Start the API server:**
   ```bash
   uvicorn src.main:app --reload
   ```

2. **API will be available at:**
   - Interactive docs: http://localhost:8000/docs
   - OpenAPI schema: http://localhost:8000/openapi.json
   - ReDoc: http://localhost:8000/redoc

## Authentication

Currently no authentication required for development. Production will include:
- API keys
- JWT tokens
- Rate limiting

```http
Authorization: Bearer YOUR_API_KEY
```

## API Endpoints

### Health Check

Check if the API is running and healthy.

```http
GET /health
```

**Response:**
```json
{
  "status": "healthy",
  "version": "2.0.0",
  "timestamp": "2024-03-15T10:30:00Z"
}
```

### Detailed Health Check

Get detailed health status of all services.

```http
GET /health/detailed
```

**Response:**
```json
{
  "status": "healthy",
  "version": "2.0.0",
  "services": {
    "opensearch": "connected",
    "redis": "connected",
    "llm": "connected"
  }
}
```

## Biomarker Analysis

### Structured Analysis

Analyze biomarkers using structured input.

```http
POST /analyze/structured
```

**Request Body:**
```json
{
  "biomarkers": {
    "Glucose": 140,
    "HbA1c": 10.0,
    "Hemoglobin": 11.5,
    "MCV": 75
  },
  "patient_context": {
    "age": 45,
    "gender": "male",
    "symptoms": ["fatigue", "thirst"]
  }
}
```

**Response:**
```json
{
  "status": "success",
  "analysis": {
    "primary_findings": [
      {
        "condition": "Diabetes",
        "confidence": 0.95,
        "evidence": {
          "glucose": 140,
          "hba1c": 10.0
        }
      }
    ],
    "critical_alerts": [
      {
        "type": "hyperglycemia",
        "severity": "high",
        "message": "Very high glucose levels detected"
      }
    ],
    "recommendations": [
      {
        "action": "Seek immediate medical attention",
        "priority": "urgent"
      }
    ],
    "biomarker_flags": [
      {
        "name": "Glucose",
        "value": 140,
        "status": "high",
        "reference_range": "70-100 mg/dL"
      }
    ]
  },
  "metadata": {
    "timestamp": "2024-03-15T10:30:00Z",
    "model_version": "2.0.0",
    "processing_time": 1.2
  }
}
```

### Natural Language Analysis

Analyze biomarkers from natural language input.

```http
POST /analyze/natural
```

**Request Body:**
```json
{
  "text": "My recent blood test shows glucose of 140 and HbA1c of 10. I'm a 45-year-old male feeling very tired lately.",
  "extract_biomarkers": true
}
```

**Response:**
```json
{
  "status": "success",
  "extracted_data": {
    "biomarkers": {
      "Glucose": 140,
      "HbA1c": 10.0
    },
    "patient_context": {
      "age": 45,
      "gender": "male",
      "symptoms": ["tired"]
    }
  },
  "analysis": {
    // Same structure as structured analysis
  }
}
```

## Medical Q&A

### Ask Question

Ask medical questions with RAG-powered answers.

```http
POST /ask
```

**Request Body:**
```json
{
  "question": "What are the symptoms of diabetes?",
  "context": {
    "patient_age": 45,
    "gender": "male"
  }
}
```

**Response:**
```json
{
  "status": "success",
  "answer": {
    "content": "Common symptoms of diabetes include increased thirst, frequent urination, fatigue, and blurred vision...",
    "sources": [
      {
        "title": "Diabetes Mellitus - Clinical Guidelines",
        "snippet": "Patients often present with polyuria, polydipsia, and unexplained weight loss...",
        "confidence": 0.92
      }
    ],
    "related_questions": [
      "How is diabetes diagnosed?",
      "What are the treatment options for diabetes?"
    ]
  },
  "metadata": {
    "timestamp": "2024-03-15T10:30:00Z",
    "model": "llama-3.3-70b",
    "retrieval_count": 5
  }
}
```

### Streaming Ask

Get streaming responses for real-time chat.

```http
POST /ask/stream
```

**Request Body:**
```json
{
  "question": "Explain what HbA1c means",
  "stream": true
}
```

**Response (Server-Sent Events):**
```
data: {"type": "start", "id": "msg_123"}

data: {"type": "token", "content": "HbA1c is a "}

data: {"type": "token", "content": "blood test that "}

data: {"type": "token", "content": "measures your "}

...

data: {"type": "end", "id": "msg_123"}
```

## Knowledge Base Search

### Search Documents

Search the medical knowledge base.

```http
POST /search
```

**Request Body:**
```json
{
  "query": "diabetes management guidelines",
  "top_k": 5,
  "filters": {
    "document_type": ["guideline", "research"],
    "date_range": {
      "start": "2020-01-01",
      "end": "2024-12-31"
    }
  }
}
```

**Response:**
```json
{
  "status": "success",
  "results": [
    {
      "id": "doc_123",
      "title": "ADA Standards of Medical Care in Diabetes",
      "snippet": "The ADA recommends HbA1c testing every 3 months for patients with diabetes...",
      "score": 0.95,
      "metadata": {
        "document_type": "guideline",
        "publication_date": "2024-01-15",
        "authors": ["American Diabetes Association"]
      }
    }
  ],
  "total_found": 1247,
  "search_time": 0.15
}
```

## Error Handling

### Error Response Format

All errors return a consistent format:

```json
{
  "status": "error",
  "error": {
    "code": "VALIDATION_ERROR",
    "message": "Invalid biomarker values",
    "details": [
      {
        "field": "biomarkers.Glucose",
        "issue": "Value must be between 0 and 1000"
      }
    ]
  },
  "request_id": "req_789"
}
```

### Common Error Codes

| Code | Description |
|------|-------------|
| VALIDATION_ERROR | Invalid input data |
| PROCESSING_ERROR | Error during analysis |
| RATE_LIMIT_EXCEEDED | Too many requests |
| SERVICE_UNAVAILABLE | Required service is down |
| AUTHENTICATION_ERROR | Invalid API key |

## SDK Examples

### Python

```python
import httpx

client = httpx.Client(base_url="http://localhost:8000")

# Analyze biomarkers
response = client.post("/analyze/structured", json={
    "biomarkers": {"Glucose": 140, "HbA1c": 10.0}
})
analysis = response.json()

# Ask question
response = client.post("/ask", json={
    "question": "What causes diabetes?"
})
answer = response.json()
```

### JavaScript

```javascript
const client = http.createClient({
  baseURL: 'http://localhost:8000'
});

// Analyze biomarkers
const analysis = await client.post('/analyze/structured', {
  biomarkers: { Glucose: 140, HbA1c: 10.0 }
});

// Stream response
const stream = await client.post('/ask/stream', {
  question: 'Explain diabetes',
  stream: true
});

for await (const chunk of stream) {
  if (chunk.type === 'token') {
    process.stdout.write(chunk.content);
  }
}
```

### cURL

```bash
# Analyze biomarkers
curl -X POST http://localhost:8000/analyze/structured \
  -H "Content-Type: application/json" \
  -d '{
    "biomarkers": {"Glucose": 140, "HbA1c": 10.0}
  }'

# Ask question
curl -X POST http://localhost:8000/ask \
  -H "Content-Type: application/json" \
  -d '{
    "question": "What are the symptoms of diabetes?"
  }'
```

## Rate Limiting

- **Development**: No limits
- **Production**: 1000 requests per hour per API key

Rate limit headers are included in responses:

```http
X-RateLimit-Limit: 1000
X-RateLimit-Remaining: 999
X-RateLimit-Reset: 1642790400
```

## Data Models

### Biomarker Analysis Request

```typescript
interface BiomarkerAnalysisRequest {
  biomarkers: Record<string, number>;
  patient_context?: {
    age?: number;
    gender?: "male" | "female" | "other";
    symptoms?: string[];
    medications?: string[];
    medical_history?: string[];
  };
}
```

### Biomarker Analysis Response

```typescript
interface BiomarkerAnalysisResponse {
  status: "success" | "error";
  analysis?: {
    primary_findings: Finding[];
    critical_alerts: Alert[];
    recommendations: Recommendation[];
    biomarker_flags: BiomarkerFlag[];
  };
  metadata?: {
    timestamp: string;
    model_version: string;
    processing_time: number;
  };
}
```

## API Changelog

### v2.0.0 (Current)
- Added multi-agent workflow
- Improved confidence scoring
- Added streaming responses
- Enhanced error handling

### v1.5.0
- Added natural language analysis
- Improved biomarker normalization
- Added batch processing

### v1.0.0
- Initial release
- Basic biomarker analysis
- Medical Q&A

## Support

For API support:
- Documentation: https://docs.mediguard-ai.com
- Email: api-support@mediguard-ai.com
- GitHub Issues: https://github.com/yourusername/Agentic-RagBot/issues