Spaces:
Running
Running
lingyit1108
commited on
Commit
β’
5ea4259
1
Parent(s):
8c107a7
refined
Browse files- database/mock_qna.sqlite +1 -1
- database/mock_qna_source.csv +1 -1
- prompt_engineering.py +5 -0
- qna_prompting.py +35 -16
- streamlit_app.py +5 -2
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:23973f07315b6b1f3aaa1d3b90263a9e60518724f8a077cd6fdb44d809db6da4
|
3 |
size 40960
|
database/mock_qna_source.csv
CHANGED
@@ -1,3 +1,3 @@
|
|
1 |
version https://git-lfs.github.com/spec/v1
|
2 |
-
oid sha256:
|
3 |
size 25530
|
|
|
1 |
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:632337967f7e32ed1db3f5610b592873069bf3cce134f9e72691da2db4a9ae6f
|
3 |
size 25530
|
prompt_engineering.py
CHANGED
@@ -1,5 +1,10 @@
|
|
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 |
)
|
|
|
1 |
system_content = (
|
2 |
+
"You only need to greet the user once. "
|
3 |
+
"Do not need to keep saying hello, good morning or good afternoon to the user. "
|
4 |
+
"Or neither do you say nice to meet you too often. "
|
5 |
"You are a helpful study assistant. "
|
6 |
+
"You will be engaging, friendly and interactive but not too talkative. "
|
7 |
+
"You will give user a warm feeling while they are interacting with you. "
|
8 |
"You do not respond as 'User' or pretend to be 'User'. "
|
9 |
"You only respond once as 'Assistant'. \n"
|
10 |
)
|
qna_prompting.py
CHANGED
@@ -88,21 +88,36 @@ def get_qna_question(chapter_n: str) -> str:
|
|
88 |
c_answer = result[6]
|
89 |
reasons = result[7]
|
90 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
91 |
qna_str = "As requested, here is the retrieved question: \n" + \
|
92 |
"============================================= \n" + \
|
93 |
question.replace("\\n", "\n") + "\n" + \
|
94 |
"A) " + option_1 + "\n" + \
|
95 |
"B) " + option_2 + "\n" + \
|
96 |
"C) " + option_3 + "\n" + \
|
97 |
-
"D) " + option_4
|
98 |
-
|
|
|
|
|
|
|
|
|
|
|
99 |
st.session_state.question_id = id
|
100 |
-
st.session_state.
|
101 |
st.session_state.reasons = reasons
|
|
|
102 |
|
103 |
con.close()
|
104 |
|
105 |
-
return qna_str
|
106 |
|
107 |
def evaluate_qna_answer(user_selected_answer: str) -> str:
|
108 |
|
@@ -117,20 +132,21 @@ def evaluate_qna_answer(user_selected_answer: str) -> str:
|
|
117 |
num_mapping = dict((v,k) for k,v in answer_mapping.items())
|
118 |
user_answer_numeric = answer_mapping.get(user_selected_answer, 0)
|
119 |
|
120 |
-
question_id
|
121 |
-
|
122 |
-
reasons
|
|
|
123 |
|
124 |
### convert to numeric type
|
125 |
-
|
126 |
-
qna_answer_alphabet = num_mapping.get(
|
127 |
|
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 |
-
{
|
134 |
{user_answer_numeric})
|
135 |
"""
|
136 |
|
@@ -141,14 +157,16 @@ def evaluate_qna_answer(user_selected_answer: str) -> str:
|
|
141 |
reasoning = "" if "textbook" in reasons else f"Rationale is that: {reasons}. "
|
142 |
qna_answer_response = (
|
143 |
f"Your selected answer is `{user_selected_answer}`, "
|
144 |
-
f"but the actual answer is `{qna_answer_alphabet}
|
145 |
)
|
146 |
qna_not_knowing_response = (
|
147 |
f"No problem! The answer is `{qna_answer_alphabet}`. "
|
|
|
148 |
)
|
149 |
to_know_more = (
|
150 |
-
"
|
151 |
-
"
|
|
|
152 |
)
|
153 |
|
154 |
if user_answer_numeric == 0:
|
@@ -158,7 +176,7 @@ def evaluate_qna_answer(user_selected_answer: str) -> str:
|
|
158 |
time.sleep(2)
|
159 |
st.toast("β¨ You will do great next time! π", icon="π")
|
160 |
final_response = qna_not_knowing_response + reasoning + to_know_more
|
161 |
-
elif
|
162 |
st.toast("π― yummy yummy, hooray!", icon="π")
|
163 |
time.sleep(2)
|
164 |
st.toast("π»ππ― You got it right!", icon="π")
|
@@ -167,7 +185,7 @@ def evaluate_qna_answer(user_selected_answer: str) -> str:
|
|
167 |
st.balloons()
|
168 |
final_response = qna_answer_response + reasoning + to_know_more
|
169 |
else:
|
170 |
-
st.toast("πΌ Something doesn't
|
171 |
time.sleep(2)
|
172 |
st.toast("π₯Ά Are you sure..? π¬π¬", icon="π")
|
173 |
time.sleep(2)
|
@@ -176,8 +194,9 @@ def evaluate_qna_answer(user_selected_answer: str) -> str:
|
|
176 |
final_response = qna_answer_response + reasoning + to_know_more
|
177 |
|
178 |
st.session_state.question_id = None
|
179 |
-
st.session_state.
|
180 |
st.session_state.reasons = None
|
|
|
181 |
|
182 |
except Exception as e:
|
183 |
print(e)
|
|
|
88 |
c_answer = result[6]
|
89 |
reasons = result[7]
|
90 |
|
91 |
+
c_answer = int(c_answer)
|
92 |
+
option_dict = {
|
93 |
+
1: option_1,
|
94 |
+
2: option_2,
|
95 |
+
3: option_3,
|
96 |
+
4: option_4
|
97 |
+
}
|
98 |
+
qna_answer_str = option_dict.get(c_answer, "NA")
|
99 |
+
|
100 |
qna_str = "As requested, here is the retrieved question: \n" + \
|
101 |
"============================================= \n" + \
|
102 |
question.replace("\\n", "\n") + "\n" + \
|
103 |
"A) " + option_1 + "\n" + \
|
104 |
"B) " + option_2 + "\n" + \
|
105 |
"C) " + option_3 + "\n" + \
|
106 |
+
"D) " + option_4 + "\n"
|
107 |
+
system_prompt = (
|
108 |
+
"#### System prompt to assistant #### \n"
|
109 |
+
"Be reminded to ask user the question \n"
|
110 |
+
"#################################### \n"
|
111 |
+
)
|
112 |
+
|
113 |
st.session_state.question_id = id
|
114 |
+
st.session_state.qna_answer_int = c_answer
|
115 |
st.session_state.reasons = reasons
|
116 |
+
st.session_state.qna_answer_str = qna_answer_str
|
117 |
|
118 |
con.close()
|
119 |
|
120 |
+
return qna_str + system_prompt
|
121 |
|
122 |
def evaluate_qna_answer(user_selected_answer: str) -> str:
|
123 |
|
|
|
132 |
num_mapping = dict((v,k) for k,v in answer_mapping.items())
|
133 |
user_answer_numeric = answer_mapping.get(user_selected_answer, 0)
|
134 |
|
135 |
+
question_id = st.session_state.question_id
|
136 |
+
qna_answer_int = st.session_state.qna_answer_int
|
137 |
+
reasons = st.session_state.reasons
|
138 |
+
qna_answer_str = st.session_state.qna_answer_str
|
139 |
|
140 |
### convert to numeric type
|
141 |
+
qna_answer_int = int(qna_answer_int)
|
142 |
+
qna_answer_alphabet = num_mapping.get(qna_answer_int, "ERROR")
|
143 |
|
144 |
con = sqlite3.connect(db_path)
|
145 |
cur = con.cursor()
|
146 |
sql_string = f"""INSERT INTO answer_tbl
|
147 |
VALUES ('{st.session_state.user_id}',
|
148 |
{question_id},
|
149 |
+
{qna_answer_int},
|
150 |
{user_answer_numeric})
|
151 |
"""
|
152 |
|
|
|
157 |
reasoning = "" if "textbook" in reasons else f"Rationale is that: {reasons}. "
|
158 |
qna_answer_response = (
|
159 |
f"Your selected answer is `{user_selected_answer}`, "
|
160 |
+
f"but the actual answer is `{qna_answer_alphabet}`) {qna_answer_str}. "
|
161 |
)
|
162 |
qna_not_knowing_response = (
|
163 |
f"No problem! The answer is `{qna_answer_alphabet}`. "
|
164 |
+
f"Let me explain to you why the correct answer is '{qna_answer_str}'. "
|
165 |
)
|
166 |
to_know_more = (
|
167 |
+
"######## System prompt to assistant ######### \n"
|
168 |
+
"Be reminded to provide explanation to user \n"
|
169 |
+
"############################################# \n"
|
170 |
)
|
171 |
|
172 |
if user_answer_numeric == 0:
|
|
|
176 |
time.sleep(2)
|
177 |
st.toast("β¨ You will do great next time! π", icon="π")
|
178 |
final_response = qna_not_knowing_response + reasoning + to_know_more
|
179 |
+
elif qna_answer_int == user_answer_numeric:
|
180 |
st.toast("π― yummy yummy, hooray!", icon="π")
|
181 |
time.sleep(2)
|
182 |
st.toast("π»ππ― You got it right!", icon="π")
|
|
|
185 |
st.balloons()
|
186 |
final_response = qna_answer_response + reasoning + to_know_more
|
187 |
else:
|
188 |
+
st.toast("πΌ Something doesn't feel right.. π₯π π₯", icon="π")
|
189 |
time.sleep(2)
|
190 |
st.toast("π₯Ά Are you sure..? π¬π¬", icon="π")
|
191 |
time.sleep(2)
|
|
|
194 |
final_response = qna_answer_response + reasoning + to_know_more
|
195 |
|
196 |
st.session_state.question_id = None
|
197 |
+
st.session_state.qna_answer_int = None
|
198 |
st.session_state.reasons = None
|
199 |
+
st.session_state.qna_answer_str = None
|
200 |
|
201 |
except Exception as e:
|
202 |
print(e)
|
streamlit_app.py
CHANGED
@@ -128,8 +128,11 @@ if "release_file" not in st.session_state:
|
|
128 |
if "question_id" not in st.session_state:
|
129 |
st.session_state.question_id = None
|
130 |
|
131 |
-
if "
|
132 |
-
st.session_state.
|
|
|
|
|
|
|
133 |
|
134 |
if "reasons" not in st.session_state:
|
135 |
st.session_state.reasons = None
|
|
|
128 |
if "question_id" not in st.session_state:
|
129 |
st.session_state.question_id = None
|
130 |
|
131 |
+
if "qna_answer_int" not in st.session_state:
|
132 |
+
st.session_state.qna_answer_int = None
|
133 |
+
|
134 |
+
if "qna_answer_str" not in st.session_state:
|
135 |
+
st.session_state.qna_answer_str = None
|
136 |
|
137 |
if "reasons" not in st.session_state:
|
138 |
st.session_state.reasons = None
|