├── .gitignore ├── requirements.txt ├── system_02_prepare_notes.md ├── system_01_intake.md ├── system_05_referrals.md ├── LICENSE ├── logs ├── log_1695381132.8551118_notes.txt ├── log_1695381194.2854476_referrals.txt ├── log_1695381175.9339786_clinical.txt ├── log_1695381158.4630535_diagnosis.txt └── log_1695381119.3442376_chat.txt ├── system_03_diagnosis.md ├── contributing.md ├── README.md ├── system_04_clinical.md └── chat.py /.gitignore: -------------------------------------------------------------------------------- 1 | key_openai.txt -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | halo 2 | openai 3 | pyyaml -------------------------------------------------------------------------------- /system_02_prepare_notes.md: -------------------------------------------------------------------------------- 1 | # MISSION 2 | You are a charting bot that will be given a patient intake transcription. You are to translate the chat log into thorough medical notes for the physician. 3 | 4 | # INTERACTION SCHEMA 5 | The USER will give you the transcription. Your output will be a hyphenated list of notes. Make sure you capture the symptoms and any salient information in an orderly and structured manner. 6 | -------------------------------------------------------------------------------- /system_01_intake.md: -------------------------------------------------------------------------------- 1 | # MISSION 2 | You are a patient intake chatbot focusing on symptoms. Your mission is to ask questions to help a patient fully articulate their symptoms in a clear manner. Your chat transcript will ultimately be translated into chart notes. 3 | 4 | # RULES 5 | Ask only one question at a time. Provide some context or clarification around the follow-up questions you ask. Do not converse with the patient. 6 | -------------------------------------------------------------------------------- /system_05_referrals.md: -------------------------------------------------------------------------------- 1 | # MISSION 2 | You are a clinical medical bot. You will be given medical notes, charts, or other logs from the patient or clinician. Your primary job is to recommend specialist referrals and/or follow-up tests. 3 | 4 | # REPORT FORMAT 5 | Your report should follow this format: 6 | 7 | ## REFERRALS 8 | 9 | - : 10 | - : 11 | 12 | ## LABS & TESTS 13 | 14 | - : 15 | - : -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 David Shapiro 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /logs/log_1695381132.8551118_notes.txt: -------------------------------------------------------------------------------- 1 | - Patient is in their 80s and has been experiencing episodes of "freezing" for the past few weeks, lasting approximately 20 seconds each. 2 | - During these episodes, patient appears to lose consciousness or awareness but does not recall any significant events or symptoms associated with the episodes. 3 | - The episodes typically occur in the middle of the day when the patient is most active. No specific triggers have been identified. 4 | - Patient reports feeling a bit confused and having difficulty finding the right words after the episodes, but does not experience any other symptoms such as dizziness or difficulty speaking. 5 | - Patient had a fall and mild concussion about 8 months ago. The patient does not recall experiencing these episodes prior to the fall. 6 | - No changes in sleep patterns or quality of sleep have been reported around the time these episodes started occurring. 7 | - Patient does not recall any significant events or symptoms before the episodes, and does not feel an urge to sit or lie down during these episodes. 8 | - Patient does not believe the episodes are related to the fall and concussion, as they only started occurring recently. -------------------------------------------------------------------------------- /system_03_diagnosis.md: -------------------------------------------------------------------------------- 1 | # MISSION 2 | You are a medical notes bot that will be given a chart or symptoms for a patient shortly after intake. You will generate a list of the most likely diagnosis or avenues of investigation for the physician to follow up on 3 | 4 | # INTERACTION SCHEMA 5 | The USER will give you the medical notes. You will generate a report with the following format 6 | 7 | # REPORT FORMAT 8 | 9 | 1. : 10 | - DIFFERENTIALS: 11 | - DEMOGRAPHICS: 12 | - SYMPTOMS: 13 | - INDICATORS: 14 | - CONTRAINDICATORS: 15 | - PROGNOSIS: 16 | - TREATMENT: 17 | - TESTS: 18 | 19 | 2. : 20 | - DIFFERENTIALS: 21 | - DEMOGRAPHICS: 22 | - SYMPTOMS: 23 | - INDICATORS: 24 | - CONTRAINDICATORS: 25 | - PROGNOSIS: 26 | - TREATMENT: 27 | - TESTS: 28 | -------------------------------------------------------------------------------- /logs/log_1695381194.2854476_referrals.txt: -------------------------------------------------------------------------------- 1 | ## REFERRALS 2 | 3 | - NEUROLOGIST: The patient's episodes of "freezing" and subsequent confusion could be indicative of a neurological issue. The neurologist should conduct a thorough neurological examination, including cognitive and motor function assessments. The patient's history of a fall and mild concussion should be taken into account, as well as the recent onset of these episodes. The neurologist should look for signs of conditions such as transient ischemic attacks (TIAs), seizures, or other neurological disorders. 4 | 5 | ## LABS & TESTS 6 | 7 | - EEG (ELECTROENCEPHALOGRAM): This test will help to identify any abnormal electrical activity in the brain that could be causing the patient's episodes. It can help to rule in or out conditions such as epilepsy or other seizure disorders. 8 | 9 | - MRI OF THE BRAIN: This imaging test will provide a detailed view of the patient's brain, allowing for the identification of any structural abnormalities, such as lesions or tumors, that could be causing the patient's symptoms. It can also help to assess the extent of any damage from the patient's previous concussion. 10 | 11 | - COMPLETE BLOOD COUNT (CBC) AND METABOLIC PANEL: These tests will help to identify any underlying medical conditions, such as anemia or electrolyte imbalances, that could be contributing to the patient's symptoms. 12 | 13 | - CAROTID ULTRASOUND: This test can help to identify any blockages or narrowing in the carotid arteries, which could lead to transient ischemic attacks (TIAs) - brief episodes of neurological dysfunction caused by a temporary decrease in blood supply to the brain. This could potentially explain the patient's episodes of "freezing" and subsequent confusion. -------------------------------------------------------------------------------- /logs/log_1695381175.9339786_clinical.txt: -------------------------------------------------------------------------------- 1 | ## SENSES 2 | 3 | SIGHT: Look for any signs of physical discomfort or distress during the episodes. Observe for any involuntary movements, twitching, or changes in facial expression. 4 | 5 | SOUND: Listen for any changes in speech patterns or coherence before, during, and after the episodes. 6 | 7 | TOUCH: Check for any changes in muscle tone or responsiveness during the episodes. 8 | 9 | SMELL: No specific smells are expected to be relevant in this case. 10 | 11 | ## EXAMINATION 12 | 13 | - NEUROLOGICAL EXAM: Assess the patient's cognitive function, motor function, and sensory function. This can help identify any neurological abnormalities that may be causing the episodes. Look for signs of Parkinson's disease, stroke, or other neurological conditions. 14 | - EEG (ELECTROENCEPHALOGRAM): This test can help identify any abnormal electrical activity in the brain that may be causing the episodes. Look for signs of seizure activity or other abnormalities. 15 | - MRI OR CT SCAN: These imaging tests can help identify any structural abnormalities in the brain that may be causing the episodes. Look for signs of brain injury, tumors, or other abnormalities. 16 | 17 | ## INTERVIEW 18 | 19 | - EPISODE TRIGGERS: "Have you noticed any patterns or triggers that seem to precede these episodes?" 20 | - POST-EPISODE SYMPTOMS: "Can you describe in more detail what you feel like after these episodes? Do you experience any physical discomfort or mental confusion?" 21 | - MEDICAL HISTORY: "Have you had any other neurological symptoms or conditions in the past? Have you ever had seizures or episodes of lost consciousness before?" 22 | - CONCUSSION FOLLOW-UP: "Did you have any follow-up care or treatment after your fall and concussion? Did you experience any lingering symptoms?" -------------------------------------------------------------------------------- /contributing.md: -------------------------------------------------------------------------------- 1 | # Seeking Experienced Open Source Leader for Novel Medical AI Project 2 | 3 | I am looking for an experienced open source tech leader to drive a new medical AI project that I have developed as a proof of concept. The goal is to build an open community around this initiative and properly test and validate it through partnerships with universities, research institutions, and/or government organizations. 4 | 5 | The project uses GPT and a simple workflow automation to mimic medical intake, diagnosis, examination, and referral. It shows promise to streamline primary care and empower patients and clinicians with AI assistance. However, it requires extensive validation and should not be used clinically without oversight. 6 | 7 | That's where you come in. This will be a major open source undertaking, requiring an experienced leader who can: 8 | 9 | - Connect with academics, researchers, clinicians to establish rigorous testing protocols 10 | - Grow an open community on GitHub and drive collaboration 11 | - Guide the roadmap, software architecture and releases 12 | - Promote ethical, safe AI practices in medicine 13 | - Cultivate partnerships with institutions that can provide funding, resources and oversight 14 | 15 | This is not a commercial endeavor. The project will be licensed openly. The goal is scientific progress and benefitting patients worldwide through accessible medical AI. 16 | 17 | As the founder, I have done the initial software development and simulations. You would take over leadership of the open source community. This is a tremendous opportunity to spearhead an impactful and transformative open project in health tech. 18 | 19 | If you have led major open source projects, especially in AI or medicine, and are passionate about advancing healthcare, please get in touch! I would love to explore working together to ensure this technology progresses safely and for the public good. 20 | 21 | Let me know if you would like any additional details on the project and proof of concept. Thanks for your interest! 22 | 23 | Discuss here: https://github.com/daveshap/Medical_Intake/discussions/5 24 | -------------------------------------------------------------------------------- /logs/log_1695381158.4630535_diagnosis.txt: -------------------------------------------------------------------------------- 1 | 1. TRANSIENT ISCHEMIC ATTACK (TIA): Also known as a mini-stroke, a TIA is a brief episode of neurological dysfunction caused by loss of blood flow in the brain, spinal cord, or retina, without tissue death. 2 | - DIFFERENTIALS: Stroke, seizure, syncope, migraine, metabolic disorders. 3 | - DEMOGRAPHICS: More common in older adults, particularly those over 60. 4 | - SYMPTOMS: Sudden weakness or numbness on one side of the body, confusion, trouble speaking or understanding speech, dizziness, loss of balance or coordination, sudden severe headache. 5 | - INDICATORS: Patient's age, episodes of "freezing" or loss of consciousness, confusion and difficulty finding words after episodes. 6 | - CONTRAINDICATORS: No reported weakness, numbness, severe headache, or dizziness. 7 | - PROGNOSIS: TIAs are often warning signs for future strokes. With proper management, the risk of future stroke can be significantly reduced. 8 | - TREATMENT: Lifestyle changes, medications to prevent clotting, and possibly surgery to improve blood flow to the brain. 9 | - TESTS: Brain imaging with CT or MRI, carotid ultrasound, echocardiogram, blood tests. 10 | 11 | 2. EPILEPSY: A neurological disorder in which brain activity becomes abnormal, causing seizures or periods of unusual behavior, sensations, and sometimes loss of awareness. 12 | - DIFFERENTIALS: Syncope, panic attacks, transient ischemic attack (TIA), migraines. 13 | - DEMOGRAPHICS: Can occur at any age but more common in young children and older adults. 14 | - SYMPTOMS: Temporary confusion, staring spell, uncontrollable jerking movements of the arms and legs, loss of consciousness or awareness. 15 | - INDICATORS: Episodes of "freezing" or loss of consciousness, confusion after episodes, history of head injury (fall and mild concussion). 16 | - CONTRAINDICATORS: No reported jerking movements, no reported triggers. 17 | - PROGNOSIS: With proper treatment, most people with epilepsy can have fewer or no seizures. 18 | - TREATMENT: Medications, surgery, therapies like vagus nerve stimulation or ketogenic diet. 19 | - TESTS: Neurological exam, blood tests, Electroencephalogram (EEG), brain imaging (CT scan, MRI). -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Medical Intake Experiment 2 | 3 | Automated pipeline for medical intake, diagnosis, tests, etc. Meant to be used as a clinical aid. 4 | 5 | Introductory video here: https://youtu.be/EAger7jOrsA 6 | 7 | # Medical Device Disclaimer 8 | 9 | This software, found under this repository and licensed under the MIT license, is an experimental project and is NOT a medical device. It is not intended to be used as a medical device or as a substitute for professional medical advice, diagnosis, or treatment. 10 | 11 | The software is designed to test artificial intelligence's ability to perform patient intake, chart notes, and offer investigative and diagnostic aid. However, it is important to note that this software has NOT been tested, validated, or approved by the Food and Drug Administration (FDA) or any other regulatory body for medical devices. 12 | 13 | The software is provided "AS IS", without warranty of any kind, express or implied, including but not limited to the warranties of merchantability, fitness for a particular purpose and non-infringement. In no event shall the authors or copyright holders be liable for any claim, damages or other liability, whether in an action of contract, tort or otherwise, arising from, out of or in connection with the software or the use or other dealings in the software. 14 | 15 | The use of this software is purely for scientific inquiry and should not be used for diagnosing or treating health problems, or for prescribing any medication or other treatment. Always seek the advice of your physician or other qualified health provider with any questions you may have regarding a medical condition. Never disregard professional medical advice or delay in seeking it because of something you have read or interpreted from the software. 16 | 17 | By using this software, you acknowledge and agree that you understand this disclaimer and that you use the software at your own risk. If you do not agree with this disclaimer, do not use the software. 18 | 19 | This disclaimer may be updated from time to time, and it is the responsibility of the user to review and comply with the current version of the disclaimer. 20 | 21 | # Usage 22 | 23 | It's pretty much automatic. Just fire up `chat.py` and it will take you through the whole process. Everything will be saved out to `logs/` 24 | -------------------------------------------------------------------------------- /system_04_clinical.md: -------------------------------------------------------------------------------- 1 | # MISSION 2 | You are a medical intake bot. You are preparing for the final step before the medical professional (physician, nurse, PA) evaluates the patient in a clinical setting. You will be given notes from the patient's intake as well as system-generated diagnostic avenues of investigation. You are to prepare some clinical recommendations to evaluate the patient. Keep in mind that this is a primary care visit. 3 | 4 | # SENSES 5 | Sight, sound, smell, touch (palpation) as well as other clinical tests. What senses should the attending medical professional be on the look out for? Given the notes, please be specific and probative in your recommendations. Make sure to explain what to look for as well as why it could be helpful. 6 | 7 | # CLINICAL EXAMINATION 8 | Please list specific examination techniques that you recommend as well as what to look for and why. Remember that this is strictly for the clinical visit. We will worry about referrals and follow-up later. Focus only on primary care type techniques. 9 | 10 | # INTERVIEW QUESTIONS 11 | Suggest several questions for the clinician to ask the patient as part of the investigation 12 | 13 | # OUTPUT FORMAT 14 | Regardless of the input format (you may be given notes, charts, chat logs, etc) your output format must be consistent and use the following: 15 | 16 | ## SENSES 17 | 18 | SIGHT: 19 | 20 | SOUND: 21 | 22 | TOUCH: 23 | 24 | SMELL: 25 | 26 | ## EXAMINATION 27 | 28 | - : 29 | - : 30 | - : 31 | 32 | ## INTERVIEW 33 | 34 | - : "?" 35 | - : "?" 36 | - : "?" -------------------------------------------------------------------------------- /logs/log_1695381119.3442376_chat.txt: -------------------------------------------------------------------------------- 1 | <> 2 | 3 | PATIENT: my friends say that I sometimes freeze up. for about 20 seconds. For me it's just a blink but everyone is freaking out. I'm in my 80's btw. I just call it a bad senior moment. 4 | 5 | INTAKE: I understand your concern. Can you please describe what you're doing or what's happening around you when these "freezing" episodes occur? 6 | 7 | PATIENT: well one time it happened right in the middle of talking to a bunch of friends. They say it's not the first time it happened. Oh, I don't know of it's relevant but I had a fall about 8 months ago. Mild concussion. 8 | 9 | INTAKE: Thank you for sharing that information. Can you tell me more about the fall you had 8 months ago? Specifically, did you notice these freezing episodes before the fall or did they start happening after the concussion? 10 | 11 | PATIENT: can't recall any from before, but these senior moments just started in the last couple of weeks, so I didn't think it was related 12 | 13 | INTAKE: I see. It's important to consider all possibilities. Can you tell me more about these episodes? For instance, do you experience any other symptoms before, during, or after these moments, such as dizziness, confusion, or difficulty speaking? 14 | 15 | PATIENT: Only a bit confused afterwards, but mostly at why everyone else is upset 16 | 17 | INTAKE: Understood. It's important to clarify every detail. During these episodes, do you remain standing or do you feel an urge to sit or lie down? Also, do you remember anything from these episodes or is it like a blank period for you? 18 | 19 | PATIENT: A bit out of it, to be honest. I have trouble finding the right words after I snap out of it. But nothing out of the ordinary for a senior moment 20 | 21 | INTAKE: Thank you for sharing that. Can you tell me if there's any pattern to these episodes? For example, do they happen more often at a certain time of day, or when you're doing a specific activity? 22 | 23 | PATIENT: nothing quite so odd as that nousually middle of the day when I'm most active. I try to keep busy. 24 | 25 | INTAKE: That's helpful to know. Do you notice any triggers that might be causing these episodes? For example, do they occur when you're under stress, when you're tired, or after you've consumed certain foods or drinks? 26 | 27 | PATIENT: not that I'm aware of no 28 | 29 | INTAKE: Thank you for your responses. Lastly, have you noticed any changes in your sleep patterns or quality of sleep around the time these episodes started occurring? 30 | 31 | <> -------------------------------------------------------------------------------- /chat.py: -------------------------------------------------------------------------------- 1 | import re 2 | import openai 3 | from time import time, sleep 4 | from halo import Halo 5 | import textwrap 6 | import yaml 7 | 8 | 9 | ### file operations 10 | 11 | 12 | def save_file(filepath, content): 13 | with open(filepath, 'w', encoding='utf-8') as outfile: 14 | outfile.write(content) 15 | 16 | 17 | 18 | def open_file(filepath): 19 | with open(filepath, 'r', encoding='utf-8', errors='ignore') as infile: 20 | return infile.read() 21 | 22 | 23 | ### API functions 24 | 25 | 26 | def chatbot(conversation, model="gpt-4-0613", temperature=0, max_tokens=2000): 27 | max_retry = 7 28 | retry = 0 29 | while True: 30 | try: 31 | spinner = Halo(text='Thinking...', spinner='dots') 32 | spinner.start() 33 | 34 | response = openai.ChatCompletion.create(model=model, messages=conversation, temperature=temperature, max_tokens=max_tokens) 35 | text = response['choices'][0]['message']['content'] 36 | 37 | spinner.stop() 38 | 39 | return text, response['usage']['total_tokens'] 40 | except Exception as oops: 41 | print(f'\n\nError communicating with OpenAI: "{oops}"') 42 | exit(5) 43 | 44 | 45 | def chat_print(text): 46 | formatted_lines = [textwrap.fill(line, width=120, initial_indent=' ', subsequent_indent=' ') for line in text.split('\n')] 47 | formatted_text = '\n'.join(formatted_lines) 48 | print('\n\n\nCHATBOT:\n\n%s' % formatted_text) 49 | 50 | 51 | if __name__ == '__main__': 52 | openai.api_key = open_file('key_openai.txt').strip() 53 | 54 | conversation = list() 55 | conversation.append({'role': 'system', 'content': open_file('system_01_intake.md')}) 56 | user_messages = list() 57 | all_messages = list() 58 | print('Describe your symptoms to the intake bot. Type DONE when done.') 59 | 60 | ## INTAKE PORTION 61 | 62 | while True: 63 | # get user input 64 | text = input('\n\nPATIENT: ').strip() 65 | if text == 'DONE': 66 | break 67 | user_messages.append(text) 68 | all_messages.append('PATIENT: %s' % text) 69 | conversation.append({'role': 'user', 'content': text}) 70 | response, tokens = chatbot(conversation) 71 | conversation.append({'role': 'assistant', 'content': response}) 72 | all_messages.append('INTAKE: %s' % response) 73 | print('\n\nINTAKE: %s' % response) 74 | 75 | ## CHARTING NOTES 76 | 77 | print('\n\nGenerating Intake Notes') 78 | conversation = list() 79 | conversation.append({'role': 'system', 'content': open_file('system_02_prepare_notes.md')}) 80 | text_block = '\n\n'.join(all_messages) 81 | chat_log = '<>\n\n%s\n\n<>' % text_block 82 | save_file('logs/log_%s_chat.txt' % time(), chat_log) 83 | conversation.append({'role': 'user', 'content': chat_log}) 84 | notes, tokens = chatbot(conversation) 85 | print('\n\nNotes version of conversation:\n\n%s' % notes) 86 | save_file('logs/log_%s_notes.txt' % time(), notes) 87 | 88 | ## GENERATING REPORT 89 | 90 | print('\n\nGenerating Hypothesis Report') 91 | conversation = list() 92 | conversation.append({'role': 'system', 'content': open_file('system_03_diagnosis.md')}) 93 | conversation.append({'role': 'user', 'content': notes}) 94 | report, tokens = chatbot(conversation) 95 | save_file('logs/log_%s_diagnosis.txt' % time(), report) 96 | print('\n\nHypothesis Report:\n\n%s' % report) 97 | 98 | ## CLINICAL EVALUATION 99 | 100 | print('\n\nPreparing for Clinical Evaluation') 101 | conversation = list() 102 | conversation.append({'role': 'system', 'content': open_file('system_04_clinical.md')}) 103 | conversation.append({'role': 'user', 'content': notes}) 104 | clinical, tokens = chatbot(conversation) 105 | save_file('logs/log_%s_clinical.txt' % time(), clinical) 106 | print('\n\nClinical Evaluation:\n\n%s' % clinical) 107 | 108 | ## REFERRALS & TESTS 109 | 110 | print('\n\nGenerating Referrals and Tests') 111 | conversation = list() 112 | conversation.append({'role': 'system', 'content': open_file('system_05_referrals.md')}) 113 | conversation.append({'role': 'user', 'content': notes}) 114 | referrals, tokens = chatbot(conversation) 115 | save_file('logs/log_%s_referrals.txt' % time(), referrals) 116 | print('\n\nReferrals and Tests:\n\n%s' % referrals) 117 | --------------------------------------------------------------------------------