DEV Community

Cover image for How I Built ChartPilot: An AI Agent That Gives Nigerian Hospital Doctors Instant Access to Patient Records in Under 5 Seconds
Ekpenyong Mfon
Ekpenyong Mfon

Posted on

How I Built ChartPilot: An AI Agent That Gives Nigerian Hospital Doctors Instant Access to Patient Records in Under 5 Seconds

The Problem I See Every Day

I work as a Statistician at a University Teaching Hospital in Calabar, Nigeria.

Every day I watch the same thing happen.

A doctor is mid-consultation. A patient is sitting across from them. There are 40 more patients waiting outside. The doctor needs to know what the patient's last malaria RDT result was, or what their haemoglobin was three months ago, or whether their blood pressure has been trending upward across visits.

The answer is in the Hospital Management Information System. The data exists.

But finding it requires opening the HMIS, searching by exact patient ID or full name, navigating to the correct module, and filtering by date and test type.

That takes 2 to 4 minutes. So the doctor does one of three things:

  1. Re-orders the test, wasting reagents and patient money
  2. Makes a decision without the prior context, a patient safety risk
  3. Skips the HMIS entirely, making the system investment worthless

This is the problem ChartPilot solves.


What Is ChartPilot?

ChartPilot is an Autopilot Agent that adds a conversational retrieval layer on top of a hospital's existing HMIS database.

A doctor types naturally:

"Show me Emeka's last two malaria RDT results"

ChartPilot then does five things:

  1. Parses the query using Qwen-Max on Alibaba Cloud
  2. Identifies the patient, handling partial names, abbreviations, and Nigerian Pidgin
  3. Executes safe read-only SQL against the HMIS database
  4. Scans results for critical clinical values
  5. Returns a Qwen-generated clinical summary

All in under 5 seconds. No new hospital infrastructure required.


Why Qwen-Max?

This is not a generic answer. Qwen-Max was the right choice for ChartPilot for specific technical reasons.

Nigerian clinical communication involves constant code-switching between standard English, medical abbreviations like FBC and PCV, and Nigerian Pidgin such as "Wetin be im last result?" which means "What is their latest result?"

Models trained primarily on Western clinical text fail on these inputs. Qwen-Max, with its multilingual foundation, handles them naturally.

Additionally, Qwen-Max's native tool-calling capability maps directly onto ChartPilot's SQL generation step. The model returns structured JSON with the interpreted query and safe SQL, which the backend executes directly.


Architecture

The system has five core components.

Tech Stack

  • Language Model: Qwen-Max via DashScope International API
  • Backend: FastAPI on Alibaba Cloud ECS
  • Database: SQLite for development, ApsaraDB RDS for production
  • Frontend: React + Vite
  • Compliance: NDPR audit logging

1. Qwen-Max Intent Parser

Receives the doctor's natural language query and returns structured output containing the interpreted meaning, a safe read-only SQL statement, and any patient name hints for disambiguation.

2. HMIS Database Adapter

Read-only connector to the hospital's existing database. The full schema is passed to Qwen-Max so it generates accurate SQL without hallucinating table names.

3. Structured Query Engine

All write operations including INSERT, UPDATE, DELETE, DROP, and ALTER are blocked at this layer before any query reaches the database. This is a hard constraint, not a prompt instruction.

4. Critical Value Flag Engine

Scans every result for clinically dangerous values using Nigerian teaching hospital reference ranges.

  • Haemoglobin below 7.0 g/dL = CRITICAL
  • Malaria RDT Positive = CRITICAL
  • Systolic BP above 180 mmHg = CRITICAL
  • SpO2 below 90% = CRITICAL

5. NDPR Audit Logger

Every access is logged with timestamp, doctor ID, SHA-256 query hash, and patient IDs accessed, in compliance with Nigeria's Data Protection Regulation.


The Challenges I Hit

Challenge 1: Nigerian IP Address Block

The standard DashScope endpoint at dashscope.aliyuncs.com rejected connections from Nigerian IP addresses with a 401 error.

Solution: The international endpoint at dashscope-intl.aliyuncs.com works perfectly from Nigeria.

This is important for every African developer building on Qwen Cloud. Use the international endpoint from day one.

Challenge 2: Package Naming Conflict

I named my module qwen_agent.py which conflicted with the qwen-agent pip package installed as a dependency. Python kept importing the wrong module.

Solution: Renamed to chartpilot_qwen.py. Simple fix but it took an hour to diagnose.

Challenge 3: Python 3.13 Compatibility

pydantic-core 2.7.1 required Rust compilation on Python 3.13 and failed without Visual Studio Build Tools installed.

Solution: Upgraded to pydantic 2.9.0 or higher which ships pre-built wheels for Python 3.13.


What It Looks Like Running

When a doctor types "Show me Emeka's last two malaria RDT results", ChartPilot finds three patients named Emeka and asks the doctor to confirm which one. This patient disambiguation feature is a genuine clinical safety mechanism. It prevents wrong-patient errors that happen when doctors work at speed.

When the doctor specifies the patient ID, ChartPilot retrieves all lab results and flags the abnormal Urinalysis RBC result automatically. The doctor sees what matters most, first.


What I Learned

Build for the specific context, not the general case.
ChartPilot works because it was designed for Nigerian teaching hospitals, not for "healthcare in general." The Pidgin support, the NDPR compliance, the Nigerian clinical flag thresholds, these details make it real.

Qwen-Max is genuinely strong for African clinical contexts.
This is a technical finding, not marketing. The multilingual reasoning capability handles code-switching that breaks other models.

The dashscope-intl.aliyuncs.com endpoint is essential for Africa.
If you are building on Qwen Cloud from Nigeria, Ghana, Kenya, or anywhere else in Africa, use the international endpoint from day one.

Read-only enforcement belongs at the database layer.
LLMs can be manipulated through prompt injection. SQL keyword blocking at the adapter layer cannot be bypassed regardless of what the model generates.


What's Next

  • Voice input so doctors can speak queries hands-free during consultations
  • Multi-facility deployment across teaching hospital departments
  • Integration with Nigeria's National Health Insurance Authority
  • National rollout to over 200 Nigerian teaching hospitals

Try It

GitHub: https://github.com/ekpenyongasuquo/chartpilot

Demo Video: https://youtu.be/LrapOaIzfio

Track: Global AI Hackathon with Qwen Cloud, Track 4: Autopilot Agent


Built by Ekpenyong Asuquo, Statistician at the University of Calabar Teaching Hospital, Calabar, Nigeria.

The data exists. The need is real. ChartPilot makes existing hospital data conversationally accessible to the doctors who need it most.

Top comments (1)

Collapse
 
alexshev profile image
Alex Shev

This is the kind of agent use case that benefits from tight workflow boundaries. Fast lookup, clear source records, and a human decision at the end is a strong pattern for healthcare.