This repository contains code to fine-tune a Hugging Face LLM on the SAMS and LDL datasets to generate synthetic structured data.
The code in the repository requires the datasets in a CSV/Parquet format for the fine-tuning and evaluation processes.
Since the 2 datasets have some overlap at the patient level, it is necessary to perform the train-test (and train-validation) splits in a coherent manner to avoid having information leakage between them. More details are reported in the following section.
First, create and activate a virtual environment and install this package in it by running:
pip install -e .[eval,pydantic]To perform a coherent split between patients across the 2 datasets, the utility script scripts/split_data.py is provided.
This script receives as arguments the paths to the original datasets and creates train and test splits alongside them
(it is possible to customize the output files and the split ratio/seed).
As an example, the following command:
python scripts/split_data.py --input-paths '{"sams": "data/data/data_sams.parquet", "ldl": "data/data/data_ldl.parquet"}'will produce the following files: data/data/data_sams.train.parquet, data/data/data_sams.test.parquet, data/data/data_ldl.train.parquet and data/data/data_ldl.test.parquet.
The same procedure should be applied to the obtained train datasets for the training-validation splits.
To fine-tune a model on the datasets, run:
python scripts/finetune.py --config configs/finetune.yaml(NOTE: you must adjust the data_path, data_path_valid and desc_path fields in the configs/finetune.yaml file according to your own local paths.)
In order to enable the structured output generation for the LLM, it is necessary to define a valid JSON schema for the data.
The schemas provided as data/schema_<dataset>.json acts as templates, where the following fields must be replaced
with the actual dataset values in order to obtain a valid schema: enum, maximum, minimum, const.
It is possible to perform this operation using the scripts/infill_schema.py script as follows:
python scripts/infill_schema.py --dataset sams --input-file data/schema_sams.json --output-file data/schema_sams_infilled.json --data-path path/to/dataAs a simpler alternative, the schema can also be directly infilled in the generation script (see below).
Once a valid JSON schema is available, it is possible to convert it to the corresponding Pydantic model by running:
make data-model schema=path/to/infilled/schema.jsonThis will create the correct Model class in data/model.py, which can then be used for the structured output generation and validation.
To generate a synthetic dataset using a fine-tuned model and the defined schema, run:
python scripts/generate.py --config configs/generate.yaml --model path/to/model(NOTE: you must adjust the data_path, desc_path and schema_file fields in the configs/generate.yaml file according to your own local paths.)
You can specify a JSON schema and whether it must be infilled via the schema-file and infill-schema/no-infill-schema arguments.
In case no JSON schema is provided, the script will try to import a Model class from data/model.py,
falling back to unstructured generation in case of failure.
To evaluate a synthetic dataset, run:
python scripts/eval.py --config configs/eval.yamlThis will run the DCR and the Discriminator evaluations, saving the results to the specified JSON file (NOTE: you must adjust the all the data paths according to your own local ones).
To perform the Anonymeter evaluations, refer to the official repository.