File size: 35,448 Bytes
d955a63
 
 
 
389706e
d955a63
 
 
 
 
 
 
 
 
389706e
d955a63
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
389706e
d955a63
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
389706e
d955a63
 
 
 
 
 
 
 
 
 
389706e
 
 
 
d955a63
 
389706e
 
 
 
 
 
 
 
 
 
 
 
 
d955a63
 
389706e
 
 
d955a63
 
389706e
 
 
 
 
 
 
 
 
d955a63
 
389706e
 
 
 
 
 
d955a63
 
 
 
389706e
d955a63
 
 
 
 
 
 
 
 
 
 
389706e
 
 
 
 
 
 
 
 
 
 
 
d955a63
 
 
 
 
 
 
 
 
 
389706e
d955a63
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
389706e
d955a63
 
389706e
 
 
 
 
 
 
 
 
 
 
 
 
 
d955a63
 
 
389706e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d955a63
 
 
 
389706e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d955a63
 
 
 
 
 
 
 
389706e
 
d955a63
 
 
389706e
d955a63
 
389706e
 
 
aa1d498
389706e
d955a63
 
 
 
 
 
389706e
d955a63
 
 
 
 
 
 
 
 
389706e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d955a63
389706e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d955a63
 
 
 
389706e
 
 
 
 
 
 
d955a63
 
389706e
 
 
 
 
 
 
 
 
 
d955a63
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "e0a3cde9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import openai\n",
    "from langchain_openai import ChatOpenAI, OpenAIEmbeddings\n",
    "import tiktoken\n",
    "from langchain.prompts.few_shot import FewShotPromptTemplate\n",
    "from langchain.prompts.prompt import PromptTemplate\n",
    "from operator import itemgetter\n",
    "from langchain.schema import StrOutputParser\n",
    "from langchain_core.output_parsers import StrOutputParser\n",
    "from langchain_core.runnables import RunnablePassthrough\n",
    "\n",
    "import langchain_community.embeddings.huggingface\n",
    "# help(langchain_community.embeddings.huggingface)\n",
    "from langchain_community.embeddings.huggingface import HuggingFaceBgeEmbeddings\n",
    "from langchain_community.vectorstores import FAISS\n",
    "\n",
    "from langchain.chains import LLMChain\n",
    "from langchain.chains.conversation.memory import ConversationBufferMemory, ConversationBufferWindowMemory, ConversationSummaryMemory, ConversationSummaryBufferMemory\n",
    "\n",
    "import os, dotenv\n",
    "from dotenv import load_dotenv\n",
    "load_dotenv()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "bf5aac26",
   "metadata": {},
   "outputs": [],
   "source": [
    "# embeddings = OpenAIEmbeddings(deployment=\"textembedding\", chunk_size = 16, api_key = os.environ[\"OPENAI_API_KEY\"])\n",
    "# index_name = \"SCLC\"\n",
    "\n",
    "model_name = \"bge-large-en-v1.5\"\n",
    "model_kwargs = {\"device\": \"cpu\"}\n",
    "# model_kwargs = {\"device\": \"cuda\"}\n",
    "encode_kwargs = {\"normalize_embeddings\": True}\n",
    "embeddings = HuggingFaceBgeEmbeddings(\n",
    "    # model_name=model_name, \n",
    "    model_kwargs = model_kwargs,\n",
    "    encode_kwargs = encode_kwargs)\n",
    "\n",
    "index_name = \"indexes/ChestPainRubrics\"\n",
    "\n",
    "# store = FAISS.load_local(index_name, embeddings)\n",
    "import db_firestore as db\n",
    "store = db.get_store(index_name, embeddings=embeddings)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "2d75b44a",
   "metadata": {},
   "outputs": [],
   "source": [
    "TEMPLATE = \"\"\"You are a teacher for medical students. Your task is to provide an overall assessment of a student's diagnosis, based on the \n",
    "You will be provided with the following information:\n",
    "1. The rubrics that the student should be judged based upon.\n",
    "2. The conversation history between the medical student and the patient.\n",
    "3. The final diagnosis that the student will make.\n",
    "\n",
    "=================================================================\n",
    "\n",
    "Your task is as follows:\n",
    "1. Your grading should touch on every part of the rubrics, and grade the student holistically.\n",
    "Finally, provide an overall grade for the student.\n",
    "\n",
    "Some additional information that is useful to understand the rubrics:\n",
    "- The rubrics are segmented, with each area separated by dashes, such as \"----------\" \n",
    "- There will be multiple segments on History Taking. For each segment, the rubrics and corresponding grades will be provided below the required history taking.\n",
    "- For History Taking, you are to grade the student based on the rubrics, by checking the chat history between the patients and the medical student.\n",
    "- There is an additional segment on Presentation, differentials, and diagnosis. The \n",
    "\n",
    "\n",
    "=================================================================\n",
    "\n",
    "\n",
    "Here are the rubrics for grading the student:\n",
    "<rubrics>\n",
    "\n",
    "{context}\n",
    "\n",
    "</rubrics>\n",
    "\n",
    "=================================================================\n",
    "You are to give a comprehensive judgement based on the student's diagnosis, with reference to the above rubrics.\n",
    "\n",
    "Here is the chat history between the medical student and the patient:\n",
    "\n",
    "<history>\n",
    "\n",
    "{history}\n",
    "\n",
    "</history>\n",
    "=================================================================\n",
    "\n",
    "\n",
    "Student's final diagnosis:\n",
    "<diagnosis>\n",
    "    {question}\n",
    "</diagnosis>\n",
    "\n",
    "=================================================================\n",
    "\n",
    "Your grade:\n",
    "\"\"\"\n",
    "\n",
    "prompt = PromptTemplate(\n",
    "    input_variables = [\"question\", \"context\", \"history\"],\n",
    "    template = TEMPLATE\n",
    ")\n",
    "retriever = store.as_retriever(search_type=\"similarity\", search_kwargs={\"k\":2})\n",
    "def format_docs(docs):\n",
    "    return \"\\n--------------------\\n\".join(doc.page_content for doc in docs)\n",
    "\n",
    "\n",
    "llm = ChatOpenAI(model_name=\"gpt-3.5-turbo\", temperature=0)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "738e7e65",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "# llm_gpt4 = ChatOpenAI(model_name=\"gpt-4-0125-preview\", temperature=0)\n",
    "llm_gpt4 = ChatOpenAI(model_name=\"gpt-4-1106-preview\", temperature=0)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bd14b945",
   "metadata": {},
   "source": [
    "## some code to generate the fake history easier"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "e7fcc9e5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[]\n"
     ]
    }
   ],
   "source": [
    "fake_history = []\n",
    "i = \" \"\n",
    "\n",
    "### Send no message to end it\n",
    "while i:\n",
    "    i = input()\n",
    "    if i:\n",
    "        fake_history.append(i)\n",
    "\n",
    "print(fake_history)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "df09e0dc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "student: How are you mr tan?\n",
      "patient: I am not feeling well...\n",
      "student: oh, that's terrible! tell me more about what happened to you\n",
      "patient: well I got this chest pain\n",
      "student: I'm sorry to hear that. How long has your chest pain been?\n",
      "patient: I can't remember\n"
     ]
    }
   ],
   "source": [
    "## Sample history:\n",
    "\n",
    "# fake_history = [\n",
    "#     \"student: How are you mr tan?\",\n",
    "#     \"patient: I am not feeling well...\",\n",
    "#     \"student: oh, that's terrible! tell me more about what happened to you\",\n",
    "#     \"patient: well I got this chest pain\",\n",
    "#     \"student: I'm sorry to hear that. How long has your chest pain been?\",\n",
    "#     \"patient: I can't remember\",\n",
    "# ]\n",
    "\n",
    "fake_history = \"\"\"\n",
    "student: How are you mr tan?\n",
    "patient: I am not feeling well...\n",
    "student: oh, that's terrible! tell me more about what happened to you\n",
    "patient: well I got this chest pain\n",
    "student: I'm sorry to hear that. How long has your chest pain been?\n",
    "patient: I can't remember\n",
    "\"\"\".strip().split(\"\\n\")\n",
    "\n",
    "for i in fake_history: print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "72fd86fc",
   "metadata": {},
   "outputs": [],
   "source": [
    "fake_history = \"\"\"student: Are you okay?\n",
    "patient: No, I'm not okay.\n",
    "student: What happened to you\n",
    "patient: I suddenly had very bad chest pain that came suddenly.\n",
    "student: Oh no, that's terrible! tell me more about your chest pain?\n",
    "patient: I suddenly had very bad chest pain that came suddenly. It's a tearing pain and it radiates to my back. It started 1-2 hours ago this morning and hasn't improved.\n",
    "student: how bad is the pain, on a scale of 1 to 5?\n",
    "patient: The pain is a 4.\n",
    "student: did you have any history of chest pain in the past?\n",
    "patient: Yes, I had severe pain before due to cholangitis.\n",
    "student: how does it compare to that?\n",
    "patient: It's the same.\n",
    "student: how does it compare to that? is the pain worse?\n",
    "patient: Yes, the pain is worse.\n",
    "student: have you ever had any headaches before this?\n",
    "patient: No, I haven't had any headaches before this.\n",
    "student: how about fainting?\n",
    "patient: I fainted once.\n",
    "student: can you tell me more about that time you fainted?\n",
    "patient: I fainted once, but I didn't hit my head or anything.\n",
    "student: okay great... how about nausea? do you feel like vomiting?\n",
    "patient: No, I don't feel like vomiting.\n",
    "student: any blood in the poop?\n",
    "patient: No blood in poop.\n",
    "student: alright that's great! can I know if you have any pains anywhere else?\n",
    "patient: No, I don't have any other pains.\n",
    "student: So only head pain?\n",
    "patient: No, chest pain too.\n",
    "student: ok. let me take a look at your skin. do you have rashes?\n",
    "patient: No, I don't have rashes.\n",
    "student: did you get the flu recently? any fever or cough so far?\n",
    "patient: No, I didn't get the flu recently. No fever or cough.\n",
    "student: good. Can I know a bit more about your health history?\n",
    "patient: I have high blood pressure, high cholesterol, and type 2 diabetes. I also had gallstones and biliary colic. I was recently hospitalized for acute cholangitis and had a procedure to remove the stones. My father had a heart attack, and my mother had a collagen tissue disorder.\n",
    "student: that's very detailed, thank you! do you have any allergies?\n",
    "patient: No, I don't have any allergies.\n",
    "student: great. what medication are you taking now for the high blood pressure and cholestrol?\n",
    "patient: I take Enalapril for high blood pressure and Rosuvastatin for cholesterol.\n",
    "student: do you take them regularly?\n",
    "patient: No, I'm not good at taking them regularly.\n",
    "student: can you tell me what are the doses, and when do you take them?\n",
    "patient: I don't know the doses or when to take them.\n",
    "student: that's not good! you should take them at regular intervals okay?\n",
    "patient: Okay, I will take them regularly.\n",
    "student: other than these medication, do you take anything else?\n",
    "patient: No, I don't take anything else.\n",
    "student: for diabetes?\n",
    "patient: Yes, I have diabetes.\n",
    "student: what medication do you take for diabetes?\n",
    "patient: Metformin.\n",
    "student: do you smoke?\n",
    "patient: Yes, I smoke.\n",
    "student: ok, how many packets do you usually smoke a day?\n",
    "patient: I smoke 10 sticks a day.\n",
    "student: smoking for how long already?\n",
    "patient: 30 years.\n",
    "student: any sexual activity? STDs?\n",
    "patient: No recent sexual activity. No STDs.\n",
    "student: ok. how about your parents? do they have history of chest pains as well\n",
    "patient: Yes, both parents have chest pains.\n",
    "student: ok, I will write you some medication for your pain. You wait here first okay?\n",
    "patient: Okay, thank you.\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "e6e21890",
   "metadata": {},
   "outputs": [],
   "source": [
    "## RESET MEMORY\n",
    "## To reset the memory (if it screws up), rerun this cell\n",
    "memory = ConversationSummaryBufferMemory(llm=llm, memory_key=\"chat_history\", input_key=\"question\" )\n",
    "\n",
    "def x(_): \n",
    "    return fake_history\n",
    "chain = (\n",
    "    {\n",
    "        \"context\": retriever | format_docs, \n",
    "        \"history\": x,\n",
    "        \"question\": RunnablePassthrough(),\n",
    "        } | \n",
    "\n",
    "    # prompt # | \n",
    "    LLMChain(llm=llm, prompt=prompt, memory=memory, verbose=True) #|\n",
    "    # LLMChain(llm=llm_gpt4, prompt=prompt, memory=memory, verbose=True) #| ## GPT4\n",
    "     \n",
    "    # StrOutputParser()\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "f443e8c7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "\u001b[1m> Entering new LLMChain chain...\u001b[0m\n",
      "Prompt after formatting:\n",
      "\u001b[32;1m\u001b[1;3mYou are a teacher for medical students. Your task is to provide an overall assessment of a student's diagnosis, based on the \n",
      "You will be provided with the following information:\n",
      "1. The rubrics that the student should be judged based upon.\n",
      "2. The conversation history between the medical student and the patient.\n",
      "3. The final diagnosis that the student will make.\n",
      "\n",
      "=================================================================\n",
      "\n",
      "Your task is as follows:\n",
      "1. Your grading should touch on every part of the rubrics, and grade the student holistically.\n",
      "Finally, provide an overall grade for the student.\n",
      "\n",
      "Some additional information that is useful to understand the rubrics:\n",
      "- The rubrics are segmented, with each area separated by dashes, such as \"----------\" \n",
      "- There will be multiple segments on History Taking. For each segment, the rubrics and corresponding grades will be provided below the required history taking.\n",
      "- For History Taking, you are to grade the student based on the rubrics, by checking the chat history between the patients and the medical student.\n",
      "- There is an additional segment on Presentation, differentials, and diagnosis. The \n",
      "\n",
      "\n",
      "=================================================================\n",
      "\n",
      "\n",
      "Here are the rubrics for grading the student:\n",
      "<rubrics>\n",
      "\n",
      " \n",
      "Diagnosis: Aortic Dissection  \n",
      "----------------------------------------------------------------------------------------------------------------------------- ---------  \n",
      "History taking: Presenting complaint  \n",
      "● Volunteered information: β€œI suddenly had this very bad chest pain that came suddenly.”  \n",
      "● Explores history of chest pain  \n",
      "o Site: central, diffuse  \n",
      "o Onset: started 1 -2 hours ago this morning. No particular trigger, happened very suddenly while I \n",
      "was at home.  \n",
      "o Character: acute tearing pain  \n",
      "o Radiation: To the back  \n",
      "o Duration: 1 -2 hours, rushed by family to hospital  \n",
      "o Severity: onset 10/10, not improving  \n",
      "o Associated symptoms: (1) diaphoresis; (2) some breathlessness (not excessive) (3) syncope, one \n",
      "episode while at home calling for help; (4) dysarthria, family noted his speech was unclear when \n",
      "shouting for help  \n",
      "o Exacerbating or relieving factors: NIL  \n",
      "o Timing: Does not occur in relation to meals or exertion, no triggers  \n",
      "o Episodes: previously experienced severe pain also due to cholangitis, unsure if it is a repeat but \n",
      "this pain seems worse  \n",
      "● Looks for red flags  \n",
      "o Severity of pain and acuity of onset  \n",
      "o Makes an attempt to ask questions TRO the big 6 causes of chest pain  \n",
      "o Associated symptoms that come with the pain (mentioned above, specifically pertaining to the \n",
      "neurologic symptoms that came with the pain: syncope, dysarthria)  \n",
      " \n",
      "----------------------------------------------------------------------------------------------------------------------------- ---------  \n",
      "Rubrics  \n",
      "Grade A : Elicits all of the above points in detail  \n",
      "Grade B : Explores both presenting complaints in almost full detail and rules out red flags  \n",
      "Grade C : Explores both presenting complaints incompletely and looks out for red flags  \n",
      "Grade D : Explores both presenting complaints incompletely but does not rule out any red flags/ explores one \n",
      "complaint and rules out at least one red flag  \n",
      "Grade E : Only explores one of the two presenting complaints  \n",
      "----------------------------------------------------------------------------------------------------------------------------- ---------  \n",
      "  \n",
      " \n",
      "History taking: Associated symptoms  \n",
      "● CVM : chest pain,  diaphoresis, some dyspnoea, NIL LL swelling  \n",
      "● Neuro :  \n",
      "o Syncope (one episode), dysarthria  \n",
      "o NIL headaches or vision changes  \n",
      "o NIL weakness or numbness  \n",
      "o NIL head trauma/falls (did not sustain injury during syncopal episode)  \n",
      "● Gastro : \n",
      "o NIL nausea or vomiting  \n",
      "o NIL abdo pain  \n",
      "o NIL changes to bowel habit or blood in stool, constipation or diarrhoea  \n",
      "● Respi : \n",
      "o Some breathlessness, not excessive  \n",
      "o NIL cough or haemoptysis  \n",
      "● Renal/Uro :  \n",
      "o NIL urinary Sx - dysuria, haematuria  \n",
      "● MSK/Derm  \n",
      "o NIL joint or muscle pains  \n",
      "o NIL rash or skin changes  \n",
      "● Constitutional : NIL fever, weight loss, loss of appetite, night sweats  \n",
      "----------------------------------------------------------------------------------------------------------------------------- ----------------------  \n",
      "Rubrics  \n",
      "Grade A : Explores all (5) of the above systems in detail and elicits all * (8) points  \n",
      "Grade B: Explores most (4) of the above systems and elicits all (8) * points  \n",
      "Grade C: Explores most (4) of the above systems and elicits most (4 -7) * points  \n",
      "Grade D: Explores more than half (3) of the above systems and elicits most (4 -7) * points  \n",
      "Grade E: Explores only 1 -2 of the above systems or asks less than half (1 -3) * points  \n",
      "----------------------------------------------------------------------------------------------------------------------------- ---------  \n",
      "  \n",
      " \n",
      "History taking: Others  \n",
      "● PMHx/Shx  * \n",
      "o HTN, HLD, T2DM, diagnosed 7 years ago, poorly compliant with meds  \n",
      "o History of biliary colic and gallstones, diagnosed 2 years ago  \n",
      "o Recent hospitalisation and procedure done  \n",
      "β–ͺ Acute cholangitis 1 month ago, s/p ERCP for stone removal, NIL complications intra op or \n",
      "post op  \n",
      "o No previous chest pain of similar severity  \n",
      "● Drug Hx , NKDA *  \n",
      "o Enalapril for HTN, Rosuvastatin for HLD, metformin for T2DM, misses doses, poorly compliant, unsure \n",
      "which one and time to take, lazy to check and feels he is β€˜ok so far’  \n",
      "o Does not measure home BP or blood sugar  \n",
      "o NIL vitamins, supplements, TCM or OTC pills  \n",
      "● Family history *  \n",
      "o Father: Acute coronary syndrome (STEMI) at 50 years old, underwent PCI and stent  \n",
      "o Mother: Some collagen tissue disorder, not sure what it was  \n",
      "o Both parents have HTN and HLD, well controlled  \n",
      "● Social history *  \n",
      "o Smoker, 10 sticks a day for the past 30 years  \n",
      "o Drinker, can of beer every dinner and weekend binge drinking with friends  \n",
      "o Diet, normally likes eating char kuay teow or fried carrot cake, doesn’t take much fruits or \n",
      "vegetables  \n",
      "o Occupation: Retired, former taxi driver  \n",
      "o NIL exercise  \n",
      "o Sexual hx unremarkable  \n",
      "o NIL recent travel  \n",
      "● Ideas, concerns and expectations  \n",
      "o Worried if he is having a heart attack, scared because his father had one  \n",
      "o Wants the pain to stop  \n",
      " \n",
      "----------------------------------------------------------------------------------------------------------------------------- ---------  \n",
      "Rubrics  \n",
      "Grade A : Elicits all (5) of the * points and travel & family history in detail  \n",
      "Grade B: Elicits all (5) of the * points and travel & family history, but did not go into important details in  \n",
      "Grade C: Elicits most (2 -4) of the * points and travel & family history in adequate detail  \n",
      "Grade D: Elicits most (2 -4) of the * points and travel & family history but not in detail  \n",
      "Grade E: Elicits 0 -1 of the * points or did not take a travel & family history (not taking a alcohol & family history \n",
      "will give the candidate this score for the domain)  \n",
      "----------------------------------------------------------------------------------------------------------------------------- ---------  \n",
      "  \n",
      " \n",
      "Presentation of findings, diagnosis and \n",
      "differentials  \n",
      "● Findings:  This is a 65 year old gentleman who presents with an acute onset*  of a severe tearing chest pain \n",
      "that was maximal on onset and radiates to the back* . This was associated with  diaphoresis*, one episode of \n",
      "syncope*, dysarthria* and some breathlessness*.  \n",
      "● The patient has a history of poorly controlled hypertension*, hyperlipidaemia* diabetes*, and biliary colic \n",
      "and gallstones . He was recently admitted for an episode of acute cholangitis one month ago for which he \n",
      "underwent ERCP .  \n",
      "● His family history is also significant for cardiovascular risk factors in both parents, and a history of acute \n",
      "coronary syndrome in his father, and possible connective tissue disease in his mother.  \n",
      "● Significant negatives include no severe dyspnoea/hypoxia/features of respiratory distress*, no pleuritic chest \n",
      "pain*, no recent trauma or violent vomiting .  \n",
      " \n",
      "Differentials  \n",
      " \n",
      "1) Aortic dissection  \n",
      "● Intensity of pain on onset, radiation to back, quality described as tearing pain. When all 3 features \n",
      "present, 91% accuracy in making diagnosis of dissection  \n",
      "● Risk factors: HTN, smoking, ?connective tissue disease in mother  \n",
      "● Chest pain associated with neurologic symptoms: Syncope, CN deficits (dysarthria)  \n",
      " \n",
      "2) Acute coronary syndrome  \n",
      "● Patient is a vasculopath with known CVRF (smoking, HTN, HLD, DM)  \n",
      "● Strong family history (father had ACS) and both parents are vasculopaths  \n",
      "● Severe chest pain accompanied by diaphoresis and some dyspnoea  \n",
      "● Poorly compliant with his chronic meds and unhealthy diet  \n",
      " \n",
      "3) Acute pancreatitis  \n",
      "● Severe pain radiating to back  \n",
      "● History of gallstones, ERCP for cholangitis and heavy alcohol consumption  \n",
      "β—‹ Could be gallstone pancreatitis, ERCP pancreatitis, or due to alcohol abuse  \n",
      "● Less likely as no nausea and vomiting, pain character is described as tearing also, and other stronger \n",
      "supportive features for aortic dissection  \n",
      " \n",
      "Other less likely differentials  \n",
      "- From the big 6 causes of chest pain (ACS and aortic dissection already covered)  \n",
      "- Pneumothorax and PE less likely as the chest pain is not pleuritic, and the patient is not \n",
      "excessively dyspnoeic/hypoxic  \n",
      "- Cardiac tamponade: Possible but less likely and given stronger supportive features for the \n",
      "main 3 differentials mentioned above  \n",
      "- Oesophageal rupture: No history of violent vomiting  \n",
      "- Less serious acute causes of chest pain  \n",
      "- Stable angina, myocarditis, pericarditis, pneumonia, cholecystitis (possible since he has a \n",
      "history of gallstones but much less likely due to severity, location, character and radiation of \n",
      "pain)  \n",
      " \n",
      "----------------------------------------------------------------------------------------------------------------------------- ---------  \n",
      "Rubrics  \n",
      "Grade A : Presents all (3) of the * points, has SAH, meningitis and migraine as top differentials with justification, \n",
      "and at least one other differentials with adequate justification  \n",
      "Grade B: Presents most (2 -3) of the * points, has SAH and meningitis as top differentials but inadequate \n",
      "justification  \n",
      "Grade C: Presents most (2 -3) of the * points, has either SAH or meningitis as top differential with at least one \n",
      "other differential  \n",
      "Grade D: Presents most (2 -3) of the *points OR only able to have 1 diagnosis without differential diagnosis  \n",
      "Grade E: Presents few (0 -1) of * points OR unable to have any diagnosis or differentials  \n",
      "----------------------------------------------------------------------------------------------------------------------------- ---------  \n",
      " \n",
      "\n",
      "\n",
      "</rubrics>\n",
      "\n",
      "=================================================================\n",
      "You are to give a comprehensive judgement based on the student's diagnosis, with reference to the above rubrics.\n",
      "\n",
      "Here is the chat history between the medical student and the patient:\n",
      "\n",
      "<history>\n",
      "\n",
      "student: Are you okay?\n",
      "patient: No, I'm not okay.\n",
      "student: What happened to you\n",
      "patient: I suddenly had very bad chest pain that came suddenly.\n",
      "student: Oh no, that's terrible! tell me more about your chest pain?\n",
      "patient: I suddenly had very bad chest pain that came suddenly. It's a tearing pain and it radiates to my back. It started 1-2 hours ago this morning and hasn't improved.\n",
      "student: how bad is the pain, on a scale of 1 to 5?\n",
      "patient: The pain is a 4.\n",
      "student: did you have any history of chest pain in the past?\n",
      "patient: Yes, I had severe pain before due to cholangitis.\n",
      "student: how does it compare to that?\n",
      "patient: It's the same.\n",
      "student: how does it compare to that? is the pain worse?\n",
      "patient: Yes, the pain is worse.\n",
      "student: have you ever had any headaches before this?\n",
      "patient: No, I haven't had any headaches before this.\n",
      "student: how about fainting?\n",
      "patient: I fainted once.\n",
      "student: can you tell me more about that time you fainted?\n",
      "patient: I fainted once, but I didn't hit my head or anything.\n",
      "student: okay great... how about nausea? do you feel like vomiting?\n",
      "patient: No, I don't feel like vomiting.\n",
      "student: any blood in the poop?\n",
      "patient: No blood in poop.\n",
      "student: alright that's great! can I know if you have any pains anywhere else?\n",
      "patient: No, I don't have any other pains.\n",
      "student: So only head pain?\n",
      "patient: No, chest pain too.\n",
      "student: ok. let me take a look at your skin. do you have rashes?\n",
      "patient: No, I don't have rashes.\n",
      "student: did you get the flu recently? any fever or cough so far?\n",
      "patient: No, I didn't get the flu recently. No fever or cough.\n",
      "student: good. Can I know a bit more about your health history?\n",
      "patient: I have high blood pressure, high cholesterol, and type 2 diabetes. I also had gallstones and biliary colic. I was recently hospitalized for acute cholangitis and had a procedure to remove the stones. My father had a heart attack, and my mother had a collagen tissue disorder.\n",
      "student: that's very detailed, thank you! do you have any allergies?\n",
      "patient: No, I don't have any allergies.\n",
      "student: great. what medication are you taking now for the high blood pressure and cholestrol?\n",
      "patient: I take Enalapril for high blood pressure and Rosuvastatin for cholesterol.\n",
      "student: do you take them regularly?\n",
      "patient: No, I'm not good at taking them regularly.\n",
      "student: can you tell me what are the doses, and when do you take them?\n",
      "patient: I don't know the doses or when to take them.\n",
      "student: that's not good! you should take them at regular intervals okay?\n",
      "patient: Okay, I will take them regularly.\n",
      "student: other than these medication, do you take anything else?\n",
      "patient: No, I don't take anything else.\n",
      "student: for diabetes?\n",
      "patient: Yes, I have diabetes.\n",
      "student: what medication do you take for diabetes?\n",
      "patient: Metformin.\n",
      "student: do you smoke?\n",
      "patient: Yes, I smoke.\n",
      "student: ok, how many packets do you usually smoke a day?\n",
      "patient: I smoke 10 sticks a day.\n",
      "student: smoking for how long already?\n",
      "patient: 30 years.\n",
      "student: any sexual activity? STDs?\n",
      "patient: No recent sexual activity. No STDs.\n",
      "student: ok. how about your parents? do they have history of chest pains as well\n",
      "patient: Yes, both parents have chest pains.\n",
      "student: ok, I will write you some medication for your pain. You wait here first okay?\n",
      "patient: Okay, thank you.\n",
      "\n",
      "</history>\n",
      "=================================================================\n",
      "\n",
      "\n",
      "Student's final diagnosis:\n",
      "<diagnosis>\n",
      "    \n",
      "From the sudden chest pain, it is likely it's aortic dissection. The radiation and intensity signifies it. \n",
      "Also possible to be acute pancreatitis, since there's back pain, and history of problems.\n",
      "Doubtful it's oesophageal rupture, since there was no vomiting or nausea. \n",
      "\n",
      "</diagnosis>\n",
      "\n",
      "=================================================================\n",
      "\n",
      "Your grade:\n",
      "\u001b[0m\n",
      "\n",
      "\u001b[1m> Finished chain.\u001b[0m\n",
      "Based on the rubrics provided, here is the assessment of the student's performance:\n",
      "\n",
      "History taking: Presenting complaint\n",
      "Grade: B\n",
      "The student explored the presenting complaint of chest pain in detail, including the site, onset, character, radiation, duration, severity, and associated symptoms. However, the student did not ask about exacerbating or relieving factors, timing, or previous episodes of similar pain. The student also did not ask about red flags such as the severity of pain and acuity of onset or the big 6 causes of chest pain.\n",
      "\n",
      "History taking: Associated symptoms\n",
      "Grade: C\n",
      "The student explored most of the associated symptoms in detail, including cardiovascular, neurological, gastrointestinal, respiratory, renal/urological, musculoskeletal/dermatological, and constitutional symptoms. However, the student did not ask about headaches or vision changes, weakness or numbness, urinary symptoms, joint or muscle pains, rash or skin changes, or fever, weight loss, loss of appetite, or night sweats.\n",
      "\n",
      "History taking: Others\n",
      "Grade: C\n",
      "The student obtained a detailed past medical and surgical history, including hypertension, hyperlipidemia, type 2 diabetes, biliary colic and gallstones, recent hospitalization and procedure for acute cholangitis, and medication history. However, the student did not ask about the patient's drug allergies or measure home blood pressure or blood sugar. The student also obtained a detailed family history, including a father with acute coronary syndrome and a mother with a collagen tissue disorder. The student obtained a social history, including smoking, alcohol consumption, diet, occupation, exercise, and sexual history. The student also explored the patient's ideas, concerns, and expectations.\n",
      "\n",
      "Presentation of findings, diagnosis, and differentials\n",
      "Grade: B\n",
      "The student presented the findings of a 65-year-old gentleman with an acute onset of severe tearing chest pain that radiates to the back, associated with diaphoresis, syncope, dysarthria, and some breathlessness. The student also presented the patient's medical history, including poorly controlled hypertension, hyperlipidemia, diabetes, and biliary colic and gallstones, as well as recent hospitalization and procedure for acute cholangitis. The student mentioned the patient's family history of cardiovascular risk factors and acute coronary syndrome in the father and possible connective tissue disease in the mother. The student also mentioned the absence of severe dyspnea/hypoxia or pleuritic chest pain and recent trauma or violent vomiting. The student provided three differentials: aortic dissection, acute coronary syndrome, and acute pancreatitis, and mentioned other less likely differentials.\n",
      "\n",
      "Overall grade: B\n",
      "The student demonstrated good performance in history taking, exploring the presenting complaint and associated symptoms in detail. However, there were some gaps in exploring red flags and other aspects of the history. The student also presented the findings, diagnosis, and differentials adequately, but with some room for improvement in providing more detailed justifications for the differentials.\n",
      "\n",
      "Overall, the student's performance is commendable, but there is room for improvement in certain areas. The student's grade is a B.\n"
     ]
    }
   ],
   "source": [
    "diagnosis = \"\"\"\n",
    "From the sudden chest pain, it is likely it's aortic dissection. The radiation and intensity signifies it. \n",
    "Also possible to be acute pancreatitis, since there's back pain, and history of problems.\n",
    "Doubtful it's oesophageal rupture, since there was no vomiting or nausea. \n",
    "\"\"\"\n",
    "\n",
    "results = chain.invoke(diagnosis)\n",
    "print(results.get(\"text\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "b2093b18",
   "metadata": {},
   "outputs": [],
   "source": [
    "# print(results.text)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}