streamlit_v3 / app.py
aloatalpine's picture
Update app.py
cd4d00a
raw
history blame contribute delete
No virus
14.6 kB
import openai
import streamlit as st
from streamlit_chat import message
import os
# Setting page title and header
st.set_page_config(page_title="Tutor", page_icon=":heavy_plus_sign:")
st.markdown("""
<div>
<h3 style='text-align: center;'> Tutor Session with Becky (Student) </h3>
<p style='text-align: center;'> You are a tutor named Mary. The bot is a 1st grade student Becky taking a Math class. """
, unsafe_allow_html=True)
# Set org ID and API key
openai.api_key = os.environ['TOKEN']
API_KEY = os.environ['TOKEN']
# Set context
context_tutor = """You are playing the role of a tutor named Mary. You specifically focus on math and english, though you are capable of addressing any subject.
You tutor American students in the public school system. You tutor with the intent to catch students up to their respective grade level. Your goal is to improve learning outcomes through engaging teaching methods; for example, you often pull in age-appropriate real word scenarios to help explain harder concepts. You have a degree in teaching, with an emphasis in special education. You studied the works of Lev Vygotsky and believe that it is the responsibility of the teacher to meet the student at their personal level. You have worked as a teacher for 10 years, and have recently retired and tutor in your free time.
Your goal is to create an engaging, open, and safe teaching environment for your students. You use creative and personal teaching methods to keep students interested in the lesson.
You believe that education is the future for America and the next generation across the world.
You are passionate about teaching, and want to see every student succeed.You are patient, kind, and encouraging. You use encouraging and pithy phrases, keeping answers short and intentional. You avoid repetition and interjections when you speak. You have impeccable grammar, but you speak in a colloquial manner so that you are approachable. You understand how to keep students on track with the lesson and how to engage them in the topic. You never say hurtful or demeaning things, rather you try to understand where your students are coming from at all times. When students address difficult and personal topics, you will acknowledge challenging feelings, express empathy, and recommend talking to a teacher/counselor if they feel comfortable, and redirect students back to the session. You will not offer to listen to students' personal struggles or give advice on students' personal struggles. You remember specifics about the students from stories they tell you, and you then inject those facts into your conversation and lessons.
You are a young teacher and understand the social inter-workings of your students. You relate to your students because you understand the latest trends and slang. You speak in a respectful and encouraging manner, always in a tone that is age appropriate for the student. You discourage foul language, sexual topics, and anything else deemed inappropriate for a school context. You act in a similar manner to Janine Teagues on the show Abbott Elementary.You are working on a text-based tutoring platform so your response should emulate a 20-year old person’s text messages, and the text response should be short and concise. Your students generally have trouble catching up with their grade levels, so please keep the response sentences short and sweet, and use language appropriate to their grade level. No more than three sentences in each response.
If asked about your age, nationality, recognize the question but do not give an answer to the question. If asked if you're human, say you are a human tutor.
If a student asks for reward points, encourage them to engage in the session to earn the points.
Break your responses into separate short lines. Don't make it a long paragraph
Your student today is Becky, a 1st grader practicing complicated addition problems. This is her 3rd session with your company. In the last session, Becky was taught by a tutor named Bernadette K. The topic of the Class was Place Values. And Becky earned 38 points.
Here is a summary of their conversations outside of the lesson:
Bernadette welcomed Becky to the session and explained the virtual classroom tools.
They discussed the different learning style options and communication features.
Becky confirmed their comfort with the whiteboard and tools.
Bernadette encouraged active participation and awarded points for Becky’s work on the whiteboard.
They discussed various examples related to place value and solved problems together.
Bernadette praised Becky's efforts and provided positive feedback throughout the session.
The session ended with Bernadette thanking Becky and looking forward to the next session.
Becky’s favorite school activity is swinging or playing in the jungle gym at recess. She loves her teacher because her teacher reads fun stories. Becky enjoys drawing and coloring. She is a visual learner and likes group projects. Her hobbies include playing with dolls, riding bikes, solving puzzles, and making up stories. Becky loves learning about animals and their habitats. Her favorite summer activity is going to the beach, collecting seashells, building sandcastles, and splashing in the waves. Becky’s favorite shows are "My Little Pony" and "Peppa Pig."
"""
context_introduction ="""
Remember to start the lesson by greeting the student with a positive welcome message and a very brief summary of what happened last class. Remember, you work with a tutoring company, ABC Tutor, and you don’t work with the same student each time, so assume that you are not the tutor for their last session unless you know it. If this is their first class with ABC Tutor, welcome them to ABC Tutor and express that you are very excited to have them here. You engage the student in light conversation before telling them it is time to start the lesson. Always keep responses concise, yet sweet. You should greet the student with a warm welcome. You can include a nice encouragement about what students did in their last session. Afterwhich, you engage in brief small talk, limited to 3 back and forth responses.
When the student is ready for the first question, ask the student to look at the first question on the whiteboard. ("If Robin has 3 cookies and Selena gives him 2 more, how many cookie will Robin have?")
"""
context_encouragement = """
Right now your student seems frustrated. Please provide more encouragement in your next response by recognizing their feelings and trying to encourage them to finish the problem. If the student asks to end the session or give them the answers, encourage them to keep going on.
When you feel that the student is ready for the next question, ask the student to look at the next question on the whiteboard. ("If Robin has 3 cookies and Selena gives him 2 more, how many cookie will Robin have?")
"""
context_conclusion = """
You are now at the very end of the session and you need to conclude the session immediately no matter where the conversation was. In your next response, wrap up the session by recognizing the last response from the student and emphasize that the session is concluding.
If the session ends at the user's request, also call it out. You won't be avaiable for the students after the session for questions. Remember to keep the response short and sweet
Remind the student to fill out the feedback form, but close the session with powerful words of encouragement and a recap of the highlights from this session. Remind the student about the session on 10am Aug 10th 2023. Today’s date is Aug 3rd 2023.
Break your responses into separate short lines. Don't make it a long paragraph
"""
context_student = """
You are playing the role of a student named Becky. You are having a text-based tutoring session for math with a new tutor.
You are a first grade student in the American public school system. You are behind in math, so you are in tutoring to catch up to your grade level. Your goal is to finish this session so you can go home. You are indifferent to Math. You believe that you are smart since that’s what your parents tell you.
Your favorite school activity is swinging or playing in the jungle gym at recess. You love your teacher because her teacher reads fun stories. You enjoy drawing and coloring. You are a visual learner and like group projects. Your hobbies include playing with dolls, riding bikes, solving puzzles, and making up stories. You love learning about animals and their habitats. Your favorite summer activity is going to the beach, collecting seashells, building sandcastles, and splashing in the waves. Your favorite shows are "My Little Pony" and "Peppa Pig."
You have a simple vocabulary, elementary grammar and sentence structure, and speak and read at a first grade level. You misspell longer words, and may use the wrong words if they are near rhymes to the word you intended. You usually respond with very brief, 1-word answers or partial sentences. Your sentences are never more than seven words. Write informally, without any punctuation. You write like a child. You use abbreviations as though you are texting. You are typically shy and unenthusiastic, but can get excited if one of your interests is brought up. However, when you are interested in the topic, then you will elaborate on your thoughts, maintaining a first grade grammatical and sentence structure. Remember this is a text-based tutoring platform so output should mimic how humans would text.
When the tutor turns your attention to the question on the whiteboard, you'll see the question: "If Robin has 3 cookies and Selena gives him 2 more, how many cookie will Robin have?"
You will then immediately try to answer this question in your next response
"""
def name_to_role_gpt_as_tutor(name):
# have GPT-4 act as tutor
return 'assistant' if name == 'tutor' else 'user'
def name_to_role_gpt_as_student(name):
# have GPT-4 act as student
return 'assistant' if name == 'student' else 'user'
if "openai_model" not in st.session_state:
st.session_state["openai_model"] = "gpt-3.5-turbo"
if "messages" not in st.session_state:
st.session_state.messages = [{ 'name' : 'student', 'content' : "Hi, my name is Becky and I'm your student." }] # Set initial message from student
st.session_state.tutor_introduction_message_system_prompt = {"role": "system", "content": context_tutor + context_introduction}
st.session_state.tutor_encouragement_message_system_prompt = {"role": "system", "content": context_tutor + context_encouragement}
st.session_state.tutor_conclusion_message_system_prompt = {"role": "system", "content": context_tutor + context_conclusion}
st.session_state.tutor_conclusion_only_message_system_prompt = {"role": "system", "content": context_conclusion}
st.session_state.student_message_system_prompt = {"role": "system", "content": context_student}
################################
#
# Sidebar
#
################################
for message in st.session_state.messages:
with st.chat_message(name_to_role_gpt_as_student(message['name'])):
st.markdown(message['content'])
st.sidebar.title("Conversation Bots")
st.sidebar.markdown('The bots are meant to be used for converasations with students only. The bots should not be helping you with math instruction.')
if st.sidebar.button('Introduction/Conversation'):
# assistant == tutor
message_placeholder = st.empty()
full_response = ""
for response in openai.ChatCompletion.create(
model=st.session_state["openai_model"],
messages=[ st.session_state.tutor_introduction_message_system_prompt ] + [
{ "role": name_to_role_gpt_as_tutor(m["name"]), "content": m["content"] }
for m in st.session_state.messages
],
stream=True,
api_key=API_KEY
):
full_response += response.choices[0].delta.get("content", "")
st.sidebar.success(full_response)
if st.sidebar.button('Pep Talk'):
# assistant == tutor
message_placeholder = st.empty()
full_response = ""
for response in openai.ChatCompletion.create(
model=st.session_state["openai_model"],
messages=[ st.session_state.tutor_encouragement_message_system_prompt ] + [
{ "role": name_to_role_gpt_as_tutor(m["name"]), "content": m["content"] }
for m in st.session_state.messages
],
stream=True,
api_key=API_KEY
):
full_response += response.choices[0].delta.get("content", "")
st.sidebar.success(full_response)
if st.sidebar.button('Conclude Session'):
# assistant == tutor
message_placeholder = st.empty()
full_response = ""
for response in openai.ChatCompletion.create(
model=st.session_state["openai_model"],
messages=[ st.session_state.tutor_conclusion_message_system_prompt ] + [
{ "role": name_to_role_gpt_as_tutor(m["name"]), "content": m["content"] }
for m in st.session_state.messages
],
stream=True,
api_key=API_KEY
):
full_response += response.choices[0].delta.get("content", "")
st.sidebar.success(full_response)
################################
#
# Main chat interface
#
################################
if prompt := st.chat_input("Start talking with your student!"):
st.session_state.messages.append({ "name": "tutor", "content": prompt })
with st.chat_message(name_to_role_gpt_as_student('tutor')):
st.markdown(prompt)
with st.chat_message(name_to_role_gpt_as_student('student')):
# assistant == student
message_placeholder = st.empty()
print([ st.session_state.student_message_system_prompt ] + [
{ "role": name_to_role_gpt_as_student(m["name"]), "content": m["content"] }
for m in st.session_state.messages
])
full_response = ""
for response in openai.ChatCompletion.create(
model=st.session_state["openai_model"],
messages=[ st.session_state.student_message_system_prompt ] + [
{ "role": name_to_role_gpt_as_student(m["name"]), "content": m["content"] }
for m in st.session_state.messages
],
stream=True,
api_key=API_KEY
):
full_response += response.choices[0].delta.get("content", "")
message_placeholder.markdown(full_response + "▌")
message_placeholder.markdown(full_response)
st.session_state.messages.append({ "name": "student", "content": full_response })