Audit-ready medical insurance verification & clinical-routing agent for AfyaPlus Health — RAG-grounded answers, validated medication-dosing tool, session memory, and PII masking compliant with the Kenya Data Protection Act (2019).
# AfyaPlus Health — RAG-Powered Verification & Clinical Routing Agent
An audit-ready medical insurance verification and clinical-routing assistant for
AfyaPlus Health. It answers member and agent inquiries by grounding every
response in a local knowledge base, calculates medication dose volumes through a
validated tool, tracks context across a conversation, and protects personal data
with a masking layer that keeps identifiers out of the language model entirely.
> **Note on data.** The guideline documents in `knowledge_manual/` and the dosing
> figures are **synthetic**, authored for this capstone. They are realistic but
> are not clinical authority and must not be used for real medical decisions.
---
## Architecture
Raw input flows through a fixed pipeline:
```
raw input
─▶ PII masking phone/email ─▶ placeholder tokens (before the model)
─▶ LangChain agent reasoning + tool calls + session memory
├─ search_knowledge_base grounded LlamaIndex retriever (RAG)
└─ calculate_dose_volume validated medication-math tool
─▶ PII de-masking tokens ─▶ real values (after approval)
─▶ final answer
```
The masking boundary means the OpenAI API never receives a raw Kenyan phone
number or email address, while the member still sees their real details in the
final reply.
### Components
| File | Responsibility |
|------|----------------|
| `src/masking.py` | Detect Kenyan PII (phone/email), swap for stable tokens, restore on output. |
| `src/knowledge.py` | Load → chunk → embed → persist the manuals; expose a LangChain retriever tool. |
| `src/tools.py` | `@tool` medication dose→volume calculator with layered validation. |
| `src/prompts.py` | System prompt encoding the no-diagnosis / no-prescribing guardrails. |
| `src/agent.py` | Tool-calling agent + per-session message history. |
| `src/main.py` | End-to-end entry point wiring mask → agent → unmask. |
---
## Setup
Requires Python 3.11+ and an OpenAI API key.
```bash
# 1. Create and activate a virt …