File size: 3,711 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
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

import json
import os
from app import app, db
from models import AgentKnowledge, ContentLibrary
from datetime import datetime

def load_json(filepath):
    try:
        with open(filepath, 'r') as f:
            return json.load(f)
    except FileNotFoundError:
        print(f"Error: File not found at {filepath}")
        return None

def train_teaching_agent():
    print("Training Teaching Agent...")
    data = load_json('agent_knowledge/knowledge_bases/teaching_strategies.json')
    if not data: return

    # Clear existing to avoid duplicates (for this script)
    AgentKnowledge.query.filter_by(agent_type='teaching').delete()
    
    count = 0
    # 1. Strategies
    for strategy in data.get('strategies', []):
        entry = AgentKnowledge(
            agent_type='teaching',
            knowledge_type='strategy',
            content=strategy,
            effectiveness_score=strategy.get('effectiveness_score', 0.5),
            usage_count=0
        )
        db.session.add(entry)
        count += 1
        
    # 2. Templates
    templates = data.get('lesson_templates', {})
    for name, template in templates.items():
        entry = AgentKnowledge(
            agent_type='teaching',
            knowledge_type='template',
            content={'name': name, 'template': template},
            effectiveness_score=0.8,
            usage_count=0
        )
        db.session.add(entry)
        count += 1
        
    print(f"  - Loaded {count} teaching knowledge items.")

def train_assessment_agent():
    print("Training Assessment Agent...")
    data = load_json('agent_knowledge/knowledge_bases/question_templates.json')
    if not data: return

    AgentKnowledge.query.filter_by(agent_type='assessment').delete()
    
    count = 0
    for template in data.get('templates', []):
        entry = AgentKnowledge(
            agent_type='assessment',
            knowledge_type='question_template',
            content=template,
            effectiveness_score=0.8,
            usage_count=0
        )
        db.session.add(entry)
        count += 1
        
    print(f"  - Loaded {count} assessment templates.")

def train_tutor_agent():
    print("Training Tutor Agent...")
    data = load_json('agent_knowledge/knowledge_bases/hint_strategies.json')
    if not data: return

    AgentKnowledge.query.filter_by(agent_type='tutor').delete()
    
    count = 0
    for strategy in data.get('strategies', []):
        entry = AgentKnowledge(
            agent_type='tutor',
            knowledge_type='hint_strategy',
            content=strategy,
            effectiveness_score=strategy.get('effectiveness_score', 0.5),
            usage_count=0
        )
        db.session.add(entry)
        count += 1
        
    print(f"  - Loaded {count} tutor strategies.")

def training_summary():
    print("\n" + "="*40)
    print("AGENT TRAINING COMPLETE")
    print("="*40)
    
    total_knowledge = AgentKnowledge.query.count()
    print(f"Total Knowledge Items in DB: {total_knowledge}")
    
    by_agent = db.session.query(AgentKnowledge.agent_type, db.func.count(AgentKnowledge.id)).group_by(AgentKnowledge.agent_type).all()
    for agent, count in by_agent:
        print(f"  - {agent.capitalize()} Agent: {count} items")
        
if __name__ == '__main__':
    with app.app_context():
        try:
            train_teaching_agent()
            train_assessment_agent()
            train_tutor_agent()
            
            db.session.commit()
            print("Database committed successfully.")
            
            training_summary()
            
        except Exception as e:
            db.session.rollback()
            print(f"Error during training: {str(e)}")