Yew Chong commited on
Commit
d955a63
1 Parent(s): 9d211d6

LLM for grading testing, streamlit code testing

Browse files
Files changed (3) hide show
  1. .gitignore +7 -3
  2. LLM for Grading (test).ipynb +241 -0
  3. streamlit/app7.py +41 -1
.gitignore CHANGED
@@ -6,7 +6,6 @@ healthhack-store-firebase-adminsdk-sq7ne-32400d49de.json
6
  /venv
7
 
8
  # notebooks
9
- test*.ipynb
10
  /.ipynb_checkpoints
11
 
12
  # docs
@@ -19,6 +18,11 @@ indexes/
19
  # firebase
20
  .firebase
21
 
22
- # others
23
  *.log
24
- test*.py
 
 
 
 
 
 
6
  /venv
7
 
8
  # notebooks
 
9
  /.ipynb_checkpoints
10
 
11
  # docs
 
18
  # firebase
19
  .firebase
20
 
21
+ # logs and testing
22
  *.log
23
+ test*.py
24
+ test*.html
25
+ test*.ipynb
26
+
27
+ ## Images
28
+ *.png
LLM for Grading (test).ipynb ADDED
@@ -0,0 +1,241 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": 3,
6
+ "id": "e0a3cde9",
7
+ "metadata": {},
8
+ "outputs": [
9
+ {
10
+ "data": {
11
+ "text/plain": [
12
+ "True"
13
+ ]
14
+ },
15
+ "execution_count": 3,
16
+ "metadata": {},
17
+ "output_type": "execute_result"
18
+ }
19
+ ],
20
+ "source": [
21
+ "import openai\n",
22
+ "from langchain_openai import ChatOpenAI, OpenAIEmbeddings\n",
23
+ "import tiktoken\n",
24
+ "from langchain.prompts.few_shot import FewShotPromptTemplate\n",
25
+ "from langchain.prompts.prompt import PromptTemplate\n",
26
+ "from operator import itemgetter\n",
27
+ "from langchain.schema import StrOutputParser\n",
28
+ "from langchain_core.output_parsers import StrOutputParser\n",
29
+ "from langchain_core.runnables import RunnablePassthrough\n",
30
+ "\n",
31
+ "import langchain_community.embeddings.huggingface\n",
32
+ "# help(langchain_community.embeddings.huggingface)\n",
33
+ "from langchain_community.embeddings.huggingface import HuggingFaceBgeEmbeddings\n",
34
+ "from langchain_community.vectorstores import FAISS\n",
35
+ "\n",
36
+ "from langchain.chains import LLMChain\n",
37
+ "from langchain.chains.conversation.memory import ConversationBufferMemory, ConversationBufferWindowMemory, ConversationSummaryMemory, ConversationSummaryBufferMemory\n",
38
+ "\n",
39
+ "import os, dotenv\n",
40
+ "from dotenv import load_dotenv\n",
41
+ "load_dotenv()"
42
+ ]
43
+ },
44
+ {
45
+ "cell_type": "code",
46
+ "execution_count": 5,
47
+ "id": "bf5aac26",
48
+ "metadata": {},
49
+ "outputs": [],
50
+ "source": [
51
+ "# embeddings = OpenAIEmbeddings(deployment=\"textembedding\", chunk_size = 16, api_key = os.environ[\"OPENAI_API_KEY\"])\n",
52
+ "# index_name = \"SCLC\"\n",
53
+ "\n",
54
+ "model_name = \"bge-large-en-v1.5\"\n",
55
+ "model_kwargs = {\"device\": \"cpu\"}\n",
56
+ "# model_kwargs = {\"device\": \"cuda\"}\n",
57
+ "encode_kwargs = {\"normalize_embeddings\": True}\n",
58
+ "embeddings = HuggingFaceBgeEmbeddings(\n",
59
+ " # model_name=model_name, \n",
60
+ " model_kwargs = model_kwargs,\n",
61
+ " encode_kwargs = encode_kwargs)\n",
62
+ "\n",
63
+ "index_name = \"indexes/ChestPainRubrics\"\n",
64
+ "\n",
65
+ "# store = FAISS.load_local(index_name, embeddings)\n",
66
+ "import db_firestore as db\n",
67
+ "store = db.get_store(index_name, embeddings=embeddings)"
68
+ ]
69
+ },
70
+ {
71
+ "cell_type": "code",
72
+ "execution_count": 28,
73
+ "id": "2d75b44a",
74
+ "metadata": {},
75
+ "outputs": [],
76
+ "source": [
77
+ "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",
78
+ "You will be provided with the following information:\n",
79
+ "1. The rubrics that the student should be judged based upon.\n",
80
+ "2. The conversation history between the medical student and the patient.\n",
81
+ "3. The final diagnosis that the student will make.\n",
82
+ "\n",
83
+ "Your grading should touch on every part of the rubrics, and grade the student holistically.\n",
84
+ "Finally, provide an overall grade for the student.\n",
85
+ "\n",
86
+ "Here is the rubrics:\n",
87
+ "{context}\n",
88
+ "\n",
89
+ "----------------------------------------------------------------\n",
90
+ "You are to give a comprehensive judgement based on the student's diagnosis, with reference to the above rubrics.\n",
91
+ "\n",
92
+ "Here is the chat history, enclosed in triple backticks:\n",
93
+ "```\n",
94
+ "{chat_history}\n",
95
+ "```\n",
96
+ "----------------------------------------------------------------\n",
97
+ "\n",
98
+ "Student's final diagnosis:\n",
99
+ "{question}\n",
100
+ "----------------------------------------------------------------\n",
101
+ "Your grade:\n",
102
+ "\"\"\"\n",
103
+ "\n",
104
+ "prompt = PromptTemplate(\n",
105
+ " input_variables = [\"question\", \"context\"],\n",
106
+ " template = TEMPLATE\n",
107
+ ")\n",
108
+ "retriever = store.as_retriever(search_type=\"similarity\", search_kwargs={\"k\":2})\n",
109
+ "def format_docs(docs):\n",
110
+ " return \"\\n--------------------\\n\".join(doc.page_content for doc in docs)\n",
111
+ "\n",
112
+ "\n",
113
+ "llm = ChatOpenAI(model_name=\"gpt-3.5-turbo\", temperature=0)\n",
114
+ "\n"
115
+ ]
116
+ },
117
+ {
118
+ "cell_type": "markdown",
119
+ "id": "bd14b945",
120
+ "metadata": {},
121
+ "source": [
122
+ "## some code to generate the fake history easier"
123
+ ]
124
+ },
125
+ {
126
+ "cell_type": "code",
127
+ "execution_count": 29,
128
+ "id": "e7fcc9e5",
129
+ "metadata": {},
130
+ "outputs": [
131
+ {
132
+ "name": "stdout",
133
+ "output_type": "stream",
134
+ "text": [
135
+ "[]\n"
136
+ ]
137
+ }
138
+ ],
139
+ "source": [
140
+ "fake_history = []\n",
141
+ "i = \" \"\n",
142
+ "\n",
143
+ "### Send no message to end it\n",
144
+ "while i:\n",
145
+ " i = input()\n",
146
+ " if i:\n",
147
+ " fake_history.append(i)\n",
148
+ "\n",
149
+ "print(fake_history)"
150
+ ]
151
+ },
152
+ {
153
+ "cell_type": "code",
154
+ "execution_count": 30,
155
+ "id": "df09e0dc",
156
+ "metadata": {},
157
+ "outputs": [],
158
+ "source": [
159
+ "## Sample history:\n",
160
+ "\n",
161
+ "fake_history = [\n",
162
+ " \"student: How are you mr tan?\",\n",
163
+ " \"patient: I am not feeling well...\",\n",
164
+ " \"student: oh, that's terrible! tell me more about what happened to you\",\n",
165
+ " \"patient: well I got this chest pain\",\n",
166
+ " \"student: I'm sorry to hear that. How long has your chest pain been?\",\n",
167
+ " \"patient: I can't remember\",\n",
168
+ "]"
169
+ ]
170
+ },
171
+ {
172
+ "cell_type": "code",
173
+ "execution_count": 31,
174
+ "id": "e6e21890",
175
+ "metadata": {},
176
+ "outputs": [],
177
+ "source": [
178
+ "## RESET MEMORY\n",
179
+ "## To reset the memory (if it screws up), rerun this cell\n",
180
+ "memory = ConversationSummaryBufferMemory(llm=llm, memory_key=\"chat_history\", input_key=\"question\" )\n",
181
+ "\n",
182
+ "chain = (\n",
183
+ " {\n",
184
+ " \"context\": retriever | format_docs, \n",
185
+ " \"question\": RunnablePassthrough(),\n",
186
+ " \"chat_history\": lambda x: '\\n'.join(fake_history)\n",
187
+ " } | \n",
188
+ " # prompt | \n",
189
+ " LLMChain(llm=llm, prompt=prompt, memory=memory, verbose=True) #| \n",
190
+ " # StrOutputParser()\n",
191
+ ")"
192
+ ]
193
+ },
194
+ {
195
+ "cell_type": "code",
196
+ "execution_count": 24,
197
+ "id": "f443e8c7",
198
+ "metadata": {},
199
+ "outputs": [
200
+ {
201
+ "name": "stdout",
202
+ "output_type": "stream",
203
+ "text": [
204
+ "Based on the provided rubrics and the conversation history, I would assess the student's diagnosis as Grade E. \n",
205
+ "\n",
206
+ "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",
207
+ "\n",
208
+ "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",
209
+ "\n",
210
+ "Overall, the student's diagnosis and assessment of the patient's condition are incomplete and do not meet the expected standards.\n"
211
+ ]
212
+ }
213
+ ],
214
+ "source": [
215
+ "results = chain.invoke(\"I believe he has chest pain, but it will go away soon\")\n",
216
+ "print(results.get(\"text\"))"
217
+ ]
218
+ }
219
+ ],
220
+ "metadata": {
221
+ "kernelspec": {
222
+ "display_name": "Python 3 (ipykernel)",
223
+ "language": "python",
224
+ "name": "python3"
225
+ },
226
+ "language_info": {
227
+ "codemirror_mode": {
228
+ "name": "ipython",
229
+ "version": 3
230
+ },
231
+ "file_extension": ".py",
232
+ "mimetype": "text/x-python",
233
+ "name": "python",
234
+ "nbconvert_exporter": "python",
235
+ "pygments_lexer": "ipython3",
236
+ "version": "3.8.5"
237
+ }
238
+ },
239
+ "nbformat": 4,
240
+ "nbformat_minor": 5
241
+ }
streamlit/app7.py CHANGED
@@ -1,10 +1,19 @@
1
  from openai import OpenAI
2
  import streamlit as st
 
3
  import datetime
4
 
 
 
 
 
 
 
 
 
 
5
  st.title("ChatGPT-like clone")
6
 
7
- client = OpenAI(api_key=st.secrets['openai']["OPENAI_API_KEY"])
8
 
9
  if "openai_model" not in st.session_state:
10
  st.session_state["openai_model"] = "gpt-3.5-turbo"
@@ -18,6 +27,37 @@ if "messages_2" not in st.session_state:
18
  if "start_time" not in st.session_state:
19
  st.session_state.start_time = None
20
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
  # Create two columns for the two chat interfaces
22
  col1, col2 = st.columns(2)
23
 
 
1
  from openai import OpenAI
2
  import streamlit as st
3
+ import streamlit.components.v1 as components
4
  import datetime
5
 
6
+
7
+ # client = OpenAI(api_key=st.secrets['openai']["OPENAI_API_KEY"])
8
+
9
+ from dotenv import load_dotenv
10
+ import os
11
+ load_dotenv()
12
+ key = os.environ.get("OPENAI_API_KEY")
13
+ client = OpenAI(api_key=key)
14
+
15
  st.title("ChatGPT-like clone")
16
 
 
17
 
18
  if "openai_model" not in st.session_state:
19
  st.session_state["openai_model"] = "gpt-3.5-turbo"
 
27
  if "start_time" not in st.session_state:
28
  st.session_state.start_time = None
29
 
30
+ ## Testing HTML
31
+ # html_string = """
32
+ # <canvas></canvas>
33
+
34
+
35
+ # <script>
36
+ # canvas = document.querySelector('canvas');
37
+ # canvas.width = 1024;
38
+ # canvas.height = 576;
39
+ # console.log(canvas);
40
+
41
+ # const c = canvas.getContext('2d');
42
+ # c.fillStyle = "green";
43
+ # c.fillRect(0,0,canvas.width,canvas.height);
44
+
45
+ # const img = new Image();
46
+ # img.src = "./tksfordumtrive.png";
47
+ # c.drawImage(img, 10, 10);
48
+ # </script>
49
+
50
+ # <style>
51
+ # body {
52
+ # margin: 0;
53
+ # }
54
+ # </style>
55
+ # """
56
+ # components.html(html_string,
57
+ # width=1280,
58
+ # height=640)
59
+
60
+
61
  # Create two columns for the two chat interfaces
62
  col1, col2 = st.columns(2)
63