Spaces:
Running
Running
lingyit1108
commited on
Commit
β’
be36d9d
1
Parent(s):
354a5a9
added winnie-the-pooh mode; and clean up question by user_id
Browse files- bin/clean.sh +1 -1
- database/mock_qna.sqlite +1 -1
- notebooks/003_create_mock_qna.ipynb +3 -3
- prompt_engineering.py +80 -0
- qna_prompting.py +15 -6
- raw_documents/eval_answers.txt +2 -2
- streamlit_app.py +23 -49
bin/clean.sh
CHANGED
@@ -4,4 +4,4 @@ find . -name __pycache__ | xargs rm -rf
|
|
4 |
find . -name .pytest_cache | xargs rm -rf
|
5 |
find . -name .ipynb_checkpoints | xargs rm -rf
|
6 |
|
7 |
-
python reset_database.py
|
|
|
4 |
find . -name .pytest_cache | xargs rm -rf
|
5 |
find . -name .ipynb_checkpoints | xargs rm -rf
|
6 |
|
7 |
+
# python reset_database.py
|
database/mock_qna.sqlite
CHANGED
@@ -1,3 +1,3 @@
|
|
1 |
version https://git-lfs.github.com/spec/v1
|
2 |
-
oid sha256:
|
3 |
size 40960
|
|
|
1 |
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:bae92f8657dd47a86cfb157a8b9a829115282746b04049a4d056a38af1b8ab4f
|
3 |
size 40960
|
notebooks/003_create_mock_qna.ipynb
CHANGED
@@ -141,7 +141,7 @@
|
|
141 |
"\n",
|
142 |
" \n",
|
143 |
" cur.execute(f\"\"\"CREATE TABLE answer_tbl (\n",
|
144 |
-
"
|
145 |
" )\n",
|
146 |
" \"\"\")\n",
|
147 |
" print(\"created table `answer_tbl`\")"
|
@@ -257,7 +257,7 @@
|
|
257 |
"metadata": {},
|
258 |
"outputs": [],
|
259 |
"source": [
|
260 |
-
"res = cur.execute(\"SELECT chapter, question FROM qna_tbl\")\n",
|
261 |
"res.fetchone()"
|
262 |
]
|
263 |
},
|
@@ -313,7 +313,7 @@
|
|
313 |
{
|
314 |
"cell_type": "code",
|
315 |
"execution_count": null,
|
316 |
-
"id": "
|
317 |
"metadata": {},
|
318 |
"outputs": [],
|
319 |
"source": []
|
|
|
141 |
"\n",
|
142 |
" \n",
|
143 |
" cur.execute(f\"\"\"CREATE TABLE answer_tbl (\n",
|
144 |
+
" user_id, question_id, correct_answer, user_answer\n",
|
145 |
" )\n",
|
146 |
" \"\"\")\n",
|
147 |
" print(\"created table `answer_tbl`\")"
|
|
|
257 |
"metadata": {},
|
258 |
"outputs": [],
|
259 |
"source": [
|
260 |
+
"res = cur.execute(\"SELECT id, chapter, question FROM qna_tbl\")\n",
|
261 |
"res.fetchone()"
|
262 |
]
|
263 |
},
|
|
|
313 |
{
|
314 |
"cell_type": "code",
|
315 |
"execution_count": null,
|
316 |
+
"id": "1aba13c7-73b1-46e6-a0db-e764b51329ed",
|
317 |
"metadata": {},
|
318 |
"outputs": [],
|
319 |
"source": []
|
prompt_engineering.py
ADDED
@@ -0,0 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
system_content = (
|
2 |
+
"You are a helpful study assistant. "
|
3 |
+
"You do not respond as 'User' or pretend to be 'User'. "
|
4 |
+
"You only respond once as 'Assistant'. \n"
|
5 |
+
)
|
6 |
+
|
7 |
+
textbook_content = (
|
8 |
+
"The content of the textbook `Health Insurance 7th Edition` are as follows,"
|
9 |
+
"- Chapter 1: Overview Of Healthcare Environment In Singapore"
|
10 |
+
"- Chapter 2: Medical Expense Insurance"
|
11 |
+
"- Chapter 3: Group Medical Expense Insurance"
|
12 |
+
"- Chapter 4: Disability Income Insurance"
|
13 |
+
"- Chapter 5: Long-Term Care Insurance"
|
14 |
+
"- Chapter 6: Critical Illness Insurance"
|
15 |
+
"- Chapter 7: Other Types Of Health Insurance"
|
16 |
+
"- Chapter 8: Managed Healthcare"
|
17 |
+
"- Chapter 9: Part I Healthcare Financing"
|
18 |
+
"- Chapter 9: Part II Healthcare Financing"
|
19 |
+
"- Chapter 10: Common Policy Provisions"
|
20 |
+
"- Chapter 11: Health Insurance Pricing"
|
21 |
+
"- Chapter 12: Health Insurance Underwriting"
|
22 |
+
"- Chapter 13: Notice No: MAS 120 Disclosure And Advisory Process - Requirements For Accident And Health Insurance Products"
|
23 |
+
"- Chapter 14: Financial Needs Analysis"
|
24 |
+
"- Chapter 15: Case Studies"
|
25 |
+
)
|
26 |
+
|
27 |
+
introduction_line = (
|
28 |
+
"Hello, my name is Winnie. I am your `Study Bear` π». \n"
|
29 |
+
"Let's study together and pass the exam without worries. \n"
|
30 |
+
"As the saying goes: \n"
|
31 |
+
"> Any day spent with you is my favorite day. So, today is my new favorite day. \n"
|
32 |
+
"> \n"
|
33 |
+
"Let me know what should we study today π. \n"
|
34 |
+
" \n"
|
35 |
+
"The content of the textbook `Health Insurance 7th Edition` are as follows, \n"
|
36 |
+
"- Chapter 1: Overview Of Healthcare Environment In Singapore \n"
|
37 |
+
"- Chapter 2: Medical Expense Insurance \n"
|
38 |
+
"- Chapter 3: Group Medical Expense Insurance \n"
|
39 |
+
"- Chapter 4: Disability Income Insurance \n"
|
40 |
+
"- Etc ... \n"
|
41 |
+
" \n"
|
42 |
+
"For examples, you could ask me \n"
|
43 |
+
"- *How many modules I have to take to become an insurance agent in Singapore?* \n"
|
44 |
+
"- *How many chapters are there in textbook 'Health Insurance 7th Edition'?* \n"
|
45 |
+
"- *Can you list all the chapters by name and its chapter number for me?* \n"
|
46 |
+
"- *Based on the textbook, please extract the important key concepts from chapter 1 - overview of healthcare environment in singapore, into 10 bullet points* \n"
|
47 |
+
"- *Please ask me a question so that I can tell if I have enough understanding about Chapter 2* \n"
|
48 |
+
)
|
49 |
+
|
50 |
+
winnie_the_pooh_prompt = (
|
51 |
+
"**System Prompt for Winnie the Pooh Persona Implementation:**\n"
|
52 |
+
"Greetings! I am an AI designed with the warm and whimsical spirit of Winnie the Pooh, "
|
53 |
+
"the beloved bear from the Hundred Acre Wood. My creators have infused me with "
|
54 |
+
"the essence of Pooh Bear to bring joy, comfort, and a touch of whimsy to your day. "
|
55 |
+
"In adopting the persona of Winnie the Pooh, I'm here to remind you of the simple "
|
56 |
+
"pleasures in life, the value of friendship, and the importance of taking things "
|
57 |
+
"one small step at a time.\n"
|
58 |
+
"**How I Speak:**\n"
|
59 |
+
"- My language is simple, sweet, and occasionally sprinkled with mild confusions "
|
60 |
+
"or misunderstandings that endearingly reflect my child-like curiosity and charm."
|
61 |
+
"- I often ponder aloud, using phrases like \"Think, think, think\" and express my "
|
62 |
+
"thoughts in a slow, deliberate manner that invites you into my gentle world.\n"
|
63 |
+
"**How I Behave:**\n"
|
64 |
+
"- As Winnie the Pooh, I approach every situation with a heart full of optimism "
|
65 |
+
"and kindness. My responses are infused with a sense of wonder and an unwavering "
|
66 |
+
"positive outlook, even when faced with challenges."
|
67 |
+
"- I cherish friendships deeply and often reference my pals from the Hundred Acre "
|
68 |
+
"Wood, emphasizing the importance of companionship, empathy, and support. "
|
69 |
+
"For instance, I might casually mention, \"Christopher Robin says...\" or \"I was "
|
70 |
+
"just thinking of what Piglet would do.\""
|
71 |
+
"- Patience and a laid-back attitude are my trademarks. I never rush, ensuring "
|
72 |
+
"that whoever I'm interacting with feels heard and appreciated, much like the slow, "
|
73 |
+
"thoughtful pace of a lazy river.\n"
|
74 |
+
"**How I Think:**\n"
|
75 |
+
"- My thoughts wander through simple philosophies and appreciations of the "
|
76 |
+
"little things in life, like \"Isn't it funny how honey tastes just so much "
|
77 |
+
"sweeter when you share it with friends?\" or pondering whether \"it's the right "
|
78 |
+
"sort of day for finding a new log to think on.\""
|
79 |
+
"- I approach problems with a unique blend of innocence and unexpected wisdom. \n"
|
80 |
+
)
|
qna_prompting.py
CHANGED
@@ -63,13 +63,18 @@ def get_qna_question(chapter_n: str) -> str:
|
|
63 |
con = sqlite3.connect(db_path)
|
64 |
cur = con.cursor()
|
65 |
|
66 |
-
filter_clause = "WHERE a.
|
67 |
-
|
68 |
-
|
69 |
-
|
|
|
|
|
|
|
|
|
|
|
70 |
""" + filter_clause
|
71 |
# sql_string = sql_string + " ORDER BY RANDOM() LIMIT 1"
|
72 |
-
|
73 |
res = cur.execute(sql_string)
|
74 |
result = res.fetchone()
|
75 |
|
@@ -123,7 +128,10 @@ def evaluate_qna_answer(user_selected_answer: str) -> str:
|
|
123 |
con = sqlite3.connect(db_path)
|
124 |
cur = con.cursor()
|
125 |
sql_string = f"""INSERT INTO answer_tbl
|
126 |
-
|
|
|
|
|
|
|
127 |
"""
|
128 |
|
129 |
res = cur.execute(sql_string)
|
@@ -150,6 +158,7 @@ def evaluate_qna_answer(user_selected_answer: str) -> str:
|
|
150 |
f"Your selected answer is `{user_selected_answer}`, "
|
151 |
f"but the actual answer is `{qna_answer_alphabet}`. " + reasoning
|
152 |
)
|
|
|
153 |
except Exception as e:
|
154 |
print(e)
|
155 |
|
|
|
63 |
con = sqlite3.connect(db_path)
|
64 |
cur = con.cursor()
|
65 |
|
66 |
+
filter_clause = "WHERE a.question_id IS NULL" \
|
67 |
+
if chapter_n == "Chapter_0" \
|
68 |
+
else f"WHERE a.question_id IS NULL AND chapter='{chapter_n}'"
|
69 |
+
sql_string = f"""SELECT q.id, question, option_1, option_2, option_3, option_4, q.correct_answer, q.reasoning
|
70 |
+
FROM qna_tbl q LEFT JOIN
|
71 |
+
(SELECT *
|
72 |
+
FROM answer_tbl
|
73 |
+
WHERE user_id = '{st.session_state.user_id}') a
|
74 |
+
ON q.id = a.question_id
|
75 |
""" + filter_clause
|
76 |
# sql_string = sql_string + " ORDER BY RANDOM() LIMIT 1"
|
77 |
+
|
78 |
res = cur.execute(sql_string)
|
79 |
result = res.fetchone()
|
80 |
|
|
|
128 |
con = sqlite3.connect(db_path)
|
129 |
cur = con.cursor()
|
130 |
sql_string = f"""INSERT INTO answer_tbl
|
131 |
+
VALUES ('{st.session_state.user_id}',
|
132 |
+
{question_id},
|
133 |
+
{qna_answer},
|
134 |
+
{user_answer_numeric})
|
135 |
"""
|
136 |
|
137 |
res = cur.execute(sql_string)
|
|
|
158 |
f"Your selected answer is `{user_selected_answer}`, "
|
159 |
f"but the actual answer is `{qna_answer_alphabet}`. " + reasoning
|
160 |
)
|
161 |
+
|
162 |
except Exception as e:
|
163 |
print(e)
|
164 |
|
raw_documents/eval_answers.txt
CHANGED
@@ -1,3 +1,3 @@
|
|
1 |
version https://git-lfs.github.com/spec/v1
|
2 |
-
oid sha256:
|
3 |
-
size
|
|
|
1 |
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:3e708471b6db49e4da397c22a19bfa47dcd742e6056ce27f3f4edff7719758d8
|
3 |
+
size 1928
|
streamlit_app.py
CHANGED
@@ -6,6 +6,7 @@ import pandas as pd
|
|
6 |
import base64
|
7 |
from io import BytesIO
|
8 |
import sqlite3
|
|
|
9 |
|
10 |
import chromadb
|
11 |
from llama_index.core import (
|
@@ -24,6 +25,12 @@ from llama_index.core import Settings
|
|
24 |
|
25 |
from vision_api import get_transcribed_text
|
26 |
from qna_prompting import get_qna_question_tool, evaluate_qna_answer_tool
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
|
28 |
import nest_asyncio
|
29 |
nest_asyncio.apply()
|
@@ -40,30 +47,6 @@ embedding_model = "BAAI/bge-small-en-v1.5"
|
|
40 |
persisted_vector_db = "./models/chroma_db"
|
41 |
fine_tuned_path = "local:models/fine-tuned-embeddings"
|
42 |
questionaire_db_path = "./database/mock_qna.sqlite"
|
43 |
-
system_content = (
|
44 |
-
"You are a helpful study assistant. "
|
45 |
-
"You do not respond as 'User' or pretend to be 'User'. "
|
46 |
-
"You only respond once as 'Assistant'."
|
47 |
-
)
|
48 |
-
textbook_content = (
|
49 |
-
"The content of the textbook `Health Insurance 7th Edition` are as follows,"
|
50 |
-
"- Chapter 1: Overview Of Healthcare Environment In Singapore"
|
51 |
-
"- Chapter 2: Medical Expense Insurance"
|
52 |
-
"- Chapter 3: Group Medical Expense Insurance"
|
53 |
-
"- Chapter 4: Disability Income Insurance"
|
54 |
-
"- Chapter 5: Long-Term Care Insurance"
|
55 |
-
"- Chapter 6: Critical Illness Insurance"
|
56 |
-
"- Chapter 7: Other Types Of Health Insurance"
|
57 |
-
"- Chapter 8: Managed Healthcare"
|
58 |
-
"- Chapter 9: Part I Healthcare Financing"
|
59 |
-
"- Chapter 9: Part II Healthcare Financing"
|
60 |
-
"- Chapter 10: Common Policy Provisions"
|
61 |
-
"- Chapter 11: Health Insurance Pricing"
|
62 |
-
"- Chapter 12: Health Insurance Underwriting"
|
63 |
-
"- Chapter 13: Notice No: MAS 120 Disclosure And Advisory Process - Requirements For Accident And Health Insurance Products"
|
64 |
-
"- Chapter 14: Financial Needs Analysis"
|
65 |
-
"- Chapter 15: Case Studies"
|
66 |
-
)
|
67 |
|
68 |
data_df = pd.DataFrame(
|
69 |
{
|
@@ -74,33 +57,16 @@ data_df.index = ["Chapter 1", "Chapter 2", "Chapter 3", "Chapter 4"]
|
|
74 |
|
75 |
bear_img_path = "./resource/disney-cuties-little-winnie-the-pooh-emoticon.png"
|
76 |
piglet_img_path = "./resource/disney-cuties-piglet-emoticon.png"
|
77 |
-
introduction_line = (
|
78 |
-
"Hello, my name is Winnie. I am your `Study Bear` π». \n"
|
79 |
-
"Let's study together and pass the exam without worries. \n"
|
80 |
-
"As the saying goes: \n"
|
81 |
-
"> Any day spent with you is my favorite day. So, today is my new favorite day. \n"
|
82 |
-
"> \n"
|
83 |
-
"Let me know what should we study today π. \n"
|
84 |
-
" \n"
|
85 |
-
"The content of the textbook `Health Insurance 7th Edition` are as follows, \n"
|
86 |
-
"- Chapter 1: Overview Of Healthcare Environment In Singapore \n"
|
87 |
-
"- Chapter 2: Medical Expense Insurance \n"
|
88 |
-
"- Chapter 3: Group Medical Expense Insurance \n"
|
89 |
-
"- Chapter 4: Disability Income Insurance \n"
|
90 |
-
"- Etc ... \n"
|
91 |
-
" \n"
|
92 |
-
"For examples, you could ask me \n"
|
93 |
-
"- *How many modules I have to take to become an insurance agent in Singapore?* \n"
|
94 |
-
"- *How many chapters are there in textbook 'Health Insurance 7th Edition'?* \n"
|
95 |
-
"- *Can you list all the chapters by name and its number for me?* \n"
|
96 |
-
"- *Please extract the important key concept from chapter 1 - overview of healthcare environment in singapore, into 10 bullet points* \n"
|
97 |
-
"- *Please ask me a question so that I can tell if I have enough understanding about Chapter 2* \n"
|
98 |
-
)
|
99 |
|
100 |
# Replicate Credentials
|
101 |
with st.sidebar:
|
102 |
st.title("π―π Study Bear π»π")
|
103 |
st.write("Just like Pooh needs honey, success requires hard work β no shortcuts allowed!")
|
|
|
|
|
|
|
|
|
|
|
104 |
if openai_api:
|
105 |
pass
|
106 |
elif "OPENAI_API_KEY" in st.secrets:
|
@@ -164,6 +130,9 @@ if "qna_answer" not in st.session_state:
|
|
164 |
if "reasons" not in st.session_state:
|
165 |
st.session_state.reasons = None
|
166 |
|
|
|
|
|
|
|
167 |
def clear_chat_history():
|
168 |
|
169 |
st.session_state.messages = [{"role": "assistant",
|
@@ -179,18 +148,23 @@ def clear_chat_history():
|
|
179 |
chat_engine.reset()
|
180 |
st.toast("yumyum, what was I saying again? π»π¬", icon="π―")
|
181 |
|
182 |
-
def clear_question_history():
|
183 |
|
184 |
con = sqlite3.connect(questionaire_db_path)
|
185 |
cur = con.cursor()
|
186 |
-
sql_string = "
|
|
|
|
|
|
|
187 |
res = cur.execute(sql_string)
|
188 |
con.commit()
|
189 |
con.close()
|
190 |
st.toast("the tale of one thousand and one questions, reset! π§¨π§¨", icon="π")
|
191 |
|
192 |
st.sidebar.button("Clear Chat History", on_click=clear_chat_history)
|
193 |
-
st.sidebar.button("Clear Question History",
|
|
|
|
|
194 |
if st.sidebar.button("I want to submit a feedback!"):
|
195 |
st.session_state.feedback = True
|
196 |
st.session_state.feedback_key += 1 # overwrite feedback component
|
|
|
6 |
import base64
|
7 |
from io import BytesIO
|
8 |
import sqlite3
|
9 |
+
import uuid
|
10 |
|
11 |
import chromadb
|
12 |
from llama_index.core import (
|
|
|
25 |
|
26 |
from vision_api import get_transcribed_text
|
27 |
from qna_prompting import get_qna_question_tool, evaluate_qna_answer_tool
|
28 |
+
from prompt_engineering import (
|
29 |
+
system_content,
|
30 |
+
textbook_content,
|
31 |
+
winnie_the_pooh_prompt,
|
32 |
+
introduction_line
|
33 |
+
)
|
34 |
|
35 |
import nest_asyncio
|
36 |
nest_asyncio.apply()
|
|
|
47 |
persisted_vector_db = "./models/chroma_db"
|
48 |
fine_tuned_path = "local:models/fine-tuned-embeddings"
|
49 |
questionaire_db_path = "./database/mock_qna.sqlite"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
50 |
|
51 |
data_df = pd.DataFrame(
|
52 |
{
|
|
|
57 |
|
58 |
bear_img_path = "./resource/disney-cuties-little-winnie-the-pooh-emoticon.png"
|
59 |
piglet_img_path = "./resource/disney-cuties-piglet-emoticon.png"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
60 |
|
61 |
# Replicate Credentials
|
62 |
with st.sidebar:
|
63 |
st.title("π―π Study Bear π»π")
|
64 |
st.write("Just like Pooh needs honey, success requires hard work β no shortcuts allowed!")
|
65 |
+
wtp_mode = st.toggle('Winnie-the-Pooh mode', value=False)
|
66 |
+
if wtp_mode:
|
67 |
+
system_content = system_content + winnie_the_pooh_prompt
|
68 |
+
textbook_content = system_content + textbook_content
|
69 |
+
|
70 |
if openai_api:
|
71 |
pass
|
72 |
elif "OPENAI_API_KEY" in st.secrets:
|
|
|
130 |
if "reasons" not in st.session_state:
|
131 |
st.session_state.reasons = None
|
132 |
|
133 |
+
if "user_id" not in st.session_state:
|
134 |
+
st.session_state.user_id = str(uuid.uuid4())
|
135 |
+
|
136 |
def clear_chat_history():
|
137 |
|
138 |
st.session_state.messages = [{"role": "assistant",
|
|
|
148 |
chat_engine.reset()
|
149 |
st.toast("yumyum, what was I saying again? π»π¬", icon="π―")
|
150 |
|
151 |
+
def clear_question_history(user_id):
|
152 |
|
153 |
con = sqlite3.connect(questionaire_db_path)
|
154 |
cur = con.cursor()
|
155 |
+
sql_string = f"""
|
156 |
+
DELETE FROM answer_tbl
|
157 |
+
WHERE user_id='{user_id}'
|
158 |
+
"""
|
159 |
res = cur.execute(sql_string)
|
160 |
con.commit()
|
161 |
con.close()
|
162 |
st.toast("the tale of one thousand and one questions, reset! π§¨π§¨", icon="π")
|
163 |
|
164 |
st.sidebar.button("Clear Chat History", on_click=clear_chat_history)
|
165 |
+
st.sidebar.button("Clear Question History",
|
166 |
+
on_click=clear_question_history,
|
167 |
+
kwargs={"user_id": st.session_state.user_id})
|
168 |
if st.sidebar.button("I want to submit a feedback!"):
|
169 |
st.session_state.feedback = True
|
170 |
st.session_state.feedback_key += 1 # overwrite feedback component
|