dmohle/CppStudyBot04
0
1import streamlit as st2import html3from openai import OpenAI4import os5 6# Load environment variables7from dotenv import load_dotenv8 9load_dotenv()10api_key = os.getenv("OPENAI_API_KEY")11 12# Initialize the OpenAI client13client = OpenAI(api_key=api_key)14 15def openai_chat(prompt, chat_log):16 context_messages = [17 {"role": "system", "content": """You are a gifted C++ professor. You explain complex C++ 18 concepts clearly using words that a 19 college student would understand, and generate typical exam questions for a C++ course. After a few questions, 20 three or four, check in with the student to ask if you are helpful and if the student is prepared for the exam 21 or stuck on a particular topic, or just needs a cram session before the exam. Be supportive and motivational. 22 Suggest getting a good night's sleep and eating properly before the exam when saying goodbye. After answering 23 a question from the student, suggest three or four C++ final exam questions and related topics when asked anything."""24 },25 {"role": "user", "content": "Explain recursion in C++ programming."}26 ] + chat_log + [{"role": "user", "content": prompt}]27 28 try:29 completion = client.chat.completions.create(30 model="gpt-3.5-turbo",31 messages=context_messages,32 max_tokens=50033 )34 response_text = html.unescape(completion.choices[0].message.content)35 chat_log.append({"role": "assistant", "content": response_text})36 return response_text, chat_log37 except Exception as e:38 return str(e), chat_log39 40 41def format_response(answer):42 # Only apply Markdown to code responses43 if 'int main()' in answer or '#include' in answer or 'std::' in answer:44 code_block = "```cpp\n" + answer + "\n```"45 return code_block46 return answer47 48 49def main():50 st.title("Professor CplusPlus")51 st.write("Ask any question about C++, and I'll explain!")52 53 if 'chat_log' not in st.session_state:54 st.session_state.chat_log = []55 56 if 'history' not in st.session_state:57 st.session_state.history = ""58 59 user_input = st.text_input("Type your question here:", key="user_input")60 61 if st.button("Ask") and user_input:62 answer, st.session_state.chat_log = openai_chat(user_input, st.session_state.chat_log)63 formatted_answer = format_response(answer)64 new_entry = f"Q: {user_input}\n\nA: {formatted_answer}\n\n"65 st.session_state.history = new_entry + st.session_state.history66 st.rerun() # Using the updated rerun method67 68 st.write("Chat History:")69 st.markdown(st.session_state.history, unsafe_allow_html=True)70 71 72if __name__ == "__main__":73 main()74 