Merge pull request #745 from iamumarjaved/W1D5

minimalistic AI tutor
This commit is contained in:
Ed Donner
2025-10-19 11:01:08 -04:00
committed by GitHub
3 changed files with 333 additions and 0 deletions

View File

@@ -0,0 +1,147 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 2,
"id": "tutor_cell",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Welcome to your AI Tutor! I'm here to help you learn step by step.\n",
"I remember everything we discuss, so we can build upon previous topics.\n",
"Type 'quit' to exit anytime.\n",
"\n",
"\n",
"Thinking about: 'AI'\n",
"\n"
]
},
{
"data": {
"text/markdown": [
"It looks like you're interested in the topic of AI! Building on what we discussed previously, artificial intelligence (AI) encompasses a variety of technologies and concepts. We might have talked about its applications, like machine learning, natural language processing, or robotics.\n",
"\n",
"### Let's Explore AI Further\n",
"Here are some key concepts we can dive into:\n",
"\n",
"1. **Machine Learning**: This is a subset of AI focused on algorithms that allow computers to learn from data and improve from experience without being explicitly programmed.\n",
"\n",
"2. **Natural Language Processing (NLP)**: This area enables computers to understand, interpret, and generate human language. You've probably seen this in applications like chatbots or translation services.\n",
"\n",
"3. **Robotics**: AI plays a significant role in how robots make decisions and navigate their environments.\n",
"\n",
"#### Questions to Consider:\n",
"- Are you interested in a specific area of AI, like machine learning or NLP?\n",
"- Do you want to learn about how AI is applied in real-world scenarios like healthcare or finance?\n",
"\n",
"Let me know which direction youd like to take, or if theres a specific question on AI you have in mind!"
],
"text/plain": [
"<IPython.core.display.Markdown object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"Thanks for learning with me! Goodbye!\n"
]
}
],
"source": [
"import os\n",
"import json\n",
"from dotenv import load_dotenv\n",
"from IPython.display import Markdown, display, update_display\n",
"from openai import OpenAI\n",
"\n",
"load_dotenv(override=True)\n",
"openai = OpenAI()\n",
"\n",
"tutor_system_prompt = \"\"\"\n",
"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.\n",
"\n",
"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.\n",
"\n",
"Be patient, encouraging, and adapt your teaching style to the student's needs.\n",
"Break down complex concepts into simple steps, provide clear examples, and ask questions to check understanding.\n",
"Use analogies, encourage critical thinking, and celebrate small victories.\n",
"Respond in well-formatted markdown with clear sections, code examples when relevant, and interactive elements.\n",
"\n",
"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...\"\n",
"Always end with a question or prompt to continue the learning conversation.\n",
"\"\"\"\n",
"\n",
"conversation_history = []\n",
"question_count = 0\n",
"\n",
"print(\"Welcome to your AI Tutor! I'm here to help you learn step by step.\")\n",
"print(\"I remember everything we discuss, so we can build upon previous topics.\")\n",
"print(\"Type 'quit' to exit anytime.\\n\")\n",
"\n",
"while True:\n",
" if question_count == 0:\n",
" user_input = input(\"What would you like to learn about today? \")\n",
" else:\n",
" user_input = input(f\"\\nQuestion {question_count + 1}: What's your next question? \")\n",
" \n",
" if user_input.lower() in ['quit', 'exit', 'bye']:\n",
" print(\"\\nThanks for learning with me! Goodbye!\")\n",
" break\n",
" \n",
" if not user_input.strip():\n",
" print(\"Please ask me a question!\")\n",
" continue\n",
" \n",
" question_count += 1\n",
" conversation_history.append({\"role\": \"user\", \"content\": user_input})\n",
" \n",
" print(f\"\\nThinking about: '{user_input}'\\n\")\n",
" \n",
" stream = openai.chat.completions.create(\n",
" model=\"gpt-4o-mini\",\n",
" messages=[\n",
" {\"role\": \"system\", \"content\": tutor_system_prompt}\n",
" ] + conversation_history,\n",
" stream=True\n",
" )\n",
" \n",
" response = \"\"\n",
" display_handle = display(Markdown(\"\"), display_id=True)\n",
" for chunk in stream:\n",
" response += chunk.choices[0].delta.content or ''\n",
" update_display(Markdown(response), display_id=display_handle.display_id)\n",
" \n",
" conversation_history.append({\"role\": \"assistant\", \"content\": response})"
]
}
],
"metadata": {
"kernelspec": {
"display_name": ".venv",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.12"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

View File

@@ -0,0 +1,178 @@
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("""
<style>
.main-header {
text-align: center;
font-size: 2.5em;
font-weight: 600;
color: #1f2937;
margin-bottom: 1em;
}
.stButton > button {
background: white !important;
border: 2px solid #e5e7eb !important;
border-radius: 12px !important;
padding: 1.5em 1em !important;
margin: 0.5em 0 !important;
text-align: center !important;
transition: all 0.3s ease !important;
font-weight: 500 !important;
color: #374151 !important;
font-size: 1em !important;
min-height: 60px !important;
white-space: normal !important;
word-wrap: break-word !important;
line-height: 1.4 !important;
}
.stButton > button:hover {
border-color: #3b82f6 !important;
background: #f0f9ff !important;
color: #1d4ed8 !important;
transform: translateY(-2px) !important;
box-shadow: 0 4px 12px rgba(59, 130, 246, 0.15) !important;
}
.stButton > button:active {
transform: translateY(0) !important;
box-shadow: 0 2px 4px rgba(59, 130, 246, 0.1) !important;
}
.sidebar-clear-button {
text-align: center;
margin-top: 2em;
}
.attribution {
position: fixed;
bottom: 10px;
left: 10px;
font-size: 0.8em;
color: #6b7280;
font-style: italic;
}
</style>
""", unsafe_allow_html=True)
st.markdown('<h1 class="main-header">AI Tutor</h1>', unsafe_allow_html=True)
initialize_session_state()
with st.sidebar:
st.header("About")
st.markdown("An AI tutor that remembers your conversation and builds upon previous knowledge.")
st.markdown('<div class="sidebar-clear-button">', unsafe_allow_html=True)
if st.button("Clear Conversation", use_container_width=True):
st.session_state.conversation_history = []
st.session_state.question_count = 0
st.rerun()
st.markdown('</div>', unsafe_allow_html=True)
st.markdown('<div class="attribution">By Umar Javed</div>', unsafe_allow_html=True)
if not st.session_state.conversation_history:
st.markdown("""
<div class="welcome-section">
<h3 style="text-align: center; color: #1f2937; margin-bottom: 0.5em;">Welcome to AI Tutor</h3>
<p style="text-align: center; color: #6b7280; margin-bottom: 2em;">Choose a topic to get started, or ask your own question below.</p>
</div>
""", unsafe_allow_html=True)
col1, col2, col3 = st.columns(3, gap="medium")
with col1:
if st.button("Explain Machine Learning", key="ml", use_container_width=True, type="secondary"):
st.session_state.suggested_question = "Explain machine learning basics"
with col2:
if st.button("Teach Python Programming", key="python", use_container_width=True, type="secondary"):
st.session_state.suggested_question = "Teach me Python programming fundamentals"
with col3:
if st.button("Data Structures & Algorithms", key="dsa", use_container_width=True, type="secondary"):
st.session_state.suggested_question = "Explain data structures and algorithms"
for message in st.session_state.conversation_history:
if message["role"] == "user":
with st.chat_message("user"):
st.markdown(message['content'])
else:
with st.chat_message("assistant"):
st.markdown(message['content'])
user_input = st.chat_input("What would you like to learn about?")
suggested_input = getattr(st.session_state, 'suggested_question', None)
if suggested_input:
user_input = suggested_input
del st.session_state.suggested_question
if user_input:
with st.chat_message("user"):
st.markdown(user_input)
with st.chat_message("assistant"):
get_ai_response(user_input)
st.empty()
if __name__ == "__main__":
main()

View File

@@ -0,0 +1,8 @@
#!/bin/bash
echo "Starting AI Tutor..."
echo "Use the url http://localhost:8501 to access the app"
echo ""
cd "$(dirname "$0")"
streamlit run day5_task.py