File size: 12,629 Bytes
9699bea
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3ca1d38
a8c922b
3ca1d38
 
 
a8c922b
aefac4f
a8c922b
3ca1d38
 
 
 
 
aefac4f
3ca1d38
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a8c922b
aefac4f
a8c922b
6dc9d46
a8c922b
 
6dc9d46
 
 
 
 
 
a8c922b
6dc9d46
 
 
a8c922b
6dc9d46
 
a8c922b
6dc9d46
 
a8c922b
 
6dc9d46
a8c922b
aefac4f
a8c922b
6dc9d46
 
 
 
 
 
 
 
 
a8c922b
aefac4f
a8c922b
6dc9d46
a8c922b
 
6dc9d46
a8c922b
6dc9d46
a8c922b
aefac4f
 
 
 
a8c922b
 
6dc9d46
a8c922b
 
3ca1d38
 
6dc9d46
aefac4f
3ca1d38
6dc9d46
 
 
 
aefac4f
3ca1d38
 
 
 
 
 
 
 
 
aefac4f
 
3ca1d38
 
aefac4f
6dc9d46
 
 
 
aefac4f
6dc9d46
 
 
 
aefac4f
6dc9d46
aefac4f
6dc9d46
aefac4f
 
6dc9d46
aefac4f
6dc9d46
aefac4f
 
 
 
 
 
 
6dc9d46
aefac4f
6dc9d46
aefac4f
 
 
6dc9d46
 
aefac4f
6dc9d46
 
 
aefac4f
6dc9d46
 
 
 
 
aefac4f
6dc9d46
 
 
 
 
 
 
 
 
aefac4f
6dc9d46
 
 
 
 
aefac4f
3ca1d38
 
6dc9d46
3ca1d38
aefac4f
3ca1d38
 
6dc9d46
aefac4f
6dc9d46
 
 
3ca1d38
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6dc9d46
 
aefac4f
6dc9d46
aefac4f
 
 
 
 
 
 
 
 
6dc9d46
aefac4f
6dc9d46
aefac4f
6dc9d46
 
 
 
 
 
 
 
aefac4f
6dc9d46
 
aefac4f
6dc9d46
 
aefac4f
6dc9d46
aefac4f
6dc9d46
aefac4f
 
 
6dc9d46
 
aefac4f
a8c922b
 
aefac4f
 
 
 
 
 
 
 
 
 
 
 
 
a8c922b
 
aefac4f
a8c922b
6dc9d46
 
 
 
 
a8c922b
aefac4f
a8c922b
6dc9d46
a8c922b
6dc9d46
 
 
 
a8c922b
aefac4f
a8c922b
6dc9d46
a8c922b
aefac4f
a8c922b
6dc9d46
 
 
 
a8c922b
6dc9d46
a8c922b
aefac4f
a8c922b
aefac4f
a8c922b
aefac4f
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
---
title: Agentic RagBot
emoji: πŸ₯
colorFrom: blue
colorTo: indigo
sdk: docker
pinned: true
license: mit
app_port: 7860
tags:
  - medical
  - biomarker
  - rag
  - healthcare
  - langgraph
  - agents
short_description: Multi-Agent RAG System for Medical Biomarker Analysis
---

# MediGuard AI: Multi-Agent RAG System for Medical Biomarker Analysis

A biomarker analysis system combining 6 specialized AI agents with medical knowledge retrieval (RAG) to provide evidence-based insights on blood test results.

> **⚠️ Disclaimer:** This is an AI-assisted analysis tool, NOT a medical device. Always consult healthcare professionals for medical decisions.

## Key Features

- **6 Specialist Agents** - Biomarker validation, disease scoring, RAG-powered explanation, confidence assessment
- **Medical Knowledge Base** - Clinical guidelines stored in vector database (FAISS or OpenSearch)
- **Multiple Interfaces** - Interactive CLI chat, REST API, Gradio web UI
- **Evidence-Based** - All recommendations backed by retrieved medical literature with citations
- **Free Cloud LLMs** - Uses Groq (LLaMA 3.3-70B) or Google Gemini - no API costs
- **Biomarker Normalization** - 80+ aliases mapped to 24 canonical biomarker names
- **Production Architecture** - Full error handling, safety alerts, confidence scoring

## Architecture Overview

```
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                     MediGuard AI Pipeline                      β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚  Input β†’ Guardrail β†’ Router β†’ ┬→ Biomarker Analysis Path      β”‚
β”‚                                β”‚   (6 specialist agents)       β”‚
β”‚                                β””β†’ General Medical Q&A Path     β”‚
β”‚                                    (RAG: retrieve β†’ grade)     β”‚
β”‚                          β†’ Response Synthesizer β†’ Output       β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
```

### Disease Scoring

The system uses **rule-based heuristics** (not ML models) to score disease likelihood:
- Diabetes: Glucose > 126, HbA1c β‰₯ 6.5
- Anemia: Hemoglobin < 12, MCV < 80
- Heart Disease: Cholesterol > 240, Troponin > 0.04
- Thrombocytopenia: Platelets < 150,000
- Thalassemia: MCV + Hemoglobin pattern

> **Note:** Future versions may include trained ML classifiers for improved accuracy.

## Quick Start

**Installation (5 minutes):**

```bash
# Clone & setup
git clone https://github.com/yourusername/ragbot.git
cd ragbot
python -m venv .venv
.venv\Scripts\activate  # Windows
pip install -r requirements.txt

# Get free API key
# 1. Sign up: https://console.groq.com/keys
# 2. Copy API key to .env

# Run setup
python scripts/setup_embeddings.py

# Start chatting
python scripts/chat.py
```

See **[QUICKSTART.md](QUICKSTART.md)** for detailed setup instructions.

## Documentation

| Document | Purpose |
|----------|---------|
| [**QUICKSTART.md**](QUICKSTART.md) | 5-minute setup guide |
| [**CONTRIBUTING.md**](CONTRIBUTING.md) | How to contribute |
| [**docs/ARCHITECTURE.md**](docs/ARCHITECTURE.md) | System design & components |
| [**docs/API.md**](docs/API.md) | REST API reference |
| [**docs/DEVELOPMENT.md**](docs/DEVELOPMENT.md) | Development & extension guide |
| [**scripts/README.md**](scripts/README.md) | Utility scripts reference |
| [**examples/README.md**](examples/) | Web/mobile integration examples |

## Usage

### Interactive CLI

```bash
python scripts/chat.py

You: My glucose is 140 and HbA1c is 10

Primary Finding: Diabetes (100% confidence)
Critical Alerts: Hyperglycemia, elevated HbA1c
Recommendations: Seek medical attention, lifestyle changes
Actions: Physical activity, reduce carbs, weight loss
```

### REST API

```bash
# Start the unified production server
uvicorn src.main:app --reload

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

# Ask medical questions (RAG-powered)
curl -X POST http://localhost:8000/ask \
  -H "Content-Type: application/json" \
  -d '{
    "question": "What does high HbA1c mean?"
  }'

# Search knowledge base directly
curl -X POST http://localhost:8000/search \
  -H "Content-Type: application/json" \
  -d '{
    "query": "diabetes management guidelines",
    "top_k": 5
  }'
```

See **[docs/API.md](docs/API.md)** for full API reference.

## Project Structure

```
RagBot/
β”œβ”€β”€ src/                           # Core application
β”‚   β”œβ”€β”€ __init__.py
β”‚   β”œβ”€β”€ workflow.py               # Multi-agent orchestration (LangGraph)
β”‚   β”œβ”€β”€ state.py                  # Pydantic state models
β”‚   β”œβ”€β”€ biomarker_validator.py    # Validation logic
β”‚   β”œβ”€β”€ biomarker_normalization.py # Name normalization (80+ aliases)
β”‚   β”œβ”€β”€ llm_config.py             # LLM/embedding provider config
β”‚   β”œβ”€β”€ pdf_processor.py          # Vector store management
β”‚   β”œβ”€β”€ config.py                 # Global configuration
β”‚   └── agents/                   # 6 specialist agents
β”‚       β”œβ”€β”€ __init__.py
β”‚       β”œβ”€β”€ biomarker_analyzer.py
β”‚       β”œβ”€β”€ disease_explainer.py
β”‚       β”œβ”€β”€ biomarker_linker.py
β”‚       β”œβ”€β”€ clinical_guidelines.py
β”‚       β”œβ”€β”€ confidence_assessor.py
β”‚       └── response_synthesizer.py
β”‚
β”œβ”€β”€ api/                          # REST API (FastAPI)
β”‚   β”œβ”€β”€ app/main.py              # FastAPI server
β”‚   β”œβ”€β”€ app/routes/              # API endpoints
β”‚   β”œβ”€β”€ app/models/schemas.py    # Pydantic request/response schemas
β”‚   └── app/services/            # Business logic
β”‚
β”œβ”€β”€ scripts/                      # Utilities
β”‚   β”œβ”€β”€ chat.py                  # Interactive CLI chatbot
β”‚   └── setup_embeddings.py      # Vector store builder
β”‚
β”œβ”€β”€ config/                       # Configuration
β”‚   └── biomarker_references.json # 24 biomarker reference ranges
β”‚
β”œβ”€β”€ data/                         # Data storage
β”‚   β”œβ”€β”€ medical_pdfs/            # Source documents
β”‚   └── vector_stores/           # FAISS database
β”‚
β”œβ”€β”€ tests/                        # Test suite (30 tests)
β”œβ”€β”€ examples/                     # Integration examples
β”œβ”€β”€ docs/                         # Documentation
β”‚
β”œβ”€β”€ QUICKSTART.md               # Setup guide
β”œβ”€β”€ CONTRIBUTING.md             # Contribution guidelines
β”œβ”€β”€ requirements.txt            # Python dependencies
└── LICENSE
```

## Technology Stack

| Component | Technology | Purpose |
|-----------|-----------|---------|
| Orchestration | **LangGraph** | Multi-agent workflow control |
| LLM | **Groq (LLaMA 3.3-70B)** | Fast, free inference |
| LLM (Alt) | **Google Gemini 2.0 Flash** | Free alternative |
| Embeddings | **HuggingFace / Jina / Google** | Vector representations |
| Vector DB | **FAISS** (local) / **OpenSearch** (production) | Similarity search |
| API | **FastAPI** | REST endpoints |
| Web UI | **Gradio** | Interactive analysis interface |
| Validation | **Pydantic V2** | Type safety & schemas |
| Cache | **Redis** (optional) | Response caching |
| Observability | **Langfuse** (optional) | LLM tracing & monitoring |

## How It Works

```
User Input ("My glucose is 140...")
    β”‚
    β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  Biomarker Extraction & Normalization β”‚  ← LLM parses text, maps 80+ aliases
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
    β”‚
    β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  Disease Scoring (Rule-Based)         β”‚  ← Heuristic scoring, NOT ML
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
    β”‚
    β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  RAG Knowledge Retrieval              β”‚  ← FAISS/OpenSearch vector search
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
    β”‚
    β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  6-Agent LangGraph Pipeline           β”‚
β”‚  β”œβ”€ Biomarker Analyzer (validation)   β”‚
β”‚  β”œβ”€ Disease Explainer (pathophysiology)β”‚
β”‚  β”œβ”€ Biomarker Linker (key drivers)    β”‚
β”‚  β”œβ”€ Clinical Guidelines (treatment)   β”‚
β”‚  β”œβ”€ Confidence Assessor (reliability) β”‚
β”‚  └─ Response Synthesizer (final)      β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
    β”‚
    β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  Structured Response + Safety Alerts  β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
```

## Supported Biomarkers (24)

- **Glucose Control**: Glucose, HbA1c, Insulin
- **Lipids**: Cholesterol, LDL Cholesterol, HDL Cholesterol, Triglycerides
- **Body Metrics**: BMI
- **Blood Cells**: Hemoglobin, Platelets, White Blood Cells, Red Blood Cells, Hematocrit
- **RBC Indices**: Mean Corpuscular Volume, Mean Corpuscular Hemoglobin, MCHC
- **Cardiovascular**: Heart Rate, Systolic Blood Pressure, Diastolic Blood Pressure, Troponin
- **Inflammation**: C-reactive Protein
- **Liver**: ALT, AST
- **Kidney**: Creatinine

See [config/biomarker_references.json](config/biomarker_references.json) for full reference ranges.

## Disease Coverage

- Diabetes
- Anemia
- Heart Disease
- Thrombocytopenia
- Thalassemia
- (Extensible - add custom domains)

## Privacy & Security

- All processing runs **locally** after setup
- No personal health data stored
- Embeddings computed locally or cached
- Vector store derived from public medical literature
- Can operate completely offline with Ollama provider

## Performance

- **Response Time**: 15-25 seconds (6 agents + RAG retrieval)
- **Knowledge Base**: 750 pages, 2,609 document chunks
- **Cost**: Free (Groq/Gemini API + local/cloud embeddings)
- **Hardware**: CPU-only (no GPU needed)

## Testing

```bash
# Run unit tests (30 tests)
.venv\Scripts\python.exe -m pytest tests/ -q \
  --ignore=tests/test_basic.py \
  --ignore=tests/test_diabetes_patient.py \
  --ignore=tests/test_evolution_loop.py \
  --ignore=tests/test_evolution_quick.py \
  --ignore=tests/test_evaluation_system.py

# Run specific test file
.venv\Scripts\python.exe -m pytest tests/test_codebase_fixes.py -v

# Run all tests (includes integration tests requiring LLM API keys)
.venv\Scripts\python.exe -m pytest tests/ -v
```

## Contributing

Contributions welcome! See **[CONTRIBUTING.md](CONTRIBUTING.md)** for:
- Code style guidelines
- Pull request process
- Testing requirements
- Development setup

## Development

Want to extend RagBot?

- **Add custom biomarkers**: [docs/DEVELOPMENT.md](docs/DEVELOPMENT.md#adding-a-new-biomarker)
- **Add medical domains**: [docs/DEVELOPMENT.md](docs/DEVELOPMENT.md#adding-a-new-medical-domain)
- **Create custom agents**: [docs/DEVELOPMENT.md](docs/DEVELOPMENT.md#creating-a-custom-analysis-agent)
- **Switch LLM providers**: [docs/DEVELOPMENT.md](docs/DEVELOPMENT.md#switching-llm-providers)

## License

MIT License - See [LICENSE](LICENSE)

## Resources

- [LangGraph Documentation](https://langchain-ai.github.io/langgraph/)
- [Groq API Docs](https://console.groq.com)
- [FAISS GitHub](https://github.com/facebookresearch/faiss)
- [FastAPI Guide](https://fastapi.tiangolo.com/)

---

**Ready to get started?** -> [QUICKSTART.md](QUICKSTART.md)

**Want to understand the architecture?** -> [docs/ARCHITECTURE.md](docs/ARCHITECTURE.md)

**Looking to integrate with your app?** -> [examples/README.md](examples/)