|
| 1 | +__author__ = "qiao" |
| 2 | + |
| 3 | +""" |
| 4 | +Draft risk calculators |
| 5 | +""" |
| 6 | + |
| 7 | +import json |
| 8 | +import os |
| 9 | + |
| 10 | +from openai import AzureOpenAI |
| 11 | + |
| 12 | +client = AzureOpenAI( |
| 13 | + api_version="2023-09-01-preview", |
| 14 | + azure_endpoint=os.getenv("OPENAI_ENDPOINT"), |
| 15 | + api_key=os.getenv("OPENAI_API_KEY"), |
| 16 | +) |
| 17 | + |
| 18 | + |
| 19 | +if __name__ == "__main__": |
| 20 | + system = "You are a helpful assistant programmer for medical calculators. Your task is to read a PubMed article about a medical calculator, and if applicable, write a two-step calculator: (1) calculator a risk score based on multiple criteria; (2) interpret different ranges of the computed risk score into probabilities of risks." |
| 21 | + |
| 22 | + pmid2info = json.load(open("file0_sample_candidate_articles.json")) |
| 23 | + |
| 24 | + cand_pmids = [] |
| 25 | + with open("file1_full_classification_results.jsonl", "r") as f: |
| 26 | + for line in f.readlines(): |
| 27 | + line = json.loads(line.strip()) |
| 28 | + |
| 29 | + if line["result"].lower() == "yes": |
| 30 | + cand_pmids.append(line["pmid"]) |
| 31 | + |
| 32 | + for pmid in cand_pmids: |
| 33 | + target_path = os.path.join("dir1_pubmed_risk_calcs", pmid) |
| 34 | + |
| 35 | + if os.path.exists(target_path): |
| 36 | + continue |
| 37 | + |
| 38 | + if pmid not in pmid2info: |
| 39 | + print(f"PMID {pmid} information not found. Please load the full file 0 candidate articles!") |
| 40 | + continue |
| 41 | + |
| 42 | + prompt = "Here is a PubMed article:\n" |
| 43 | + prompt += pmid2info[pmid]["t"] + "\n" |
| 44 | + prompt += pmid2info[pmid]["a"] + "\n" |
| 45 | + |
| 46 | + prompt += "Does the article describes a simple two-step risk calculator, where the first step is to compute a risk score, and the second step is to interpret different risk scores? If no, please directly and only output \"NO\". Otherwise, please standardize the calculator into:\n" |
| 47 | + |
| 48 | + prompt += "#Title\nThe name of the calculator(s).\n" |
| 49 | + |
| 50 | + prompt += "##Purpose\nDescribe when this calculator should be used.\n" |
| 51 | + |
| 52 | + prompt += "##Specialty\nshould be a list of calculator types, one or more of (Allergy and Immunology, Anesthesiology, Cardiology, Dermatology, Emergency Medicine, Endocrinology, Family Medicine, Gastroenterology, Geriatrics, Hematology, Infectious Disease, Internal Medicine, Nephrology, Neurology, Obstetrics and Gynecology, Oncology, Ophthalmology, Orthopedic Surgery, Otolaryngology, Pathology, Pediatrics, Physical Medicine and Rehabilitation, Plastic Surgery, Psychiatry, Pulmonology, Radiology, Rheumatology, Surgery, Urology), seperated by \",\".\n" |
| 53 | + |
| 54 | + prompt += "##Eligibility\nDescribe what patients are eligible.\n" |
| 55 | + |
| 56 | + prompt += "##Size\nThe exact number of patients used to derive this calculator. Only put a number here without any other texts.\n" |
| 57 | + |
| 58 | + prompt += "##Computation\nDetailed instructions of how to use the calculator, including Python functions with clear docstring documentation. Please be self-contained and detailed. For example, if the computation involves multiple items, please clearly list each item. If one item has multiple possible values (e.g., 0-2), you also need to clearly define what each value means.\n" |
| 59 | + |
| 60 | + prompt += "##Interpretation\nShould be a list, where each item describes the interpretation (actual risk) for a value or a range of the computed risk score.\n" |
| 61 | + |
| 62 | + prompt += "##Utility\nEvaluation results of the clinical utility of the risk score, such as AUC, F-score, PPV.\n" |
| 63 | + |
| 64 | + prompt += "##Example\nGenerate a sample patient note and a detailed demonstration of using the calculator and interpret the results. Think step-by-step here.\n" |
| 65 | + |
| 66 | + prompt += "Please be as detailed as possible.\n" |
| 67 | + |
| 68 | + messages = [ |
| 69 | + {"role": "system", "content": system}, |
| 70 | + {"role": "user", "content": prompt}, |
| 71 | + ] |
| 72 | + |
| 73 | + response = client.chat.completions.create( |
| 74 | + model="gpt-4", |
| 75 | + messages=messages, |
| 76 | + temperature=0, |
| 77 | + ) |
| 78 | + |
| 79 | + result = response.choices[0].message.content |
| 80 | + |
| 81 | + with open(target_path, "w") as f: |
| 82 | + f.write(result) |
0 commit comments