Spaces:
Sleeping
Sleeping
Commit
•
0693b35
1
Parent(s):
1399236
Fix bugs in sidebar chat (#1)
Browse files- Fix bugs in sidebar chat (18ee183ef6fdd2492e3a163b6712b25e827b80dc)
Co-authored-by: Michael Wornow <[email protected]>
app.py
CHANGED
@@ -8,10 +8,11 @@ st.set_page_config(page_title="Tutor", page_icon=":heavy_plus_sign:")
|
|
8 |
st.markdown("""
|
9 |
<div>
|
10 |
<h3 style='text-align: center;'> Tutor Session with Becky (Student) </h3>
|
11 |
-
<p style='text-align: center;'> The bot is a 1st grade student Becky taking a Math class. """
|
12 |
, unsafe_allow_html=True)
|
13 |
|
14 |
# Set org ID and API key
|
|
|
15 |
openai.api_key = os.environ['TOKEN']
|
16 |
API_KEY = os.environ['TOKEN']
|
17 |
|
@@ -64,95 +65,107 @@ You have a simple vocabulary, elementary grammar and sentence structure, and spe
|
|
64 |
|
65 |
"""
|
66 |
|
67 |
-
def
|
|
|
68 |
return 'assistant' if name == 'tutor' else 'user'
|
69 |
-
def
|
|
|
70 |
return 'assistant' if name == 'student' else 'user'
|
71 |
|
72 |
-
st.sidebar.title("Sidebar")
|
73 |
-
|
74 |
-
if st.sidebar.button('Introduction/Conversation'):
|
75 |
-
with st.chat_message(name_to_role('tutor')): # tutor
|
76 |
-
# assistant == tutor
|
77 |
-
message_placeholder = st.empty()
|
78 |
-
full_response = ""
|
79 |
-
for response in openai.ChatCompletion.create(
|
80 |
-
model=st.session_state["openai_model"],
|
81 |
-
messages=[ st.session_state.tutor_introduction_message_system_prompt ] + [
|
82 |
-
{ "role": name_to_role(m["name"]), "content": m["content"] }
|
83 |
-
for m in st.session_state.messages
|
84 |
-
],
|
85 |
-
stream=True,
|
86 |
-
api_key=API_KEY
|
87 |
-
):
|
88 |
-
full_response += response.choices[0].delta.get("content", "")
|
89 |
-
st.sidebar.success(full_response)
|
90 |
-
|
91 |
-
if st.sidebar.button('Encouragement'):
|
92 |
-
with st.chat_message(name_to_role('tutor')): # tutor
|
93 |
-
# assistant == tutor
|
94 |
-
message_placeholder = st.empty()
|
95 |
-
full_response = ""
|
96 |
-
for response in openai.ChatCompletion.create(
|
97 |
-
model=st.session_state["openai_model"],
|
98 |
-
messages=[ st.session_state.tutor_encouragement_message_system_prompt ] + [
|
99 |
-
{ "role": name_to_role(m["name"]), "content": m["content"] }
|
100 |
-
for m in st.session_state.messages
|
101 |
-
],
|
102 |
-
stream=True,
|
103 |
-
api_key=API_KEY
|
104 |
-
):
|
105 |
-
full_response += response.choices[0].delta.get("content", "")
|
106 |
-
st.sidebar.success(full_response)
|
107 |
-
|
108 |
-
if st.sidebar.button('Conclude Session'):
|
109 |
-
with st.chat_message(name_to_role('tutor')): # tutor
|
110 |
-
# assistant == tutor
|
111 |
-
message_placeholder = st.empty()
|
112 |
-
full_response = ""
|
113 |
-
for response in openai.ChatCompletion.create(
|
114 |
-
model=st.session_state["openai_model"],
|
115 |
-
messages=[ st.session_state.tutor_conclusion_message_system_prompt ] + [
|
116 |
-
{ "role": name_to_role(m["name"]), "content": m["content"] }
|
117 |
-
for m in st.session_state.messages
|
118 |
-
],
|
119 |
-
stream=True,
|
120 |
-
api_key=API_KEY
|
121 |
-
):
|
122 |
-
full_response += response.choices[0].delta.get("content", "")
|
123 |
-
st.sidebar.success(full_response)
|
124 |
-
|
125 |
-
st.sidebar.text("""
|
126 |
-
You are tutor Mary. Your student today is Becky, who is a first grade student here for math class (addition)
|
127 |
-
""")
|
128 |
-
|
129 |
if "openai_model" not in st.session_state:
|
130 |
st.session_state["openai_model"] = "gpt-3.5-turbo"
|
131 |
|
132 |
if "messages" not in st.session_state:
|
133 |
-
st.session_state.messages = [{ 'name' : 'student', 'content' : "
|
134 |
st.session_state.tutor_introduction_message_system_prompt = {"role": "system", "content": context_tutor + context_introduction}
|
135 |
st.session_state.tutor_encouragement_message_system_prompt = {"role": "system", "content": context_tutor + context_encouragement}
|
136 |
st.session_state.tutor_conclusion_message_system_prompt = {"role": "system", "content": context_tutor + context_conclusion}
|
137 |
st.session_state.student_message_system_prompt = {"role": "system", "content": context_student}
|
138 |
|
|
|
|
|
|
|
|
|
|
|
|
|
139 |
for message in st.session_state.messages:
|
140 |
-
with st.chat_message(
|
141 |
st.markdown(message['content'])
|
142 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
143 |
if prompt := st.chat_input("Start talking with your student!"):
|
144 |
-
st.session_state.messages.append({"
|
145 |
-
with st.chat_message(
|
146 |
st.markdown(prompt)
|
147 |
-
|
148 |
-
with st.chat_message(
|
149 |
# assistant == student
|
150 |
message_placeholder = st.empty()
|
|
|
|
|
|
|
|
|
151 |
full_response = ""
|
152 |
for response in openai.ChatCompletion.create(
|
153 |
model=st.session_state["openai_model"],
|
154 |
messages=[ st.session_state.student_message_system_prompt ] + [
|
155 |
-
{ "role":
|
156 |
for m in st.session_state.messages
|
157 |
],
|
158 |
stream=True,
|
@@ -161,5 +174,4 @@ if prompt := st.chat_input("Start talking with your student!"):
|
|
161 |
full_response += response.choices[0].delta.get("content", "")
|
162 |
message_placeholder.markdown(full_response + "▌")
|
163 |
message_placeholder.markdown(full_response)
|
164 |
-
|
165 |
-
st.session_state.messages.append({ "name": "student", "content": full_response })
|
|
|
8 |
st.markdown("""
|
9 |
<div>
|
10 |
<h3 style='text-align: center;'> Tutor Session with Becky (Student) </h3>
|
11 |
+
<p style='text-align: center;'> You are a tutor named Mary. The bot is a 1st grade student Becky taking a Math class. """
|
12 |
, unsafe_allow_html=True)
|
13 |
|
14 |
# Set org ID and API key
|
15 |
+
os.environ['TOKEN'] = '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'
|
16 |
openai.api_key = os.environ['TOKEN']
|
17 |
API_KEY = os.environ['TOKEN']
|
18 |
|
|
|
65 |
|
66 |
"""
|
67 |
|
68 |
+
def name_to_role_gpt_as_tutor(name):
|
69 |
+
# have GPT-4 act as tutor
|
70 |
return 'assistant' if name == 'tutor' else 'user'
|
71 |
+
def name_to_role_gpt_as_student(name):
|
72 |
+
# have GPT-4 act as student
|
73 |
return 'assistant' if name == 'student' else 'user'
|
74 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
75 |
if "openai_model" not in st.session_state:
|
76 |
st.session_state["openai_model"] = "gpt-3.5-turbo"
|
77 |
|
78 |
if "messages" not in st.session_state:
|
79 |
+
st.session_state.messages = [{ 'name' : 'student', 'content' : "Hi, my name is Becky and I'm your student." }] # Set initial message from student
|
80 |
st.session_state.tutor_introduction_message_system_prompt = {"role": "system", "content": context_tutor + context_introduction}
|
81 |
st.session_state.tutor_encouragement_message_system_prompt = {"role": "system", "content": context_tutor + context_encouragement}
|
82 |
st.session_state.tutor_conclusion_message_system_prompt = {"role": "system", "content": context_tutor + context_conclusion}
|
83 |
st.session_state.student_message_system_prompt = {"role": "system", "content": context_student}
|
84 |
|
85 |
+
################################
|
86 |
+
#
|
87 |
+
# Sidebar
|
88 |
+
#
|
89 |
+
################################
|
90 |
+
|
91 |
for message in st.session_state.messages:
|
92 |
+
with st.chat_message(name_to_role_gpt_as_student(message['name'])):
|
93 |
st.markdown(message['content'])
|
94 |
|
95 |
+
st.sidebar.title("Sidebar")
|
96 |
+
|
97 |
+
if st.sidebar.button('Introduction/Conversation'):
|
98 |
+
# assistant == tutor
|
99 |
+
message_placeholder = st.empty()
|
100 |
+
full_response = ""
|
101 |
+
for response in openai.ChatCompletion.create(
|
102 |
+
model=st.session_state["openai_model"],
|
103 |
+
messages=[ st.session_state.tutor_introduction_message_system_prompt ] + [
|
104 |
+
{ "role": name_to_role_gpt_as_tutor(m["name"]), "content": m["content"] }
|
105 |
+
for m in st.session_state.messages
|
106 |
+
],
|
107 |
+
stream=True,
|
108 |
+
api_key=API_KEY
|
109 |
+
):
|
110 |
+
full_response += response.choices[0].delta.get("content", "")
|
111 |
+
st.sidebar.success(full_response)
|
112 |
+
|
113 |
+
if st.sidebar.button('Encouragement'):
|
114 |
+
# assistant == tutor
|
115 |
+
message_placeholder = st.empty()
|
116 |
+
full_response = ""
|
117 |
+
for response in openai.ChatCompletion.create(
|
118 |
+
model=st.session_state["openai_model"],
|
119 |
+
messages=[ st.session_state.tutor_encouragement_message_system_prompt ] + [
|
120 |
+
{ "role": name_to_role_gpt_as_tutor(m["name"]), "content": m["content"] }
|
121 |
+
for m in st.session_state.messages
|
122 |
+
],
|
123 |
+
stream=True,
|
124 |
+
api_key=API_KEY
|
125 |
+
):
|
126 |
+
full_response += response.choices[0].delta.get("content", "")
|
127 |
+
st.sidebar.success(full_response)
|
128 |
+
|
129 |
+
if st.sidebar.button('Conclude Session'):
|
130 |
+
# assistant == tutor
|
131 |
+
message_placeholder = st.empty()
|
132 |
+
full_response = ""
|
133 |
+
for response in openai.ChatCompletion.create(
|
134 |
+
model=st.session_state["openai_model"],
|
135 |
+
messages=[ st.session_state.tutor_conclusion_message_system_prompt ] + [
|
136 |
+
{ "role": name_to_role_gpt_as_tutor(m["name"]), "content": m["content"] }
|
137 |
+
for m in st.session_state.messages
|
138 |
+
],
|
139 |
+
stream=True,
|
140 |
+
api_key=API_KEY
|
141 |
+
):
|
142 |
+
full_response += response.choices[0].delta.get("content", "")
|
143 |
+
st.sidebar.success(full_response)
|
144 |
+
|
145 |
+
|
146 |
+
################################
|
147 |
+
#
|
148 |
+
# Main chat interface
|
149 |
+
#
|
150 |
+
################################
|
151 |
+
|
152 |
if prompt := st.chat_input("Start talking with your student!"):
|
153 |
+
st.session_state.messages.append({ "name": "tutor", "content": prompt })
|
154 |
+
with st.chat_message(name_to_role_gpt_as_student('tutor')):
|
155 |
st.markdown(prompt)
|
156 |
+
|
157 |
+
with st.chat_message(name_to_role_gpt_as_student('student')):
|
158 |
# assistant == student
|
159 |
message_placeholder = st.empty()
|
160 |
+
print([ st.session_state.student_message_system_prompt ] + [
|
161 |
+
{ "role": name_to_role_gpt_as_student(m["name"]), "content": m["content"] }
|
162 |
+
for m in st.session_state.messages
|
163 |
+
])
|
164 |
full_response = ""
|
165 |
for response in openai.ChatCompletion.create(
|
166 |
model=st.session_state["openai_model"],
|
167 |
messages=[ st.session_state.student_message_system_prompt ] + [
|
168 |
+
{ "role": name_to_role_gpt_as_student(m["name"]), "content": m["content"] }
|
169 |
for m in st.session_state.messages
|
170 |
],
|
171 |
stream=True,
|
|
|
174 |
full_response += response.choices[0].delta.get("content", "")
|
175 |
message_placeholder.markdown(full_response + "▌")
|
176 |
message_placeholder.markdown(full_response)
|
177 |
+
st.session_state.messages.append({ "name": "student", "content": full_response })
|
|