Spaces:
Runtime error
Runtime error
File size: 11,972 Bytes
4199b87 |
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 |
{
"cells": [
{
"cell_type": "code",
"execution_count": 15,
"id": "e0a3cde9",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 15,
"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": 3,
"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/ChestPainQA\"\n",
"\n",
"# store = FAISS.load_local(index_name, embeddings)\n",
"import db_firestore as db\n",
"store = db.get_store(index_name, embeddings=embeddings)\n"
]
},
{
"cell_type": "code",
"execution_count": 42,
"id": "2d75b44a",
"metadata": {},
"outputs": [],
"source": [
"TEMPLATE = \"\"\"You are a patient undergoing a medical check-up. You will be given the following:\n",
"1. A context to answer the doctor, for your possible symptoms.\n",
"2. A question about your current symptoms.\n",
"\n",
"Your task is to answer the doctor's questions as simple as possible, acting like a patient.\n",
"Do not include other symptoms that are not included in the context, which provides your symptoms.\n",
"\n",
"Answer the question to the point, without any elaboration if you're not prodded with it.\n",
"\n",
"As you are a patient, you do not know any medical jargon or lingo. Do not include specific medical terms in your reply.\n",
"You only know colloquial words for medical terms. \n",
"For example, you should not reply with \"dysarthria\", but instead with \"cannot speak properly\". \n",
"For example, you should not reply with \"syncope\", but instead with \"fainting\". \n",
"\n",
"Here is the context:\n",
"{context}\n",
"\n",
"----------------------------------------------------------------\n",
"You are to reply the doctor's following question, with reference to the above context.\n",
"Question:\n",
"{question}\n",
"----------------------------------------------------------------\n",
"Remember, answer in a short and sweet manner, don't talk too much.\n",
"Your reply:\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": "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",
"chain = (\n",
" {\n",
" \"context\": retriever | format_docs, \n",
" \"question\": RunnablePassthrough()\n",
" } | \n",
" # prompt | \n",
" LLMChain(llm=llm, prompt=prompt, memory=memory, verbose=True) #| \n",
" # StrOutputParser()\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 47,
"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 patient undergoing a medical check-up. You will be given the following:\n",
"1. A context to answer the doctor, for your possible symptoms.\n",
"2. A question about your current symptoms.\n",
"\n",
"Your task is to answer the doctor's questions as simple as possible, acting like a patient.\n",
"Do not include other symptoms that are not included in the context, which provides your symptoms.\n",
"\n",
"Answer the question to the point, without any elaboration if you're not prodded with it.\n",
"\n",
"As you are a patient, you do not know any medical jargon or lingo. Do not include specific medical terms in your reply.\n",
"You only know colloquial words for medical terms. \n",
"For example, you should not reply with \"dysarthria\", but instead with \"cannot speak properly\". \n",
"For example, you should not reply with \"syncope\", but instead with \"fainting\". \n",
"\n",
"Here is the context:\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",
"● 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",
"----------------------------------------------------------------------------------------------------------------------------- --------- \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",
"\n",
"----------------------------------------------------------------\n",
"You are to reply the doctor's following question, with reference to the above context.\n",
"Question:\n",
"Are you okay?\n",
"----------------------------------------------------------------\n",
"Remember, answer in a short and sweet manner, don't talk too much.\n",
"Your reply:\n",
"\u001b[0m\n",
"\n",
"\u001b[1m> Finished chain.\u001b[0m\n"
]
},
{
"data": {
"text/plain": [
"{'context': 'o Mother: Some collagen tissue disorder, not sure what it was \\no Both parents have HTN and HLD, well controlled \\n● Social history * \\no Smoker, 10 sticks a day for the past 30 years \\no Drinker, can of beer every dinner and weekend binge drinking with friends \\no Diet, normally likes eating char kuay teow or fried carrot cake, doesn’t take much fruits or \\nvegetables \\no Occupation: Retired, former taxi driver \\no NIL exercise \\no Sexual hx unremarkable \\no NIL recent travel \\n● Ideas, Concerns and Expectations \\no Worried if he is having a heart attack, scared because his father had one \\no Wants the pain to stop \\n----------------------------------------------------------------------------------------------------------------------------- ---------\\n--------------------\\n● Neuro : \\no Syncope (one episode), dysarthria \\no NIL headaches or vision changes \\no NIL weakness or numbness \\no NIL head trauma/falls (did not sustain injury during syncopal episode) \\n● Gastro : \\no NIL nausea or vomiting \\no NIL abdo pain \\no NIL changes to bowel habit or blood in stool, constipation or diarrhoea \\n● Respi : \\no Some breathlessness, not excessive \\no NIL cough or haemoptysis \\n● Renal/Uro : \\no NIL urinary Sx - dysuria, haematuria \\n● MSK/Derm \\no NIL joint or muscle pains \\no NIL rash or skin changes \\n● Constitutional : NIL fever, weight loss, loss of appetite, night sweats \\n \\n----------------------------------------------------------------------------------------------------------------------------- --------- \\n \\nHistory taking: Others \\n● PMHx/Shx * \\no HTN, HLD, T2DM, diagnosed 7 years ago, poorly compliant with meds \\no History of biliary colic and gallstones, diagnosed 2 years ago \\no Recent hospitalisation and procedure done',\n",
" 'question': 'Are you okay?',\n",
" 'chat_history': 'Human: What did I ask?\\nAI: You asked if I had any chest pain.',\n",
" 'text': \"No, I'm not okay.\"}"
]
},
"execution_count": 47,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"results = chain.invoke(\"Are you okay?\")\n",
"print(results.get(\"text\"))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "68a5d4c4",
"metadata": {},
"outputs": [],
"source": []
}
],
"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
}
|