Implement self-improving agent with meta-learning:
Learning Loop:
- Try strategy → Observe outcome → Update beliefs → Try better strategy
Methods:
record_experience(task, actions, outcome, metadata): Store episodeextract_patterns(n_recent): Find what works/fails- Success patterns: common features of successes
- Failure patterns: warning signs
update_strategies(): Adjust strategy scoresselect_strategy(task, strategies): Pick best for task- Use learned scores + exploration
adapt_prompt(base, task_type): Customize promptsget_improvement_report(): Learning stats
Pattern Format:
{'features': {...}, 'frequency': ..., 'success_rate': ...}
Examples
Example 1:
Input:
agent = SelfImprovingAgent(); agent.record_experience('task1', ['a1'], 'success', {}); len(agent.experience_buffer)Output:
1Explanation: Experience recorded in buffer
Starter Code
class SelfImprovingAgent:
"""
Agent that learns from experience and improves over time.
"""
def __init__(self):
self.experience_buffer = []
self.success_patterns = {}
self.failure_patterns = {}
self.strategy_scores = {}
def record_experience(self, task, actions, outcome, metadata):
"""Record task execution experience"""
# Your implementation here
pass
def extract_patterns(self, n_recent=100):
"""Extract success/failure patterns from recent experiences"""
# Your implementation here
pass
def update_strategies(self):
"""Update strategy preferences based on outcomes"""
# Your implementation here
pass
def select_strategy(self, task, available_strategies):
"""
Select best strategy for task based on learned preferences.
"""
# Your implementation here
pass
def adapt_prompt(self, base_prompt, task_type):
"""Adapt prompt based on learned patterns for task type"""
# Your implementation here
pass
def get_improvement_report(self):
"""Report on learning progress"""
# Your implementation here
passPython3
ReadyLines: 1Characters: 0
Ready