lingyit1108 commited on
Commit
c81f36b
β€’
1 Parent(s): 9b5e886

added logical reasoning

Browse files
Files changed (3) hide show
  1. database/mock_qna.sqlite +1 -1
  2. qna_prompting.py +53 -44
  3. streamlit_app.py +7 -3
database/mock_qna.sqlite CHANGED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:d2a27be7e05ba8545ed59d9f364028d86130cfb2460c74fa71fa16dd3b214e30
3
  size 40960
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:b1b4ac6a58713069f3545b7894a99e15587940b08ca42645a5146daf377456d4
3
  size 40960
qna_prompting.py CHANGED
@@ -64,10 +64,11 @@ def get_qna_question(chapter_n: str) -> str:
64
  cur = con.cursor()
65
 
66
  filter_clause = "WHERE a.id IS NULL" if chapter_n == "Chapter_0" else f"WHERE a.id IS NULL AND chapter='{chapter_n}'"
67
- sql_string = """SELECT q.id, question, option_1, option_2, option_3, option_4, q.correct_answer
68
  FROM qna_tbl q LEFT JOIN answer_tbl a
69
  ON q.id = a.id
70
  """ + filter_clause
 
71
 
72
  res = cur.execute(sql_string)
73
  result = res.fetchone()
@@ -79,6 +80,7 @@ def get_qna_question(chapter_n: str) -> str:
79
  option_3 = result[4]
80
  option_4 = result[5]
81
  c_answer = result[6]
 
82
 
83
  qna_str = "As requested, here is the retrieved question: \n" + \
84
  "============================================= \n" + \
@@ -90,6 +92,7 @@ def get_qna_question(chapter_n: str) -> str:
90
 
91
  st.session_state.question_id = id
92
  st.session_state.qna_answer = c_answer
 
93
 
94
  con.close()
95
 
@@ -97,49 +100,55 @@ def get_qna_question(chapter_n: str) -> str:
97
 
98
  def evaluate_qna_answer(user_selected_answer: str) -> str:
99
 
100
- answer_mapping = {
101
- "A": 1,
102
- "B": 2,
103
- "C": 3,
104
- "D": 4,
105
- "Z": 0
106
- }
107
- num_mapping = dict((v,k) for k,v in answer_mapping.items())
108
- user_answer_numeric = answer_mapping.get(user_selected_answer, 0)
109
-
110
- question_id = st.session_state.question_id
111
- qna_answer = st.session_state.qna_answer
112
- qna_answer_alphabet = num_mapping[qna_answer]
113
-
114
- con = sqlite3.connect(db_path)
115
- cur = con.cursor()
116
- sql_string = f"""INSERT INTO answer_tbl
117
- VALUES ({question_id}, {qna_answer}, {user_answer_numeric})
118
- """
119
-
120
- res = cur.execute(sql_string)
121
- con.commit()
122
- con.close()
123
-
124
- if qna_answer == user_answer_numeric:
125
- st.toast("🍯 yummy yummy, hooray!", icon="πŸŽ‰")
126
- time.sleep(2)
127
- st.toast("πŸ»πŸ’•πŸ― You got it right!", icon="🎊")
128
- time.sleep(2)
129
- st.toast("πŸ₯‡ You are amazing! πŸ’―πŸ’―", icon="πŸ’ͺ")
130
- st.balloons()
131
- else:
132
- st.toast("🐼 Something doesn't seem right.. πŸ”₯🏠πŸ”₯", icon="πŸ˜‚")
133
- time.sleep(2)
134
- st.toast("πŸ₯Ά Are you sure..? 😬😬", icon="😭")
135
- time.sleep(2)
136
- st.toast("πŸ€œπŸ€› Nevertheless, it was a good try!! πŸ‹οΈβ€β™‚οΈπŸ‹οΈβ€β™‚οΈ", icon="πŸ‘")
137
- st.snow()
138
-
139
- qna_answer_response = (
140
- f"Your selected answer is `{user_selected_answer}`, "
141
- f"but the actual answer is `{qna_answer_alphabet}`. "
142
- )
 
 
 
 
 
 
143
 
144
  return qna_answer_response
145
 
 
64
  cur = con.cursor()
65
 
66
  filter_clause = "WHERE a.id IS NULL" if chapter_n == "Chapter_0" else f"WHERE a.id IS NULL AND chapter='{chapter_n}'"
67
+ sql_string = """SELECT q.id, question, option_1, option_2, option_3, option_4, q.correct_answer, q.reasoning
68
  FROM qna_tbl q LEFT JOIN answer_tbl a
69
  ON q.id = a.id
70
  """ + filter_clause
71
+ # sql_string = sql_string + " ORDER BY RANDOM() LIMIT 1"
72
 
73
  res = cur.execute(sql_string)
74
  result = res.fetchone()
 
80
  option_3 = result[4]
81
  option_4 = result[5]
82
  c_answer = result[6]
83
+ reasons = result[7]
84
 
85
  qna_str = "As requested, here is the retrieved question: \n" + \
86
  "============================================= \n" + \
 
92
 
93
  st.session_state.question_id = id
94
  st.session_state.qna_answer = c_answer
95
+ st.session_state.reasons = reasons
96
 
97
  con.close()
98
 
 
100
 
101
  def evaluate_qna_answer(user_selected_answer: str) -> str:
102
 
103
+ try:
104
+ answer_mapping = {
105
+ "A": 1,
106
+ "B": 2,
107
+ "C": 3,
108
+ "D": 4,
109
+ "Z": 0
110
+ }
111
+ num_mapping = dict((v,k) for k,v in answer_mapping.items())
112
+ user_answer_numeric = answer_mapping.get(user_selected_answer, 0)
113
+
114
+ question_id = st.session_state.question_id
115
+ qna_answer = st.session_state.qna_answer
116
+ reasons = st.session_state.reasons
117
+
118
+ qna_answer_alphabet = num_mapping.get(int(qna_answer), "ERROR")
119
+
120
+ con = sqlite3.connect(db_path)
121
+ cur = con.cursor()
122
+ sql_string = f"""INSERT INTO answer_tbl
123
+ VALUES ({question_id}, {qna_answer}, {user_answer_numeric})
124
+ """
125
+
126
+ res = cur.execute(sql_string)
127
+ con.commit()
128
+ con.close()
129
+
130
+ if qna_answer == user_answer_numeric:
131
+ st.toast("🍯 yummy yummy, hooray!", icon="πŸŽ‰")
132
+ time.sleep(2)
133
+ st.toast("πŸ»πŸ’•πŸ― You got it right!", icon="🎊")
134
+ time.sleep(2)
135
+ st.toast("πŸ₯‡ You are amazing! πŸ’―πŸ’―", icon="πŸ’ͺ")
136
+ st.balloons()
137
+ else:
138
+ st.toast("🐼 Something doesn't seem right.. πŸ”₯🏠πŸ”₯", icon="πŸ˜‚")
139
+ time.sleep(2)
140
+ st.toast("πŸ₯Ά Are you sure..? 😬😬", icon="😭")
141
+ time.sleep(2)
142
+ st.toast("πŸ€œπŸ€› Nevertheless, it was a good try!! πŸ‹οΈβ€β™‚οΈπŸ‹οΈβ€β™‚οΈ", icon="πŸ‘")
143
+ st.snow()
144
+
145
+ reasoning = "" if "textbook" in reasons else "Rationale is that: " + reasons
146
+ qna_answer_response = (
147
+ f"Your selected answer is `{user_selected_answer}`, "
148
+ f"but the actual answer is `{qna_answer_alphabet}`. " + reasoning
149
+ )
150
+ except Exception as e:
151
+ print(e)
152
 
153
  return qna_answer_response
154
 
streamlit_app.py CHANGED
@@ -117,9 +117,10 @@ with st.sidebar:
117
  os.environ["OPENAI_API_KEY"] = openai_api
118
 
119
  st.subheader("Models and parameters")
120
- selected_model = st.sidebar.selectbox("Choose an OpenAI model",
121
- ["gpt-4-0125-preview", "gpt-3.5-turbo-0125"],
122
- key="selected_model")
 
123
  temperature = st.sidebar.slider("temperature", min_value=0.0, max_value=2.0,
124
  value=0.0, step=0.01)
125
  st.data_editor(
@@ -160,6 +161,9 @@ if "question_id" not in st.session_state:
160
  if "qna_answer" not in st.session_state:
161
  st.session_state.qna_answer = None
162
 
 
 
 
163
  def clear_chat_history():
164
 
165
  st.session_state.messages = [{"role": "assistant",
 
117
  os.environ["OPENAI_API_KEY"] = openai_api
118
 
119
  st.subheader("Models and parameters")
120
+ selected_model = st.sidebar.selectbox(label="Choose an OpenAI model",
121
+ options=["gpt-3.5-turbo-0125", "gpt-4-0125-preview"],
122
+ index=0,
123
+ key="selected_model")
124
  temperature = st.sidebar.slider("temperature", min_value=0.0, max_value=2.0,
125
  value=0.0, step=0.01)
126
  st.data_editor(
 
161
  if "qna_answer" not in st.session_state:
162
  st.session_state.qna_answer = None
163
 
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",