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
 
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
"""
Training Recommender Module

Provides AI-driven recommendations for next training session based on gap analysis.
"""

from typing import List, Dict, Optional, Any, Tuple
import json
from pathlib import Path
from collections import defaultdict


class TrainingRecommender:
    """
    Recommends training strategies based on performance gaps.

    Features:
    - Targeted training recommendations
    - Data generation suggestions
    - Priority-based training plans
    - Progress tracking
    """

    def __init__(self, gap_analyzer: Optional[Any] = None):
        """
        Initialize training recommender.

        Args:
            gap_analyzer: GapAnalyzer instance with performance data
        """
        self.gap_analyzer = gap_analyzer
        self.recommendations: List[Dict[str, Any]] = []

    def generate_recommendations(
        self,
        max_recommendations: int = 5,
        focus_on_weak: bool = True
    ) -> List[Dict[str, Any]]:
        """
        Generate training recommendations based on gaps.

        Args:
            max_recommendations: Maximum number of recommendations
            focus_on_weak: Prioritize weak areas over moderate ones

        Returns:
            List of training recommendations
        """
        if not self.gap_analyzer or not self.gap_analyzer.gaps:
            return [{
                'category': 'General',
                'priority': 'MEDIUM',
                'action': 'No performance data available. Start with general training.',
                'estimated_examples': 100,
                'topics': ['General training data']
            }]

        recommendations = []

        # Get gaps to address
        gaps = self.gap_analyzer.gaps

        if focus_on_weak:
            # Focus on weak and declining areas first
            priority_gaps = [
                g for g in gaps
                if g['level'] == 'WEAK' or g['trend'] == 'declining'
            ]
            if not priority_gaps:
                # Fall back to moderate areas
                priority_gaps = [g for g in gaps if g['level'] == 'MODERATE']
        else:
            # Include all non-strong areas
            priority_gaps = [g for g in gaps if g['level'] != 'STRONG']

        # Generate recommendations for top gaps
        for gap in priority_gaps[:max_recommendations]:
            recommendation = self._create_recommendation(gap)
            recommendations.append(recommendation)

        self.recommendations = recommendations
        return recommendations

    def _create_recommendation(self, gap: Dict[str, Any]) -> Dict[str, Any]:
        """
        Create a detailed recommendation for a gap.

        Args:
            gap: Gap analysis data

        Returns:
            Training recommendation
        """
        category = gap['category']
        avg_score = gap['avg_score']
        level = gap['level']

        # Determine number of examples needed
        if avg_score < 40:
            estimated_examples = 100
            intensity = "intensive"
        elif avg_score < 60:
            estimated_examples = 50
            intensity = "moderate"
        else:
            estimated_examples = 25
            intensity = "light"

        # Generate specific action items
        action_items = self._generate_action_items(category, level, gap['trend'])

        # Suggest topics
        topics = self._suggest_topics(category)

        recommendation = {
            'category': category,
            'priority': gap['priority'],
            'current_score': avg_score,
            'trend': gap['trend'],
            'intensity': intensity,
            'estimated_examples': estimated_examples,
            'action': f"Focus on {category} with {intensity} training",
            'action_items': action_items,
            'suggested_topics': topics,
            'expected_improvement': self._estimate_improvement(avg_score, estimated_examples)
        }

        return recommendation

    def _generate_action_items(
        self,
        category: str,
        level: str,
        trend: str
    ) -> List[str]:
        """
        Generate specific action items for a category.

        Args:
            category: Category name
            level: Performance level
            trend: Performance trend

        Returns:
            List of action items
        """
        items = []

        # Base recommendations based on level
        if level == 'WEAK':
            items.append(f"Add 50-100 {category} examples to training data")
            items.append(f"Review fundamental {category} concepts")
            items.append("Include diverse question types and difficulty levels")
        elif level == 'MODERATE':
            items.append(f"Add 25-50 {category} examples focusing on edge cases")
            items.append(f"Review intermediate {category} topics")
        else:
            items.append(f"Maintain current {category} performance with 10-20 examples")

        # Add trend-specific items
        if trend == 'declining':
            items.append("⚠️ Address declining performance immediately")
            items.append(f"Review recent {category} training data for quality issues")
        elif trend == 'improving':
            items.append("βœ… Continue current training approach")

        # Add testing recommendation
        items.append(f"Test specifically on {category} after training")

        return items

    def _suggest_topics(self, category: str) -> List[str]:
        """
        Suggest specific topics for a category.

        Args:
            category: Category name

        Returns:
            List of suggested topics
        """
        # Topic suggestions by common categories
        topic_map = {
            'Estate Planning': [
                'Revocable living trusts',
                'Wills and probate',
                'Power of attorney',
                'Estate tax strategies',
                'Charitable giving',
                'Trust structures'
            ],
            'Retirement Planning': [
                '401(k) and IRA strategies',
                'Required minimum distributions',
                'Social Security optimization',
                'Pension planning',
                'Retirement income strategies',
                'Healthcare in retirement'
            ],
            'Tax Planning': [
                'Tax-efficient investing',
                'Capital gains strategies',
                'Tax-loss harvesting',
                'Deductions and credits',
                'Alternative minimum tax',
                'Estate and gift taxes'
            ],
            'Investment Planning': [
                'Asset allocation',
                'Portfolio diversification',
                'Risk management',
                'Modern portfolio theory',
                'Performance evaluation',
                'Rebalancing strategies'
            ],
            'Insurance Planning': [
                'Life insurance types',
                'Disability insurance',
                'Long-term care insurance',
                'Property and casualty',
                'Umbrella policies',
                'Insurance needs analysis'
            ],
            'Education Planning': [
                '529 plans',
                'Coverdell ESAs',
                'Financial aid strategies',
                'Student loan planning',
                'Education tax benefits'
            ]
        }

        # Return specific topics if available, otherwise generic suggestions
        if category in topic_map:
            return topic_map[category]
        else:
            return [
                f"Fundamental {category} concepts",
                f"Intermediate {category} topics",
                f"Advanced {category} strategies",
                f"{category} best practices",
                f"Common {category} scenarios"
            ]

    def _estimate_improvement(
        self,
        current_score: float,
        num_examples: int
    ) -> str:
        """
        Estimate expected improvement from training.

        Args:
            current_score: Current performance score
            num_examples: Number of training examples

        Returns:
            Improvement estimate description
        """
        # Simple heuristic: more examples = more improvement, diminishing returns
        base_improvement = min(num_examples * 0.3, 30)  # Max 30% improvement

        # Lower scores have more room for improvement
        if current_score < 40:
            multiplier = 1.5
        elif current_score < 60:
            multiplier = 1.2
        else:
            multiplier = 0.8

        estimated_improvement = base_improvement * multiplier
        new_score = min(current_score + estimated_improvement, 95)

        return f"+{estimated_improvement:.1f}% (to ~{new_score:.1f}%)"

    def create_training_plan(
        self,
        priority: str = "all",
        include_data_generation: bool = True
    ) -> Dict[str, Any]:
        """
        Create a comprehensive training plan.

        Args:
            priority: Focus on "high", "medium", "low", or "all" priority items
            include_data_generation: Include data generation instructions

        Returns:
            Training plan
        """
        if not self.recommendations:
            self.generate_recommendations()

        # Filter by priority
        if priority.upper() != "ALL":
            filtered_recs = [
                r for r in self.recommendations
                if r['priority'] == priority.upper()
            ]
        else:
            filtered_recs = self.recommendations

        # Calculate totals
        total_examples = sum(r['estimated_examples'] for r in filtered_recs)
        categories = [r['category'] for r in filtered_recs]

        plan = {
            'plan_name': f"Training Plan - Priority: {priority.title()}",
            'num_focus_areas': len(filtered_recs),
            'focus_categories': categories,
            'total_examples_needed': total_examples,
            'recommendations': filtered_recs,
            'execution_steps': self._generate_execution_steps(filtered_recs),
        }

        if include_data_generation:
            plan['data_generation'] = self._generate_data_instructions(filtered_recs)

        return plan

    def _generate_execution_steps(
        self,
        recommendations: List[Dict[str, Any]]
    ) -> List[str]:
        """Generate step-by-step execution plan."""
        steps = [
            "1. Review gap analysis and recommendations",
            "2. Prepare training data:"
        ]

        for i, rec in enumerate(recommendations, 1):
            steps.append(f"   {chr(96+i)}. {rec['category']}: {rec['estimated_examples']} examples")

        steps.extend([
            "3. Generate or collect training examples",
            "4. Validate data quality (score > 60)",
            "5. Execute training session",
            "6. Run targeted benchmark tests",
            "7. Analyze results and compare to previous performance",
            "8. Iterate if needed"
        ])

        return steps

    def _generate_data_instructions(
        self,
        recommendations: List[Dict[str, Any]]
    ) -> Dict[str, Any]:
        """Generate data generation instructions."""
        instructions = {
            'method': 'synthetic_generation',
            'by_category': {}
        }

        for rec in recommendations:
            category = rec['category']
            instructions['by_category'][category] = {
                'num_examples': rec['estimated_examples'],
                'difficulty': 'mixed',
                'topics': rec['suggested_topics'],
                'sample_prompt': f"Generate financial advisory questions about {category}, covering topics like: {', '.join(rec['suggested_topics'][:3])}"
            }

        return instructions

    def generate_report(self) -> str:
        """
        Generate human-readable training recommendations report.

        Returns:
            Formatted report
        """
        if not self.recommendations:
            self.generate_recommendations()

        report = ["=" * 80]
        report.append("TRAINING RECOMMENDATIONS REPORT")
        report.append("=" * 80)
        report.append("")

        if not self.recommendations:
            report.append("No recommendations available. Performance data needed.")
            return "\n".join(report)

        # Summary
        total_examples = sum(r['estimated_examples'] for r in self.recommendations)
        report.append(f"Total Focus Areas: {len(self.recommendations)}")
        report.append(f"Total Training Examples Needed: {total_examples}")
        report.append("")

        # Detailed recommendations
        report.append("RECOMMENDED TRAINING PRIORITIES:")
        report.append("-" * 80)

        for i, rec in enumerate(self.recommendations, 1):
            priority_symbol = {
                'HIGH': 'πŸ”΄',
                'MEDIUM': '🟑',
                'LOW': '🟒'
            }.get(rec['priority'], 'βšͺ')

            report.append(f"\n{i}. {priority_symbol} {rec['category']} - Priority: {rec['priority']}")
            report.append(f"   Current Score: {rec['current_score']:.1f}%")
            report.append(f"   Trend: {rec['trend']}")
            report.append(f"   Training Intensity: {rec['intensity']}")
            report.append(f"   Recommended Examples: {rec['estimated_examples']}")
            report.append(f"   Expected Improvement: {rec['expected_improvement']}")
            report.append("")
            report.append("   Action Items:")
            for item in rec['action_items']:
                report.append(f"     β€’ {item}")
            report.append("")
            report.append("   Suggested Topics:")
            for topic in rec['suggested_topics'][:5]:  # Top 5 topics
                report.append(f"     - {topic}")

        report.append("")
        report.append("=" * 80)
        report.append("NEXT STEPS:")
        report.append("")
        report.append("1. Generate training data for priority categories")
        report.append("2. Focus on weak/declining areas first")
        report.append("3. Use diverse examples covering suggested topics")
        report.append("4. Run targeted tests after training")
        report.append("5. Track improvement and adjust strategy")
        report.append("=" * 80)

        return "\n".join(report)

    def save_recommendations(self, filepath: str):
        """
        Save recommendations to JSON file.

        Args:
            filepath: Output file path
        """
        if not self.recommendations:
            self.generate_recommendations()

        Path(filepath).parent.mkdir(parents=True, exist_ok=True)

        data = {
            'recommendations': self.recommendations,
            'training_plan': self.create_training_plan(),
            'report': self.generate_report()
        }

        with open(filepath, 'w', encoding='utf-8') as f:
            json.dump(data, f, indent=2, ensure_ascii=False)

        print(f"Recommendations saved to: {filepath}")

    def load_recommendations(self, filepath: str):
        """
        Load recommendations from JSON file.

        Args:
            filepath: Input file path
        """
        with open(filepath, 'r', encoding='utf-8') as f:
            data = json.load(f)

        self.recommendations = data.get('recommendations', [])

    def get_quick_wins(self) -> List[Dict[str, Any]]:
        """
        Identify quick wins - categories that can improve quickly.

        Returns:
            List of quick win opportunities
        """
        if not self.recommendations:
            self.generate_recommendations()

        # Quick wins: moderate performance, not too many examples needed
        quick_wins = [
            rec for rec in self.recommendations
            if 50 <= rec['current_score'] < 70 and rec['estimated_examples'] <= 50
        ]

        return quick_wins

    def prioritize_by_impact(self) -> List[Dict[str, Any]]:
        """
        Sort recommendations by expected impact.

        Returns:
            Recommendations sorted by impact
        """
        if not self.recommendations:
            self.generate_recommendations()

        # Calculate impact score (combination of priority and potential improvement)
        def impact_score(rec):
            priority_weight = {'HIGH': 3, 'MEDIUM': 2, 'LOW': 1}
            improvement_potential = 100 - rec['current_score']
            return priority_weight.get(rec['priority'], 1) * improvement_potential

        sorted_recs = sorted(
            self.recommendations,
            key=impact_score,
            reverse=True
        )

        return sorted_recs