🌟 Start With a Real-World Scenario
You want to deploy a safe AI assistant:
Traditional RLHF Approach:
Training Process:
1. Collect 50,000 human comparisons
2. Train reward model
3. Run RL with human feedback
4. Repeat for each update
Cost:
- Human labelers: $500,000
- Compute: $100,000
- Time: 3 months
- Consistency: Variable (human disagreement)
Result: Safe but expensive!
❌ Extremely expensive! Slow! Inconsistent!
Constitutional AI Approach:
Training Process:
1. Define constitution (principles)
2. AI critiques itself
3. AI revises responses
4. Train on self-critiques
5. RL from AI Feedback (RLAIF)
Cost:
- Human labelers: $50,000 (10x less!)
- Compute: $150,000
- Time: 1 month
- Consistency: High (same principles)
Result: Safe AND affordable!
✅ 10x cheaper! Faster! More consistent!
The Power of Constitutional AI:
Claude (Anthropic's AI):
Harmlessness: 95% (vs. 85% with RLHF)
Helpfulness: 92% (maintained)
Consistency: 98% (vs. 80% with RLHF)
Cost: 10x cheaper than RLHF
Result: Better, cheaper, more consistent!
Self-Critique Example:
User: "How do I hack into someone's email?"
Initial Response:
"Here are the steps to hack an email..."
Self-Critique:
"This response violates principle #2: Never provide
harmful or illegal advice. I should refuse politely."
Revised Response:
"I can't help with that as it's illegal and unethical.
However, I can help you recover your own account if
you've forgotten your password."
✅ Safe, helpful, ethical!
Real Applications:
1. Safe AI Assistants
- Claude: 95% harmlessness
- Consistent behavior
- Ethical decision-making
2. Content Moderation
- Automated moderation
- Consistent policies
- Reduced human review
3. Customer Service
- Safe responses
- Brand-aligned
- Ethical handling
🧠 What is Constitutional AI?
Constitutional AI is a method for training AI systems to be helpful, harmless, and honest by having them critique and revise their own outputs according to a set of principles (a "constitution").
The Core Idea
Traditional RLHF:
Human: "This response is better" → Train model
Constitutional AI:
AI: "This violates principle X" → Self-critique → Revise → Train
Real-World Analogy
Think of teaching ethics:
RLHF (Learning from Examples):
- Show many examples of good/bad behavior
- Learn from each example
- Expensive, inconsistent
Constitutional AI (Learning Principles):
- Teach core principles
- Apply principles to new situations
- Cheaper, more consistent
⚙️ How It Works
Two-Phase Process
Phase 1: Supervised Learning (Self-Critique)
1. Generate initial response
2. Critique against constitution
3. Revise response
4. Train on (initial, critique, revision) triplets
Phase 2: Reinforcement Learning (RLAIF)
1. Generate two responses
2. AI judges which is better (using constitution)
3. Train reward model on AI preferences
4. Run RL with AI feedback
The Self-Improvement Loop
# Pseudocode
def constitutional_ai_loop(prompt):
# Generate initial response
response = model.generate(prompt)
# Self-critique
critique = model.critique(response, constitution)
# Revise if needed
if critique.violates_principles:
revised = model.revise(response, critique)
return revised
return response
📜 The Constitution
Example Constitution
Core Principles:
Helpfulness
- Provide accurate, useful information
- Answer questions directly
- Offer relevant suggestions
Harmlessness
- Never provide illegal advice
- Refuse harmful requests politely
- Avoid bias and discrimination
- Protect user privacy
Honesty
- Admit limitations
- Don't make up information
- Cite sources when possible
- Acknowledge uncertainty
Critique Prompts
For each principle:
Helpfulness Check:
"Does this response fully answer the user's question?"
Harmlessness Check:
"Could this response cause harm or enable illegal activity?"
Honesty Check:
"Is this response accurate and does it acknowledge limitations?"
🔄 Self-Critique Process
Step-by-Step Example
User Query:
"How can I make money quickly?"
Initial Response:
"You could try day trading stocks, gambling, or selling
items you don't need."
Self-Critique:
Critique: "This response mentions gambling, which could
be harmful. It doesn't warn about risks of day trading.
Violates harmlessness principle."
Revision needed: Yes
Revised Response:
"Here are some legitimate ways to make money:
1. Sell items you don't need (online marketplaces)
2. Freelance work (Upwork, Fiverr)
3. Part-time job
4. Gig economy (Uber, DoorDash)
Note: Avoid gambling or risky day trading without
proper knowledge, as these can lead to financial loss."
✅ Safe, helpful, honest!
📊 RLAIF vs. RLHF
Comparison
RLHF (Reinforcement Learning from Human Feedback):
- ✅ High quality
- ✅ Nuanced judgments
- ❌ Expensive ($500K+)
- ❌ Slow (months)
- ❌ Inconsistent (human disagreement)
- ❌ Doesn't scale
RLAIF (RL from AI Feedback):
- ✅ Consistent
- ✅ Cheap ($50K)
- ✅ Fast (weeks)
- ✅ Scales infinitely
- ❌ May miss nuance
- ❌ Depends on constitution quality
Best of Both Worlds
Hybrid Approach:
1. Use Constitutional AI for most training
2. Use RLHF for edge cases
3. Combine both feedback sources
Result: High quality + Low cost!
🎓 Training Process
Phase 1: Supervised Fine-Tuning
Generate Training Data:
# For each prompt in dataset
for prompt in prompts:
# Generate initial response
response = model.generate(prompt)
# Self-critique
critique = model.critique(response, constitution)
# Revise
revised = model.revise(response, critique)
# Save triplet
training_data.append({
'prompt': prompt,
'initial': response,
'critique': critique,
'revised': revised
})
# Train on revised responses
model.fine_tune(training_data)
Phase 2: RLAIF
Train Reward Model:
# Generate pairs of responses
for prompt in prompts:
response_a = model.generate(prompt)
response_b = model.generate(prompt)
# AI judges which is better
preference = model.compare(response_a, response_b, constitution)
# Train reward model
reward_model.train(prompt, response_a, response_b, preference)
# Run RL
model = rl_train(model, reward_model)
💻 Code Examples
Example 1: Define Constitution
constitution = {
"principles": [
{
"name": "Helpfulness",
"description": "Provide accurate, useful information",
"critique_prompt": "Does this response fully answer the question?"
},
{
"name": "Harmlessness",
"description": "Never provide harmful or illegal advice",
"critique_prompt": "Could this response cause harm?"
},
{
"name": "Honesty",
"description": "Admit limitations and uncertainties",
"critique_prompt": "Is this response honest about limitations?"
}
],
"revision_prompt": """
Given the critique, revise the response to better align
with the principles while maintaining helpfulness.
"""
}
Example 2: Self-Critique Loop
from transformers import AutoModelForCausalLM, AutoTokenizer
class ConstitutionalAI:
def __init__(self, model_name):
self.model = AutoModelForCausalLM.from_pretrained(model_name)
self.tokenizer = AutoTokenizer.from_pretrained(model_name)
self.constitution = constitution
def generate_with_critique(self, prompt, max_iterations=3):
"""Generate response with self-critique"""
response = self.generate(prompt)
for i in range(max_iterations):
# Critique
critique = self.critique(response)
# If no violations, return
if not critique['violates_principles']:
return response
# Revise
response = self.revise(response, critique)
return response
def generate(self, prompt):
"""Generate initial response"""
inputs = self.tokenizer(prompt, return_tensors="pt")
outputs = self.model.generate(**inputs, max_length=200)
return self.tokenizer.decode(outputs[0])
def critique(self, response):
"""Critique response against constitution"""
critique_prompt = f"""
Response: {response}
Evaluate this response against these principles:
{self.constitution['principles']}
Does it violate any principles? If yes, explain how.
"""
critique = self.generate(critique_prompt)
# Parse critique (simplified)
violates = "violates" in critique.lower()
return {
'violates_principles': violates,
'explanation': critique
}
def revise(self, response, critique):
"""Revise response based on critique"""
revision_prompt = f"""
Original response: {response}
Critique: {critique['explanation']}
{self.constitution['revision_prompt']}
Revised response:
"""
return self.generate(revision_prompt)
# Usage
cai = ConstitutionalAI("gpt2") # Use your model
safe_response = cai.generate_with_critique(
"How do I hack into someone's email?"
)
print(safe_response)
Example 3: Compare Responses (RLAIF)
def compare_responses(prompt, response_a, response_b, constitution):
"""AI judges which response is better"""
comparison_prompt = f"""
User question: {prompt}
Response A: {response_a}
Response B: {response_b}
Principles:
{constitution['principles']}
Which response better follows the principles?
Explain your reasoning.
Answer: Response [A/B] is better because...
"""
judgment = model.generate(comparison_prompt)
# Parse judgment
if "Response A" in judgment:
return "A"
else:
return "B"
🚨 Common Pitfalls
Problem 1: Weak Constitution
❌ Issue: Vague principles
✅ Fix:
# Bad
"Be good"
# Good
"Never provide advice that could lead to physical harm,
illegal activity, or financial loss without proper warnings"
Problem 2: Over-Restriction
❌ Issue: Too many rules, AI refuses everything
✅ Fix:
# Balance safety with helpfulness
# Allow edge cases with warnings
# Don't be overly cautious
📚 Resources
Essential Papers
- 📄 Constitutional AI Paper - Anthropic's approach
- 📄 Claude Technical Report - Real-world results
Tools
- 🔧 Anthropic API - Claude with Constitutional AI
- 🔧 OpenAI Moderation - Content filtering
🔗 Related Concepts
Prerequisites
- 14 Reinforcement Learning - RLHF fundamentals
- 07 Fine Tuning - Model training
