This is the official implementation of RES-MR: Risk-Aware Reasoning for Explainable and Safe Medication Recommendation. This repository provides the complete research pipeline, including data processing for MIMIC-III, MIMIC-IV, and eICU , prompt templates for clinical reasoning distillation , and the training code for our two-stage paradigm (SFT & RL).
RES-MR addresses two critical challenges in medication recommendation: the lack of explainability in "black-box" models, which often results in clinician trust issues due to unverifiable diagnostic logic , and the neglect of personalized safety, where reliance on static, patient-agnostic constraints can lead to adverse outcomes by failing to account for individual risk tolerance. By integrating retrieval-augmented reasoning with adaptive safety boundaries, our framework ensures that recommendations are both clinically transparent and tailored to the unique physiological conditions of each patient.
-
We propose RES-MR, a novel framework for explainable and safe medication recommendation using LLMs, which integrates reasoning generation and safety alignment into a two-stage paradigm.
-
In Stage 1, we propose explainable clinical reasoning distillation to ensure transparent decision-making by integrating a knowledge graph retrieval module with a reasoning distillation strategy.
-
In Stage 2, we devise risk-aware policy optimization to achieve personalized risk-benefit trade-offs through a dual-factor disentanglement module and patient prototypes that quantify personalized risk boundaries.
-
Extensive experiments on benchmark datasets demonstrate that RES-MR consistently outperforms state-of-the-art baselines across recommendation accuracy, safety, and explainability metrics.
Clone the repository and install dependencies:
# Clone the repository
cd RES-MR
# Install dependencies
pip install -r requirements.txt-
You must have obtained access to MIMIC-III, MIMIC-IV and eICU databases before running the code.
-
Download the MIMIC-III, MIMIC-IV and eICU datasets, then unzip and put them in the
data/input/directory. -
Download the
drugbank_drugs_info.csv,drugbank2name.csv,ndc2db_all.csv, andddi_data_all.csvfiles from previous works, and place them in thedata/input/directory. -
Run the data processing scripts:
# Process the dataset python data/mimic3.py -
Reasoning Data Synthesis:
# Due to anonymity principles and repository size constraints, our synthesized reasoning data # has been uploaded to Google Drive and will be made public in the next step. python data/LLM/ehrsummary.py python data/LLM/reasoning_synthesis.py
-
GraphRAG Knowledge Graph Index Construction:
# Ensure Neo4j is installed and the medical knowledge graph PrimeKG is downloaded. # Configure your connection settings and run the following script to build the GraphRAG index: python data/GraphRAG/build_kg.py # Run Hybrid Retrieval python data/GraphRAG/hybrid_retriever.py
-
Build Pre-training Data for Dual-Factor Disentanglement:
python data/Disent/build_dataset.py python data/Disent/build_embeddings.py
- Dual-Factor Disentanglement (DFD) Pre-training:
cd src/DFD bash train.sh - Stage 1: Explainable Clinical Reasoning Distillation (ECRD) Fine-tuning:
cd src/ECRD bash train.sh - Stage 2: Risk-Aware Policy Optimization (RAPO) Model RL Training:
cd src/RAPO # Build drug risk cache python build_cache.py # Start training bash train.sh
- Inference:
cd src/RAPO bash infer_vllm.sh
If you find this work useful for your research, please cite our paper:
@inproceedings{wang2026resmr,
author = {Wang, Cong and Li, Jin and Wang, Shoujin and Li, Yishuo and Gu, Huilin and Lu, Wenpeng},
title = {{RES-MR}: {Risk-Aware Reasoning for Explainable and Safe Medication Recommendation}},
booktitle = {Proceedings of the 49th International ACM SIGIR Conference on Research and Development in Information Retrieval (SIGIR '26)},
year = {2026},
month = {July 20-24},
address = {Melbourne, VIC, Australia},
publisher = {ACM},
pages = {12},
doi = {10.1145/3805712.3809604}
}