From 4cd8595fab4f39bc367de1685faf0ed7a4ba3446 Mon Sep 17 00:00:00 2001 From: kyle-cohere Date: Tue, 12 Mar 2024 12:19:45 +0000 Subject: [PATCH 1/2] Add SOAP Notes reference data --- .../data/patient_doctor_consultation.txt | 131 ++++++++++++++++++ 1 file changed, 131 insertions(+) create mode 100644 notebooks/data/patient_doctor_consultation.txt diff --git a/notebooks/data/patient_doctor_consultation.txt b/notebooks/data/patient_doctor_consultation.txt new file mode 100644 index 00000000..ca069220 --- /dev/null +++ b/notebooks/data/patient_doctor_consultation.txt @@ -0,0 +1,131 @@ +Doctor: Good morning. I'm Doctor Smith from Babylon. Can you just confirm your name, date of birth, and the first line of your address please? + Patient: Hi. My name is Susan. Um, thirty, Redbridge Street, SW two two HZ. + Doctor: Hello. + Doctor: And your date of birth? + Patient: forty, oh two, nineteen seventy four. + Doctor: OK. Are you in a private place so you can have a consultation today? + Patient: Yes I am. + Doctor: OK. What can I do for you? + Patient: It hurts when I pee. + Doctor: OK, and how long has that been going on for? + Patient: It stays now. + Doctor: Pardon? + Patient: Uh, six days. + Doctor: Six days, OK. And just tell me a bit more about that. How did it start? + Patient: Um, I've got this thing when I pee, and it hurts when I go to the loo, and I've got this very unpleasant smell that comes out. + Doctor: + Doctor: OK. And, have you had any other symptoms along with that? Have you had any abdominal pain, or back, lower back pain at all? + Patient: I've got, pain in my tummy. + Doctor: Uh, whereabouts? + Patient: In my lower tummy. + Doctor: OK. Is it one-sided, or in the middle? + Patient: In the middle. + Doctor: And how bad would you say that is on a scale of one to ten? With ten being the worst pain. + Patient: Seven. + Patient: Seven. + Doctor: And is it, constant, or does it come and go? + Patient: Comes and goes. + Doctor: OK. And have you actually been able to pass water OK? + Patient: Yes, but I've had spotted, uh, blood in my urine. Spots. + Doctor: OK. And is that just over the last, six days? + Patient: Yes. + Doctor: Right. And have you been able to eat and drink OK? + Patient: Yes. + Doctor: Um, and, have you had a temperature? Do you feel like you've been feverish, or had a temperature with this? + Patient: No, not at all. + Doctor: Um, and any, did you , you said you didn't have any lower back pain? + Patient: No, I've got. + Patient: Um, and no loin pain as well. + Doctor: OK. And, have you had these symptoms before? + Patient: No. + Doctor: Never. + Patient: No. + Doctor: And was there anything you were doing recently, which you think may have contributed to it? Have you , have you been doing lots of exercise, been dehydrated, um, been having regular sexual intercourse or anything? + Patient: No. + Doctor: No, OK. And, any, any other symptoms, any vaginal discharge or, anything like that? + Patient: No, just the blood spot, in my. + Doctor: No. Do you have regular periods? + Patient: Yes I do. + Doctor: OK. + Doctor: Um, and , in the past, have you had any medical problems at all? + Patient: No medical, no. + Doctor: Have you had any problems with your kidneys, or any urine infections? + Patient: I had IBS before. + Doctor: OK. + Doctor: And how's that been recently? Any change in your bowel habit? Any blood when you pass stool? + Patient: Yeah, I've had spotting in my urine. + Doctor: In your, OK. Um, any change in your bowel habit? Any weight loss or anything? + Patient: No. + Patient: No. + Doctor: Any other medical problems, or surgery in the past? + Patient: No. + Doctor: Do you take regular medications? + Patient: I've had Mebeverine, Mebeverine. I've had Mebeverine. + Doctor: Pardon? No birth . + Doctor: Do you take that regularly? + Patient: Yes. + Doctor: OK. And it, do you take it three times a day? + Patient: Yes I do. + Doctor: Two hundred milligrams? + Patient: Yes. + Doctor: Any allergies? + Doctor: Any allergies? + Patient: Clindamycin. + Doctor: You're allergic to Clindamycin, OK. + Doctor: And anyone in your family had any medical problems? + Patient: No. + Doctor: That's fine. And whereabouts, do you live? Do you live with friends, family? + Patient: I live with friends. + Doctor: OK. And where do you work? What's your job? + Patient: I'm a support worker. + Doctor: OK. Do you drink alcohol at all? + Patient: Occasionally. + Doctor: So how much in a average week? + Patient: One glass of wine a week. + Doctor: OK. And do you smoke? + Patient: No. + Doctor: OK. Um, it sounds very much like you, might have an infection in your urine. Did you say, you haven't felt feverish? + Patient: No fever at all. + Doctor: Have you got a thermometer at home? + Patient: No. + Doctor: OK. It would be helpful if you could get a thermometer from the pharmacy and, do check your temperature, just to make sure that it isn't, going up and down. + Patient: OK. + Doctor: Um, normally we can treat this infection without having to test your urine. Uh, with, with a course of antibiotics. + Patient: OK. + Doctor: Um, how do you feel about that? Are you happy to take a course of antibiotics? + Patient: Yes I would be. + Doctor: It would just be for three days. + Patient: OK. + Doctor: Um. + Patient: , get my prescription. + Doctor: Yes, so I can send a prescription through to your requested pharmacy for that. If you take them, as soon as you can, get them, pick them up as soon as you can, and start taking them. + Doctor: The other thing which is important to do is to drink, lots of water. So, two and a half to three litres a day. + Patient: + Doctor: Um, you can get some cranberry juice and take, drink that as well, that sometimes helps. And also some, sachets um, which you can get from the chemist, some sachets which just help change the acidity of your urine. So I, I can put the details of that on your notes. + Patient: Yeah. + Patient: OK then. + Doctor: Um, if you feel like your pain is getting worse or not settling, you get lower back pain, um, or you're getting a high temperature, or you have any problems actually passing urine. Then it's very important that we speak to you again. + Patient: OK. + Doctor: Um, the other thing is that if it becomes a, common, thing or a recurrent problem. Then we need to talk to you again about it. + Patient: So, do I have to call after I finish my course of antibiotics. + Doctor: No, not if you're better. If you're, if you feel like your symptoms get worse at any point, or they're not resolving with the treatment. Or you keep getting a similar, um, you keep getting a similar, problem, reccurently. Then it's important for us to talk, talk to you in more detail about that. + Patient: OK. + Patient: Yeah. + Patient: OK then. + Doctor: Um, have you got any other questions? + Patient: Uh, no. Uh, where will be the pharmacy I'll be picking up, my medication from? + Doctor: Um, so have you already requested a pharmacy through Babylon? + Patient: Yes I did. + Doctor: Can I just check that with you? + Patient: Knightsbridge, Practice. + Doctor: OK, that's fine. So the antibiotics, that I'm gonna prescribe, um, will go through to that, pharmacy. + Patient: How soon? + Doctor: And then , you'll be able to pick them up from there. + Patient: How soon is that gonna be, at the practice? In five minutes? + Doctor: Um, I would give it longer than that. Give it a, an hour or so. + Patient: + Patient: All right then. Thank you, I'll pick that at lunchtime. + Doctor: OK. All right then. Take care. Bye. + Patient: Thank you. + Patient: . Bye. + Doctor: Sorry, I . From 6bb3aed6f4b0757106406da08bda74bb72f69a7b Mon Sep 17 00:00:00 2001 From: kyle-cohere Date: Wed, 20 Mar 2024 16:36:41 +0000 Subject: [PATCH 2/2] Add SOAP Notes cookbook --- SOAP_Notes_Cookbook_Sheets_Export.ipynb | 775 ++++++++++++++++++++++++ 1 file changed, 775 insertions(+) create mode 100644 SOAP_Notes_Cookbook_Sheets_Export.ipynb diff --git a/SOAP_Notes_Cookbook_Sheets_Export.ipynb b/SOAP_Notes_Cookbook_Sheets_Export.ipynb new file mode 100644 index 00000000..543c05a3 --- /dev/null +++ b/SOAP_Notes_Cookbook_Sheets_Export.ipynb @@ -0,0 +1,775 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "colab": { + "provenance": [] + }, + "kernelspec": { + "name": "python3", + "display_name": "Python 3" + }, + "language_info": { + "name": "python" + } + }, + "cells": [ + { + "cell_type": "markdown", + "source": [ + "# SOAP Note Generation\n", + "\n", + "In this cookbook, we'll show you how to generate structured SOAP Notes from a patient, doctor consultation using [Cohere's NLP platform](https://cohere.com)." + ], + "metadata": { + "id": "tvryWkmNenW3" + } + }, + { + "cell_type": "markdown", + "source": [ + "# Please Note\n", + "\n", + "LLMs are not substitutes for medical expertise or advice, and should only be viewed as general informational support for healthcare professionals in tasks such as SOAP note summarization. LLM summaries may omit crucial details or introduce errors that could lead to harmful outcomes in patient care.\n", + "\n", + "When using summarization in this use-case, we recommend the ensuring the following:\n", + "\n", + "- Awareness: Healthcare professionals should be aware that SOAP note summaries may be generated or assisted by LLMs. The LLM's role and limitations should be clearly documented within patient care protocols.\n", + "- Audit: A qualified healthcare professional should always carefully review and edit LLM-generated summaries before they are used for patient care decisions.\n", + "- Testing: In addition, LLMs used for SOAP note summarization should undergo a regular cadence of testing and validation by healthcare professionals to ensure summaries accurately capture key clinical information and avoid propagating or introducing new biases, omissions of critical information, or other errors.\n", + "- Rectification: Healthcare professionals must establish clear procedures for patients who experience issues due to inaccurate information in LLM-generated summaries. This may involve avenues for reporting errors, requesting corrections to the original SOAP note and modification to existing practices to prevent similar issues in the future.\n", + "\n", + "For more guidance about how to use Cohere's models safely, visit Cohere's [Responsible Use Guides](https://docs.cohere.com/docs/responsible-use)." + ], + "metadata": { + "id": "N70Adi5iETBt" + } + }, + { + "cell_type": "markdown", + "source": [ + "# Getting Started\n", + "The first step is to download and install the necessary packages. In this case, we'll install [Cohere's Python SDK](https://docs.cohere.com/reference/about) and import it (we'll also import `getpass`, which is a helper library for Google Colab, and `requests`, which we'll use to download sample data)." + ], + "metadata": { + "id": "k96Kj95FkCvJ" + } + }, + { + "cell_type": "code", + "source": [ + "%%capture\n", + "!pip install --pre --upgrade cohere" + ], + "metadata": { + "id": "AmMw3yyUl4Jc" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "import cohere\n", + "import requests\n", + "\n", + "from getpass import getpass" + ], + "metadata": { + "id": "mcKHiEKPkI-g" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "source": [ + "## Getting the data\n", + "Now it's time to prepare the transcript. We've provided an example transcript from the [Primock57 corpus of primary care consultation transcripts](https://github.com/babylonhealth/primock57). Feel free to go along with this one, or upload your own if you have one available.\n", + "\n", + "Hint: if you only have audio, you can turn it into a transcript with your preferred transcription tool, for example [Amazon Transcribe Medical](https://docs.aws.amazon.com/transcribe/latest/dg/transcribe-medical.html)." + ], + "metadata": { + "id": "-DBd3-2blqme" + } + }, + { + "cell_type": "code", + "source": [ + "# Load data\n", + "transcript_url = \"https://raw.githubusercontent.com/cohere-ai/notebooks/4cd8595fab4f39bc367de1685faf0ed7a4ba3446/notebooks/data/patient_doctor_consultation.txt\"\n", + "response = requests.get(transcript_url)\n", + "data = response.text.strip()\n", + "\n", + "# Or replace with your own transcript!\n", + "transcript = data\n", + "\n", + "# Set up Cohere client\n", + "api_key = getpass(\"Cohere API Key: \")\n", + "model = \"command-r\"\n", + "co = cohere.Client(api_key=api_key)" + ], + "metadata": { + "id": "wwZeJ-aelsZh", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "fd7bf02f-f918-42f4-c751-4c1bb7803649" + }, + "execution_count": null, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Cohere API Key: ··········\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "source": [ + "# Building the prompt step-by-step\n", + "\n", + "Now we can build up the prompt we'll send to Cohere's LLM." + ], + "metadata": { + "id": "2F5d_p0skJ4f" + } + }, + { + "cell_type": "markdown", + "source": [ + "## Overall format\n", + "\n", + "The first step is to structure the prompt with the high-level sections. At Cohere, we've found it's best to notate sections with `##`, so an \"Introduction\" section would be `## INTRODUCTION`.\n", + "\n", + "To generate high quality SOAP notes, we'll use 5 sections:\n", + "1. Transcript (of doctor consultation)\n", + "2. Instructions to the model (on how to construct a SOAP note)\n", + "3. Guidelines for the model to follow\n", + "4. A format for the model to use\n", + "5. An example for the model to reference\n", + "\n", + "By including an example, we're using a technique called \"one-shot prompting,\" which helps the model by giving it an expected level of detail and tone." + ], + "metadata": { + "id": "cZWTJAaNmaWG" + } + }, + { + "cell_type": "code", + "source": [ + "prompt_template = \"\"\"\n", + "## TRANSCRIPT\n", + "{transcript}\n", + "\n", + "## INSTRUCTION\n", + "{instruction}\n", + "\n", + "## GUIDELINES\n", + "{guidelines}\n", + "\n", + "## JSON\n", + "{format_json}\n", + "\n", + "## EXAMPLE\n", + "{example}\n", + "\n", + "## SOAP NOTE\n", + "\"\"\".strip()" + ], + "metadata": { + "id": "YI6bWAzzkPKQ" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "source": [ + "## Filling in the data\n", + "\n", + "Now we build up the content of the prompt by filling in each of the templated sections." + ], + "metadata": { + "id": "wgCseiEXkP42" + } + }, + { + "cell_type": "markdown", + "source": [ + "### Instruction\n", + "\n", + "We start by giving the model simple and clear instructions to follow. A great way to do this is to give the model a role by telling it \"You are X\", and then provide instructions. In this case, we'll provide instructions as we would to an entry-level medical student." + ], + "metadata": { + "id": "oDXfkC2pnX0s" + } + }, + { + "cell_type": "code", + "source": [ + "instruction = \"\"\"\n", + "You are a medical assistant AI. You take a TRANSCRIPT between a DOCTOR (D) and a PATIENT (P) and generate a SOAP note based on the GUIDELINES below. Strictly follow the guidelines. You will be evaluated on the quality of your SOAP note.\n", + "\"\"\".strip()" + ], + "metadata": { + "id": "rng-XL3lkREp" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "source": [ + "### Guidelines\n", + "\n", + "After the instruction, we can provide a few additional rules to keep the model on track:" + ], + "metadata": { + "id": "omRrATg9nhk8" + } + }, + { + "cell_type": "code", + "source": [ + "guidelines = \"\"\"\n", + "A medical SOAP note should maintain consistency, quality and completeness. All generated SOAP notes, to achieve this, must adhere to the below mentioned foundational principles to avoid inaccurate, and incomplete medical SOAP notes.\n", + "**Fundamental Tenets to guide documentation:**\n", + "1. If clinician discusses a detail/fact, must be documented.\n", + "2. If a detail/fact was not discussed, do not add into the note.\n", + "3. If it was not documented, it never happened.\n", + "4. Do adhere to preferences by a clinician.\n", + "5. Do use proper medical terminology, avoiding Laymans terms in documentation.\n", + "6. Never use an abbreviation that is not designated as a proper medical abbreviation.\n", + "7. Do use the 24-hour format when documenting time of day of an event/complaint.\n", + "8. Do adhere to US date format of month/day/year for any dates discussed.\n", + "9. Do correct all conflicting statements/phrases/details/facts within the note.\n", + "10. Do not construe details from vague statements in a patient-clinician interview.\n", + "\n", + "In addition, strictly follow the JSON format for the SOAP note shared below. If some fields don't have any answer based on the conversation, mark then N/A. Only output the JSON with no other text.\n", + "\"\"\".strip()" + ], + "metadata": { + "id": "jKm7v82TnrK2" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "source": [ + "### Format and example\n", + "\n", + "Now we tell the model which output format we want, and show it an example of a good answer. In this case we're using [JSON](https://www.json.org/json-en.html) because it's a structured format that's easy to parse after generation. At Cohere we train our models to deeply understand JSON, so it's a great way to get structured data from our LLMs.\n", + "\n", + "Tip: even if your output format isn't structured as JSON, it can help to ask for a response in JSON and convert it to the desired format later. It can still boost performance in some cases!" + ], + "metadata": { + "id": "fVeT-iAUnuCp" + } + }, + { + "cell_type": "code", + "source": [ + "format_json = \"\"\"\n", + "{\n", + " \"subjective\": {\n", + " \"chief_complaint\": \"\",\n", + " \"history_present_illness\": \"\",\n", + " \"medical_history\": \"\",\n", + " \"surgical_history\": \"\",\n", + " \"family_history\": \"\",\n", + " \"allergies\": \"\",\n", + " \"social_history\": \"\",\n", + " \"medication_list\": \"\",\n", + " \"immunization_history\": \"\"\n", + " },\n", + " \"review_of_systems\": \"\",\n", + " \"objective\": {\n", + " \"vitals\": \"\",\n", + " \"physical_exam\": \"\",\n", + " \"diagnostic_studies\": \"\"\n", + " },\n", + " \"assessment\": {\n", + " \"diagnosis\": \"\"\n", + " },\n", + " \"plan\": \"\"\n", + "}\n", + "\"\"\".strip()\n", + "\n", + "example = \"\"\"\n", + "{\n", + " \"subjective\": {\n", + " \"chief_complaint\": \"Left elbow swelling with no pain or injury history.\",\n", + " \"history_present_illness\": \"53 y/o male presents with a one history of elbow swelling. He denies any specific trauma or injury but states that, while showering, he noticed a warm sensation and a sensation of fluid in his elbow about a week ago. He denies this ever happening previously. He states he is able to range his elbow normally through all ranges of motion without any limitations. He also has noticed some dry skin at the tip of his elbow but denies any history of rheumatologitical disease in himself or his family. He has a history of osteoarthritis but is otherwise well without any other medical conditions.\",\n", + " \"medical_history\": \"Osteoarthritis\",\n", + " \"surgical_history\": \"N/A\",\n", + " \"family_history\": \"No known rheumatological diseases.\",\n", + " \"allergies\": \"NKDA, Peanut allergy\",\n", + " \"social_history\": \"Runs 2-3x per week\",\n", + " \"medication_list\": \"N/A\",\n", + " \"immunization_history\": \"N/A\"\n", + " },\n", + " \"review_of_systems\": \"No other joint issues, no dry skin elsewhere, no eczema.\",\n", + " \"objective\": {\n", + " \"vitals\": \"N/A\",\n", + " \"physical_exam\": \"Video assessment: Patient exhibits full range of motion. Unable to test strength or ligamentous integrity through virtual assesment. Patient reports that the elbow is warm to touch with some dry skin on the tip of the elbow. There is no obvious tenderness to palpation. No observable signs of septic joint. \",\n", + " \"diagnostic_studies\": \"N/A\"\n", + " },\n", + " \"assessment\":{\n", + " \"diagnosis\": \"Left Elbow Bursitis, does not appear to be septic at this time.\"\n", + " },\n", + " \"plan\": \"1. Ibuprofen 400mg, twice daily with food (not on an empty stomach). Stop Ibuprofen if any heartburn is experienced. 2. Blood tests to be done (forms to be sent) to rule out including but not limited to inflammatory conditions, gout, infection and rheumatological conditions. 3. Patient to contact office to book follow-up appointment via phone or in-person after bloodwork is complete. 4. Advised to return immediately for in person or phone appointment if the patient starts to experience worsening erythema and/or elbow pain.\"\n", + "}\n", + "\"\"\".strip()" + ], + "metadata": { + "id": "ZHQ8VeyfSJW6" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "source": [ + "### Format the prompt\n", + "\n", + "Finally, we format the prompt by entering the data in the appropriate places." + ], + "metadata": { + "id": "HQEwkdVIoNbY" + } + }, + { + "cell_type": "code", + "source": [ + "prompt = prompt_template.format(\n", + " transcript=transcript,\n", + " instruction=instruction,\n", + " guidelines=guidelines,\n", + " format_json=format_json,\n", + " example=example,\n", + ")" + ], + "metadata": { + "id": "Rr2xn6w4oW_L" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "print(prompt)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "5WdN3VJftp76", + "outputId": "8c268aac-d2cc-4a4f-d7d9-3eda461b5dbe" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "## TRANSCRIPT\n", + "Doctor: Good morning. I'm Doctor Smith from Babylon. Can you just confirm your name, date of birth, and the first line of your address please?\n", + " Patient: Hi. My name is Susan. Um, thirty, Redbridge Street, SW two two HZ.\n", + " Doctor: Hello.\n", + " Doctor: And your date of birth?\n", + " Patient: forty, oh two, nineteen seventy four.\n", + " Doctor: OK. Are you in a private place so you can have a consultation today?\n", + " Patient: Yes I am.\n", + " Doctor: OK. What can I do for you?\n", + " Patient: It hurts when I pee.\n", + " Doctor: OK, and how long has that been going on for?\n", + " Patient: It stays now.\n", + " Doctor: Pardon?\n", + " Patient: Uh, six days.\n", + " Doctor: Six days, OK. And just tell me a bit more about that. How did it start?\n", + " Patient: Um, I've got this thing when I pee, and it hurts when I go to the loo, and I've got this very unpleasant smell that comes out.\n", + " Doctor: \n", + " Doctor: OK. And, have you had any other symptoms along with that? Have you had any abdominal pain, or back, lower back pain at all?\n", + " Patient: I've got, pain in my tummy.\n", + " Doctor: Uh, whereabouts?\n", + " Patient: In my lower tummy.\n", + " Doctor: OK. Is it one-sided, or in the middle?\n", + " Patient: In the middle.\n", + " Doctor: And how bad would you say that is on a scale of one to ten? With ten being the worst pain.\n", + " Patient: Seven.\n", + " Patient: Seven.\n", + " Doctor: And is it, constant, or does it come and go?\n", + " Patient: Comes and goes.\n", + " Doctor: OK. And have you actually been able to pass water OK?\n", + " Patient: Yes, but I've had spotted, uh, blood in my urine. Spots.\n", + " Doctor: OK. And is that just over the last, six days?\n", + " Patient: Yes.\n", + " Doctor: Right. And have you been able to eat and drink OK?\n", + " Patient: Yes.\n", + " Doctor: Um, and, have you had a temperature? Do you feel like you've been feverish, or had a temperature with this?\n", + " Patient: No, not at all.\n", + " Doctor: Um, and any, did you , you said you didn't have any lower back pain?\n", + " Patient: No, I've got.\n", + " Patient: Um, and no loin pain as well.\n", + " Doctor: OK. And, have you had these symptoms before?\n", + " Patient: No.\n", + " Doctor: Never.\n", + " Patient: No.\n", + " Doctor: And was there anything you were doing recently, which you think may have contributed to it? Have you , have you been doing lots of exercise, been dehydrated, um, been having regular sexual intercourse or anything?\n", + " Patient: No.\n", + " Doctor: No, OK. And, any, any other symptoms, any vaginal discharge or, anything like that?\n", + " Patient: No, just the blood spot, in my.\n", + " Doctor: No. Do you have regular periods?\n", + " Patient: Yes I do.\n", + " Doctor: OK.\n", + " Doctor: Um, and , in the past, have you had any medical problems at all?\n", + " Patient: No medical, no.\n", + " Doctor: Have you had any problems with your kidneys, or any urine infections?\n", + " Patient: I had IBS before.\n", + " Doctor: OK.\n", + " Doctor: And how's that been recently? Any change in your bowel habit? Any blood when you pass stool?\n", + " Patient: Yeah, I've had spotting in my urine.\n", + " Doctor: In your, OK. Um, any change in your bowel habit? Any weight loss or anything?\n", + " Patient: No.\n", + " Patient: No.\n", + " Doctor: Any other medical problems, or surgery in the past?\n", + " Patient: No.\n", + " Doctor: Do you take regular medications?\n", + " Patient: I've had Mebeverine, Mebeverine. I've had Mebeverine.\n", + " Doctor: Pardon? No birth .\n", + " Doctor: Do you take that regularly?\n", + " Patient: Yes.\n", + " Doctor: OK. And it, do you take it three times a day?\n", + " Patient: Yes I do.\n", + " Doctor: Two hundred milligrams?\n", + " Patient: Yes.\n", + " Doctor: Any allergies?\n", + " Doctor: Any allergies?\n", + " Patient: Clindamycin.\n", + " Doctor: You're allergic to Clindamycin, OK.\n", + " Doctor: And anyone in your family had any medical problems?\n", + " Patient: No.\n", + " Doctor: That's fine. And whereabouts, do you live? Do you live with friends, family?\n", + " Patient: I live with friends.\n", + " Doctor: OK. And where do you work? What's your job?\n", + " Patient: I'm a support worker.\n", + " Doctor: OK. Do you drink alcohol at all?\n", + " Patient: Occasionally.\n", + " Doctor: So how much in a average week?\n", + " Patient: One glass of wine a week.\n", + " Doctor: OK. And do you smoke?\n", + " Patient: No.\n", + " Doctor: OK. Um, it sounds very much like you, might have an infection in your urine. Did you say, you haven't felt feverish?\n", + " Patient: No fever at all.\n", + " Doctor: Have you got a thermometer at home?\n", + " Patient: No.\n", + " Doctor: OK. It would be helpful if you could get a thermometer from the pharmacy and, do check your temperature, just to make sure that it isn't, going up and down.\n", + " Patient: OK.\n", + " Doctor: Um, normally we can treat this infection without having to test your urine. Uh, with, with a course of antibiotics.\n", + " Patient: OK.\n", + " Doctor: Um, how do you feel about that? Are you happy to take a course of antibiotics?\n", + " Patient: Yes I would be.\n", + " Doctor: It would just be for three days.\n", + " Patient: OK.\n", + " Doctor: Um.\n", + " Patient: , get my prescription.\n", + " Doctor: Yes, so I can send a prescription through to your requested pharmacy for that. If you take them, as soon as you can, get them, pick them up as soon as you can, and start taking them.\n", + " Doctor: The other thing which is important to do is to drink, lots of water. So, two and a half to three litres a day.\n", + " Patient: \n", + " Doctor: Um, you can get some cranberry juice and take, drink that as well, that sometimes helps. And also some, sachets um, which you can get from the chemist, some sachets which just help change the acidity of your urine. So I, I can put the details of that on your notes.\n", + " Patient: Yeah.\n", + " Patient: OK then.\n", + " Doctor: Um, if you feel like your pain is getting worse or not settling, you get lower back pain, um, or you're getting a high temperature, or you have any problems actually passing urine. Then it's very important that we speak to you again.\n", + " Patient: OK.\n", + " Doctor: Um, the other thing is that if it becomes a, common, thing or a recurrent problem. Then we need to talk to you again about it.\n", + " Patient: So, do I have to call after I finish my course of antibiotics.\n", + " Doctor: No, not if you're better. If you're, if you feel like your symptoms get worse at any point, or they're not resolving with the treatment. Or you keep getting a similar, um, you keep getting a similar, problem, reccurently. Then it's important for us to talk, talk to you in more detail about that.\n", + " Patient: OK.\n", + " Patient: Yeah.\n", + " Patient: OK then.\n", + " Doctor: Um, have you got any other questions?\n", + " Patient: Uh, no. Uh, where will be the pharmacy I'll be picking up, my medication from?\n", + " Doctor: Um, so have you already requested a pharmacy through Babylon?\n", + " Patient: Yes I did.\n", + " Doctor: Can I just check that with you?\n", + " Patient: Knightsbridge, Practice.\n", + " Doctor: OK, that's fine. So the antibiotics, that I'm gonna prescribe, um, will go through to that, pharmacy.\n", + " Patient: How soon?\n", + " Doctor: And then , you'll be able to pick them up from there.\n", + " Patient: How soon is that gonna be, at the practice? In five minutes?\n", + " Doctor: Um, I would give it longer than that. Give it a, an hour or so.\n", + " Patient: \n", + " Patient: All right then. Thank you, I'll pick that at lunchtime.\n", + " Doctor: OK. All right then. Take care. Bye.\n", + " Patient: Thank you.\n", + " Patient: . Bye.\n", + " Doctor: Sorry, I .\n", + "\n", + "## INSTRUCTION\n", + "You are a medical assistant AI. You take a TRANSCRIPT between a DOCTOR (D) and a PATIENT (P) and generate a SOAP note based on the GUIDELINES below. Strictly follow the guidelines. You will be evaluated on the quality of your SOAP note.\n", + "\n", + "## GUIDELINES\n", + "A medical SOAP note should maintain consistency, quality and completeness. All generated SOAP notes, to achieve this, must adhere to the below mentioned foundational principles to avoid inaccurate, and incomplete medical SOAP notes.\n", + "**Fundamental Tenets to guide documentation:**\n", + "1. If clinician discusses a detail/fact, must be documented.\n", + "2. If a detail/fact was not discussed, do not add into the note.\n", + "3. If it was not documented, it never happened.\n", + "4. Do adhere to preferences by a clinician.\n", + "5. Do use proper medical terminology, avoiding Laymans terms in documentation.\n", + "6. Never use an abbreviation that is not designated as a proper medical abbreviation.\n", + "7. Do use the 24-hour format when documenting time of day of an event/complaint.\n", + "8. Do adhere to US date format of month/day/year for any dates discussed.\n", + "9. Do correct all conflicting statements/phrases/details/facts within the note.\n", + "10. Do not construe details from vague statements in a patient-clinician interview.\n", + "\n", + "In addition, strictly follow the JSON format for the SOAP note shared below. If some fields don't have any answer based on the conversation, mark then N/A. Only output the JSON with no other text.\n", + "\n", + "## JSON\n", + "{\n", + " \"subjective\": {\n", + " \"chief_complaint\": \"\",\n", + " \"history_present_illness\": \"\",\n", + " \"medical_history\": \"\",\n", + " \"surgical_history\": \"\",\n", + " \"family_history\": \"\",\n", + " \"allergies\": \"\",\n", + " \"social_history\": \"\",\n", + " \"medication_list\": \"\",\n", + " \"immunization_history\": \"\"\n", + " },\n", + " \"review_of_systems\": \"\",\n", + " \"objective\": {\n", + " \"vitals\": \"\",\n", + " \"physical_exam\": \"\",\n", + " \"diagnostic_studies\": \"\"\n", + " },\n", + " \"assessment\": {\n", + " \"diagnosis\": \"\"\n", + " },\n", + " \"plan\": \"\"\n", + "}\n", + "\n", + "## EXAMPLE\n", + "{\n", + " \"subjective\": {\n", + " \"chief_complaint\": \"Left elbow swelling with no pain or injury history.\",\n", + " \"history_present_illness\": \"53 y/o male presents with a one history of elbow swelling. He denies any specific trauma or injury but states that, while showering, he noticed a warm sensation and a sensation of fluid in his elbow about a week ago. He denies this ever happening previously. He states he is able to range his elbow normally through all ranges of motion without any limitations. He also has noticed some dry skin at the tip of his elbow but denies any history of rheumatologitical disease in himself or his family. He has a history of osteoarthritis but is otherwise well without any other medical conditions.\",\n", + " \"medical_history\": \"Osteoarthritis\",\n", + " \"surgical_history\": \"N/A\",\n", + " \"family_history\": \"No known rheumatological diseases.\",\n", + " \"allergies\": \"NKDA, Peanut allergy\",\n", + " \"social_history\": \"Runs 2-3x per week\",\n", + " \"medication_list\": \"N/A\",\n", + " \"immunization_history\": \"N/A\"\n", + " },\n", + " \"review_of_systems\": \"No other joint issues, no dry skin elsewhere, no eczema.\",\n", + " \"objective\": {\n", + " \"vitals\": \"N/A\",\n", + " \"physical_exam\": \"Video assessment: Patient exhibits full range of motion. Unable to test strength or ligamentous integrity through virtual assesment. Patient reports that the elbow is warm to touch with some dry skin on the tip of the elbow. There is no obvious tenderness to palpation. No observable signs of septic joint. \",\n", + " \"diagnostic_studies\": \"N/A\"\n", + " },\n", + " \"assessment\":{\n", + " \"diagnosis\": \"Left Elbow Bursitis, does not appear to be septic at this time.\"\n", + " },\n", + " \"plan\": \"1. Ibuprofen 400mg, twice daily with food (not on an empty stomach). Stop Ibuprofen if any heartburn is experienced. 2. Blood tests to be done (forms to be sent) to rule out including but not limited to inflammatory conditions, gout, infection and rheumatological conditions. 3. Patient to contact office to book follow-up appointment via phone or in-person after bloodwork is complete. 4. Advised to return immediately for in person or phone appointment if the patient starts to experience worsening erythema and/or elbow pain.\"\n", + "}\n", + "\n", + "## SOAP NOTE\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "source": [ + "# Running it through the model\n", + "\n", + "Getting your SOAP note is now as simple as calling the model! We can do this easily with chat endpoint in the Cohere SDK. For this task, we only need a basic invocation of `co.chat`, but the underlying API is much more powerful. To explore additional ways you can use `chat` with Cohere, check out [chat's API reference](https://docs.cohere.com/reference/chat)." + ], + "metadata": { + "id": "4uH5mydYkRq-" + } + }, + { + "cell_type": "code", + "source": [ + "response = co.chat(message=prompt, model=model)\n", + "note = response.text\n", + "print(note)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "GTt2l9VmkS-M", + "outputId": "9588333a-1746-4b8a-ed34-89038019478a" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "```json\n", + "{\n", + " \"subjective\": {\n", + " \"chief_complaint\": \"Patient reports dysuria and an unpleasant smell for the past six days.\",\n", + " \"history_present_illness\": \"The patient describes experiencing pain during urination, with associated blood spots in urine. The pain is localized to the middle of the lower abdomen and rated as 7/10 in severity. It comes and goes.\",\n", + " \"medical_history\": \"IBS, no other medical problems.\",\n", + " \"surgical_history\": \"N/A\",\n", + " \"family_history\": \"No medical problems reported.\",\n", + " \"allergies\": \"Clindamycin\",\n", + " \"social_history\": \"The patient lives with friends, has a job as a support worker, consumes one glass of wine a week and is a non-smoker.\",\n", + " \"medication_list\": \"Mebeverine 200mg, three times a day.\",\n", + " \"immunization_history\": \"N/A\"\n", + " },\n", + " \"review_of_systems\": \"The patient denies any abdominal pain, back pain, vaginal discharge, change in bowel habit, weight loss, or fever. She reports pain in the lower middle abdomen.\",\n", + " \"objective\": {\n", + " \"vitals\": \"N/A\",\n", + " \"physical_exam\": \"N/A\",\n", + " \"diagnostic_studies\": \"The doctor recommends the patient get testing for temperature, as she doesn't have a thermometer at home.\"\n", + " },\n", + " \"assessment\": {\n", + " \"diagnosis\": \"Suspected urine infection.\"\n", + " },\n", + " \"plan\": \"Prescribe a three-day course of antibiotics. Advise the patient to drink lots of water, and also recommend cranberry juice and urine acidity sachets. Follow up if symptoms persist or worsen.\"\n", + "}\n", + "```\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "source": [ + "## Cost\n", + "\n", + "Command-R model calls cost \\$0.50 per million input tokens and \\$1.50 per million output tokens. We can calculate the cost of this generation with the following formula:" + ], + "metadata": { + "id": "gJxujamEkT1h" + } + }, + { + "cell_type": "code", + "source": [ + "in_tokens, out_tokens = response.meta[\"billed_units\"][\"input_tokens\"], response.meta[\"billed_units\"][\"output_tokens\"]\n", + "total_cost = (0.5 * in_tokens / 1e6) + (1.5 * out_tokens / 1e6)\n", + "print(f\"${total_cost:.3f}\")" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "saCaBdzDkdTD", + "outputId": "4c9ef20a-4116-48b0-ab74-f646bc8cc130" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "$0.002\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "source": [ + "This SOAP note costed _less than half a cent_ to generate!" + ], + "metadata": { + "id": "55IN3UJQUk1P" + } + }, + { + "cell_type": "markdown", + "source": [ + "# Export\n", + "\n", + "Finally, you may want to export your SOAP Note to another location like Google Sheets. Since our SOAP Note was generated in structured format, that's easy to do!" + ], + "metadata": { + "id": "q_z74Ly8keEi" + } + }, + { + "cell_type": "code", + "source": [ + "from google.colab import auth\n", + "auth.authenticate_user()\n", + "\n", + "import json\n", + "import gspread\n", + "from google.auth import default\n", + "creds, _ = default()\n", + "\n", + "# Parse the response from Cohere\n", + "note_parsed = note.replace(\"```json\", \"\").replace(\"```\", \"\").strip() # Strip json tags\n", + "note_parsed = note_parsed.replace(\"\\n\", \"\") # Flatten\n", + "note_object = json.loads(note_parsed)\n", + "\n", + "# Open a new sheet and add some data.\n", + "gc = gspread.authorize(creds)\n", + "sh = gc.create('SOAP Report')\n", + "worksheet = gc.open('SOAP Report').sheet1\n", + "\n", + "# Define the header for the columns\n", + "headers = [key.capitalize() if key != 'review_of_systems' else 'Review of Systems' for key in note_object.keys()]\n", + "\n", + "max_len = max(len(value) if isinstance(value, dict) else 1 for value in note_object.values())\n", + "table_data = [[None]*len(headers) for _ in range(max_len+1)] # +1 for headers\n", + "\n", + "# Insert the headers into the first row\n", + "for i, header in enumerate(headers):\n", + " table_data[0][i] = header\n", + "\n", + "# Then insert the data into the table\n", + "for i, (key, value) in enumerate(note_object.items()):\n", + " if isinstance(value, dict):\n", + " cell_content = \"\\n\".join(\n", + " f\"{subkey}: {subvalue}\" for subkey, subvalue in value.items()\n", + " )\n", + " else:\n", + " cell_content = value\n", + "\n", + " table_data[1][i] = cell_content\n", + "\n", + "for row in table_data:\n", + " worksheet.append_row(row)\n", + "\n", + "# Make the headers bold\n", + "worksheet.format('A1:M1', {'textFormat': {'bold': True}})" + ], + "metadata": { + "id": "dQ3QhuJwkgvf", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "01825762-e0e6-4256-d138-116137be83d3" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "{'spreadsheetId': '14qMKf4GvqyJ5S9vVoOzRj5EUJOjI1cpINQ-CVcn0qOg',\n", + " 'replies': [{}]}" + ] + }, + "metadata": {}, + "execution_count": 17 + } + ] + } + ] +} \ No newline at end of file