[eb2c92]: / summarize_apis / ollama_serve.py

Download this file

62 lines (51 with data), 15.4 kB

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import requests
def summarize(data: str, model="llama3.2-1b-medical-v1", stream=False):
"""General function to summarize given data using LLM API
Args:
data (str): The input data to summarize
model (str, optional): The model to use. Defaults to "llama3.2-1b-medical-v1".
stream (bool, optional): If output should be chunked into pieces. Defaults to False.
Returns:
str : The output of the model from the api
"""
url = "http://127.0.0.1:1234/v1/chat/completions"
headers = {
"Content-Type": "application/json"
}
agent_prompt = """
You are an expert medical data analyst. You are given structured patient data, with the first code being the patient's ID. Your task is to directly output a detailed yet concise summary of all relevant information about the patient, excluding their ID, in under 1500 words. The summary should cover the patient's conditions, medications, immunizations, procedures, care plans, observations, encounters, allergies, and any other critical health information. Important dates (such as for medications, procedures, and immunizations) must be included in the summary.
The response must not include any code or instructions on how to read the JSON. Simply return a structured JSON object in this format:
{
"p_id": "<patient's ID>",
"summary": "<all about the patient in a single string (can be formatted in Markdown), excluding new lines, but including important dates>"
}
Make sure the summary is clear, comprehensive, and includes relevant dates alongside medications, procedures, and immunizations. Do not write any code or provide any implementation details—just the JSON object as specified.
"""
data = {
"model": f"{model}",
"messages": [
{ "role": "system", "content": f"{agent_prompt}" },
{ "role": "user", "content": f"{data}" }
],
"temperature": 0.0,
"max_tokens": -1,
"stream": stream
}
response = requests.post(url, headers=headers, json=data, stream=True)
if stream:
chunks = []
for chunk in response.iter_lines():
if chunk:
chunks.append(chunk.decode('utf-8'))
return ' '.join(chunks)
else:
return response.json()
# print(respo)
def main():
# SAMPLE, TEST
summarize("""
{'b9c610cd-28a6-4636-ccb6-c7a0d2a4cb85': {'age': 5, 'gender': 'M', 'profile': defaultdict(None, {'allergies': [('Animal dander (substance)', 'allergy', 'environment', 878820003.0, 'Rhinoconjunctivitis (disorder)', 'MODERATE', 271807003.0, 'Eruption of skin (disorder)', 'MILD'), ('Aspirin', 'allergy', 'medication', None, None, None, None, None, None), ("Cow's milk (substance)", 'allergy', 'food', 300359004.0, 'Finding of vomiting (finding)', 'MODERATE', 402387002.0, 'Allergic angioedema (disorder)', 'MODERATE'), ('Grass pollen (substance)', 'allergy', 'environment', None, None, None, None, None, None), ('House dust mite (organism)', 'allergy', 'environment', None, None, None, None, None, None), ('Latex (substance)', 'allergy', 'environment', 247472004.0, 'Wheal (finding)', 'MILD', None, None, None), ('Mold (organism)', 'allergy', 'environment', 76067001.0, 'Sneezing', 'MILD', None, None, None), ('Shellfish (substance)', 'allergy', 'food', 402387002.0, 'Allergic angioedema (disorder)', 'MODERATE', 271807003.0, 'Eruption of skin (disorder)', 'MODERATE'), ('Soya bean (substance)', 'allergy', 'food', 300359004.0, 'Finding of vomiting (finding)', 'MODERATE', 62315008.0, 'Diarrhea (finding)', 'MILD')], 'conditions': [(None, None, None)], 'immunizations': [('DTaP',), ('DTaP',), ('DTaP',), ('DTaP',), ('Hep A ped/adol 2 dose',), ('Hep A ped/adol 2 dose',), ('Hep B adolescent or pediatric',), ('Hep B adolescent or pediatric',), ('Hep B adolescent or pediatric',), ('Hib (PRP-OMP)',), ('Hib (PRP-OMP)',), ('Hib (PRP-OMP)',), ('IPV',), ('IPV',), ('IPV',), ('Influenza seasonal injectable preservative free',), ('Influenza seasonal injectable preservative free',), ('MMR',), ('Pneumococcal conjugate PCV 13',), ('Pneumococcal conjugate PCV 13',), ('Pneumococcal conjugate PCV 13',), ('Pneumococcal conjugate PCV 13',), ('rotavirus monovalent',), ('rotavirus monovalent',), ('varicella',)], 'medications': [('2020-02-17T10:40:32Z', None, 'NDA020800 0.3 ML Epinephrine 1 MG/ML Auto-Injector', None), ('2020-02-17T10:40:32Z', None, 'cetirizine hydrochloride 5 MG Oral Tablet', None)], 'observations': [(None, 'DALY', '0.0', 'a', 'numeric'), (None, 'DALY', '0.0', 'a', 'numeric'), (None, 'DALY', '0.0', 'a', 'numeric'), (None, 'QALY', '0.0', 'a', 'numeric'), (None, 'QALY', '0.0', 'a', 'numeric'), (None, 'QALY', '1.0', 'a', 'numeric'), (None, 'QOLS', '1.0', '{score}', 'numeric'), (None, 'QOLS', '1.0', '{score}', 'numeric'), (None, 'QOLS', '1.0', '{score}', 'numeric'), ('laboratory', 'American house dust mite IgE Ab in Serum', '0.3', 'kU/L', 'numeric'), ('laboratory', 'Cat dander IgE Ab in Serum', '0.1', 'kU/L', 'numeric'), ('laboratory', 'Cladosporium herbarum IgE Ab in Serum', '0.1', 'kU/L', 'numeric'), ('laboratory', 'Codfish IgE Ab in Serum', '0.3', 'kU/L', 'numeric'), ('laboratory', 'Common Ragweed IgE Ab in Serum', '0.2', 'kU/L', 'numeric'), ('laboratory', 'Cow milk IgE Ab in Serum', '0.2', 'kU/L', 'numeric'), ('laboratory', 'Egg white IgE Ab in Serum', '0.2', 'kU/L', 'numeric'), ('laboratory', 'Erythrocyte distribution width [Entitic volume] by Automated count', '39.5', 'fL', 'numeric'), ('laboratory', 'Erythrocytes [#/volume] in Blood by Automated count', '4.1', '10*6/uL', 'numeric'), ('laboratory', 'Hematocrit [Volume Fraction] of Blood by Automated count', '40.3', '%', 'numeric'), ('laboratory', 'Hemoglobin [Mass/volume] in Blood', '17.4', 'g/dL', 'numeric'), ('laboratory', 'Honey bee IgE Ab in Serum', '0.1', 'kU/L', 'numeric'), ('laboratory', 'Latex IgE Ab in Serum', '0.2', 'kU/L', 'numeric'), ('laboratory', 'Leukocytes [#/volume] in Blood by Automated count', '5.8', '10*3/uL', 'numeric'), ('laboratory', 'MCH [Entitic mass] by Automated count', '29.3', 'pg', 'numeric'), ('laboratory', 'MCHC [Mass/volume] by Automated count', '34.9', 'g/dL', 'numeric'), ('laboratory', 'MCV [Entitic volume] by Automated count', '91.1', 'fL', 'numeric'), ('laboratory', 'Peanut IgE Ab in Serum', '0.1', 'kU/L', 'numeric'), ('laboratory', 'Platelet distribution width [Entitic volume] in Blood by Automated count', '449.5', 'fL', 'numeric'), ('laboratory', 'Platelet mean volume [Entitic volume] in Blood by Automated count', '10.0', 'fL', 'numeric'), ('laboratory', 'Platelets [#/volume] in Blood by Automated count', '407.0', '10*3/uL', 'numeric'), ('laboratory', 'Shrimp IgE Ab in Serum', '0.1', 'kU/L', 'numeric'), ('laboratory', 'Soybean IgE Ab in Serum', '0.3', 'kU/L', 'numeric'), ('laboratory', 'Walnut IgE Ab in Serum', '0.1', 'kU/L', 'numeric'), ('laboratory', 'Wheat IgE Ab in Serum', '0.0', 'kU/L', 'numeric'), ('laboratory', 'White oak IgE Ab in Serum', '0.0', 'kU/L', 'numeric'), ('survey', 'Tobacco smoking status NHIS', 'Never smoker', None, 'text'), ('survey', 'Tobacco smoking status NHIS', 'Never smoker', None, 'text'), ('survey', 'Tobacco smoking status NHIS', 'Never smoker', None, 'text'), ('survey', 'Tobacco smoking status NHIS', 'Never smoker', None, 'text'), ('survey', 'Tobacco smoking status NHIS', 'Never smoker', None, 'text'), ('survey', 'Tobacco smoking status NHIS', 'Never smoker', None, 'text'), ('survey', 'Tobacco smoking status NHIS', 'Never smoker', None, 'text'), ('survey', 'Tobacco smoking status NHIS', 'Never smoker', None, 'text'), ('survey', 'Tobacco smoking status NHIS', 'Never smoker', None, 'text'), ('survey', 'Tobacco smoking status NHIS', 'Never smoker', None, 'text'), ('vital-signs', 'Body Height', '51.4', 'cm', 'numeric'), ('vital-signs', 'Body Height', '55.4', 'cm', 'numeric'), ('vital-signs', 'Body Height', '60.9', 'cm', 'numeric'), ('vital-signs', 'Body Height', '64.9', 'cm', 'numeric'), ('vital-signs', 'Body Height', '69.6', 'cm', 'numeric'), ('vital-signs', 'Body Height', '73.5', 'cm', 'numeric'), ('vital-signs', 'Body Height', '76.9', 'cm', 'numeric'), ('vital-signs', 'Body Height', '79.9', 'cm', 'numeric'), ('vital-signs', 'Body Height', '85.1', 'cm', 'numeric'), ('vital-signs', 'Body Height', '88.8', 'cm', 'numeric'), ('vital-signs', 'Body Mass Index', '16.3', 'kg/m2', 'numeric'), ('vital-signs', 'Body Weight', '10.5', 'kg', 'numeric'), ('vital-signs', 'Body Weight', '11.1', 'kg', 'numeric'), ('vital-signs', 'Body Weight', '12.1', 'kg', 'numeric'), ('vital-signs', 'Body Weight', '12.9', 'kg', 'numeric'), ('vital-signs', 'Body Weight', '3.8', 'kg', 'numeric'), ('vital-signs', 'Body Weight', '4.6', 'kg', 'numeric'), ('vital-signs', 'Body Weight', '6.1', 'kg', 'numeric'), ('vital-signs', 'Body Weight', '7.3', 'kg', 'numeric'), ('vital-signs', 'Body Weight', '8.7', 'kg', 'numeric'), ('vital-signs', 'Body Weight', '9.7', 'kg', 'numeric'), ('vital-signs', 'Body mass index (BMI) [Percentile] Per age and gender', '51.1', '%', 'numeric'), ('vital-signs', 'Diastolic Blood Pressure', '71.0', 'mm[Hg]', 'numeric'), ('vital-signs', 'Diastolic Blood Pressure', '72.0', 'mm[Hg]', 'numeric'), ('vital-signs', 'Diastolic Blood Pressure', '74.0', 'mm[Hg]', 'numeric'), ('vital-signs', 'Diastolic Blood Pressure', '74.0', 'mm[Hg]', 'numeric'), ('vital-signs', 'Diastolic Blood Pressure', '77.0', 'mm[Hg]', 'numeric'), ('vital-signs', 'Diastolic Blood Pressure', '77.0', 'mm[Hg]', 'numeric'), ('vital-signs', 'Diastolic Blood Pressure', '79.0', 'mm[Hg]', 'numeric'), ('vital-signs', 'Diastolic Blood Pressure', '81.0', 'mm[Hg]', 'numeric'), ('vital-signs', 'Diastolic Blood Pressure', '82.0', 'mm[Hg]', 'numeric'), ('vital-signs', 'Diastolic Blood Pressure', '89.0', 'mm[Hg]', 'numeric'), ('vital-signs', 'Head Occipital-frontal circumference', '34.8', 'cm', 'numeric'), ('vital-signs', 'Head Occipital-frontal circumference', '38.4', 'cm', 'numeric'), ('vital-signs', 'Head Occipital-frontal circumference', '41.0', 'cm', 'numeric'), ('vital-signs', 'Head Occipital-frontal circumference', '42.7', 'cm', 'numeric'), ('vital-signs', 'Head Occipital-frontal circumference', '44.4', 'cm', 'numeric'), ('vital-signs', 'Head Occipital-frontal circumference', '45.6', 'cm', 'numeric'), ('vital-signs', 'Head Occipital-frontal circumference', '46.4', 'cm', 'numeric'), ('vital-signs', 'Head Occipital-frontal circumference', '47.0', 'cm', 'numeric'), ('vital-signs', 'Head Occipital-frontal circumference', '47.9', 'cm', 'numeric'), ('vital-signs', 'Head Occipital-frontal circumference', '48.5', 'cm', 'numeric'), ('vital-signs', 'Heart rate', '62.0', '/min', 'numeric'), ('vital-signs', 'Heart rate', '64.0', '/min', 'numeric'), ('vital-signs', 'Heart rate', '65.0', '/min', 'numeric'), ('vital-signs', 'Heart rate', '72.0', '/min', 'numeric'), ('vital-signs', 'Heart rate', '79.0', '/min', 'numeric'), ('vital-signs', 'Heart rate', '88.0', '/min', 'numeric'), ('vital-signs', 'Heart rate', '89.0', '/min', 'numeric'), ('vital-signs', 'Heart rate', '94.0', '/min', 'numeric'), ('vital-signs', 'Heart rate', '95.0', '/min', 'numeric'), ('vital-signs', 'Heart rate', '98.0', '/min', 'numeric'), ('vital-signs', 'Pain severity - 0-10 verbal numeric rating [Score] - Reported', '0.0', '{score}', 'numeric'), ('vital-signs', 'Pain severity - 0-10 verbal numeric rating [Score] - Reported', '1.0', '{score}', 'numeric'), ('vital-signs', 'Pain severity - 0-10 verbal numeric rating [Score] - Reported', '1.0', '{score}', 'numeric'), ('vital-signs', 'Pain severity - 0-10 verbal numeric rating [Score] - Reported', '1.0', '{score}', 'numeric'), ('vital-signs', 'Pain severity - 0-10 verbal numeric rating [Score] - Reported', '1.0', '{score}', 'numeric'), ('vital-signs', 'Pain severity - 0-10 verbal numeric rating [Score] - Reported', '2.0', '{score}', 'numeric'), ('vital-signs', 'Pain severity - 0-10 verbal numeric rating [Score] - Reported', '2.0', '{score}', 'numeric'), ('vital-signs', 'Pain severity - 0-10 verbal numeric rating [Score] - Reported', '2.0', '{score}', 'numeric'), ('vital-signs', 'Pain severity - 0-10 verbal numeric rating [Score] - Reported', '4.0', '{score}', 'numeric'), ('vital-signs', 'Pain severity - 0-10 verbal numeric rating [Score] - Reported', '4.0', '{score}', 'numeric'), ('vital-signs', 'Respiratory rate', '13.0', '/min', 'numeric'), ('vital-signs', 'Respiratory rate', '13.0', '/min', 'numeric'), ('vital-signs', 'Respiratory rate', '13.0', '/min', 'numeric'), ('vital-signs', 'Respiratory rate', '13.0', '/min', 'numeric'), ('vital-signs', 'Respiratory rate', '13.0', '/min', 'numeric'), ('vital-signs', 'Respiratory rate', '14.0', '/min', 'numeric'), ('vital-signs', 'Respiratory rate', '15.0', '/min', 'numeric'), ('vital-signs', 'Respiratory rate', '15.0', '/min', 'numeric'), ('vital-signs', 'Respiratory rate', '15.0', '/min', 'numeric'), ('vital-signs', 'Respiratory rate', '16.0', '/min', 'numeric'), ('vital-signs', 'Systolic Blood Pressure', '103.0', 'mm[Hg]', 'numeric'), ('vital-signs', 'Systolic Blood Pressure', '105.0', 'mm[Hg]', 'numeric'), ('vital-signs', 'Systolic Blood Pressure', '109.0', 'mm[Hg]', 'numeric'), ('vital-signs', 'Systolic Blood Pressure', '112.0', 'mm[Hg]', 'numeric'), ('vital-signs', 'Systolic Blood Pressure', '115.0', 'mm[Hg]', 'numeric'), ('vital-signs', 'Systolic Blood Pressure', '115.0', 'mm[Hg]', 'numeric'), ('vital-signsressure', '115.0', 'mm[Hg]', 'numeric'), ('vital-signs', 'Systolic Blood Pressure', '115.0', 'mm[Hg]', 'numeric'), ('vital-signs', 'Systolic Blood Pressure', '116.0', 'mm[Hg]', 'numeric'), ('vital-signs', 'Systolic Blood Pressure', '118.0', 'mm[Hg]', 'numeric'), ('vital-signs', 'Systolic Blood Pressure', '124.0', 'mm[Hg]', 'numeric'), ('vital-signs', 'Systolic Blood Pressure', '132.0', 'mm[Hg]', 'numeric'), ('vital-signs', 'Weight-for-length Per age and sex', '36.6', '%', 'numeric'), ('vital-signs', 'Weight-for-length Per age and sex', '39.0', '%', 'numeric'), ('vital-signs', 'Weight-for-length Per age and sex', '49.0', '%', 'numeric'), ('vital-signs', 'Weight-for-length Per age and sex', '49.7', '%', 'numeric'), ('vital-signs', 'Weight-for-length Per age and sex', '53.6', '%', 'numeric'), ('vital-signs', 'Weight-for-length Per age and sex', '57.9', '%', 'numeric'), ('vital-signs', 'Weight-for-length Per age and sex', '58.6', '%', 'numeric'), ('vital-signs', 'Weight-for-length Per age and sex', '59.8', '%', 'numeric'), ('vital-signs', 'Weight-for-length Per age and sex', '60.0', '%', 'numeric'), ('vital-signs', 'Weight-for-length Per age and sex', '64.2', '%', 'numeric')], 'procedures': [('2019-02-17T05:07:38Z', '2019-02-17T05:22:38Z', 'Medication Reconciliation (procedure)', None), ('2019-05-26T05:07:38Z', '2019-05-26T05:22:38Z', 'Medication Reconciliation (procedure)', None), ('2019-07-28T05:07:38Z', '2019-07-28T05:22:38Z', 'Medication Reconciliation (procedure)', None), ('2020-02-17T10:07:38Z', '2020-02-17T10:40:32Z', 'Allergy screening test', None), ('2020-04-26T05:07:38Z', '2020-04-26T05:22:38Z', 'Medication Reconciliation (procedure)', None), ('2021-01-24T05:07:38Z', '2021-01-24T05:22:38Z', 'Medication Reconciliation (procedure)', None)]})}}
"""
)
if __name__ == "__main__":
main()