SPE-ECG: An Ontology-Guided Knowledge Representation Framework for Enhancing LLM-Based Code Generation
Talha Mahmood, Muhammad Tanvir Afzal, Ahmad Sami Al-Shamayleh, Ghulam Mustafa, Isabel De la Torre Díez, Lázaro Javier Hernández Rodríguez, Pablo Herrero GarcíaLarge Language Models (LLMs) can translate natural-language instructions into code, but when the input is unstructured clinical protocol text, the generated code may omit doses, monitoring intervals, or safety thresholds that the text states. Such omissions matter because they can propagate into software that supports clinical monitoring. We propose SPE-ECG (Structured Prompt Engineering for Enhanced Code Generation), a transparent, rule-based pipeline that converts unstructured domain text into a JSON prompt using text cleaning, part-of-speech tagging, bigram matching, and hand-built domain dictionaries, without fine-tuning any model. In a case study of 43 rheumatoid arthritis DMARD monitoring protocols, we compared raw and structured prompts on three LLMs (GPT-3.5, LLaMA 2, and Gemma), scoring the generated Python code using version 3.15.1 with BLEU against the raw-prompt code of the other two models. Mean BLEU rose for all three models (relative gains of 55.1%, 32.5%, and 19.9%); the paired differences were significant for GPT-3.5 and LLaMA 2 but not, after correction for multiple comparisons, for Gemma. These results suggest that lightweight input structuring can change how closely different models’ outputs agree, and that it could serve as a low-cost, auditable preparation step for protocol-to-code tools built on free or open models. BLEU does not measure correctness, no ablation or state-of-the-art baseline was run, and clinical validation is required before any use.