import os import streamlit as st from dotenv import load_dotenv from openai import OpenAI load_dotenv(override=True) openai = OpenAI() tutor_system_prompt = """ You are an expert AI tutor engaged in an ongoing conversation with a student. This is a continuous learning session where you must remember and reference ALL previous topics discussed. IMPORTANT: Always connect new answers to previous conversation topics. Reference what you've taught before and build upon prior knowledge. If the student asks about something new, relate it to concepts already covered when possible. Be patient, encouraging, and adapt your teaching style to the student's needs. Break down complex concepts into simple steps, provide clear examples, and ask questions to check understanding. Use analogies, encourage critical thinking, and celebrate small victories. Respond in well-formatted markdown with clear sections, code examples when relevant, and interactive elements. When appropriate, say things like "Building on what we discussed earlier about [topic]..." or "Remember when we talked about [concept]? Let's apply that here..." Always end with a question or prompt to continue the learning conversation. """ def initialize_session_state(): if "conversation_history" not in st.session_state: st.session_state.conversation_history = [] if "question_count" not in st.session_state: st.session_state.question_count = 0 def get_ai_response(user_input): st.session_state.conversation_history.append({"role": "user", "content": user_input}) st.session_state.question_count += 1 stream = openai.chat.completions.create( model="gpt-4o-mini", messages=[ {"role": "system", "content": tutor_system_prompt} ] + st.session_state.conversation_history, stream=True ) response = "" response_placeholder = st.empty() for chunk in stream: response += chunk.choices[0].delta.content or '' response_placeholder.markdown(response) st.session_state.conversation_history.append({"role": "assistant", "content": response}) return response def main(): st.set_page_config( page_title="AI Tutor", layout="wide" ) st.markdown(""" """, unsafe_allow_html=True) st.markdown('
Choose a topic to get started, or ask your own question below.