RxGenerator: Edge-Native Clinical eRx Engine

Domain: Clinical Automation, Edge AI, Offline-First Embedded Systems
Stack: Elixir, Ash Framework 3.0, AshSqlite, Nx/Bumblebee, Phoenix LiveView
Status: In Active Development
Repository: GitHub Repository


1. The Problem

Outpatient clinical consultation in developing regions frequently occurs in erratic, low-connectivity ward environments. Existing electronic health record (EHR) systems introduce high network latency, break down entirely during broadband outages, and risk patient health information (PHI) leaks over public cloud infrastructure.

Crucially, attempting to solve clinical input using generative language models introduces unacceptable safety hazards: generative models hallucinate drug formulations, dosage ceilings, and brand names.


2. The Invariants Defended

  1. Zero-Hallucination Semantic Boundary: Generative models and neural weights are strictly banned from generating medication names, active ingredients, or dosages. 100% of prescribed items must resolve deterministically to a verified primary key in the national Directorate General of Drug Administration (DGDA) catalog (~41,000 SKUs).
  2. High Court Regulatory Compliance: Prescriptions must programmatically render the active generic molecule in bold, uppercase typography (String.upcase(generic_name)) with visual prominence equal to or greater than the commercial brand, enforcing Bangladesh High Court directives.
  3. Zero-Cloud Edge Autonomy: The entire clinical loop—shorthand tokenization, catalog resolution, signa translation, tamper-evident signing, and print spooling—must execute locally on standard PC hardware with zero external network dependencies and an active RAM ceiling under 250MB.

3. The Architectural Blueprint

  • Domain Modeling & Lifecycle: Implemented natively on Ash Framework 3.0 with AshSqlite. Prescription lifecycles follow a strict state machine (:draft $\longrightarrow$ :locked). Line mutations on :locked prescriptions are rejected at the Ash validation boundary, with historical retention preserved via AshArchival (zero hard SQL deletions).
  • Two-Tier Entity Resolution Engine:
    • Tier 1 (Sub-2ms Deterministic Search): Shorthand input (napa xtra 1+0+1 5d pc) is lexed via a right-to-left Deterministic Finite Automaton (DFA) that shields clinical strength units (500mg, 100ml) from greedy frequency extraction. Isolated brand tokens query an in-process SQLite FTS5 trigram virtual table (tokenize='trigram').
    • Tier 2 (Sub-15ms Semantic Fallback): When typos or phonetic spellings fall below FTS5 confidence, an in-process Nx / Bumblebee worker embeds the query via a 270M text-only encoder (EmbeddingGemma 2 truncated to 256 dimensions via Matryoshka Representation Learning) and performs cosine neighbor lookup using sqlite-vec. The neural model acts solely as an index pointer—it never produces unvetted text.
  • Offline Cryptographic Seal: Finalized prescriptions compile a compact, deterministic canonical manifest signed offline with an Ed25519 asymmetric keypair (via Erlang :crypto), embedding the signature and metadata into an offline-verifiable 2D QR code payload (BMDC_NO || DATE || RX_ID || DRUG_IDS).
  • Reactive UI & Translation: Built with Phoenix LiveView 1.2 delivering keyboard-driven (Tab/Enter/Arrow) omnibar navigation over a persistent local WebSocket. Canonical signa strings are deterministically mapped into colloquial Bengali (১+০+১ — ভরা পেটে — ৫ দিন) with zero AI translation dependencies.

4. Verification & Hardening Milestones

  • Deterministic Crash-Free Invariance: Lexer parsing and Bengali formatting pipelines hardened via StreamData property-based fuzz testing, proving arbitrary Unicode strings, punctuation noise, and messy doctor shorthand never raise unhandled exceptions in the LiveView process.
  • Edge Latency Target: Sub-5ms Tier-1 FTS5 trigram queries and sub-15ms full round-trip DOM patches in Phoenix LiveView.
  • Hardware Decoupling: Operating system print spooler drivers decoupled via explicit Elixir behaviours, allowing clean Mox-driven verification in automated test pipelines without requiring physical printer hardware.