"""
Pattern 069: Self-Play & Self-Improvement
Description:
Self-Play & Self-Improvement enables agents to autonomously improve their capabilities
by competing or collaborating with themselves. Inspired by techniques like AlphaGo's
self-play training, this pattern allows agents to generate their own training data,
discover new strategies, and continuously refine their performance without external
supervision. The agent plays both sides of a game, problem, or scenario, learning
from each iteration to progressively improve.
This pattern is particularly powerful for domains where supervised training data is
scarce, expensive, or impossible to obtain. Through self-play, agents can explore
vast strategy spaces, discover novel solutions, and achieve superhuman performance
by learning from billions of self-generated experiences.
Components:
1. Strategy Generator: Creates candidate strategies/policies
2. Self-Play Engine: Simulates interactions with itself
3. Performance Evaluator: Measures strategy effectiveness
4. Curriculum Generator: Creates progressively harder challenges
5. Strategy Selector: Chooses best strategies to keep
6. Meta-Learner: Learns how to improve the learning process
7. Checkpoint Manager: Stores and manages strategy versions
Architecture:
```
Initial Strategy
↓
┌─────────────────────────┐
│ Self-Play Loop │
│ │
│ Strategy A vs Strategy B│
│ ↓ │
│ Play Game/Task │
│ ↓ │
│ Evaluate Performance │
│ ↓ │
│ Update Strategies │
│ ↓ │
│ Generate Curriculum │
│ ↓ │
│ Select Best │
└─────────────────────────┘
↓ (iterate)
Improved Strategy
```
Use Cases:
- Game AI development (chess, Go, poker)
- Negotiation and bargaining agents
- Creative content generation
- Code optimization and debugging
- Theorem proving and mathematical reasoning
- Strategic planning and decision-making
Advantages:
- No need for labeled training data
- Discovers novel strategies humans haven't found
- Continuously improves without external input
- Adapts to changing environments
- Scales with compute rather than data
- Explores diverse solution spaces
LangChain Implementation:
Uses ChatOpenAI for strategy generation and evaluation. Demonstrates
self-play loops, performance tracking, curriculum generation, and
iterative improvement through competition.
"""
import os
from typing import List, Dict, Any, Optional, Tuple
from dataclasses import dataclass, field
from enum import Enum
import time
import random
from collections import deque
from dotenv import load_dotenv
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
from langchain_openai import ChatOpenAI
load_dotenv()
class TaskType(Enum):
"""Types of self-play tasks."""
GAME = "game"
PROBLEM_SOLVING = "problem_solving"
NEGOTIATION = "negotiation"
CREATIVE = "creative"
OPTIMIZATION = "optimization"
class StrategyQuality(Enum):
"""Quality levels of strategies."""
NOVICE = "novice"
INTERMEDIATE = "intermediate"
ADVANCED = "advanced"
EXPERT = "expert"
SUPERHUMAN = "superhuman"
@dataclass
class Strategy:
"""Represents a strategy/policy."""
strategy_id: str
name: str
description: str
version: int
quality: StrategyQuality
win_rate: float = 0.0
games_played: int = 0
elo_rating: float = 1000.0 # Elo rating system
created_at: float = field(default_factory=time.time)
@dataclass
class GameResult:
"""Result of a self-play game."""
game_id: str
strategy_a: Strategy
strategy_b: Strategy
winner: Optional[str] # strategy_id or None for draw
score_a: float
score_b: float
moves: List[str]
insights: List[str] = field(default_factory=list)
@dataclass
class TrainingExample:
"""Training example generated from self-play."""
example_id: str
situation: str
action: str
outcome: str
reward: float
strategy_used: str
@dataclass
class CurriculumLevel:
"""Difficulty level in curriculum."""
level: int
name: str
description: str
complexity: float # 0.0 to 1.0
prerequisite_levels: List[int] = field(default_factory=list)
@dataclass
class ImprovementMetrics:
"""Metrics tracking improvement over time."""
iteration: int
best_strategy_quality: StrategyQuality
average_elo: float
total_games: int
novel_strategies_found: int
improvement_rate: float # % improvement per iteration
class SelfPlayAgent:
"""
Agent that improves through self-play and self-generated curriculum.
"""
def __init__(
self,
task_type: TaskType = TaskType.PROBLEM_SOLVING,
initial_strategies: int = 3,
elo_k_factor: int = 32
):
"""
Initialize self-play agent.
Args:
task_type: Type of task for self-play
initial_strategies: Number of initial strategies to generate
elo_k_factor: K-factor for Elo rating updates
"""
self.task_type = task_type
self.elo_k_factor = elo_k_factor
# LLMs for different tasks
self.strategy_generator = ChatOpenAI(temperature=0.8, model="gpt-3.5-turbo")
self.game_master = ChatOpenAI(temperature=0.5, model="gpt-3.5-turbo")
self.evaluator = ChatOpenAI(temperature=0.2, model="gpt-3.5-turbo")
self.meta_learner = ChatOpenAI(temperature=0.6, model="gpt-3.5-turbo")
self.parser = StrOutputParser()
# Strategy pool
self.strategies: Dict[str, Strategy] = {}
self.strategy_history: List[Strategy] = []
# Training data
self.training_examples: deque = deque(maxlen=1000)
self.game_history: List[GameResult] = []
# Curriculum
self.curriculum_levels: List[CurriculumLevel] = []
self.current_level = 0
# Metrics
self.improvement_history: List[ImprovementMetrics] = []
self.iteration = 0
# Initialize
self._initialize_strategies(initial_strategies)
self._initialize_curriculum()
def _initialize_strategies(self, count: int):
"""Initialize starting strategies."""
print(f"Initializing {count} starting strategies...")
for i in range(count):
strategy = Strategy(
strategy_id=f"strat_{i+1}",
name=f"Strategy {i+1}",
description=f"Initial strategy variant {i+1}",
version=1,
quality=StrategyQuality.NOVICE
)
self.strategies[strategy.strategy_id] = strategy
print(f" Created: {strategy.name}")
def _initialize_curriculum(self):
"""Initialize learning curriculum."""
self.curriculum_levels = [
CurriculumLevel(1, "Basics", "Learn fundamental concepts", 0.2),
CurriculumLevel(2, "Intermediate", "Apply concepts in simple scenarios", 0.4, [1]),
CurriculumLevel(3, "Advanced", "Handle complex situations", 0.6, [2]),
CurriculumLevel(4, "Expert", "Master edge cases and optimization", 0.8, [3]),
CurriculumLevel(5, "Creative", "Discover novel strategies", 1.0, [4]),
]
def generate_new_strategy(self, parent_strategies: List[Strategy]) -> Strategy:
"""
Generate new strategy by learning from existing ones.
Args:
parent_strategies: Strategies to learn from
Returns:
New improved strategy
"""
prompt = ChatPromptTemplate.from_messages([
("system", """You are a strategy generator. Create a new improved strategy by learning
from existing strategies. Combine their strengths and address their weaknesses.
Task Type: {task_type}
Current Level: {level}
Create a strategy with:
1. Name
2. Description (key principles and tactics)
3. Novel insights or improvements
Format:
Name: [strategy name]
Description: [detailed description]
Insights: [key innovations]"""),
("user", """Parent Strategies:
{parent_strategies}
Generate an improved strategy:""")
])
chain = prompt | self.strategy_generator | self.parser
try:
parent_desc = "\n".join([
f"- {s.name} (Win Rate: {s.win_rate:.2f}, Games: {s.games_played}): {s.description}"
for s in parent_strategies
])
level = self.curriculum_levels[min(self.current_level, len(self.curriculum_levels)-1)]
result = chain.invoke({
"task_type": self.task_type.value,
"level": level.name,
"parent_strategies": parent_desc
})
# Parse result
name = f"Strategy Gen{len(self.strategies)+1}"
description = ""
for line in result.split('\n'):
if line.startswith("Name:"):
name = line.replace("Name:", "").strip()
elif line.startswith("Description:"):
desc_lines = []
for next_line in result.split('\n')[result.split('\n').index(line):]:
if next_line.startswith("Insights:"):
break
desc_lines.append(next_line.replace("Description:", "").strip())
description = " ".join([d for d in desc_lines if d])
if not description:
description = result.split('\n')[0]
# Determine quality based on parent strategies
avg_quality_idx = sum([list(StrategyQuality).index(s.quality) for s in parent_strategies]) // len(parent_strategies)
quality = list(StrategyQuality)[min(avg_quality_idx + 1, len(StrategyQuality) - 1)]
strategy = Strategy(
strategy_id=f"strat_{len(self.strategies)+1}",
name=name,
description=description,
version=1,
quality=quality,
elo_rating=sum(s.elo_rating for s in parent_strategies) / len(parent_strategies)
)
return strategy
except Exception as e:
print(f"Error generating strategy: {e}")
# Fallback: create simple variant
return Strategy(
strategy_id=f"strat_{len(self.strategies)+1}",
name=f"Strategy Variant {len(self.strategies)+1}",
description="Evolved strategy",
version=1,
quality=StrategyQuality.INTERMEDIATE
)
def play_game(
self,
strategy_a: Strategy,
strategy_b: Strategy,
scenario: str
) -> GameResult:
"""
Simulate a game between two strategies.
Args:
strategy_a: First strategy
strategy_b: Second strategy
scenario: Game scenario/problem
Returns:
Game result with winner and insights
"""
prompt = ChatPromptTemplate.from_messages([
("system", """You are a game master. Simulate a match between two strategies.
Strategy A: {strategy_a_name}
{strategy_a_desc}
Strategy B: {strategy_b_name}
{strategy_b_desc}
Scenario: {scenario}
Simulate the match and provide:
Winner: [A/B/Draw]
Score A: [0.0-1.0]
Score B: [0.0-1.0]
Key Moves: [list of 3-5 key moves/decisions]
Insights: [what made the difference, what was learned]
Format each on a new line."""),
("user", "Simulate the match:")
])
chain = prompt | self.game_master | self.parser
try:
result = chain.invoke({
"strategy_a_name": strategy_a.name,
"strategy_a_desc": strategy_a.description,
"strategy_b_name": strategy_b.name,
"strategy_b_desc": strategy_b.description,
"scenario": scenario
})
# Parse result
winner = None
score_a = 0.5
score_b = 0.5
moves = []
insights = []
for line in result.split('\n'):
line = line.strip()
if line.startswith("Winner:"):
winner_str = line.replace("Winner:", "").strip().upper()
if 'A' in winner_str and 'DRAW' not in winner_str:
winner = strategy_a.strategy_id
score_a = 1.0
score_b = 0.0
elif 'B' in winner_str:
winner = strategy_b.strategy_id
score_a = 0.0
score_b = 1.0
elif line.startswith("Score A:"):
try:
score_a = float(line.replace("Score A:", "").strip())
except:
pass
elif line.startswith("Score B:"):
try:
score_b = float(line.replace("Score B:", "").strip())
except:
pass
elif line.startswith("Key Moves:"):
moves_str = line.replace("Key Moves:", "").strip()
moves = [m.strip() for m in moves_str.split(',')]
elif line.startswith("Insights:"):
insights_str = line.replace("Insights:", "").strip()
insights = [i.strip() for i in insights_str.split(',')]
game_result = GameResult(
game_id=f"game_{len(self.game_history)+1}",
strategy_a=strategy_a,
strategy_b=strategy_b,
winner=winner,
score_a=score_a,
score_b=score_b,
moves=moves if moves else ["Move 1", "Move 2", "Move 3"],
insights=insights if insights else ["Strategies tested"]
)
return game_result
except Exception as e:
print(f"Error playing game: {e}")
# Fallback: random winner
winner = random.choice([strategy_a.strategy_id, strategy_b.strategy_id, None])
return GameResult(
game_id=f"game_{len(self.game_history)+1}",
strategy_a=strategy_a,
strategy_b=strategy_b,
winner=winner,
score_a=0.5 if winner is None else (1.0 if winner == strategy_a.strategy_id else 0.0),
score_b=0.5 if winner is None else (1.0 if winner == strategy_b.strategy_id else 0.0),
moves=["Simulated game"],
insights=["Game completed"]
)
def update_elo_ratings(self, game_result: GameResult):
"""
Update Elo ratings based on game result.
Args:
game_result: Result of the game
"""
strategy_a = game_result.strategy_a
strategy_b = game_result.strategy_b
# Expected scores
expected_a = 1.0 / (1.0 + 10 ** ((strategy_b.elo_rating - strategy_a.elo_rating) / 400))
expected_b = 1.0 / (1.0 + 10 ** ((strategy_a.elo_rating - strategy_b.elo_rating) / 400))
# Actual scores
actual_a = game_result.score_a
actual_b = game_result.score_b
# Update ratings
strategy_a.elo_rating += self.elo_k_factor * (actual_a - expected_a)
strategy_b.elo_rating += self.elo_k_factor * (actual_b - expected_b)
# Update stats
strategy_a.games_played += 1
strategy_b.games_played += 1
if game_result.winner == strategy_a.strategy_id:
strategy_a.win_rate = (strategy_a.win_rate * (strategy_a.games_played - 1) + 1.0) / strategy_a.games_played
else:
strategy_a.win_rate = (strategy_a.win_rate * (strategy_a.games_played - 1)) / strategy_a.games_played
if game_result.winner == strategy_b.strategy_id:
strategy_b.win_rate = (strategy_b.win_rate * (strategy_b.games_played - 1) + 1.0) / strategy_b.games_played
else:
strategy_b.win_rate = (strategy_b.win_rate * (strategy_b.games_played - 1)) / strategy_b.games_played
def generate_curriculum_scenario(self, level: CurriculumLevel) -> str:
"""
Generate training scenario for current curriculum level.
Args:
level: Curriculum level
Returns:
Scenario description
"""
scenarios = {
1: "Solve a basic optimization problem with clear constraints.",
2: "Handle a scenario with competing objectives and trade-offs.",
3: "Manage complex multi-step problem with hidden information.",
4: "Optimize performance in adversarial environment with uncertainty.",
5: "Discover creative solution to novel problem never seen before.",
}
return scenarios.get(level.level, "General problem-solving scenario")
def self_play_iteration(self, num_games: int = 5) -> ImprovementMetrics:
"""
Run one iteration of self-play training.
Args:
num_games: Number of games to play this iteration
Returns:
Improvement metrics for this iteration
"""
self.iteration += 1
print(f"\n{'='*60}")
print(f"Self-Play Iteration {self.iteration}")
print(f"{'='*60}")
# Get current curriculum level
level = self.curriculum_levels[min(self.current_level, len(self.curriculum_levels)-1)]
print(f"Curriculum Level: {level.name} (Complexity: {level.complexity})")
# Play games
print(f"\nPlaying {num_games} games...")
iteration_games = []
strategies_list = list(self.strategies.values())
for i in range(num_games):
# Select two random strategies
if len(strategies_list) 10:
worst_strategy = min(self.strategies.values(), key=lambda s: s.elo_rating)
del self.strategies[worst_strategy.strategy_id]
print(f" Pruned: {worst_strategy.name}")
# Update curriculum if ready
if self.iteration % 5 == 0 and self.current_level 0:
prev_avg_elo = self.improvement_history[-1].average_elo
improvement_rate = ((avg_elo - prev_avg_elo) / prev_avg_elo) * 100 if prev_avg_elo > 0 else 0.0
metrics = ImprovementMetrics(
iteration=self.iteration,
best_strategy_quality=best_strategy.quality,
average_elo=avg_elo,
total_games=len(self.game_history),
novel_strategies_found=len(self.strategy_history),
improvement_rate=improvement_rate
)
self.improvement_history.append(metrics)
print(f"\n--- Iteration {self.iteration} Metrics ---")
print(f"Best Strategy: {best_strategy.name} (Elo: {best_strategy.elo_rating:.0f})")
print(f"Average Elo: {avg_elo:.0f}")
print(f"Total Strategies: {len(self.strategies)}")
print(f"Improvement Rate: {improvement_rate:+.2f}%")
return metrics
def train(self, iterations: int = 10, games_per_iteration: int = 5):
"""
Run full self-play training.
Args:
iterations: Number of training iterations
games_per_iteration: Games to play per iteration
"""
print("="*60)
print("STARTING SELF-PLAY TRAINING")
print("="*60)
print(f"Task Type: {self.task_type.value}")
print(f"Iterations: {iterations}")
print(f"Games per Iteration: {games_per_iteration}")
for i in range(iterations):
self.self_play_iteration(games_per_iteration)
print("\n" + "="*60)
print("TRAINING COMPLETE")
print("="*60)
# Final summary
best = max(self.strategies.values(), key=lambda s: s.elo_rating)
print(f"\nBest Strategy: {best.name}")
print(f" Quality: {best.quality.value}")
print(f" Elo Rating: {best.elo_rating:.0f}")
print(f" Win Rate: {best.win_rate:.2%}")
print(f" Games Played: {best.games_played}")
print(f"\nTotal Games: {len(self.game_history)}")
print(f"Strategies Discovered: {len(self.strategy_history)}")
print(f"Current Pool Size: {len(self.strategies)}")
print(f"Final Curriculum Level: {self.curriculum_levels[self.current_level].name}")
def demonstrate_self_play():
"""Demonstrate Self-Play & Self-Improvement pattern."""
print("="*80)
print("SELF-PLAY & SELF-IMPROVEMENT - DEMONSTRATION")
print("="*80)
# Test 1: Problem-solving self-play
print("\n" + "="*80)
print("TEST 1: Problem-Solving Self-Play (Short Training)")
print("="*80)
agent1 = SelfPlayAgent(
task_type=TaskType.PROBLEM_SOLVING,
initial_strategies=3
)
agent1.train(iterations=5, games_per_iteration=3)
print("\n--- Strategy Evolution ---")
for i, strategy in enumerate(agent1.strategy_history[:5], 1):
print(f"{i}. {strategy.name}")
print(f" Quality: {strategy.quality.value}, Elo: {strategy.elo_rating:.0f}")
# Test 2: Track improvement metrics
print("\n" + "="*80)
print("TEST 2: Improvement Metrics Analysis")
print("="*80)
print("\nIteration-by-Iteration Improvement:")
print(f"{'Iter':<6} {'Avg Elo':<10} {'Best Quality':<15} {'Games':<8} {'Improvement':<12}")
print("-" * 65)
for metrics in agent1.improvement_history:
print(f"{metrics.iteration:<6} {metrics.average_elo:<10.0f} "
f"{metrics.best_strategy_quality.value:<15} {metrics.total_games:+10.2f}%")
# Test 3: Strategy comparison
print("\n" + "="*80)
print("TEST 3: Final Strategy Rankings")
print("="*80)
ranked_strategies = sorted(agent1.strategies.values(), key=lambda s: s.elo_rating, reverse=True)
print(f"\n{'Rank':<6} {'Strategy':<25} {'Elo':<8} {'Win Rate':<10} {'Games':<8}")
print("-" * 65)
for i, strategy in enumerate(ranked_strategies, 1):
print(f"{i:<6} {strategy.name:<25} {strategy.elo_rating:<8.0f} "
f"{strategy.win_rate:<10.1%} {strategy.games_played:<8}")
# Test 4: Game insights
print("\n" + "="*80)
print("TEST 4: Notable Games and Insights")
print("="*80)
print("\nSample Games:")
for game in agent1.game_history[-3:]:
print(f"\n{game.game_id}: {game.strategy_a.name} vs {game.strategy_b.name}")
winner_name = "Draw"
if game.winner == game.strategy_a.strategy_id:
winner_name = game.strategy_a.name
elif game.winner == game.strategy_b.strategy_id:
winner_name = game.strategy_b.name
print(f" Winner: {winner_name}")
print(f" Scores: {game.score_a:.2f} - {game.score_b:.2f}")
if game.insights:
print(f" Insights: {', '.join(game.insights[:2])}")
# Test 5: Different task type
print("\n" + "="*80)
print("TEST 5: Creative Task Self-Play")
print("="*80)
agent2 = SelfPlayAgent(
task_type=TaskType.CREATIVE,
initial_strategies=2
)
print("\nRunning creative task training...")
agent2.train(iterations=3, games_per_iteration=2)
print("\n--- Creative Strategies Evolved ---")
best_creative = max(agent2.strategies.values(), key=lambda s: s.elo_rating)
print(f"Best Strategy: {best_creative.name}")
print(f"Description: {best_creative.description[:200]}...")
# Summary
print("\n" + "="*80)
print("SUMMARY: Self-Play & Self-Improvement")
print("="*80)
print("""
The Self-Play & Self-Improvement pattern demonstrates autonomous learning:
Key Features Demonstrated:
1. Strategy Generation: Creates new strategies from successful parents
2. Self-Play Engine: Simulates competitions between strategies
3. Performance Evaluation: Uses Elo ratings to track strategy strength
4. Curriculum Learning: Progressively increases task difficulty
5. Strategy Evolution: Generates improved strategies over time
6. Strategy Pruning: Removes weak strategies to maintain quality pool
7. Improvement Tracking: Monitors learning progress with metrics
Benefits:
• No labeled training data required
• Discovers novel strategies through exploration
• Continuously improves without human supervision
• Adapts to changing task requirements
• Scales with computational resources
• Explores diverse solution spaces
• Learns from billions of self-generated examples
Use Cases:
• Game AI (chess, Go, poker, StarCraft)
• Negotiation and bargaining agents
• Creative content generation (writing, art, music)
• Code optimization and refactoring
• Mathematical theorem proving
• Strategic planning and decision-making
• Competitive debate and argumentation
• Resource allocation and scheduling
Comparison with Supervised Learning:
┌─────────────────────┬────────────────────┬─────────────────────┐
│ Aspect │ Supervised │ Self-Play │
├─────────────────────┼────────────────────┼─────────────────────┤
│ Training Data │ Labeled examples │ Self-generated │
│ Data Requirements │ Large labeled sets │ Minimal initial │
│ Novel Strategies │ Limited │ Unlimited discovery │
│ Adaptation │ Retraining needed │ Continuous │
│ Human Expertise │ Required │ Not required │
│ Scaling │ Data-limited │ Compute-limited │
│ Performance Ceiling │ Human-level │ Can exceed humans │
└─────────────────────┴────────────────────┴─────────────────────┘
Real-World Successes:
• AlphaGo: Defeated world champion through self-play
• AlphaZero: Mastered chess, shogi, and Go with no human data
• OpenAI Five: Achieved professional-level Dota 2 play
• GPT models: Improved through self-generated reasoning
• AlphaCode: Competitive programming through self-play
LangChain Implementation Notes:
• Multiple specialized LLMs for different roles
• Strategy generation through evolutionary approaches
• Game simulation using LLM as game master
• Elo rating system for objective performance measurement
• Curriculum learning with progressive difficulty
• Strategy pool management with pruning
• Comprehensive metrics tracking
Production Considerations:
• Implement efficient game/task simulation
• Add checkpointing for long training runs
• Implement distributed self-play across multiple workers
• Add exploration bonuses to encourage diversity
• Implement opponent sampling strategies (uniform, skill-based)
• Add meta-learning to improve learning efficiency
• Implement proper evaluation against external benchmarks
• Add human evaluation for strategy interpretability
• Monitor for strategy collapse or degenerate solutions
• Implement safety checks for deployed strategies
• Add transfer learning from related tasks
• Implement strategy distillation for deployment efficiency
Advanced Extensions:
• Multi-agent self-play with teams
• Hierarchical strategies with sub-strategies
• Transfer learning across different tasks
• Meta-self-play (learning how to self-play better)
• Curiosity-driven exploration bonuses
• Population-based training with diversity metrics
• Adversarial self-play for robustness
• Self-play with human-in-the-loop feedback
""")
if __name__ == "__main__":
demonstrate_self_play()
Agentic Design Pattern: Self Play
Pattern 069: Self Play – Line-by-Line Explanation
Overview
This document provides a detailed line-by-line explanation of the Self Play pattern implementation.
Pattern Description
Pattern 069: Self-Play & Self-Improvement
Description: Self-Play & Self-Improvement enables agents to autonomously improve their capabilities by competing or collaborating with themselves. Inspired by techniques like AlphaGo’s self-play training, this pattern allows agents to generate their own training data, discover new strategies, and continuously refine their performance without external supervision. The agent plays both sides of a game, problem, or scenario, learning from each iteration to progressively improve.
This pattern is particularly powerful for domains where supervised training data is
scarce, expensive, or impossible to obtain. Through self-play, agents can explore
vast strategy spaces, discover novel solutions, and achieve superhuman performance
by learning from billions of self-generated experiences.
Components: 1. Strategy Generator: Creates candidate strategies/policies 2. Self-Play Engine: Simulates interactions with itself 3. Performance Evaluator: Measures strategy effectiveness 4. Curriculum Generator: Creates progressively harder challenges 5. Strategy Selector: Chooses best strategies to keep 6. Meta-Learner: Learns how to improve the learning process 7. Checkpoint Manager: Stores and manages strategy versions
Architecture:
Initial Strategy
↓
┌─────────────────────────┐
│ Self-Play Loop │
│ │
│ Strategy A vs Strategy B│
│ ↓ │
│ Play Game/Task │
│ ↓ │
│ Evaluate Performance │
│ ↓ │
│ Update Strategies │
│ ↓ │
│ Generate Curriculum │
│ ↓ │
│ Select Best │
└─────────────────────────┘
↓ (iterate)
Improved Strategy
Use Cases: – Game AI development (chess, Go, poker) – Negotiation and bargaining agents – Creative content generation – Code optimization and debugging – Theorem proving and mathematical reasoning – Strategic planning and decision-making
Advantages: – No need for labeled training data – Discovers novel strategies humans haven’t found – Continuously improves without external input – Adapts to changing environments – Scales with compute rather than data – Explores diverse solution spaces
LangChain Implementation: Uses ChatOpenAI for strategy generation and evaluation. Demonstrates self-play loops, performance tracking, curriculum generation, and iterative improvement through competition.
File Statistics
- Total Lines: 817
- Classes: 8
- Functions: 1
- Constants: 0
Module Documentation
"""
Pattern 069: Self-Play & Self-Improvement
Description:
Self-Play & Self-Improvement enables agents to autonomously improve their capabilities
by competing or collaborating with themselves. Inspired by techniques like AlphaGo's
self-play training, this pattern allows agents to generate their own training data,
discover new strategies, and continuously refine their performance without external
supervision. The agent plays both sides of a game, problem, or scenario, learning
from each iteration to progressively improve.
This pattern is particularly powerful for domains where supervised training data is
scarce, expensive, or impossible to obtain. Through self-play, agents can explore
vast strategy spaces, discover novel solutions, and achieve superhuman performance
by learning from billions of self-generated experiences.
Components:
1. Strategy Generator: Creates candidate strategies/policies
2. Self-Play Engine: Simulates interactions with itself
3. Performance Evaluator: Measures strategy effectiveness
4. Curriculum Generator: Creates progressively harder challenges
5. Strategy Selector: Chooses best strategies to keep
6. Meta-Learner: Learns how to improve the learning process
7. Checkpoint Manager: Stores and manages strategy versions
Architecture:
```
Initial Strategy
↓
┌─────────────────────────┐
│ Self-Play Loop │
│ │
│ Strategy A vs Strategy B│
│ ↓ │
│ Play Game/Task │
│ ↓ │
│ Evaluate Performance │
│ ↓ │
│ Update Strategies │
│ ↓ │
│ Generate Curriculum │
│ ↓ │
│ Select Best │
└─────────────────────────┘
↓ (iterate)
Improved Strategy
```
Use Cases:
- Game AI development (chess, Go, poker)
- Negotiation and bargaining agents
- Creative content generation
- Code optimization and debugging
- Theorem proving and mathematical reasoning
- Strategic planning and decision-making
Advantages:
- No need for labeled training data
- Discovers novel strategies humans haven't found
- Continuously improves without external input
- Adapts to changing environments
- Scales with compute rather than data
- Explores diverse solution spaces
LangChain Implementation:
Uses ChatOpenAI for strategy generation and evaluation. Demonstrates
self-play loops, performance tracking, curriculum generation, and
iterative improvement through competition.
"""
Purpose: This module implements the Self Play pattern, providing a structured approach to agent design and implementation.
Import Statements
Standard Library Imports
import collections.deque
import dataclasses.dataclass
import dataclasses.field
import enum.Enum
import langchain_core.output_parsers.StrOutputParser
import langchain_core.prompts.ChatPromptTemplate
import os
import random
import time
import typing.Any
import typing.Dict
import typing.List
import typing.Optional
import typing.Tuple
Purpose: Core Python functionality:
– collections.deque: Specialized container datatypes
– dataclasses.dataclass: Simplified class definitions with @dataclass
– dataclasses.field: Simplified class definitions with @dataclass
– enum.Enum: Enumeration support
– langchain_core.output_parsers.StrOutputParser: Regular expression operations
– langchain_core.prompts.ChatPromptTemplate: Regular expression operations
– os: Environment variable access and system operations
– random: Random number generation
– time: Time-related functions
– typing.Any: Type hints for better code documentation (List, Dict, Any, Optional)
– typing.Dict: Type hints for better code documentation (List, Dict, Any, Optional)
– typing.List: Type hints for better code documentation (List, Dict, Any, Optional)
– typing.Optional: Type hints for better code documentation (List, Dict, Any, Optional)
– typing.Tuple: Type hints for better code documentation (List, Dict, Any, Optional)
LangChain Imports
from langchain_openai.ChatOpenAI
Purpose: LangChain framework components: – LLM Interface: Wrapper for OpenAI models – Chains: Composable building blocks for complex workflows
Other Imports
from dotenv.load_dotenv
Purpose: Additional dependencies for specialized functionality.
Classes
Class: TaskType
Lines 87-93
Inherits from: Enum
Purpose:
Types of self-play tasks.
Class: StrategyQuality
Lines 96-102
Inherits from: Enum
Purpose:
Quality levels of strategies.
Class: Strategy
Lines 106-116
Purpose:
Represents a strategy/policy.
Class: GameResult
Lines 120-129
Purpose:
Result of a self-play game.
Class: TrainingExample
Lines 133-140
Purpose:
Training example generated from self-play.
Class: CurriculumLevel
Lines 144-150
Purpose:
Difficulty level in curriculum.
Class: ImprovementMetrics
Lines 154-161
Purpose:
Metrics tracking improvement over time.
Class: SelfPlayAgent
Lines 164-636
Purpose:
Agent that improves through self-play and self-generated curriculum.
Methods:
__init__(self, task_type, initial_strategies, elo_k_factor)
Lines 169-212
Purpose:
Initialize self-play agent.
Args:
task_type: Type of task for self-play
initial_strategies: Number of initial strategies to generate
elo_k_factor: K-factor for Elo rating updates
Parameters:
– task_type: Task type
– initial_strategies: Initial strategies
– elo_k_factor: Elo k factor
_initialize_strategies(self, count)
Lines 214-227
Purpose:
Initialize starting strategies.
Parameters:
– count: Count
_initialize_curriculum(self)
Lines 229-237
Purpose:
Initialize learning curriculum.
Parameters:
generate_new_strategy(self, parent_strategies)
Lines 239-329
Purpose:
Generate new strategy by learning from existing ones.
Args:
parent_strategies: Strategies to learn from
Returns:
New improved strategy
Parameters:
– parent_strategies: Parent strategies
Returns: Strategy
play_game(self, strategy_a, strategy_b, scenario)
Lines 331-443
Purpose:
Simulate a game between two strategies.
Args:
strategy_a: First strategy
strategy_b: Second strategy
scenario: Game scenario/problem
Returns:
Game result with winner and insights
Parameters:
– strategy_a: Strategy a
– strategy_b: Strategy b
– scenario: Scenario
Returns: GameResult
update_elo_ratings(self, game_result)
Lines 445-479
Purpose:
Update Elo ratings based on game result.
Args:
game_result: Result of the game
Parameters:
– game_result: Game result
generate_curriculum_scenario(self, level)
Lines 481-498
Purpose:
Generate training scenario for current curriculum level.
Args:
level: Curriculum level
Returns:
Scenario description
Parameters:
– level: Level
Returns: str
self_play_iteration(self, num_games)
Lines 500-601
Purpose:
Run one iteration of self-play training.
Args:
num_games: Number of games to play this iteration
Returns:
Improvement metrics for this iteration
Parameters:
– num_games: Num games
Returns: ImprovementMetrics
train(self, iterations, games_per_iteration)
Lines 603-636
Purpose:
Run full self-play training.
Args:
iterations: Number of training iterations
games_per_iteration: Games to play per iteration
Parameters:
– iterations: Iterations
– games_per_iteration: Games per iteration
Functions
demonstrate_self_play()
Lines 639-812
Purpose:
Demonstrate Self-Play & Self-Improvement pattern.
Key Concepts Summary
Pattern Overview
The Self Play pattern provides a structured approach to building AI agents with specific capabilities and behaviors. This implementation leverages LangChain’s composable components to create a robust and maintainable solution.
Core Components
- Agent Class: Main implementation of the pattern logic
- Prompt Engineering: Carefully crafted prompts to guide LLM behavior
- Tool Integration: Optional tools for extended functionality
- Memory Management: State handling across interactions
- Output Parsing: Structured response processing
When to Use
✅ Good for: – Scenarios requiring this specific pattern’s strengths – Applications needing structured agent behavior – Systems where explainability is important – Production environments requiring reliability
❌ Not ideal for: – Simple single-prompt tasks – Scenarios requiring different pattern characteristics – Resource-constrained environments (if computationally intensive)
Best Practices Demonstrated
1. Environment Configuration
- ✅ Use
.envfiles for API keys - ✅ Never hardcode credentials
- ✅ Validate environment variables on startup
2. Error Handling
- ✅ Graceful degradation on failures
- ✅ Informative error messages
- ✅ Logging for debugging
3. Prompt Engineering
- ✅ Clear, specific instructions
- ✅ Structured output formats
- ✅ Few-shot examples when beneficial
4. Code Organization
- ✅ Separation of concerns (class-based design)
- ✅ Type hints for clarity
- ✅ Docstrings for documentation
5. Performance Optimization
- ✅ Appropriate temperature settings
- ✅ Token budget management
- ✅ Caching where applicable
LangChain Components Used
- ChatOpenAI: OpenAI model interface
- Chains: Sequential component composition
Why LangChain? – Composable abstractions for LLM applications – Rich ecosystem of integrations – Production-ready patterns – Active community and development
Execution Flow
1. Initialize agent with configuration
↓
2. Load environment and dependencies
↓
3. Prepare prompt with input data
↓
4. Invoke LLM through LangChain
↓
5. Parse and validate response
↓
6. Return formatted result
Key Decision Points: – Model selection (temperature, model type) – Prompt structure (few-shot vs zero-shot) – Error handling strategy – Output format validation
Performance Considerations
Computational Cost
- LLM Calls: Primary cost factor (based on tokens)
- Latency: Depends on model and input size
- Memory: Minimal for most patterns
Optimization Strategies
- Prompt Optimization: Minimize tokens while maintaining clarity
- Caching: Cache repeated queries/responses
- Model Selection: Use smaller models when appropriate
- Batch Processing: Group similar requests
- Streaming: For long responses, stream tokens
Cost Estimates (Approximate)
- GPT-3.5-turbo: ~$0.002 per request
- GPT-4: ~$0.03 per request
- GPT-4-turbo: ~$0.01 per request
Actual costs vary based on input/output token counts
Common Pitfalls to Avoid
1. Prompt Design Issues
❌ Vague or ambiguous instructions
✅ Clear, specific, testable prompts
2. Error Handling
❌ Ignoring API errors or rate limits
✅ Implement retry logic and graceful degradation
3. Token Management
❌ Exceeding model token limits
✅ Truncate inputs, chunk processing
4. Security
❌ Exposing API keys in code
✅ Use environment variables
5. Testing
❌ Only testing happy paths
✅ Test edge cases, failures, malformed inputs
6. Cost Management
❌ Unmonitored API usage
✅ Implement budgets, alerts, and monitoring
Further Improvements
Production Enhancements
- Monitoring: Add logging, metrics, tracing
- Validation: Input/output schema validation
- Rate Limiting: Implement request throttling
- Caching: Redis or in-memory cache for responses
- Async Support: Non-blocking operations for scalability
Feature Extensions
- Multi-Model Support: Switch between providers
- Fallback Models: Graceful degradation strategy
- Custom Tools: Domain-specific function calling
- Advanced Memory: Vector stores for long-term memory
- Evaluation: Automated testing and benchmarking
Code Quality
- Testing: Unit tests, integration tests
- Documentation: API docs, usage examples
- Type Safety: Stricter type hints
- Configuration: Externalized config files
Related Patterns
- Pattern 068
- Pattern 070
Pattern Families: – Reasoning Patterns (001-011) – Multi-Agent Patterns (012-019) – Tool Use Patterns (020-025) – Memory Patterns (026-031) – And many more…
Summary
The Self Play pattern provides a powerful approach to building AI agents with specific capabilities. This implementation demonstrates:
✅ Key Strengths: – Clean, maintainable code structure – LangChain integration for composability – Type hints and documentation – Production-ready patterns
🎯 Best Use Cases: – Applications requiring this pattern’s characteristics – Systems needing structured agent behavior – Production environments requiring reliability
📚 Learning Resources: – LangChain Documentation: https://python.langchain.com/ – Pattern Documentation: See related patterns – OpenAI API Reference: https://platform.openai.com/docs/
File: 069_self_play.py
Total Lines: 817
Pattern Category: AI Agent Design Pattern #069
Generated from actual implementation code
For educational and reference purposes
Leave a Reply