| import json |
| import os |
| import random |
|
|
| class TeachingRuleEngine: |
| """ |
| Expert system for making teaching decisions based on student profile and pedagogical rules. |
| Minimizes reliability on LLM by using pre-defined strategies and templates. |
| """ |
| |
| def __init__(self, knowledge_base_path=None): |
| if knowledge_base_path is None: |
| |
| base_dir = os.path.dirname(os.path.dirname(__file__)) |
| knowledge_base_path = os.path.join(base_dir, 'knowledge_bases', 'teaching_strategies.json') |
| |
| self.strategies = [] |
| self.templates = {} |
| self._load_knowledge_base(knowledge_base_path) |
| |
| def _load_knowledge_base(self, path): |
| """Load strategies and templates from JSON file""" |
| try: |
| with open(path, 'r') as f: |
| data = json.load(f) |
| self.strategies = data.get('strategies', []) |
| self.templates = data.get('lesson_templates', {}) |
| except Exception as e: |
| print(f"Error loading teaching knowledge base: {e}") |
| |
| self.strategies = [ |
| {"name": "standard", "effectiveness_score": 0.5} |
| ] |
|
|
| def select_teaching_strategy(self, student_profile): |
| """ |
| Select the best teaching strategy based on student profile. |
| |
| Args: |
| student_profile (dict): Contains knowledge_level, learning_style, etc. |
| |
| Returns: |
| dict: The selected strategy object |
| """ |
| knowledge_level = student_profile.get('knowledge_level', 0.0) |
| learning_styles = student_profile.get('learning_style', []) |
| is_struggling = student_profile.get('recent_performance', 1.0) < 0.6 |
| |
| |
| if knowledge_level < 0.4 or is_struggling: |
| return self._find_strategy("scaffolding") |
| |
| |
| if "visual" in learning_styles or "kinesthetic" in learning_styles: |
| |
| if knowledge_level > 0.3: |
| return self._find_strategy("practical_application") |
| |
| |
| if knowledge_level > 0.7: |
| return self._find_strategy("inquiry_based") |
| |
| |
| return self._find_strategy("direct_instruction") or self.strategies[0] |
|
|
| def _find_strategy(self, name): |
| """Find strategy by name""" |
| for s in self.strategies: |
| if s['name'] == name: |
| return s |
| return None |
|
|
| def generate_lesson_structure(self, topic, strategy): |
| """ |
| Generate a lesson structure using templates instead of LLM. |
| |
| Args: |
| topic (object): The topic object (from DB) |
| strategy (dict): The selected strategy |
| |
| Returns: |
| dict: Structured lesson plan ready for content population |
| """ |
| |
| template_name = "standard" |
| if strategy['name'] == "scaffolding": |
| template_name = "standard" |
| elif strategy['name'] == "micro_learning" or topic.difficulty == "beginner": |
| if topic.description and len(topic.description) < 50: |
| template_name = "micro_learning" |
| elif strategy['name'] == "inquiry_based": |
| template_name = "deep_dive" |
| |
| template = self.templates.get(template_name, self.templates.get("standard")) |
| |
| |
| structure = { |
| "title": f"Lesson: {topic.name}", |
| "strategy_used": strategy['name'], |
| "sections": [], |
| "metadata": { |
| "estimated_time": f"{template['word_count_target'] / 100} mins", |
| "difficulty": topic.difficulty |
| } |
| } |
| |
| |
| strategy_structure = strategy.get('lesson_structure', {}) |
| |
| for section in template['sections']: |
| |
| section_key = section.lower().replace(" ", "_") |
| instruction = strategy_structure.get(section_key, f"Standard {section} content") |
| |
| structure['sections'].append({ |
| "title": section, |
| "type": section_key, |
| "instruction_for_llm": instruction |
| }) |
| |
| return structure |
|
|