Text Generation
PEFT
Safetensors
English
text-to-sql
clinical
fhir
healthcare
dora
lora
qlora
duckdb
Instructions to use adelelsayed1991/fhirsql-reasoning-sql-adapters with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- PEFT
How to use adelelsayed1991/fhirsql-reasoning-sql-adapters with PEFT:
Task type is invalid.
- Notebooks
- Google Colab
- Kaggle
Add model card
Browse files
README.md
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: cc-by-4.0
|
| 3 |
+
base_model: Qwen/Qwen2.5-Coder-14B-Instruct
|
| 4 |
+
library_name: peft
|
| 5 |
+
tags:
|
| 6 |
+
- text-to-sql
|
| 7 |
+
- clinical
|
| 8 |
+
- fhir
|
| 9 |
+
- healthcare
|
| 10 |
+
- dora
|
| 11 |
+
- lora
|
| 12 |
+
- qlora
|
| 13 |
+
- duckdb
|
| 14 |
+
datasets:
|
| 15 |
+
- adelelsayed1991/fhirsql-reasoning-sql
|
| 16 |
+
language:
|
| 17 |
+
- en
|
| 18 |
+
pipeline_tag: text-generation
|
| 19 |
+
---
|
| 20 |
+
|
| 21 |
+
# Model Card: fhirsql-reasoning-sql adapters
|
| 22 |
+
|
| 23 |
+
- **Model repo:** https://huggingface.co/adelelsayed1991/fhirsql-reasoning-sql-adapters
|
| 24 |
+
- **Dataset repo:** https://huggingface.co/datasets/adelelsayed1991/fhirsql-reasoning-sql
|
| 25 |
+
- **Code & paper:** https://github.com/adelelsayed/fhirsql-reasoning-sql
|
| 26 |
+
|
| 27 |
+
DoRA/LoRA adapters for `Qwen/Qwen2.5-Coder-14B-Instruct`, fine-tuned to translate natural-language hospital questions into a structured JSON query plan followed by DuckDB SQL, against a FHIR-derived schema. See `PAPER.md` for the full study.
|
| 28 |
+
|
| 29 |
+
## Repository contents
|
| 30 |
+
|
| 31 |
+
Six adapters: 3 random seeds (42, 43, 44) x 2 training stages. SFT checkpoints are selected by **validation loss**; RL checkpoints by **dev-set execution match** (each stage's own trainer criterion):
|
| 32 |
+
|
| 33 |
+
```
|
| 34 |
+
sft/seed_42/best/ sft/seed_43/best/ sft/seed_44/best/
|
| 35 |
+
rl/seed_42/best/ rl/seed_43/best/ rl/seed_44/best/
|
| 36 |
+
```
|
| 37 |
+
|
| 38 |
+
- `sft/` — supervised fine-tuning only (DoRA, rank 16, alpha 32). **Use these.**
|
| 39 |
+
- `rl/` — the corresponding `sft/` seed's checkpoint, continued with DAPO/GRPO reinforcement learning. Per `PAPER.md` Section 5.3, the RL stage does not improve on its SFT starting point on this task (and is marginally worse in-corpus); all three seeds early-stopped at step 40 of 200 having peaked at the first evaluation checkpoint. These adapters are published for completeness and reproducibility, not because they outperform `sft/`.
|
| 40 |
+
|
| 41 |
+
Each `best/` folder contains a standard PEFT adapter (`adapter_config.json`, `adapter_model.safetensors`, ~271MB).
|
| 42 |
+
|
| 43 |
+
## Intended use
|
| 44 |
+
|
| 45 |
+
Research artifact for reproducing or extending `PAPER.md`'s results. Generates a `{plan JSON}` + fenced ` ```sql ` completion for a natural-language question, given a prompt containing the DDL from `schema/schema.sql`. Not intended for use outside that schema/prompt format, and not validated on real (non-synthetic) patient data or real clinical schemas.
|
| 46 |
+
|
| 47 |
+
## How to load
|
| 48 |
+
|
| 49 |
+
```python
|
| 50 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
|
| 51 |
+
from peft import PeftModel
|
| 52 |
+
import torch
|
| 53 |
+
|
| 54 |
+
base_name = "Qwen/Qwen2.5-Coder-14B-Instruct"
|
| 55 |
+
tokenizer = AutoTokenizer.from_pretrained(base_name)
|
| 56 |
+
base_model = AutoModelForCausalLM.from_pretrained(
|
| 57 |
+
base_name, dtype=torch.bfloat16,
|
| 58 |
+
quantization_config=BitsAndBytesConfig(
|
| 59 |
+
load_in_4bit=True, bnb_4bit_compute_dtype=torch.bfloat16,
|
| 60 |
+
bnb_4bit_use_double_quant=True, bnb_4bit_quant_type="nf4",
|
| 61 |
+
),
|
| 62 |
+
)
|
| 63 |
+
model = PeftModel.from_pretrained(
|
| 64 |
+
base_model,
|
| 65 |
+
"adelelsayed1991/fhirsql-reasoning-sql-adapters",
|
| 66 |
+
subfolder="sft/seed_42/best", # recommended; swap to "rl/..." to reproduce the RL arm
|
| 67 |
+
)
|
| 68 |
+
```
|
| 69 |
+
|
| 70 |
+
Prompting details (system prompt template, schema DDL extraction, plan-then-SQL output format) are in `sft_train.ipynb` and `rl_train.ipynb`'s `SYSTEM_PROMPT_TEMPLATE`/`build_messages` cells.
|
| 71 |
+
|
| 72 |
+
## Training data
|
| 73 |
+
|
| 74 |
+
`data/training/sft_final_plan.jsonl` (10,696 rows: 9,680 execution-verified gold SQL + 1,016 abstention examples, spanning 82 archetypes and 2,420 distinct executable gold SQL statements), generated from a synthetic (Synthea) patient corpus — no real patient data was used anywhere in this project. Trained on Google Colab (G4 GPU, 96 GB RAM), 2 epochs per seed. See `DESIGN.md` and `METHODOLOGY_LOG.md` for full corpus and training-data generation methodology.
|
| 75 |
+
|
| 76 |
+
## Evaluation summary
|
| 77 |
+
|
| 78 |
+
Mean across the 3 SFT seeds on a held-out benchmark drawn from a disjoint patient population. Full results in `PAPER.md` Section 5; underlying per-seed data in `results/`.
|
| 79 |
+
|
| 80 |
+
| | Frozen base | SFT adapter |
|
| 81 |
+
|---|---:|---:|
|
| 82 |
+
| Execution correctness, familiar concepts | 34.8% | 100.0% |
|
| 83 |
+
| Execution correctness, unseen concepts | 60.8% | 89.1% |
|
| 84 |
+
| Abstention precision / recall (familiar arm) | 68% / 72% | 100% / 100% |
|
| 85 |
+
| Terminology-hardcoding rate | 0.0% | 0.0% |
|
| 86 |
+
|
| 87 |
+
The frozen column uses a complete schema description including the `valuesets` DDL (the fair
|
| 88 |
+
comparison). Under the exact training-time prompt, which omitted it, the frozen model scores
|
| 89 |
+
10.2% / 31.3% — see `PAPER.md` §5.6.
|
| 90 |
+
|
| 91 |
+
"Unseen concepts" are 84 clinical concepts appearing nowhere in training (verified by set
|
| 92 |
+
intersection; zero shared question/query pairs) — the adapters compose correct
|
| 93 |
+
terminology-resolution queries for them without ever having been trained on their codes. That
|
| 94 |
+
89.1% is an upper bound; text-match metrics give 81.4% as a lower bound (`PAPER.md` §6).
|
| 95 |
+
|
| 96 |
+
Abstention figures measure *retention* of trained refusal categories: the unanswerable questions
|
| 97 |
+
are reused verbatim across training and evaluation, so this is not held-out refusal
|
| 98 |
+
generalization (`PAPER.md` §2.7).
|
| 99 |
+
|
| 100 |
+
## Limitations
|
| 101 |
+
|
| 102 |
+
- Trained and evaluated entirely on synthetic (Synthea) data against one specific flattened schema (`schema/schema.sql`) — not validated against real clinical data or a different schema design.
|
| 103 |
+
- All training and evaluation questions come from the same template-and-persona back-translation factory (`scripts/question_templates.py`, `scripts/personas.py`); robustness to free-form clinician phrasing outside that distribution is untested.
|
| 104 |
+
- Single base model and scale (`Qwen2.5-Coder-14B-Instruct`, 14B parameters, 4-bit). Behavior at other scales or with other base models is untested.
|
| 105 |
+
- The frozen-baseline column above is the *fair* comparison (complete schema description). The training-time prompt omitted the `valuesets` DDL, under which the frozen model scores lower; `PAPER.md` §5.6 quantifies both.
|
| 106 |
+
- The unseen-concept arm shows a real ~11-point accuracy gap relative to familiar concepts. 78% of those failures are one benign, well-characterized pattern (over-applying SNOMED CT's parenthetical qualifier convention to concepts coded in other systems) — see `PAPER.md` Section 6.
|
| 107 |
+
|
| 108 |
+
## License
|
| 109 |
+
|
| 110 |
+
Adapter weights are released under **CC-BY-4.0**, matching the repository's paper/data license; the repository's code is Apache-2.0. See `LICENSE` and `CITATION.cff`. The base model `Qwen/Qwen2.5-Coder-14B-Instruct` retains its own license.
|