AI in Education
Intelligent Tutoring System
import openai
from typing import List, Dict
from dataclasses import dataclass
@dataclass
class StudentProfile:
student_id: str
knowledge_level: Dict[str, float]
learning_style: str
performance_history: List[Dict]
class IntelligentTutor:
def __init__(self, api_key: str):
self.client = openai.OpenAI(api_key=api_key)
self.student_profiles: Dict[str, StudentProfile] = {}
def assess_knowledge(self, student_id: str, responses: List[Dict]) -> Dict:
correct = sum(1 for r in responses if r.get("correct", False))
total = len(responses)
return {
"score": correct / total if total > 0 else 0,
"topics_mastered": [r["topic"] for r in responses if r.get("correct")],
"topics_struggling": [r["topic"] for r in responses if not r.get("correct")]
}
def generate_hint(self, question: str, student_level: float) -> str:
difficulty = "beginner" if student_level < 0.5 else "advanced"
response = self.client.chat.completions.create(
model="gpt-4",
messages=[
{"role": "system", "content": f"""You are a Socratic tutor for {difficulty} students.
Give hints without revealing the answer. Guide thinking process."""},
{"role": "user", "content": f"Question: {question}"}
],
temperature=0.7
)
return response.choices[0].message.content
def explain_concept(self, concept: str, learning_style: str) -> str:
style_instructions = {
"visual": "Use diagrams, charts, and visual metaphors",
"auditory": "Use analogies and verbal explanations",
"kinesthetic": "Use hands-on examples and activities"
}
instruction = style_instructions.get(learning_style, "Use clear explanations")
response = self.client.chat.completions.create(
model="gpt-4",
messages=[
{"role": "system", "content": f"""Explain educational concepts clearly.
{instruction}. Be engaging and age-appropriate."""},
{"role": "user", "content": f"Explain: {concept}"}
],
temperature=0.7
)
return response.choices[0].message.content
def create_quiz(self, topic: str, difficulty: int, n_questions: int) -> List[Dict]:
response = self.client.chat.completions.create(
model="gpt-4",
messages=[
{"role": "system", "content": """Create educational quiz questions.
Return JSON array with question, options, correct_answer, explanation."""},
{"role": "user", "content": f"""Topic: {topic}
Difficulty: {difficulty}/5
Number of questions: {n_questions}"""}
],
temperature=0.8,
response_format={"type": "json_object"}
)
import json
return json.loads(response.choices[0].message.content)["questions"]
tutor = IntelligentTutor(api_key="your-api-key")
hint = tutor.generate_hint("Solve 2x + 5 = 15", student_level=0.3)
explanation = tutor.explain_concept("Photosynthesis", learning_style="visual")
quiz = tutor.create_quiz("Algebra", difficulty=3, n_questions=5)
Content Generation
class ContentGenerator:
def __init__(self, api_key: str):
self.client = openai.OpenAI(api_key=api_key)
def generate_lesson_plan(self, subject: str, grade_level: int, duration: int) -> Dict:
response = self.client.chat.completions.create(
model="gpt-4",
messages=[
{"role": "system", "content": """Create detailed lesson plans with objectives,
activities, materials, and assessments."""},
{"role": "user", "content": f"""Subject: {subject}
Grade Level: {grade_level}
Duration: {duration} minutes"""}
],
temperature=0.7,
response_format={"type": "json_object"}
)
import json
return json.loads(response.choices[0].message.content)
def adapt_content(self, content: str, target_audience: str) -> str:
response = self.client.chat.completions.create(
model="gpt-4",
messages=[
{"role": "system", "content": f"""Adapt educational content for {target_audience}.
Maintain accuracy while adjusting complexity and examples."""},
{"role": "user", "content": content}
],
temperature=0.5
)
return response.choices[0].message.content
def generate_feedback(self, student_work: str, rubric: Dict) -> Dict:
response = self.client.chat.completions.create(
model="gpt-4",
messages=[
{"role": "system", "content": """Provide constructive feedback on student work.
Be specific, encouraging, and actionable."""},
{"role": "user", "content": f"""Student Work:
{student_work}
Rubric:
{rubric}"""}
],
temperature=0.3
)
return {"feedback": response.choices[0].message.content}
generator = ContentGenerator(api_key="your-api-key")
lesson = generator.generate_lesson_plan("Mathematics", grade_level=8, duration=45)
feedback = generator.generate_feedback(student_essay, rubric)
Knowledge Tracing
import torch
import torch.nn as nn
class KnowledgeTracer(nn.Module):
def __init__(self, n_skills, hidden_dim=128):
super().__init__()
self.skill_embedding = nn.Embedding(n_skills, hidden_dim)
self.lstm = nn.LSTM(hidden_dim * 2, hidden_dim, batch_first=True)
self.output = nn.Linear(hidden_dim, 1)
def forward(self, skill_ids, responses):
skill_emb = self.skill_embedding(skill_ids)
combined = torch.cat([skill_emb, responses.unsqueeze(-1).float()], dim=-1)
lstm_out, _ = self.lstm(combined)
prediction = torch.sigmoid(self.output(lstm_out[:, -1, :]))
return prediction
kt_model = KnowledgeTracer(n_skills=100)
Best Practices
- Ensure student data privacy (FERPA compliance)
- Provide teacher oversight and control
- Balance AI assistance with learning
- Use adaptive difficulty appropriately
- Monitor engagement and motivation
- Include accessibility features