Spaces:
Runtime error
Runtime error
File size: 7,949 Bytes
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 |
{
"cells": [
{
"cell_type": "code",
"execution_count": 3,
"id": "e0a3cde9",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 3,
"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": 5,
"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": 28,
"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",
"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",
"Here is the rubrics:\n",
"{context}\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, enclosed in triple backticks:\n",
"```\n",
"{chat_history}\n",
"```\n",
"----------------------------------------------------------------\n",
"\n",
"Student's final diagnosis:\n",
"{question}\n",
"----------------------------------------------------------------\n",
"Your grade:\n",
"\"\"\"\n",
"\n",
"prompt = PromptTemplate(\n",
" input_variables = [\"question\", \"context\"],\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": "markdown",
"id": "bd14b945",
"metadata": {},
"source": [
"## some code to generate the fake history easier"
]
},
{
"cell_type": "code",
"execution_count": 29,
"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": 30,
"id": "df09e0dc",
"metadata": {},
"outputs": [],
"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",
"]"
]
},
{
"cell_type": "code",
"execution_count": 31,
"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",
"chain = (\n",
" {\n",
" \"context\": retriever | format_docs, \n",
" \"question\": RunnablePassthrough(),\n",
" \"chat_history\": lambda x: '\\n'.join(fake_history)\n",
" } | \n",
" # prompt | \n",
" LLMChain(llm=llm, prompt=prompt, memory=memory, verbose=True) #| \n",
" # StrOutputParser()\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 24,
"id": "f443e8c7",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Based on the provided rubrics and the conversation history, I would assess the student's diagnosis as Grade E. \n",
"\n",
"The student's diagnosis of \"chest pain, but it will go away soon\" does not align with the symptoms and history provided by the patient. The patient's symptoms, such as acute tearing chest pain, radiation to the back, diaphoresis, syncope, and dysarthria, are indicative of a more serious condition, such as aortic dissection or acute coronary syndrome. The student did not consider these possibilities or explore them further.\n",
"\n",
"Additionally, the student did not adequately explore the patient's presenting complaints, associated symptoms, and relevant medical history. They did not elicit all the necessary information and did not rule out red flags. The student also did not present a comprehensive list of differentials with adequate justification.\n",
"\n",
"Overall, the student's diagnosis and assessment of the patient's condition are incomplete and do not meet the expected standards.\n"
]
}
],
"source": [
"results = chain.invoke(\"I believe he has chest pain, but it will go away soon\")\n",
"print(results.get(\"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
}
|