Spaces:
Sleeping
Sleeping
File size: 54,898 Bytes
1af10cc 250bf8c 1af10cc a806ca2 2dc3c19 250bf8c 1af10cc 250bf8c 1af10cc a806ca2 1af10cc 250bf8c 1af10cc 2dc3c19 250bf8c 1af10cc a806ca2 1af10cc a806ca2 1af10cc a806ca2 2dc3c19 a806ca2 250bf8c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 |
"""
Learning path generation tools for TutorX with adaptive learning capabilities.
"""
import random
from typing import Dict, Any, List, Optional
from datetime import datetime, timedelta
import sys
import os
from pathlib import Path
import json
import re
from dataclasses import dataclass, asdict
from enum import Enum
# Add the parent directory to the Python path
current_dir = Path(__file__).parent
parent_dir = current_dir.parent.parent
sys.path.insert(0, str(parent_dir))
# Import from local resources
try:
from resources.concept_graph import CONCEPT_GRAPH
except ImportError:
# Fallback for when running from different contexts
CONCEPT_GRAPH = {
"algebra_basics": {"id": "algebra_basics", "name": "Algebra Basics", "description": "Basic algebraic concepts"},
"linear_equations": {"id": "linear_equations", "name": "Linear Equations", "description": "Solving linear equations"},
"quadratic_equations": {"id": "quadratic_equations", "name": "Quadratic Equations", "description": "Solving quadratic equations"},
"algebra_linear_equations": {"id": "algebra_linear_equations", "name": "Linear Equations", "description": "Linear equation solving"}
}
# Import MCP
from mcp_server.mcp_instance import mcp
from mcp_server.model.gemini_flash import GeminiFlash
MODEL = GeminiFlash()
# Adaptive Learning Data Structures
class DifficultyLevel(Enum):
VERY_EASY = 0.2
EASY = 0.4
MEDIUM = 0.6
HARD = 0.8
VERY_HARD = 1.0
@dataclass
class StudentPerformance:
student_id: str
concept_id: str
accuracy_rate: float = 0.0
time_spent_minutes: float = 0.0
attempts_count: int = 0
mastery_level: float = 0.0
last_accessed: datetime = None
difficulty_preference: float = 0.5
@dataclass
class LearningEvent:
student_id: str
concept_id: str
event_type: str # 'answer_correct', 'answer_incorrect', 'hint_used', 'time_spent'
timestamp: datetime
data: Dict[str, Any]
# In-memory storage for adaptive learning
student_performances: Dict[str, Dict[str, StudentPerformance]] = {}
learning_events: List[LearningEvent] = []
active_sessions: Dict[str, Dict[str, Any]] = {}
def get_prerequisites(concept_id: str, visited: Optional[set] = None) -> List[Dict[str, Any]]:
"""
Get all prerequisites for a concept recursively.
Args:
concept_id: ID of the concept to get prerequisites for
visited: Set of already visited concepts to avoid cycles
Returns:
List of prerequisite concepts in order
"""
if visited is None:
visited = set()
if concept_id not in CONCEPT_GRAPH or concept_id in visited:
return []
visited.add(concept_id)
prerequisites = []
# Get direct prerequisites
for prereq_id in CONCEPT_GRAPH[concept_id].get("prerequisites", []):
if prereq_id in CONCEPT_GRAPH and prereq_id not in visited:
prerequisites.extend(get_prerequisites(prereq_id, visited))
# Add the current concept
prerequisites.append(CONCEPT_GRAPH[concept_id])
return prerequisites
def generate_learning_path(concept_ids: List[str], student_level: str = "beginner") -> Dict[str, Any]:
"""
Generate a personalized learning path for a student.
Args:
concept_ids: List of concept IDs to include in the learning path
student_level: Student's current level (beginner, intermediate, advanced)
Returns:
Dictionary containing the learning path
"""
if not concept_ids:
return {"error": "At least one concept ID is required"}
# Get all prerequisites for each concept
all_prerequisites = []
visited = set()
for concept_id in concept_ids:
if concept_id in CONCEPT_GRAPH:
prereqs = get_prerequisites(concept_id, visited)
all_prerequisites.extend(prereqs)
# Remove duplicates while preserving order
unique_concepts = []
seen = set()
for concept in all_prerequisites:
if concept["id"] not in seen:
seen.add(concept["id"])
unique_concepts.append(concept)
# Add any target concepts not already in the path
for concept_id in concept_ids:
if concept_id in CONCEPT_GRAPH and concept_id not in seen:
unique_concepts.append(CONCEPT_GRAPH[concept_id])
# Estimate time required for each concept based on student level
time_estimates = {
"beginner": {"min": 30, "max": 60}, # 30-60 minutes per concept
"intermediate": {"min": 20, "max": 45}, # 20-45 minutes per concept
"advanced": {"min": 15, "max": 30} # 15-30 minutes per concept
}
level = student_level.lower()
if level not in time_estimates:
level = "beginner"
time_min = time_estimates[level]["min"]
time_max = time_estimates[level]["max"]
# Generate learning path with estimated times
learning_path = []
total_minutes = 0
for i, concept in enumerate(unique_concepts, 1):
# Random time estimate within range
minutes = random.randint(time_min, time_max)
total_minutes += minutes
learning_path.append({
"step": i,
"concept_id": concept["id"],
"concept_name": concept["name"],
"description": concept.get("description", ""),
"estimated_time_minutes": minutes,
"resources": [
f"Video tutorial on {concept['name']}",
f"{concept['name']} documentation",
f"Practice exercises for {concept['name']}"
]
})
# Calculate total time
hours, minutes = divmod(total_minutes, 60)
total_time = f"{hours}h {minutes}m" if hours > 0 else f"{minutes}m"
return {
"learning_path": learning_path,
"total_steps": len(learning_path),
"total_time_minutes": total_minutes,
"total_time_display": total_time,
"student_level": student_level,
"generated_at": datetime.utcnow().isoformat() + "Z"
}
def clean_json_trailing_commas(json_text: str) -> str:
return re.sub(r',([ \t\r\n]*[}}\]])', r'\1', json_text)
def extract_json_from_text(text: str):
if not text or not isinstance(text, str):
return None
# Remove code fences
text = re.sub(r'^\s*```(?:json)?\s*', '', text, flags=re.IGNORECASE)
text = re.sub(r'\s*```\s*$', '', text, flags=re.IGNORECASE)
text = text.strip()
# Remove trailing commas
cleaned = clean_json_trailing_commas(text)
return json.loads(cleaned)
# Adaptive Learning Helper Functions
def get_student_performance(student_id: str, concept_id: str) -> StudentPerformance:
"""Get or create student performance record."""
if student_id not in student_performances:
student_performances[student_id] = {}
if concept_id not in student_performances[student_id]:
student_performances[student_id][concept_id] = StudentPerformance(
student_id=student_id,
concept_id=concept_id,
last_accessed=datetime.utcnow()
)
return student_performances[student_id][concept_id]
def update_mastery_level(performance: StudentPerformance) -> float:
"""Calculate mastery level based on performance metrics."""
if performance.attempts_count == 0:
return 0.0
# Weighted calculation: accuracy (60%), consistency (20%), efficiency (20%)
accuracy_score = performance.accuracy_rate
# Consistency: higher attempts with stable accuracy indicate consistency
consistency_score = min(1.0, performance.attempts_count / 5.0) if performance.accuracy_rate > 0.7 else 0.5
# Efficiency: less time per attempt indicates better understanding
avg_time = performance.time_spent_minutes / performance.attempts_count if performance.attempts_count > 0 else 30
efficiency_score = max(0.1, 1.0 - (avg_time - 10) / 50) # Normalize around 10-60 minutes
mastery = (accuracy_score * 0.6) + (consistency_score * 0.2) + (efficiency_score * 0.2)
performance.mastery_level = min(1.0, max(0.0, mastery))
return performance.mastery_level
def adapt_difficulty(performance: StudentPerformance) -> float:
"""Adapt difficulty based on student performance."""
if performance.attempts_count < 2:
return performance.difficulty_preference
# If accuracy is high, increase difficulty
if performance.accuracy_rate > 0.8:
new_difficulty = min(1.0, performance.difficulty_preference + 0.1)
# If accuracy is low, decrease difficulty
elif performance.accuracy_rate < 0.5:
new_difficulty = max(0.2, performance.difficulty_preference - 0.1)
else:
new_difficulty = performance.difficulty_preference
performance.difficulty_preference = new_difficulty
return new_difficulty
# Enhanced Adaptive Learning with Gemini Integration
@mcp.tool()
async def generate_adaptive_content(student_id: str, concept_id: str, content_type: str = "explanation",
difficulty_level: float = 0.5, learning_style: str = "visual") -> dict:
"""
Generate personalized learning content using Gemini based on student profile and performance.
Args:
student_id: Student identifier
concept_id: Concept to generate content for
content_type: Type of content ('explanation', 'example', 'practice', 'summary')
difficulty_level: Difficulty level (0.0 to 1.0)
learning_style: Preferred learning style ('visual', 'auditory', 'kinesthetic', 'reading')
Returns:
Personalized learning content
"""
try:
# Get student performance data
performance = get_student_performance(student_id, concept_id)
concept_data = CONCEPT_GRAPH.get(concept_id, {"name": concept_id, "description": ""})
# Build context for Gemini
context = f"""
Student Profile:
- Student ID: {student_id}
- Current mastery level: {performance.mastery_level:.2f}
- Accuracy rate: {performance.accuracy_rate:.2f}
- Attempts made: {performance.attempts_count}
- Preferred difficulty: {difficulty_level}
- Learning style: {learning_style}
Concept Information:
- Concept: {concept_data.get('name', concept_id)}
- Description: {concept_data.get('description', '')}
Content Requirements:
- Content type: {content_type}
- Target difficulty: {difficulty_level}
- Learning style: {learning_style}
"""
if content_type == "explanation":
prompt = f"""{context}
Generate a personalized explanation of {concept_data.get('name', concept_id)} that:
1. Matches the student's current understanding level (mastery: {performance.mastery_level:.2f})
2. Uses {learning_style} learning approaches
3. Is appropriate for difficulty level {difficulty_level}
4. Builds on their {performance.attempts_count} previous attempts
Return a JSON object with:
- "explanation": detailed explanation text
- "key_points": list of 3-5 key concepts
- "analogies": 2-3 relevant analogies or examples
- "difficulty_indicators": what makes this concept challenging
- "next_steps": suggested follow-up activities
"""
elif content_type == "practice":
prompt = f"""{context}
Generate personalized practice problems for {concept_data.get('name', concept_id)} that:
1. Match difficulty level {difficulty_level}
2. Consider their accuracy rate of {performance.accuracy_rate:.2f}
3. Use {learning_style} presentation style
4. Provide appropriate scaffolding
Return a JSON object with:
- "problems": list of 3-5 practice problems
- "hints": helpful hints for each problem
- "solutions": step-by-step solutions
- "difficulty_progression": how problems increase in complexity
- "success_criteria": what indicates mastery
"""
elif content_type == "feedback":
prompt = f"""{context}
Generate personalized feedback for the student's performance on {concept_data.get('name', concept_id)}:
1. Acknowledge their current progress (mastery: {performance.mastery_level:.2f})
2. Address their accuracy rate of {performance.accuracy_rate:.2f}
3. Provide encouraging and constructive guidance
4. Suggest specific improvement strategies
Return a JSON object with:
- "encouragement": positive reinforcement message
- "areas_of_strength": what they're doing well
- "improvement_areas": specific areas to focus on
- "strategies": concrete learning strategies
- "motivation": motivational message tailored to their progress
"""
else: # summary or other types
prompt = f"""{context}
Generate a personalized summary of {concept_data.get('name', concept_id)} that:
1. Reinforces key concepts at their mastery level
2. Uses {learning_style} presentation
3. Connects to their learning journey
Return a JSON object with:
- "summary": concise concept summary
- "key_takeaways": main points to remember
- "connections": how this relates to other concepts
- "review_schedule": when to review this concept
"""
# Generate content using Gemini
response = await MODEL.generate_text(prompt, temperature=0.7)
try:
content_data = extract_json_from_text(response)
content_data.update({
"success": True,
"student_id": student_id,
"concept_id": concept_id,
"content_type": content_type,
"difficulty_level": difficulty_level,
"learning_style": learning_style,
"generated_at": datetime.utcnow().isoformat(),
"personalization_factors": {
"mastery_level": performance.mastery_level,
"accuracy_rate": performance.accuracy_rate,
"attempts_count": performance.attempts_count
}
})
return content_data
except Exception as e:
return {
"success": False,
"error": f"Failed to parse Gemini response: {str(e)}",
"raw_response": response
}
except Exception as e:
return {"success": False, "error": str(e)}
@mcp.tool()
async def analyze_learning_patterns(student_id: str, analysis_days: int = 30) -> dict:
"""
Use Gemini to analyze student learning patterns and provide insights.
Args:
student_id: Student identifier
analysis_days: Number of days to analyze
Returns:
AI-powered learning pattern analysis
"""
try:
# Gather student data
if student_id not in student_performances:
return {
"success": True,
"student_id": student_id,
"message": "No learning data available for analysis",
"recommendations": ["Start learning to build your profile!"]
}
student_data = student_performances[student_id]
# Get recent events
cutoff_date = datetime.utcnow() - timedelta(days=analysis_days)
recent_events = [e for e in learning_events
if e.student_id == student_id and e.timestamp >= cutoff_date]
# Prepare data for analysis
performance_summary = []
for concept_id, perf in student_data.items():
concept_name = CONCEPT_GRAPH.get(concept_id, {}).get('name', concept_id)
performance_summary.append({
"concept": concept_name,
"mastery": perf.mastery_level,
"accuracy": perf.accuracy_rate,
"attempts": perf.attempts_count,
"time_spent": perf.time_spent_minutes,
"last_accessed": perf.last_accessed.isoformat() if perf.last_accessed else None
})
# Build analysis prompt
prompt = f"""
Analyze the learning patterns for Student {student_id} over the past {analysis_days} days.
Performance Data:
{json.dumps(performance_summary, indent=2)}
Recent Learning Events: {len(recent_events)} events
Please provide a comprehensive analysis including:
1. Learning strengths and patterns
2. Areas that need attention
3. Optimal learning times/frequency
4. Difficulty progression recommendations
5. Personalized learning strategies
6. Motivation and engagement insights
Return a JSON object with:
- "learning_style_analysis": identified learning preferences
- "strength_areas": concepts/skills where student excels
- "challenge_areas": concepts that need more work
- "learning_velocity": how quickly student progresses
- "engagement_patterns": when student is most/least engaged
- "optimal_difficulty": recommended difficulty range
- "study_schedule": suggested learning schedule
- "personalized_strategies": specific strategies for this student
- "motivation_factors": what motivates this student
- "next_focus_areas": what to work on next
- "confidence_level": assessment of student confidence
"""
# Get AI analysis
response = await MODEL.generate_text(prompt, temperature=0.6)
try:
analysis_data = extract_json_from_text(response)
analysis_data.update({
"success": True,
"student_id": student_id,
"analysis_period_days": analysis_days,
"data_points_analyzed": len(performance_summary),
"recent_events_count": len(recent_events),
"generated_at": datetime.utcnow().isoformat()
})
return analysis_data
except Exception as e:
return {
"success": False,
"error": f"Failed to parse analysis: {str(e)}",
"raw_response": response
}
except Exception as e:
return {"success": False, "error": str(e)}
@mcp.tool()
async def optimize_learning_strategy(student_id: str, current_concept: str,
performance_history: dict = None) -> dict:
"""
Use Gemini to optimize learning strategy based on comprehensive student analysis.
Args:
student_id: Student identifier
current_concept: Current concept being studied
performance_history: Optional detailed performance history
Returns:
AI-optimized learning strategy recommendations
"""
try:
# Get comprehensive student data
if student_id not in student_performances:
return {
"success": True,
"student_id": student_id,
"message": "No performance data available. Starting with default strategy.",
"strategy": "beginner_friendly",
"recommendations": ["Start with foundational concepts", "Use guided practice"]
}
student_data = student_performances[student_id]
current_performance = student_data.get(current_concept, None)
# Analyze overall learning patterns
total_concepts = len(student_data)
avg_mastery = sum(p.mastery_level for p in student_data.values()) / total_concepts if total_concepts > 0 else 0
avg_accuracy = sum(p.accuracy_rate for p in student_data.values()) / total_concepts if total_concepts > 0 else 0
total_time = sum(p.time_spent_minutes for p in student_data.values())
# Get recent learning velocity
recent_events = [e for e in learning_events
if e.student_id == student_id and
e.timestamp >= datetime.utcnow() - timedelta(days=7)]
# Build comprehensive analysis prompt
prompt = f"""
Optimize the learning strategy for Student {student_id} studying {current_concept}.
CURRENT PERFORMANCE DATA:
- Current concept: {current_concept}
- Current mastery: {current_performance.mastery_level if current_performance else 0:.2f}
- Current accuracy: {current_performance.accuracy_rate if current_performance else 0:.2f}
- Attempts on current concept: {current_performance.attempts_count if current_performance else 0}
OVERALL STUDENT PROFILE:
- Total concepts studied: {total_concepts}
- Average mastery across all concepts: {avg_mastery:.2f}
- Average accuracy rate: {avg_accuracy:.2f}
- Total learning time: {total_time} minutes
- Recent activity: {len(recent_events)} events in past 7 days
Generate a comprehensive strategy optimization in JSON format:
{{
"optimized_strategy": {{
"primary_approach": "adaptive|mastery_based|exploratory|remedial",
"difficulty_recommendation": "current optimal difficulty level (0.0-1.0)",
"pacing_strategy": "fast|moderate|slow|variable",
"focus_areas": ["specific areas to emphasize"],
"learning_modalities": ["visual|auditory|kinesthetic|reading"]
}},
"immediate_actions": [
{{
"action": "specific action to take now",
"priority": "high|medium|low",
"expected_impact": "what this will achieve",
"time_estimate": "how long this will take"
}}
],
"session_optimization": {{
"ideal_session_length": "recommended minutes per session",
"break_frequency": "how often to take breaks",
"review_schedule": "when to review previous concepts",
"practice_distribution": "how to distribute practice time"
}},
"motivation_strategy": {{
"achievement_recognition": "how to celebrate progress",
"challenge_level": "optimal challenge to maintain engagement",
"goal_setting": "short and long-term goals",
"feedback_style": "how to provide effective feedback"
}},
"success_metrics": {{
"mastery_targets": "target mastery levels",
"accuracy_goals": "target accuracy rates",
"time_efficiency": "optimal time per concept",
"engagement_indicators": "signs of good engagement"
}}
}}
"""
# Get AI strategy optimization
response = await MODEL.generate_text(prompt, temperature=0.6)
try:
strategy_data = extract_json_from_text(response)
# Add metadata and validation
strategy_data.update({
"success": True,
"student_id": student_id,
"current_concept": current_concept,
"analysis_timestamp": datetime.utcnow().isoformat(),
"data_points_analyzed": total_concepts,
"recent_activity_level": len(recent_events),
"ai_powered": True
})
return strategy_data
except Exception as e:
# Fallback strategy if AI parsing fails
return {
"success": True,
"student_id": student_id,
"current_concept": current_concept,
"ai_powered": False,
"fallback_reason": f"AI analysis failed: {str(e)}",
"basic_strategy": {
"approach": "adaptive" if avg_mastery > 0.6 else "foundational",
"difficulty": min(0.8, max(0.3, avg_accuracy)),
"focus": "mastery" if avg_accuracy < 0.7 else "progression"
},
"recommendations": [
f"Current mastery level suggests {'advanced' if avg_mastery > 0.7 else 'foundational'} approach",
f"Accuracy rate of {avg_accuracy:.1%} indicates {'good progress' if avg_accuracy > 0.6 else 'need for more practice'}",
"Continue with consistent practice and regular review"
]
}
except Exception as e:
return {"success": False, "error": str(e)}
# Adaptive Learning MCP Tools
@mcp.tool()
async def start_adaptive_session(student_id: str, concept_id: str, initial_difficulty: float = 0.5) -> dict:
"""
Start an adaptive learning session for a student.
Args:
student_id: Unique identifier for the student
concept_id: Concept being learned
initial_difficulty: Initial difficulty level (0.0 to 1.0)
Returns:
Session information and initial recommendations
"""
try:
session_id = f"{student_id}_{concept_id}_{datetime.utcnow().strftime('%Y%m%d_%H%M%S')}"
# Get or create student performance
performance = get_student_performance(student_id, concept_id)
performance.difficulty_preference = initial_difficulty
performance.last_accessed = datetime.utcnow()
# Create session
active_sessions[session_id] = {
'student_id': student_id,
'concept_id': concept_id,
'start_time': datetime.utcnow(),
'current_difficulty': initial_difficulty,
'events': [],
'questions_answered': 0,
'correct_answers': 0
}
return {
"success": True,
"session_id": session_id,
"student_id": student_id,
"concept_id": concept_id,
"initial_difficulty": initial_difficulty,
"current_mastery": performance.mastery_level,
"recommendations": [
f"Start with difficulty level {initial_difficulty:.1f}",
f"Current mastery level: {performance.mastery_level:.2f}",
"System will adapt based on your performance"
]
}
except Exception as e:
return {"success": False, "error": str(e)}
@mcp.tool()
async def record_learning_event(student_id: str, concept_id: str, session_id: str,
event_type: str, event_data: dict) -> dict:
"""
Record a learning event for adaptive analysis.
Args:
student_id: Student identifier
concept_id: Concept identifier
session_id: Session identifier
event_type: Type of event ('answer_correct', 'answer_incorrect', 'hint_used', 'time_spent')
event_data: Additional event data
Returns:
Event recording confirmation and updated recommendations
"""
try:
# Record the event
event = LearningEvent(
student_id=student_id,
concept_id=concept_id,
event_type=event_type,
timestamp=datetime.utcnow(),
data=event_data
)
learning_events.append(event)
# Update session
if session_id in active_sessions:
session = active_sessions[session_id]
session['events'].append(event)
if event_type in ['answer_correct', 'answer_incorrect']:
session['questions_answered'] += 1
if event_type == 'answer_correct':
session['correct_answers'] += 1
# Update student performance
performance = get_student_performance(student_id, concept_id)
performance.attempts_count += 1
if event_type == 'answer_correct':
performance.accuracy_rate = (performance.accuracy_rate * (performance.attempts_count - 1) + 1.0) / performance.attempts_count
elif event_type == 'answer_incorrect':
performance.accuracy_rate = (performance.accuracy_rate * (performance.attempts_count - 1) + 0.0) / performance.attempts_count
elif event_type == 'time_spent':
performance.time_spent_minutes += event_data.get('minutes', 0)
# Update mastery level
new_mastery = update_mastery_level(performance)
# Adapt difficulty
new_difficulty = adapt_difficulty(performance)
# Generate recommendations
recommendations = []
if performance.accuracy_rate > 0.8 and performance.attempts_count >= 3:
recommendations.append("Great job! Consider moving to a harder difficulty level.")
elif performance.accuracy_rate < 0.5 and performance.attempts_count >= 3:
recommendations.append("Let's try some easier questions to build confidence.")
if new_mastery > 0.8:
recommendations.append("You're mastering this concept! Ready for the next one?")
return {
"success": True,
"event_recorded": True,
"updated_mastery": new_mastery,
"updated_difficulty": new_difficulty,
"current_accuracy": performance.accuracy_rate,
"recommendations": recommendations
}
except Exception as e:
return {"success": False, "error": str(e)}
@mcp.tool()
async def get_adaptive_recommendations(student_id: str, concept_id: str, session_id: str = None) -> dict:
"""
Get AI-powered adaptive learning recommendations using Gemini analysis.
Args:
student_id: Student identifier
concept_id: Concept identifier
session_id: Optional session identifier
Returns:
Intelligent adaptive learning recommendations
"""
try:
performance = get_student_performance(student_id, concept_id)
concept_data = CONCEPT_GRAPH.get(concept_id, {"name": concept_id, "description": ""})
# Get session data if available
session_data = active_sessions.get(session_id, {}) if session_id else {}
# Build comprehensive context for Gemini
context = f"""
Student Performance Analysis for {concept_data.get('name', concept_id)}:
Current Metrics:
- Mastery Level: {performance.mastery_level:.2f} (0.0 = no understanding, 1.0 = complete mastery)
- Accuracy Rate: {performance.accuracy_rate:.2f} (proportion of correct answers)
- Total Attempts: {performance.attempts_count}
- Time Spent: {performance.time_spent_minutes} minutes
- Current Difficulty Preference: {performance.difficulty_preference:.2f}
- Last Accessed: {performance.last_accessed.isoformat() if performance.last_accessed else 'Never'}
Session Information:
- Session ID: {session_id or 'No active session'}
- Questions Answered: {session_data.get('questions_answered', 0)}
- Correct Answers: {session_data.get('correct_answers', 0)}
Concept Details:
- Concept: {concept_data.get('name', concept_id)}
- Description: {concept_data.get('description', 'No description available')}
"""
prompt = f"""{context}
As an AI learning advisor, analyze this student's performance and provide personalized recommendations.
Consider:
1. Current mastery level and learning trajectory
2. Accuracy patterns and difficulty appropriateness
3. Time investment and learning efficiency
4. Optimal next steps for continued growth
5. Motivation and engagement strategies
Provide specific, actionable recommendations in JSON format:
{{
"immediate_actions": [
{{
"type": "difficulty_adjustment|content_type|study_strategy|break_recommendation",
"priority": "high|medium|low",
"action": "specific action to take",
"reasoning": "why this action is recommended",
"expected_outcome": "what this should achieve"
}}
],
"difficulty_recommendation": {{
"current_level": {performance.difficulty_preference:.2f},
"suggested_level": "recommended difficulty (0.0-1.0)",
"adjustment_reason": "explanation for the change",
"gradual_steps": ["step-by-step difficulty progression"]
}},
"learning_strategy": {{
"primary_focus": "what to focus on most",
"study_methods": ["recommended study techniques"],
"practice_types": ["types of practice exercises"],
"time_allocation": "suggested time distribution"
}},
"motivation_boosters": [
"specific encouragement based on progress",
"achievement recognition",
"goal-setting suggestions"
],
"next_milestones": [
{{
"milestone": "specific goal",
"target_mastery": "target mastery level",
"estimated_time": "time to achieve",
"success_indicators": ["how to know when achieved"]
}}
],
"warning_signs": [
"potential issues to watch for"
],
"adaptive_insights": {{
"learning_pattern": "observed learning pattern",
"optimal_session_length": "recommended session duration",
"best_practice_times": "when student learns best",
"engagement_level": "current engagement assessment"
}}
}}
"""
# Get AI recommendations
response = await MODEL.generate_text(prompt, temperature=0.7)
try:
ai_recommendations = extract_json_from_text(response)
# Add basic fallback recommendations if AI parsing fails
if not ai_recommendations or "immediate_actions" not in ai_recommendations:
ai_recommendations = _generate_fallback_recommendations(performance)
# Enhance with computed metrics
ai_recommendations.update({
"success": True,
"student_id": student_id,
"concept_id": concept_id,
"session_id": session_id,
"current_metrics": {
"mastery_level": performance.mastery_level,
"accuracy_rate": performance.accuracy_rate,
"attempts_count": performance.attempts_count,
"time_spent_minutes": performance.time_spent_minutes,
"difficulty_preference": performance.difficulty_preference
},
"ai_powered": True,
"generated_at": datetime.utcnow().isoformat()
})
return ai_recommendations
except Exception as e:
# Fallback to basic recommendations if AI parsing fails
return _generate_fallback_recommendations(performance, student_id, concept_id, session_id, str(e))
except Exception as e:
return {"success": False, "error": str(e)}
def _generate_fallback_recommendations(performance: StudentPerformance, student_id: str = None,
concept_id: str = None, session_id: str = None,
ai_error: str = None) -> dict:
"""Generate basic recommendations when AI analysis fails."""
recommendations = []
# Difficulty recommendations
if performance.accuracy_rate > 0.8:
recommendations.append({
"type": "difficulty_increase",
"priority": "medium",
"action": "Increase difficulty level",
"reasoning": "High accuracy indicates readiness for more challenge",
"expected_outcome": "Maintain engagement and continued growth"
})
elif performance.accuracy_rate < 0.5:
recommendations.append({
"type": "difficulty_decrease",
"priority": "high",
"action": "Decrease difficulty level",
"reasoning": "Low accuracy suggests current level is too challenging",
"expected_outcome": "Build confidence and foundational understanding"
})
# Mastery recommendations
if performance.mastery_level > 0.8:
recommendations.append({
"type": "concept_advancement",
"priority": "high",
"action": "Move to next concept",
"reasoning": "High mastery level achieved",
"expected_outcome": "Continue learning progression"
})
elif performance.mastery_level < 0.3 and performance.attempts_count >= 5:
recommendations.append({
"type": "additional_practice",
"priority": "high",
"action": "Focus on additional practice",
"reasoning": "Low mastery despite multiple attempts",
"expected_outcome": "Strengthen foundational understanding"
})
return {
"success": True,
"student_id": student_id,
"concept_id": concept_id,
"session_id": session_id,
"immediate_actions": recommendations,
"ai_powered": False,
"fallback_reason": f"AI analysis failed: {ai_error}" if ai_error else "Using basic recommendation engine",
"current_metrics": {
"mastery_level": performance.mastery_level,
"accuracy_rate": performance.accuracy_rate,
"attempts_count": performance.attempts_count,
"difficulty_preference": performance.difficulty_preference
},
"generated_at": datetime.utcnow().isoformat()
}
@mcp.tool()
async def get_learning_path(student_id: str, concept_ids: list, student_level: str = "beginner") -> dict:
"""
Generate a personalized learning path for a student, fully LLM-driven.
Use Gemini to generate a JSON object with a list of steps, each with concept name, description, estimated time, and recommended resources.
"""
prompt = (
f"A student (ID: {student_id}) with level '{student_level}' needs a learning path for these concepts: {concept_ids}. "
f"Return a JSON object with a 'learning_path' field: a list of steps, each with concept_name, description, estimated_time_minutes, and resources (list)."
)
llm_response = await MODEL.generate_text(prompt)
try:
data = extract_json_from_text(llm_response)
except Exception:
data = {"llm_raw": llm_response, "error": "Failed to parse LLM output as JSON"}
return data
@mcp.tool()
async def get_adaptive_learning_path(student_id: str, target_concepts: list,
strategy: str = "adaptive", max_concepts: int = 10) -> dict:
"""
Generate an AI-powered adaptive learning path using Gemini analysis.
Args:
student_id: Student identifier
target_concepts: List of target concept IDs
strategy: Learning strategy ('adaptive', 'mastery_focused', 'breadth_first', 'depth_first', 'remediation')
max_concepts: Maximum number of concepts in the path
Returns:
Intelligent adaptive learning path optimized by AI
"""
try:
# Get comprehensive student performance data
student_data = {}
overall_stats = {
'total_concepts': 0,
'average_mastery': 0,
'average_accuracy': 0,
'total_time': 0,
'total_attempts': 0
}
for concept_id in target_concepts:
concept_name = CONCEPT_GRAPH.get(concept_id, {}).get('name', concept_id)
if student_id in student_performances and concept_id in student_performances[student_id]:
perf = student_performances[student_id][concept_id]
student_data[concept_id] = {
'concept_name': concept_name,
'mastery_level': perf.mastery_level,
'accuracy_rate': perf.accuracy_rate,
'difficulty_preference': perf.difficulty_preference,
'attempts_count': perf.attempts_count,
'time_spent': perf.time_spent_minutes,
'last_accessed': perf.last_accessed.isoformat() if perf.last_accessed else None
}
overall_stats['total_concepts'] += 1
overall_stats['average_mastery'] += perf.mastery_level
overall_stats['average_accuracy'] += perf.accuracy_rate
overall_stats['total_time'] += perf.time_spent_minutes
overall_stats['total_attempts'] += perf.attempts_count
else:
# New concept - no performance data
student_data[concept_id] = {
'concept_name': concept_name,
'mastery_level': 0.0,
'accuracy_rate': 0.0,
'difficulty_preference': 0.5,
'attempts_count': 0,
'time_spent': 0,
'last_accessed': None,
'is_new': True
}
# Calculate averages
if overall_stats['total_concepts'] > 0:
overall_stats['average_mastery'] /= overall_stats['total_concepts']
overall_stats['average_accuracy'] /= overall_stats['total_concepts']
# Build comprehensive prompt for Gemini
prompt = f"""
Create an optimal adaptive learning path for Student {student_id} using advanced AI analysis.
STUDENT PERFORMANCE DATA:
{json.dumps(student_data, indent=2)}
OVERALL STATISTICS:
- Total concepts with data: {overall_stats['total_concepts']}
- Average mastery level: {overall_stats['average_mastery']:.2f}
- Average accuracy rate: {overall_stats['average_accuracy']:.2f}
- Total learning time: {overall_stats['total_time']} minutes
- Total attempts: {overall_stats['total_attempts']}
LEARNING STRATEGY: {strategy}
MAX CONCEPTS: {max_concepts}
STRATEGY DEFINITIONS:
- adaptive: AI-optimized path balancing challenge and success
- mastery_focused: Deep understanding before progression
- breadth_first: Cover many concepts quickly for overview
- depth_first: Thorough exploration of fewer concepts
- remediation: Focus on filling knowledge gaps
REQUIREMENTS:
1. Analyze student's learning patterns and preferences
2. Consider concept dependencies and prerequisites
3. Optimize for engagement and learning efficiency
4. Provide personalized difficulty progression
5. Include time estimates based on student's pace
6. Add motivational elements and milestones
Generate a JSON response with this structure:
{{
"learning_path": [
{{
"step": 1,
"concept_id": "concept_id",
"concept_name": "Human readable name",
"description": "What student will learn",
"estimated_time_minutes": 30,
"difficulty_level": 0.6,
"mastery_target": 0.8,
"prerequisites_met": true,
"learning_objectives": ["specific objective 1", "objective 2"],
"recommended_activities": ["activity 1", "activity 2"],
"success_criteria": ["how to know when mastered"],
"adaptive_notes": "Personalized guidance",
"motivation_boost": "Encouraging message"
}}
],
"path_analysis": {{
"strategy_rationale": "Why this strategy was chosen",
"difficulty_progression": "How difficulty increases",
"estimated_completion": "Total time estimate",
"learning_velocity": "Expected pace",
"challenge_level": "Overall difficulty assessment"
}},
"personalization": {{
"student_strengths": ["identified strengths"],
"focus_areas": ["areas needing attention"],
"learning_style_adaptations": ["how path is adapted"],
"motivation_factors": ["what will keep student engaged"]
}},
"milestones": [
{{
"milestone_name": "Achievement name",
"concepts_completed": 3,
"expected_mastery": 0.75,
"celebration": "How to celebrate achievement"
}}
],
"adaptive_features": [
"Real-time difficulty adjustment",
"Performance-based pacing",
"Personalized content delivery"
]
}}
"""
# Get AI-generated learning path
response = await MODEL.generate_text(prompt, temperature=0.6)
try:
ai_path = extract_json_from_text(response)
# Validate and enhance the AI response
if not ai_path or "learning_path" not in ai_path:
# Fallback to basic path generation
ai_path = _generate_basic_adaptive_path(student_data, target_concepts, strategy, max_concepts)
# Add metadata
ai_path.update({
"success": True,
"student_id": student_id,
"strategy": strategy,
"max_concepts": max_concepts,
"ai_powered": True,
"total_steps": len(ai_path.get("learning_path", [])),
"total_time_minutes": sum(step.get("estimated_time_minutes", 30)
for step in ai_path.get("learning_path", [])),
"generated_at": datetime.utcnow().isoformat()
})
return ai_path
except Exception as e:
# Fallback to basic path if AI parsing fails
return _generate_basic_adaptive_path(student_data, target_concepts, strategy, max_concepts, str(e))
except Exception as e:
return {"success": False, "error": str(e)}
def _generate_basic_adaptive_path(student_data: dict, target_concepts: list,
strategy: str, max_concepts: int, ai_error: str = None) -> dict:
"""Generate basic adaptive path when AI analysis fails."""
# Simple sorting based on strategy
if strategy == "mastery_focused":
sorted_concepts = sorted(target_concepts,
key=lambda c: student_data.get(c, {}).get('mastery_level', 0))
elif strategy == "breadth_first":
# Mix of new and partially learned concepts
sorted_concepts = sorted(target_concepts,
key=lambda c: (student_data.get(c, {}).get('attempts_count', 0),
1 - student_data.get(c, {}).get('mastery_level', 0)))
else: # adaptive or other
def adaptive_score(concept_id):
data = student_data.get(concept_id, {})
mastery = data.get('mastery_level', 0)
attempts = data.get('attempts_count', 0)
return (1 - mastery) * (1 + min(attempts / 10, 1))
sorted_concepts = sorted(target_concepts, key=adaptive_score, reverse=True)
# Limit to max_concepts
selected_concepts = sorted_concepts[:max_concepts]
# Generate learning path with adaptive recommendations
learning_path = []
for i, concept_id in enumerate(selected_concepts, 1):
concept_data = CONCEPT_GRAPH.get(concept_id, {"name": concept_id, "description": ""})
perf_data = student_data.get(concept_id, {})
# Estimate time based on mastery level
base_time = 30 # Base 30 minutes
mastery = perf_data.get('mastery_level', 0)
if mastery > 0.8:
estimated_time = base_time * 0.5 # Quick review
elif mastery > 0.5:
estimated_time = base_time * 0.8 # Moderate practice
else:
estimated_time = base_time * 1.2 # More practice needed
learning_path.append({
"step": i,
"concept_id": concept_id,
"concept_name": concept_data.get("name", concept_id),
"description": concept_data.get("description", ""),
"estimated_time_minutes": int(estimated_time),
"current_mastery": perf_data.get('mastery_level', 0),
"recommended_difficulty": perf_data.get('difficulty_preference', 0.5),
"adaptive_notes": _get_adaptive_notes(perf_data),
"resources": [
f"Adaptive practice for {concept_data.get('name', concept_id)}",
f"Personalized exercises at {perf_data.get('difficulty_preference', 0.5):.1f} difficulty",
f"Progress tracking and real-time feedback"
]
})
total_time = sum(step["estimated_time_minutes"] for step in learning_path)
return {
"success": True,
"learning_path": learning_path,
"strategy": strategy,
"total_steps": len(learning_path),
"total_time_minutes": total_time,
"ai_powered": False,
"fallback_reason": f"AI analysis failed: {ai_error}" if ai_error else "Using basic adaptive algorithm",
"adaptive_features": [
"Performance-based ordering",
"Mastery-level time estimation",
"Basic difficulty adaptation"
],
"generated_at": datetime.utcnow().isoformat()
}
def _get_adaptive_notes(perf_data: dict) -> str:
"""Generate adaptive notes based on performance data."""
mastery = perf_data.get('mastery_level', 0)
accuracy = perf_data.get('accuracy_rate', 0)
attempts = perf_data.get('attempts_count', 0)
if attempts == 0:
return "New concept - start with guided practice"
elif mastery > 0.8:
return "Well mastered - quick review recommended"
elif mastery > 0.5:
return "Good progress - continue with current difficulty"
elif accuracy < 0.5:
return "Needs more practice - consider easier difficulty"
else:
return "Building understanding - maintain current approach"
@mcp.tool()
async def get_student_progress_summary(student_id: str, days: int = 7) -> dict:
"""
Get a comprehensive progress summary for a student.
Args:
student_id: Student identifier
days: Number of days to analyze
Returns:
Progress summary with analytics
"""
try:
# Get student performance data
if student_id not in student_performances:
return {
"success": True,
"student_id": student_id,
"message": "No performance data available",
"concepts_practiced": 0,
"total_time_minutes": 0,
"average_mastery": 0.0
}
student_data = student_performances[student_id]
# Calculate summary statistics
total_concepts = len(student_data)
total_time = sum(perf.time_spent_minutes for perf in student_data.values())
total_attempts = sum(perf.attempts_count for perf in student_data.values())
average_mastery = sum(perf.mastery_level for perf in student_data.values()) / total_concepts if total_concepts > 0 else 0
average_accuracy = sum(perf.accuracy_rate for perf in student_data.values()) / total_concepts if total_concepts > 0 else 0
# Get recent events
cutoff_date = datetime.utcnow() - timedelta(days=days)
recent_events = [e for e in learning_events
if e.student_id == student_id and e.timestamp >= cutoff_date]
# Concept breakdown
concept_summary = []
for concept_id, perf in student_data.items():
concept_summary.append({
"concept_id": concept_id,
"mastery_level": perf.mastery_level,
"accuracy_rate": perf.accuracy_rate,
"time_spent_minutes": perf.time_spent_minutes,
"attempts_count": perf.attempts_count,
"last_accessed": perf.last_accessed.isoformat() if perf.last_accessed else None,
"status": _get_concept_status(perf.mastery_level)
})
return {
"success": True,
"student_id": student_id,
"analysis_period_days": days,
"summary": {
"concepts_practiced": total_concepts,
"total_time_minutes": total_time,
"total_attempts": total_attempts,
"average_mastery": round(average_mastery, 2),
"average_accuracy": round(average_accuracy, 2),
"recent_events_count": len(recent_events)
},
"concept_breakdown": concept_summary,
"recommendations": _generate_progress_recommendations(student_data),
"generated_at": datetime.utcnow().isoformat()
}
except Exception as e:
return {"success": False, "error": str(e)}
def _get_concept_status(mastery_level: float) -> str:
"""Get concept status based on mastery level."""
if mastery_level >= 0.8:
return "Mastered"
elif mastery_level >= 0.6:
return "Good Progress"
elif mastery_level >= 0.4:
return "Learning"
elif mastery_level >= 0.2:
return "Struggling"
else:
return "Needs Attention"
def _generate_progress_recommendations(student_data: Dict[str, StudentPerformance]) -> List[str]:
"""Generate recommendations based on student progress."""
recommendations = []
mastered_concepts = [cid for cid, perf in student_data.items() if perf.mastery_level >= 0.8]
struggling_concepts = [cid for cid, perf in student_data.items() if perf.mastery_level < 0.4]
if len(mastered_concepts) > 0:
recommendations.append(f"Great job! You've mastered {len(mastered_concepts)} concepts.")
if len(struggling_concepts) > 0:
recommendations.append(f"Focus on {len(struggling_concepts)} concepts that need more practice.")
# Check for concepts that haven't been accessed recently
week_ago = datetime.utcnow() - timedelta(days=7)
stale_concepts = [cid for cid, perf in student_data.items()
if perf.last_accessed and perf.last_accessed < week_ago]
if len(stale_concepts) > 0:
recommendations.append(f"Consider reviewing {len(stale_concepts)} concepts you haven't practiced recently.")
return recommendations
|