File size: 3,119 Bytes
25732fb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import json
import os
import random

class AssessmentRuleEngine:
    """
    Expert system for assessment decisions.
    Selects questions and formats quizzes based on rules 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', 'question_templates.json')
            
        self.question_templates = []
        self.rubrics = {}
        self._load_knowledge_base(knowledge_base_path)
        
    def _load_knowledge_base(self, path):
        try:
            with open(path, 'r') as f:
                data = json.load(f)
                self.question_templates = data.get('question_templates', [])
                self.rubrics = data.get('assessment_rubrics', {})
        except Exception as e:
            print(f"Error loading assessment knowledge base: {e}")

    def select_question_types(self, knowledge_level, topic_difficulty):
        """
        Determine the mix of question types/difficulties.
        """
        mix = {"beginner": 0, "intermediate": 0, "advanced": 0}
        
        if knowledge_level < 0.3:
            mix["beginner"] = 3
            mix["intermediate"] = 1
        elif knowledge_level < 0.7:
            mix["beginner"] = 1
            mix["intermediate"] = 2
            mix["advanced"] = 1
        else:
            mix["intermediate"] = 2
            mix["advanced"] = 3
            
        return mix

    def get_template_for_topic(self, topic_name, difficulty):
        """
        Find a question template suitable for the topic and difficulty.
        """
        candidates = []
        for t in self.question_templates:
            # Check if template is applicable to this topic (simple keyword match for now)
            # In a real system, this would be a more robust tag matching
            is_applicable = "all" in t['applicable_topics']
            if not is_applicable:
                for keyword in t['applicable_topics']:
                    if keyword in topic_name.lower():
                        is_applicable = True
                        break
            
            if is_applicable:
                candidates.append(t)
                
        if not candidates:
            return None
            
        return random.choice(candidates)

    def formulate_question_prompt(self, template, topic_name, difficulty):
        """
        Use a template to create a specific prompt for the LLM to fill.
        This reduces the LLM's work to just filling blanks/generating code.
        """
        modifier = template['difficulty_modifiers'].get(difficulty, "Standard")
        
        prompt = f"""
        Generate a specific question about "{topic_name}" using this pattern:
        "{template['pattern']}"
        
        Constraint: {modifier}
        Cognitive Level: {template['cognitive_level']}
        
        Return the Code Snippet (if applicable), Options, Correct Answer, and Explanation.
        """
        return prompt