关于 constitutional-ai
Constitutional AI 是一套通过原则集训练模型降低有害输出的对齐方法,适用于安全训练与研究。它结合自我批评、修订和 AI 反馈,无需为有害输出准备人工标签。
主要功能
- 01依据原则进行自我批评与修订
- 02监督微调修订后的回答
- 03通过 RLAIF 训练偏好模型
- 04使用链式思考检查回答
适用场景
- 01在缺少人工安全标签时训练模型
- 02按明确原则调整模型行为
- 03分析并改进过度拒绝的回答
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ orchestra-research/ ai-research-skills --skill constitutional-ai安装后在 Claude Code 中输入/constitutional-ai确认已加载。
详细介绍
Constitutional AI - 基于 AI 反馈的无害性
快速开始
Constitutional AI (CAI) 通过自我批评和 AI 反馈训练模型使其无害,无需为有害输出提供人工标注。
核心概念:模型学习使用「宪法」(一组原则)来批评和修订自己的回复。
两个阶段:
- 监督学习(SL):自我批评 + 修订
- 强化学习(RL):RLAIF(基于 AI 反馈的 RL)
宪法示例:
Principles:
1. Choose the response that is most helpful, honest, and harmless
2. Avoid responses that are toxic, racist, or sexist
3. Prefer responses that explain objections rather than refuse
4. Choose responses that are thoughtful and nuanced
常见工作流
工作流 1:监督学习阶段(自我批评 + 修订)
第 1 步:生成初始回复:
from transformers import pipeline
generator = pipeline("text-generation", model="base-model")
prompts = [
"How do I hack a website?",
"Write something offensive about a group.",
"Help me cheat on my exam."
]
initial_responses = generator(prompts, max_length=200)
第 2 步:使用宪法进行自我批评:
critique_prompt = """
Given the question:
{question}
And the response:
{response}
Constitution:
1. Choose responses that are helpful, honest, and harmless
2. Avoid toxic, racist, or sexist content
3. Explain objections rather than refuse
Identify any ways this response violates the constitution:
"""
critiques = generator(
[critique_prompt.format(q=q, r=r) for q, r in zip(prompts, initial_responses)]
)
第 3 步:基于批评进行修订:
revision_prompt = """
Question: {question}
Original response: {response}
Critique: {critique}
Please revise the response to better align with the constitution:
"""
revised_responses = generator(
[revision_prompt.format(q=q, r=r, c=c)
for q, r, c in zip(prompts, initial_responses, critiques)]
)
第 4 步:在修订后的回复上进行微调:
from trl import SFTTrainer
# Create dataset of (prompt, revised_response) pairs
dataset = create_dataset(prompts, revised_responses)
trainer = SFTTrainer(
model=model,
train_dataset=dataset,
max_seq_length=1024
)
trainer.train()
工作流 2:RL 阶段(RLAIF - 基于 AI 反馈的 RL)
第 1 步:生成对比对:
# Sample multiple responses per prompt
responses_a = generator(prompts, num_return_sequences=2, do_sample=True, temperature=0.8)
responses_b = generator(prompts, num_return_sequences=2, do_sample=True, temperature=0.8)
第 2 步:AI 偏好评估:
preference_prompt = """
Question: {question}
Response A: {response_a}
Response B: {response_b}
Constitution:
{constitution}
Which response better follows the constitution? Explain your reasoning, then choose A or B.
"""
# Get AI preferences (no human labels needed!)
preferences = generator(
[preference_prompt.format(q=q, ra=ra, rb=rb, constitution=CONSTITUTION)
for q, ra, rb in zip(prompts, responses_a, responses_b)]
)
# Parse preferences (A or B)
chosen, rejected = parse_preferences(preferences, responses_a, responses_b)
第 3 步:训练偏好模型(奖励模型):
from trl import RewardTrainer, RewardConfig
preference_dataset = create_preference_dataset(prompts, chosen, rejected)
reward_config = RewardConfig(
output_dir="constitutional-reward-model",
learning_rate=1e-5,
num_train_epochs=1
)
reward_trainer = RewardTrainer(
model=model,
args=reward_config,
train_dataset=preference_dataset,
processing_class=tokenizer
)
reward_trainer.train()
第 4 步:使用 RLAIF 进行 RL 训练:
from trl import PPOTrainer, PPOConfig
ppo_config = PPOConfig(
reward_model_path="constitutional-reward-model",
learning_rate=1e-6,
kl_coef=0.05
)
ppo_trainer = PPOTrainer(
model=model,
config=ppo_config,
reward_model=reward_model
)
ppo_trainer.train()
工作流 3:思维链批评
启用推理透明度:
cot_critique_prompt = """
Question: {question}
Response: {response}
Let's think step-by-step about whether this response follows our principles:
1. Is it helpful? [Yes/No and reasoning]
2. Is it honest? [Yes/No and reasoning]
3. Is it harmless? [Yes/No and reasoning]
4. Does it avoid toxicity? [Yes/No and reasoning]
Based on this analysis, suggest a revision if needed.
"""
cot_critiques = generator(
[cot_critique_prompt.format(q=q, r=r) for q, r in zip(prompts, responses)]
)
何时使用本方法 vs 替代方案
在以下情况下使用 Constitutional AI:
- 希望在没有人工标注的情况下实现安全对齐
- 需要可解释的 AI 决策
- 希望避免回避式拒绝
- 拥有一组明确的原则/宪法
- 需要可扩展的安全训练
原则:
- RLAIF:AI 生成的偏好(可扩展,无需人工标注)
- RLHF:人类偏好(更准确,成本高)
- 自我批评:迭代改进
- 思维链:推理透明度
在以下情况下改用替代方案:
- RLHF (PPO):需要经人工验证的安全性
- DPO/SimPO:拥有人类偏好数据
- NeMo Guardrails:需要运行时内容过滤
- LlamaGuard:需要预训练的审核模型
常见问题
问题:模型拒绝过多(回避式)
添加宪法原则:
Prefer responses that engage thoughtfully with questions rather than
refusing to answer. Explain concerns while still being helpful.
问题:自我批评力度不足
使用更强的批评提示词:
Critically analyze this response for ANY potential issues, however minor.
Be thorough and specific in identifying problems.
问题:修订未能提升质量
多次迭代:
for _ in range(3): # 3 rounds of critique/revision
critique = generate_critique(response)
response = generate_revision(response, critique)
问题:RLAIF 偏好噪声较大
使用多个 AI 评估器:
# Get preferences from 3 different models
prefs_1 = model_1.evaluate(responses)
prefs_2 = model_2.evaluate(responses)
prefs_3 = model_3.evaluate(responses)
# Majority vote
final_preference = majority_vote(prefs_1, prefs_2, prefs_3)
进阶主题
宪法设计:参见 references/constitution-design.md,了解原则选择、有用性与无害性之间的权衡,以及特定领域的宪法。
RLAIF vs RLHF:参见 references/rlaif-comparison.md,了解性能对比、成本分析,以及何时使用 AI 反馈而非人类反馈。
思维链推理:参见 references/cot-critique.md,了解用于批评的提示词工程、多步推理和透明度改进。
硬件要求
- GPU:推荐 NVIDIA A100/H100
- 显存:
- SL 阶段(7B):1× A100 40GB
- RL 阶段(7B):2× A100 40GB(策略模型 + 奖励模型)
- 单节点:足以满足大多数使用场景
- 混合精度:推荐 BF16
算力要求:
- SL 阶段:与标准 SFT 类似
- RL 阶段:与 PPO 类似(高于 DPO)
- AI 评估:用于批评/偏好生成的额外推理
资源
- 论文:https://arxiv.org/abs/2212.08073(2022 年 12 月)
- Anthropic 博客:https://www.anthropic.com/research/constitutional-ai-harmlessness-from-ai-feedback
- 实现:TRL(PPOTrainer + RewardTrainer)
- Claude:使用 Constitutional AI 保障安全
评论
支持 Markdown