Views
No views yet
pip install pyhealth) or by using the
code snippet below.1import re
2import torch
3import transformers
4from transformers import AutoModelForCausalLM, AutoTokenizer
5from peft import PeftModelForCausalLM
6
7
8# parse the LLM response
9def parse_response(text):
10 res_regs = (re.compile(r'(?:.*?`([a-z,` ]{3,}`))', re.DOTALL),
11 re.compile(r'.*?[`#-]([a-z, \t\n\r]{3,}?)[`-].*', re.DOTALL))
12 matched: str = ''
13 for pat in res_regs:
14 m: re.Match = pat.match(text)
15 if m is not None:
16 matched = m.group(1)
17 break
18 return sorted(set(filter(lambda s: matched.find(s) > -1, labels)))
19
20
21# the prompt and role used to supervised-fine tune the model
22_PROMPT: str = """\
23Classify sentences for social determinants of health (SDOH).
24
25Definitions SDOHs are given with labels in back ticks:
26
27* `housing`: The status of a patient’s housing is a critical SDOH, known to affect the outcome of treatment.
28
29* `transportation`: This SDOH pertains to a patient’s inability to get to/from their healthcare visits.
30
31* `relationship`: Whether or not a patient is in a partnered relationship is an abundant SDOH in the clinical notes.
32
33* `parent`: This SDOH should be used for descriptions of a patient being a parent to at least one child who is a minor (under the age of 18 years old).
34
35* `employment`: This SDOH pertains to expressions of a patient’s employment status. A sentence should be annotated as an Employment Status SDOH if it expresses if the patient is employed (a paid job), unemployed, retired, or a current student.
36
37* `support`: This SDOH is a sentence describes a patient that is actively receiving care support, such as emotional, health, financial support. This support comes from family and friends but not health care professionals.
38
39* `-`: If no SDOH is found.
40
41Classify sentences for social determinants of health (SDOH) as a list labels in three back ticks. The sentence can be a member of multiple classes so output the labels that are mostly likely to be present.
42
43### Sentence: {sent}
44### SDOH labels:"""
45role = 'You are a social determinants of health (SDOH) classifier.'
46
47# output classes
48labels = 'transportation housing relationship employment support parent'.split()
49
50# example sentence
51sent = 'Pt is homeless and has no car and has no parents or support'
52
53base_model_id = 'meta-llama/Llama-3.1-8B-Instruct'
54adapter_model_id = 'plandes/sdoh-llama-3-1-8b'
55base_model = AutoModelForCausalLM.from_pretrained(base_model_id)
56model = PeftModelForCausalLM.from_pretrained(base_model, adapter_model_id)
57tokenizer = AutoTokenizer.from_pretrained(base_model_id)
58
59# create a pipeline for inferencing
60pipeline = transformers.pipeline(
61 'text-generation',
62 model=model,
63 tokenizer=tokenizer,
64 model_kwargs={'torch_dtype': torch.bfloat16},
65 device_map='auto')
66
67# prompt used by the chat template
68messages = [
69 {'role': 'system', 'content': 'You are a social determinants of health (SDOH) classifier.'},
70 {'role': 'user', 'content': _PROMPT.format(sent=sent)}]
71
72# inference the LLM
73outputs = pipeline(
74 messages,
75 max_new_tokens=512,
76 eos_token_id=[
77 pipeline.tokenizer.eos_token_id,
78 pipeline.tokenizer.convert_tokens_to_ids('<|eot_id|>'),
79 ],
80 pad_token_id=pipeline.tokenizer.eos_token_id,
81 do_sample=True,
82 temperature=0.01)
83
84# print the textual LLM output
85output = outputs[0]['generated_text'][-1]['content']
86print('model response:', output)
87
88# print the parsed labels from the LLM outupt
89print('labels:', parse_response(output))1@article{landesSunCross2025,
2 title={Integration of Large Language Models and Traditional Deep Learning for Social Determinants of Health Prediction},
3 url={https://arxiv.org/abs/2505.04655},
4 DOI={10.48550/arXiv.2505.04655},
5 number={arXiv:2505.04655},
6 publisher={arXiv},
7 author={Landes, Paul and Sun, Jimeng and Cross, Adam},
8 year={2025},
9 month={May}
10}