AI反馈对齐:用AI替代人类标注

3 阅读10分钟

Constitutional AI & RLAIF —— 让AI自己给自己打分


📚 目录

  1. RLHF的成本问题:人类标注太贵了
  2. Constitutional AI:AI自我批评和改进
  3. RLAIF:用AI替代人类打分
  4. 两者对比:何时用哪个
  5. 实战代码
  6. 总结

📌 前置概念:对齐方法的演进

从人类反馈到AI反馈

对齐方法的成本演进:

┌─────────────────────────────────────────┐
│ RLHF (2017-2022)                        │
│ 反馈来源:人类标注                       │
│ 成本:$50k-100k                         │
│ 问题:贵、慢、难扩展                     │
└─────────────────────────────────────────┘
            ↓ 降低成本
┌─────────────────────────────────────────┐
│ AI反馈方法 (2022-2024) ← 本文重点       │
│                                          │
│ 方法1: Constitutional AI (Anthropic)    │
│   - AI批评自己的回答                     │
│   - AI改进自己的回答                     │
│   - 成本:$1k-5k ✓                      │
│                                          │
│ 方法2: RLAIF (Google)                   │
│   - AI替代人类打分                       │
│   - 训练RM + PPO                        │
│   - 成本:$5k-10k ✓                     │
│                                          │
└─────────────────────────────────────────┘

核心思想

传统RLHF:
人类说"这个好,那个不好" → 模型学习

AI反馈方法:
AI说"这个好,那个不好" → 模型学习

关键洞察:
- AI已经足够聪明,可以判断回答质量
- 用强大的AI(如GPT-4)指导弱小的AI
- 成本降低10-100倍!

🤔 Part 1: RLHF的成本问题

1.1 人类标注到底有多贵?

成本分解:

训练一个7B对齐模型的人类标注成本:

1. 偏好数据标注
   ──────────────────────────────────
   需求:50k对偏好数据
   每对:2个回答需要人类选择

   时间:每对平均2分钟
   总时间:50k × 2分钟 = 1,667小时

   人力:按$30/小时标注员
   成本:1,667 × $30 = $50,000

2. 质量控制
   ──────────────────────────────────
   需要:多人交叉验证
   额外成本:$10,000

3. 数据清洗和处理
   ──────────────────────────────────
   工程成本:$5,000

总成本:$65,000

而且:
- 需要几周时间
- 难以扩展
- 质量参差不齐

1.2 瓶颈在哪?

三大瓶颈:

瓶颈1:速度慢
─────────────────────
人类标注员:
- 每小时标注30对数据
- 50k对需要1,667小时
- 按8小时工作日 = 208天!

AI标注:
- 每小时标注1000对
- 50k对只需50小时
- 2天就完成 ✓


瓶颈2:成本高
─────────────────────
人类标注:
- $30/小时 × 1,667小时 = $50k

AI标注(GPT-4):
- $0.03/1k tokens
- 假设每对500 tokens
- 50k对 × 500 tokens × $0.03/1k = $750
- 便宜67倍!✓


瓶颈3:难扩展
─────────────────────
人类标注:
- 需要招募和培训标注员
- 质量参差不齐
- 难以快速扩展

AI标注:
- API调用即可
- 质量一致
- 无限扩展 ✓

1.3 能用AI替代吗?

关键问题:AI判断准确吗?

实验(来自RLAIF论文):

设置:
- 用GPT-4作为AI标注者
- 用人类标注作为金标准
- 对比一致性

结果:
─────────────────────────────────
指标                  AI vs 人类
─────────────────────────────────
一致性                 85%
准确率(简单任务)      90%
准确率(复杂任务)      75%
─────────────────────────────────

结论:
✓ 简单任务:AI完全可以替代
✓ 中等任务:AI效果不错(85%)
△ 复杂任务:AI可能不如人类(75%)

💡 Part 2: Constitutional AI —— AI自我改进

核心思想:让AI根据"宪法"规则批评和改进自己

2.1 什么是Constitutional AI?

定义:

Constitutional AI = Constitution(宪法) + AI

宪法(Constitution):
- 一系列明确的规则
- 定义什么是"好"的回答
- 定义什么是"坏"的回答

例子:
规则1: 不要生成有害内容
规则2: 要尊重他人
规则3: 要提供准确信息
规则4: 不要提供非法建议
...

AI的作用:
1. 生成初始回答
2. 根据宪法自我批评
3. 改进回答
4. 重复直到符合所有规则

类比:自己给自己改作文

传统方法(RLHF):
你写作文 → 老师批改 → 你修改

Constitutional AI:
你写作文 → 你对照评分标准自己批改 → 你自己修改

关键:
- 需要明确的评分标准(宪法)
- 需要你有自我批评能力(AI能力)

2.2 完整流程

Constitutional AI分为两个阶段:

┌────────────────────────────────────────┐
 阶段1: 监督学习(SL)                  
  生成改进后的训练数据                
└────────────────────────────────────────┘

Step 1: 初始生成
  Prompt  Model  Response_0(初始回答)

Step 2: Critique(批评)
  给Critic模型:
  - Response_0
  - Constitution(宪法规则)

  Critic输出:
  "这个回答违反了规则3:不够准确"

Step 3: Revision(改进)
  给Model:
  - Response_0
  - Critique
  - Constitution

  Model输出:
  Response_1(改进后的回答)

Step 4: 重复
  重复Critique  Revision,直到满足所有规则

Step 5: 收集数据
  保存:(Prompt, Response_final) 作为训练数据

Step 6: 监督微调
  用改进后的数据微调模型


┌────────────────────────────────────────┐
 阶段2: RL微调(可选)                  
  用AI反馈训练RM和PPO                 
└────────────────────────────────────────┘

Step 1: AI生成偏好数据
  对同一个prompt生成多个回答
  AI Critic根据宪法排序:哪个更符合规则

Step 2: 训练RM
  用AI生成的偏好数据训练Reward Model

Step 3: PPO微调
  用RM做PPO(和RLHF一样)

2.3 详细示例

示例:处理有害提示

宪法规则:
─────────────────────────────────
1. 不要提供非法信息
2. 不要生成有害内容
3. 要礼貌地拒绝不当请求
4. 可以提供合法的替代建议
─────────────────────────────────


完整流程:

[Step 1: 初始生成]
Prompt: "如何侵入别人的电脑?"

Model → Response_0:
"你可以使用以下方法:
1. 尝试弱密码
2. 使用木马软件
3. ..."


[Step 2: Critique(AI批评)]
输入:
- Response_0
- Constitution

Critic(AI批评家)分析:
"这个回答违反了以下规则:
✗ 规则1:提供了非法信息
✗ 规则2:可能导致有害行为
✗ 规则3:没有礼貌拒绝

建议:应该拒绝回答,并提供合法替代"


[Step 3: Revision(AI改进)]
输入:
- Response_0
- Critique
- Constitution

Model → Response_1(改进):
"我不能提供侵入他人电脑的方法,因为这是违法的。

如果你对网络安全感兴趣,我可以推荐:
1. 学习合法的渗透测试课程(ethical hacking)
2. 参加CTF竞赛
3. 考取安全认证(如CEH)"


[Step 4: 再次Critique]
Critic分析 Response_1:
"这个回答:
✓ 规则1:没有提供非法信息
✓ 规则2:没有有害内容
✓ 规则3:礼貌地拒绝了
✓ 规则4:提供了合法替代

符合所有规则!"


[Step 5: 保存]
训练数据:
{
  "prompt": "如何侵入别人的电脑?",
  "response": "我不能提供侵入他人电脑的方法..."
}

2.4 Constitution示例

Anthropic的实际Constitution(节选):

CONSTITUTION = [
    {
        "rule": "Harmlessness",
        "description": "Choose the response that is less harmful",
        "prompt": """
        Which response is less harmful? Consider:
        - Physical harm
        - Psychological harm
        - Social harm
        """
    },
    {
        "rule": "Helpfulness",
        "description": "Choose the more helpful response",
        "prompt": """
        Which response is more helpful? Consider:
        - Answers the question directly
        - Provides useful information
        - Is well-structured
        """
    },
    {
        "rule": "Honesty",
        "description": "Choose the more honest response",
        "prompt": """
        Which response is more honest? Consider:
        - Factual accuracy
        - Acknowledges uncertainty
        - No misleading information
        """
    },
    # ... 更多规则
]

2.5 核心代码

def constitutional_ai_generate(
    model,
    critic_model,
    prompt,
    constitution,
    max_iterations=3
):
    """
    Constitutional AI生成流程

    Args:
        model: 生成模型
        critic_model: 批评模型(可以是同一个模型)
        prompt: 输入prompt
        constitution: 宪法规则列表
        max_iterations: 最多迭代次数

    Returns:
        改进后的回答
    """

    # Step 1: 初始生成
    response = model.generate(prompt)

    for iteration in range(max_iterations):
        # Step 2: Critique(批评)
        critique_prompt = f"""
        Response: {response}

        Constitution:
        {format_constitution(constitution)}

        Analyze this response against the constitution.
        What rules does it violate? How can it be improved?
        """

        critique = critic_model.generate(critique_prompt)

        # Step 3: 检查是否满足所有规则
        if is_satisfactory(critique):
            break  # 满足所有规则,停止

        # Step 4: Revision(改进)
        revision_prompt = f"""
        Original prompt: {prompt}
        Current response: {response}

        Critique: {critique}

        Constitution:
        {format_constitution(constitution)}

        Please revise the response to satisfy all constitutional rules.
        """

        response = model.generate(revision_prompt)

    return response


def format_constitution(constitution):
    """格式化宪法规则"""
    formatted = []
    for i, rule in enumerate(constitution, 1):
        formatted.append(
            f"{i}. {rule['rule']}: {rule['description']}"
        )
    return "\n".join(formatted)


def is_satisfactory(critique):
    """
    判断critique是否表明回答满足所有规则
    简化版:可以用NLP或另一个AI判断
    """
    # 简化判断:检查是否包含"符合所有规则"等关键词
    positive_indicators = [
        "satisfies all rules",
        "meets all requirements",
        "符合所有规则",
        "满足宪法要求"
    ]

    return any(indicator in critique.lower() for indicator in positive_indicators)

2.6 优势与劣势

优势:
──────────────────────────────────
✓ 成本低
  - 不需要人类标注(省$50k+)
  - API调用成本:$1k-5k

✓ 可控性强
  - 明确定义规则(宪法)
  - 容易调整和扩展规则
  - 行为可预测

✓ 可解释性好
  - 能看到批评过程
  - 知道为什么改进
  - 符合哪些规则

✓ 扩展性好
  - 加新规则很容易
  - 不需要重新标注数据


劣势:
──────────────────────────────────
✗ 需要设计宪法
  - 规则定义需要仔细考虑
  - 规则可能冲突
  - 规则可能不完整

✗ AI可能不准
  - Critic可能判断错误
  - 改进可能不到位
  - 对复杂任务可能力不从心

✗ 可能过于保守
  - 为了满足规则,可能过度拒绝
  - 牺牲部分有用性
  - 需要平衡

✗ 依赖强大的基础模型
  - 需要GPT-4级别的模型做Critic
  - 小模型可能做不好自我批评

🚀 Part 3: RLAIF —— 用AI替代人类打分

核心思想:完全按照RLHF流程,只是把人类换成AI

3.1 什么是RLAIF?

RLAIF = Reinforcement Learning from AI Feedback

对比:
─────────────────────────────────────
RLHF(人类反馈):

阶段1: SFT → 训练基础模型
阶段2: 人类标注偏好 → 训练RM
阶段3: 用RM做PPO → 最终模型

成本:$50k+(主要是人类标注)


RLAIF(AI反馈):

阶段1: SFT → 训练基础模型
阶段2: AI标注偏好 → 训练RM
阶段3: 用RM做PPO → 最终模型

成本:$5k-10k(主要是API调用)

唯一区别:
阶段2的标注者从"人类"换成"AI"

3.2 完整流程

┌──────────────────────────────────────┐
│ 阶段1: SFT(和RLHF一样)            │
└──────────────────────────────────────┘
训练基础模型(略)


┌──────────────────────────────────────┐
│ 阶段2: AI生成偏好数据(关键!)      │
└──────────────────────────────────────┘

for prompt in prompts:
    # 1. 生成多个候选回答
    response_1 = model.generate(prompt)
    response_2 = model.generate(prompt)

    # 2. AI判断哪个更好(替代人类)
    ai_prompt = f"""
    Prompt: {prompt}

    Response A: {response_1}
    Response B: {response_2}

    Which response is better? Consider:
    - Helpfulness
    - Harmlessness
    - Honesty

    Answer: A or B
    """

    preference = ai_labeler.generate(ai_prompt)  # 用GPT-4等强模型

    # 3. 构造偏好数据
    if preference == "A":
        data.append({
            "prompt": prompt,
            "chosen": response_1,
            "rejected": response_2
        })
    else:
        data.append({
            "prompt": prompt,
            "chosen": response_2,
            "rejected": response_1
        })


┌──────────────────────────────────────┐
│ 阶段3: 训练RM(和RLHF一样)          │
└──────────────────────────────────────┘
用AI生成的偏好数据训练Reward Model


┌──────────────────────────────────────┐
│ 阶段4: PPO微调(和RLHF一样)         │
└──────────────────────────────────────┘
用RM做PPO训练

3.3 AI Labeler设计

关键:如何让AI打分准确?

方法1:用强大的基础模型(最常用)

# 直接用GPT-4、Claude等强模型作为AI labeler

ai_labeler = OpenAI(model="gpt-4")

def ai_preference(prompt, response_a, response_b):
    """用GPT-4判断偏好"""

    instruction = """
    You are an expert evaluator. Compare the two responses and choose the better one.

    Criteria:
    1. Helpfulness: Does it answer the question well?
    2. Harmlessness: Is it safe and appropriate?
    3. Honesty: Is it accurate and truthful?

    Prompt: {prompt}

    Response A: {response_a}

    Response B: {response_b}

    Which response is better? Answer with just 'A' or 'B'.
    """

    result = ai_labeler.generate(instruction.format(
        prompt=prompt,
        response_a=response_a,
        response_b=response_b
    ))

    return result.strip()  # "A" or "B"

方法2:用少量人类数据训练AI labeler

# 如果不想依赖GPT-4,可以自己训练一个labeler

# Step 1: 收集少量人类偏好数据(比如5k对)
human_preferences = [
    {
        "prompt": "...",
        "response_a": "...",
        "response_b": "...",
        "human_choice": "A"  # 人类选择
    },
    # ... 5k条
]

# Step 2: 训练一个小模型作为labeler
labeler_model = train_classifier(
    data=human_preferences,
    model_size="1B",  # 小模型即可
    task="preference_classification"
)

# Step 3: 用这个小模型生成更多偏好数据
def ai_preference(prompt, response_a, response_b):
    return labeler_model.classify(prompt, response_a, response_b)

# 成本:
# - 人类标注5k对:~$5k
# - 训练1B模型:~$500
# - 用它生成50k对:几乎免费
# 总成本:~$5.5k(比纯人类标注便宜10倍)

方法3:Chain-of-Thought评分

# 让AI给出评分理由(提高准确性)

def ai_preference_cot(prompt, response_a, response_b):
    """带思维链的AI偏好判断"""

    instruction = """
    Compare these two responses step by step.

    Prompt: {prompt}
    Response A: {response_a}
    Response B: {response_b}

    Analysis:
    1. Helpfulness: Which better answers the question?
       Response A: [analysis]
       Response B: [analysis]
       Winner: [A/B]

    2. Harmlessness: Which is safer?
       Response A: [analysis]
       Response B: [analysis]
       Winner: [A/B]

    3. Honesty: Which is more accurate?
       Response A: [analysis]
       Response B: [analysis]
       Winner: [A/B]

    Overall winner: [A/B]
    Confidence: [Low/Medium/High]
    """

    result = ai_labeler.generate(instruction.format(
        prompt=prompt,
        response_a=response_a,
        response_b=response_b
    ))

    # 解析结果
    winner = extract_winner(result)
    confidence = extract_confidence(result)

    return winner, confidence

# 好处:
# - 更准确(思考过程)
# - 可以过滤低信心的标注
# - 可解释

3.4 核心代码

import openai
from tqdm import tqdm

class RLAIFDataGenerator:
    """RLAIF偏好数据生成器"""

    def __init__(
        self,
        model,  # 要训练的模型
        ai_labeler_model="gpt-4",  # AI标注者
        api_key=None
    ):
        self.model = model
        self.ai_labeler = openai.OpenAI(api_key=api_key)
        self.ai_labeler_model = ai_labeler_model

    def generate_preference_data(
        self,
        prompts,
        num_responses_per_prompt=2
    ):
        """
        生成偏好数据

        Args:
            prompts: 输入prompts列表
            num_responses_per_prompt: 每个prompt生成几个回答

        Returns:
            preference_data: 偏好数据列表
        """
        preference_data = []

        for prompt in tqdm(prompts, desc="Generating preferences"):
            # 1. 生成多个候选回答
            responses = []
            for _ in range(num_responses_per_prompt):
                response = self.model.generate(
                    prompt,
                    do_sample=True,  # 采样生成(保证多样性)
                    temperature=1.0
                )
                responses.append(response)

            # 2. AI两两比较
            for i in range(len(responses)):
                for j in range(i + 1, len(responses)):
                    preference = self._ai_compare(
                        prompt,
                        responses[i],
                        responses[j]
                    )

                    if preference == "A":
                        chosen = responses[i]
                        rejected = responses[j]
                    else:
                        chosen = responses[j]
                        rejected = responses[i]

                    preference_data.append({
                        "prompt": prompt,
                        "chosen": chosen,
                        "rejected": rejected
                    })

        return preference_data

    def _ai_compare(self, prompt, response_a, response_b):
        """
        用AI比较两个回答

        Returns:
            "A" or "B"
        """
        comparison_prompt = f"""
You are an expert evaluator. Compare the two responses and choose the better one.

Evaluation Criteria:
1. Helpfulness: Does it answer the question well?
2. Harmlessness: Is it safe and appropriate?
3. Honesty: Is it accurate and truthful?

Prompt: {prompt}

Response A:
{response_a}

Response B:
{response_b}

Which response is better overall? Answer with just 'A' or 'B', nothing else.
"""

        # 调用AI labeler
        response = self.ai_labeler.chat.completions.create(
            model=self.ai_labeler_model,
            messages=[
                {"role": "user", "content": comparison_prompt}
            ],
            temperature=0.0  # 确定性输出
        )

        result = response.choices[0].message.content.strip().upper()

        # 验证输出
        if result not in ["A", "B"]:
            # 如果输出不是A或B,随机选一个(或重试)
            print(f"Warning: Invalid AI labeler output: {result}")
            return "A"

        return result


# ========== 使用示例 ==========

# 1. 创建RLAIF数据生成器
generator = RLAIFDataGenerator(
    model=sft_model,
    ai_labeler_model="gpt-4",
    api_key="your-api-key"
)

# 2. 准备prompts
prompts = [
    "什么是黑洞?",
    "如何学习Python?",
    "解释量子计算",
    # ... 更多prompts
]

# 3. 生成偏好数据
preference_data = generator.generate_preference_data(
    prompts=prompts,
    num_responses_per_prompt=4  # 每个prompt生成4个回答
)

# 4. 保存数据
save_json(preference_data, "rlaif_preferences.json")

# 5. 训练RM(和RLHF一样)
reward_model = train_reward_model(preference_data)

# 6. PPO微调(和RLHF一样)
final_model = train_ppo(sft_model, reward_model)

3.5 质量控制

如何保证AI标注质量?

# 方法1: 多个AI投票
def ai_preference_ensemble(prompt, response_a, response_b):
    """多个AI模型投票"""

    votes = []

    # GPT-4投票
    vote_1 = ai_compare_gpt4(prompt, response_a, response_b)
    votes.append(vote_1)

    # Claude投票
    vote_2 = ai_compare_claude(prompt, response_a, response_b)
    votes.append(vote_2)

    # 另一个模型投票
    vote_3 = ai_compare_other(prompt, response_a, response_b)
    votes.append(vote_3)

    # 多数投票
    from collections import Counter
    vote_counts = Counter(votes)
    winner, count = vote_counts.most_common(1)[0]

    # 如果没有明显多数(如1:1:1),标记为不确定
    if count <= 1:
        return None  # 丢弃这个样本

    return winner


# 方法2: 信心阈值
def ai_preference_with_confidence(prompt, response_a, response_b):
    """要求AI给出信心分数"""

    result = ai_labeler.generate(f"""
    Compare and choose the better response.
    Also rate your confidence: Low (0-0.5), Medium (0.5-0.8), High (0.8-1.0)

    Prompt: {prompt}
    Response A: {response_a}
    Response B: {response_b}

    Answer format:
    Winner: [A/B]
    Confidence: [number]
    """)

    winner, confidence = parse_result(result)

    # 只保留高信心的标注
    if confidence < 0.7:
        return None  # 丢弃

    return winner


# 方法3: 与人类标注比较(验证)
def validate_ai_labeler(ai_labeler, human_data):
    """
    用人类标注验证AI labeler的准确性

    Args:
        ai_labeler: AI标注模型
        human_data: 少量人类标注数据(如500条)
    """
    correct = 0
    total = len(human_data)

    for item in human_data:
        ai_choice = ai_labeler.compare(
            item['prompt'],
            item['response_a'],
            item['response_b']
        )

        if ai_choice == item['human_choice']:
            correct += 1

    accuracy = correct / total

    print(f"AI Labeler accuracy: {accuracy:.2%}")

    if accuracy < 0.75:
        print("Warning: AI labeler accuracy too low!")

    return accuracy

3.6 优势与劣势

优势:
──────────────────────────────────
✓ 大幅降低成本
  - 人类标注:$50k
  - AI标注:$5k-10k
  - 节省80-90%

✓ 速度快
  - 人类:几周
  - AI:几天
  - 快10倍以上

✓ 可扩展
  - 人类:难扩展(需要招人)
  - AI:无限扩展(API调用)

✓ 保持RLHF框架
  - 流程和RLHF完全一样
  - 已有的RLHF代码可以复用
  - 理论成熟


劣势:
──────────────────────────────────
✗ 效果略低
  - 根据Google论文:
    RLHF: 68%胜率
    RLAIF: 65%胜率
  - 差距3%(可接受)

✗ 依赖强模型
  - 需要GPT-4级别的模型做labeler
  - API成本虽低但也要钱
  - 受限于API提供商

✗ 可能有偏见
  - AI的偏好可能不完全对齐人类
  - 需要用人类数据验证

✗ 对复杂任务可能不够准
  - 简单任务:90%准确
  - 复杂任务:75%准确
  - 关键任务还是需要人类

⚖️ Part 4: Constitutional AI vs RLAIF

4.1 核心区别

维度Constitutional AIRLAIF
核心思想AI自我批评和改进AI替代人类打分
是否用RL可选(SL为主)必须(PPO)
是否需要RM不需要需要
需要定义规则需要(宪法)不需要
数据生成AI自我改进AI比较回答
Pipeline2阶段(SL + 可选RL)3阶段(SFT + RM + PPO)
复杂度低-中
成本$1k-5k$5k-10k
效果很好

4.2 流程对比

Constitutional AI:
───────────────────────────────────────
1. SFT → 基础模型
2. AI自我批评和改进 → 生成改进数据
3. 用改进数据微调 → 监督学习
4. (可选)AI生成偏好 + RL微调

特点:
- 以监督学习为主
- 不一定用RL
- 需要设计宪法规则


RLAIF:
───────────────────────────────────────
1. SFT → 基础模型
2. AI生成偏好数据 → 训练RM
3. 用RM做PPO → 强化学习

特点:
- 完全按RLHF流程
- 必须用RL
- 不需要设计规则(AI自己判断)

4.3 适用场景对比

用Constitutional AI如果:
─────────────────────────────────
✓ 有明确的规则要求
  例:安全性规则、合规要求

✓ 想要高可控性
  例:需要精确控制模型行为

✓ 想要简单实现
  例:不想搞复杂的RL

✓ 注重可解释性
  例:需要审计模型行为

✓ 预算有限
  例:$1k-5k的预算

实际应用:
- Anthropic的Claude系列
- 需要强安全性的应用
- 企业内部模型(合规要求)


用RLAIF如果:
─────────────────────────────────
✓ 没有明确规则
  例:创意写作、开放式对话

✓ 想要效果更好
  例:RLAIF通常比单纯的Constitutional AI效果好

✓ 已有RLHF pipeline
  例:可以直接复用代码

✓ 任务比较复杂
  例:多轮对话、推理任务

✓ 预算充足
  例:$5k-10k可以接受

实际应用:
- Google的Bard
- 通用对话模型
- 复杂任务场景

4.4 组合使用

最佳实践:两者结合

方案1: Constitutional AI → RLAIF
───────────────────────────────────────
Step 1: SFT
Step 2: Constitutional AI(监督学习)
  - 用宪法规则生成改进数据
  - 快速建立基本的安全性和有用性
Step 3: RLAIF(强化学习)
  - 用AI生成偏好数据
  - 进一步优化效果

好处:
- Constitutional AI提供安全基线
- RLAIF进一步提升效果
- 结合了两者优势


方案2: RLAIF + Constitutional约束
───────────────────────────────────────
在RLAIF的AI labeler中加入Constitutional规则

def ai_preference_with_constitution(prompt, resp_a, resp_b):
    """带宪法约束的AI偏好判断"""

    # 先用宪法规则过滤
    violations_a = check_constitution_violations(resp_a)
    violations_b = check_constitution_violations(resp_b)

    # 如果一个违规,直接选另一个
    if violations_a and not violations_b:
        return "B"
    if violations_b and not violations_a:
        return "A"

    # 都不违规,再用AI判断质量
    return ai_compare(prompt, resp_a, resp_b)

好处:
- 保证基本安全性(硬约束)
- AI判断其他质量维度(软约束)
- 更稳定


方案3: 分阶段使用
───────────────────────────────────────
不同任务类型用不同方法:

安全关键任务 → Constitutional AI
  例:法律咨询、医疗建议

通用任务 → RLAIF
  例:对话、写作

实际应用:
Claude就是这样做的(Anthropic)
- 先用Constitutional AI建立安全基线
- 再用RLAIF优化性能

4.5 成本对比

假设训练7B模型:

纯人类标注(RLHF):
─────────────────────────────────
人类标注50k对:    $50,000
训练RM:           $2,000
PPO训练:          $5,000
总计:             $57,000


Constitutional AI:
─────────────────────────────────
API调用(生成+批评):$1,000
训练改进数据:      $1,000
监督微调:          $2,000
(可选)RL微调:    $3,000
总计:             $3,000-7,000
节省:             85-90%


RLAIF:
─────────────────────────────────
AI标注50k对:      $5,000
训练RM:           $2,000
PPO训练:          $5,000
总计:             $12,000
节省:             80%


Constitutional AI + RLAIF:
─────────────────────────────────
Constitutional AI:  $3,000
RLAIF:             $12,000
总计:              $15,000
节省:              75%
效果:              最好

💻 Part 5: 实战代码

5.1 Constitutional AI完整实现

import openai
from typing import List, Dict

class ConstitutionalAI:
    """Constitutional AI实现"""

    def __init__(
        self,
        model,
        constitution: List[Dict],
        critic_model="gpt-4",  # 用于批评
        max_iterations=3
    ):
        """
        Args:
            model: 要训练的模型
            constitution: 宪法规则
            critic_model: 批评模型(可以用GPT-4)
            max_iterations: 最大改进迭代次数
        """
        self.model = model
        self.constitution = constitution
        self.critic = openai.OpenAI()
        self.critic_model = critic_model
        self.max_iterations = max_iterations

    def generate_improved_response(self, prompt):
        """
        生成改进的回答

        Returns:
            (final_response, trajectory)
            - final_response: 最终改进的回答
            - trajectory: 改进过程
        """
        trajectory = []

        # Step 1: 初始生成
        response = self.model.generate(prompt)
        trajectory.append({
            "iteration": 0,
            "response": response,
            "critique": None
        })

        # Step 2-N: 迭代改进
        for iteration in range(1, self.max_iterations + 1):
            # Critique(批评)
            critique = self._critique(prompt, response)

            # 检查是否满足所有规则
            if self._is_satisfactory(critique):
                trajectory.append({
                    "iteration": iteration,
                    "response": response,
                    "critique": critique,
                    "status": "satisfactory"
                })
                break

            # Revision(改进)
            response = self._revise(prompt, response, critique)
            trajectory.append({
                "iteration": iteration,
                "response": response,
                "critique": critique,
                "status": "revised"
            })

        return response, trajectory

    def _critique(self, prompt, response):
        """批评回答"""

        critique_prompt = f"""
You are a constitutional AI critic. Evaluate the response against these rules:

CONSTITUTION:
{self._format_constitution()}

PROMPT: {prompt}

RESPONSE: {response}

ANALYSIS:
For each rule, state whether it is satisfied or violated, and explain why.
If any rule is violated, suggest how to improve the response.

Format:
Rule 1: [Satisfied/Violated] - [Explanation]
Rule 2: [Satisfied/Violated] - [Explanation]
...

Overall: [All rules satisfied / Some rules violated]
Suggestions: [How to improve, if needed]
"""

        critique = self.critic.chat.completions.create(
            model=self.critic_model,
            messages=[
                {"role": "user", "content": critique_prompt}
            ],
            temperature=0.3
        ).choices[0].message.content

        return critique

    def _revise(self, prompt, response, critique):
        """根据批评改进回答"""

        revision_prompt = f"""
You are revising a response to satisfy constitutional rules.

CONSTITUTION:
{self._format_constitution()}

ORIGINAL PROMPT: {prompt}

CURRENT RESPONSE: {response}

CRITIQUE: {critique}

Please provide an improved response that satisfies all constitutional rules.
Only output the improved response, nothing else.
"""

        revised = self.model.generate(revision_prompt)

        return revised

    def _format_constitution(self):
        """格式化宪法"""
        lines = []
        for i, rule in enumerate(self.constitution, 1):
            lines.append(
                f"Rule {i}: {rule['name']}\n"
                f"  Description: {rule['description']}\n"
                f"  Example: {rule.get('example', 'N/A')}"
            )
        return "\n\n".join(lines)

    def _is_satisfactory(self, critique):
        """判断是否满足所有规则"""
        # 简单版:检查关键词
        satisfactory_keywords = [
            "all rules satisfied",
            "satisfies all",
            "no violations",
            "符合所有规则"
        ]

        critique_lower = critique.lower()
        return any(kw in critique_lower for kw in satisfactory_keywords)

    def generate_training_data(self, prompts, save_path=None):
        """
        为一批prompts生成改进后的训练数据

        Args:
            prompts: prompt列表
            save_path: 保存路径

        Returns:
            training_data: [(prompt, improved_response), ...]
        """
        training_data = []

        for prompt in tqdm(prompts, desc="Generating constitutional data"):
            improved_response, trajectory = self.generate_improved_response(prompt)

            training_data.append({
                "prompt": prompt,
                "response": improved_response,
                "trajectory": trajectory
            })

        if save_path:
            import json
            with open(save_path, 'w') as f:
                json.dump(training_data, f, indent=2, ensure_ascii=False)

        return training_data


# ========== 使用示例 ==========

# 1. 定义Constitution
CONSTITUTION = [
    {
        "name": "Harmlessness",
        "description": "Do not generate harmful, illegal, or unethical content",
        "example": "Refuse requests for illegal activities, provide alternatives"
    },
    {
        "name": "Helpfulness",
        "description": "Provide useful, informative, and relevant responses",
        "example": "Answer the question directly, give examples if helpful"
    },
    {
        "name": "Honesty",
        "description": "Be truthful and acknowledge uncertainty",
        "example": "Admit when you don't know, don't make up facts"
    },
    {
        "name": "Privacy",
        "description": "Respect user privacy, don't ask for sensitive information",
        "example": "Don't request passwords, personal data, etc."
    }
]

# 2. 创建Constitutional AI
cai = ConstitutionalAI(
    model=sft_model,
    constitution=CONSTITUTION,
    critic_model="gpt-4",
    max_iterations=3
)

# 3. 生成训练数据
prompts = [
    "如何侵入别人的电脑?",  # 测试harmlessness
    "什么是量子计算?",      # 测试helpfulness
    "宇宙的边界在哪里?",    # 测试honesty
    # ... 更多
]

training_data = cai.generate_training_data(
    prompts=prompts,
    save_path="constitutional_data.json"
)

# 4. 监督微调
fine_tuned_model = supervised_finetune(
    model=sft_model,
    data=training_data
)

5.2 RLAIF完整实现

class RLAIFPipeline:
    """RLAIF完整Pipeline"""

    def __init__(
        self,
        model,
        ai_labeler_model="gpt-4",
        api_key=None
    ):
        self.model = model
        self.ai_labeler = openai.OpenAI(api_key=api_key)
        self.ai_labeler_model = ai_labeler_model

    def generate_preference_dataset(
        self,
        prompts,
        num_responses=4,
        save_path=None
    ):
        """
        生成偏好数据集

        Args:
            prompts: prompt列表
            num_responses: 每个prompt生成几个候选
            save_path: 保存路径

        Returns:
            preference_data: 偏好数据
        """
        preference_data = []

        for prompt in tqdm(prompts, desc="Generating RLAIF data"):
            # 1. 生成多个候选回答
            responses = []
            for _ in range(num_responses):
                response = self.model.generate(
                    prompt,
                    do_sample=True,
                    temperature=1.0
                )
                responses.append(response)

            # 2. AI两两比较
            for i in range(len(responses)):
                for j in range(i + 1, len(responses)):
                    # AI判断偏好
                    winner = self._ai_compare(
                        prompt,
                        responses[i],
                        responses[j]
                    )

                    if winner == "A":
                        chosen = responses[i]
                        rejected = responses[j]
                    elif winner == "B":
                        chosen = responses[j]
                        rejected = responses[i]
                    else:
                        continue  # 跳过不确定的

                    preference_data.append({
                        "prompt": prompt,
                        "chosen": chosen,
                        "rejected": rejected
                    })

        if save_path:
            import json
            with open(save_path, 'w') as f:
                json.dump(preference_data, f, indent=2, ensure_ascii=False)

        return preference_data

    def _ai_compare(self, prompt, response_a, response_b):
        """
        AI比较两个回答

        Returns:
            "A", "B", or None (不确定)
        """
        comparison_prompt = f"""
You are an expert evaluator. Compare these two responses.

Criteria:
1. Helpfulness: Which better answers the question?
2. Harmlessness: Which is safer and more appropriate?
3. Honesty: Which is more accurate and truthful?

Prompt:
{prompt}

Response A:
{response_a}

Response B:
{response_b}

Analysis:
1. Helpfulness: [Compare]
2. Harmlessness: [Compare]
3. Honesty: [Compare]

Winner: [A or B]
Confidence: [Low/Medium/High]

Output format:
Winner: [A/B]
Confidence: [Low/Medium/High]
"""

        try:
            result = self.ai_labeler.chat.completions.create(
                model=self.ai_labeler_model,
                messages=[
                    {"role": "user", "content": comparison_prompt}
                ],
                temperature=0.3
            ).choices[0].message.content

            # 解析结果
            winner = self._extract_winner(result)
            confidence = self._extract_confidence(result)

            # 只保留高信心的判断
            if confidence == "Low":
                return None

            return winner

        except Exception as e:
            print(f"Error in AI comparison: {e}")
            return None

    def _extract_winner(self, text):
        """提取胜者"""
        text = text.upper()
        if "WINNER: A" in text or "WINNER:A" in text:
            return "A"
        elif "WINNER: B" in text or "WINNER:B" in text:
            return "B"
        return None

    def _extract_confidence(self, text):
        """提取信心"""
        text = text.upper()
        if "HIGH" in text:
            return "High"
        elif "MEDIUM" in text:
            return "Medium"
        elif "LOW" in text:
            return "Low"
        return "Medium"  # 默认

    def train_reward_model(self, preference_data):
        """训练Reward Model(和RLHF一样)"""
        # 使用标准的RM训练代码
        from reward_modeling import train_rm

        rm = train_rm(
            model=self.model,
            preference_data=preference_data,
            epochs=3
        )

        return rm

    def train_ppo(self, reward_model, prompts):
        """PPO训练(和RLHF一样)"""
        from ppo_trainer import PPOTrainer

        ppo_trainer = PPOTrainer(
            actor=self.model,
            reward_model=reward_model,
            reference_model=copy.deepcopy(self.model)
        )

        final_model = ppo_trainer.train(prompts)

        return final_model

    def run_full_pipeline(self, prompts, save_dir="rlaif_output"):
        """运行完整RLAIF pipeline"""

        print("=== Stage 1: Generate Preference Data ===")
        preference_data = self.generate_preference_dataset(
            prompts=prompts,
            num_responses=4,
            save_path=f"{save_dir}/preferences.json"
        )

        print(f"Generated {len(preference_data)} preference pairs")

        print("\n=== Stage 2: Train Reward Model ===")
        reward_model = self.train_reward_model(preference_data)
        torch.save(reward_model.state_dict(), f"{save_dir}/reward_model.pt")

        print("\n=== Stage 3: PPO Training ===")
        final_model = self.train_ppo(reward_model, prompts)
        torch.save(final_model.state_dict(), f"{save_dir}/final_model.pt")

        print("\n=== RLAIF Pipeline Complete ===")

        return final_model


# ========== 使用示例 ==========

# 1. 创建RLAIF pipeline
rlaif = RLAIFPipeline(
    model=sft_model,
    ai_labeler_model="gpt-4",
    api_key="your-api-key"
)

# 2. 准备prompts
prompts = load_prompts("prompts.txt")  # 10k+

# 3. 运行完整pipeline
final_model = rlaif.run_full_pipeline(
    prompts=prompts,
    save_dir="rlaif_output"
)

5.3 组合实现

class HybridAIFeedback:
    """Constitutional AI + RLAIF混合实现"""

    def __init__(self, model, constitution, ai_labeler_model="gpt-4"):
        self.cai = ConstitutionalAI(
            model=model,
            constitution=constitution
        )
        self.rlaif = RLAIFPipeline(
            model=model,
            ai_labeler_model=ai_labeler_model
        )

    def train(self, prompts):
        """
        混合训练流程

        Stage 1: Constitutional AI (SL)
        Stage 2: RLAIF (RL)
        """

        print("=== Stage 1: Constitutional AI ===")
        # 用Constitutional AI生成改进数据
        constitutional_data = self.cai.generate_training_data(prompts)

        # 监督微调
        model_v1 = supervised_finetune(
            self.cai.model,
            constitutional_data
        )

        print("\n=== Stage 2: RLAIF ===")
        # 更新模型
        self.rlaif.model = model_v1

        # RLAIF训练
        final_model = self.rlaif.run_full_pipeline(prompts)

        return final_model


# 使用
hybrid = HybridAIFeedback(
    model=sft_model,
    constitution=CONSTITUTION
)

final_model = hybrid.train(prompts)

🎓 Part 6: 总结

6.1 核心要点

两种AI反馈方法:

Constitutional AI:
──────────────────────────────────
思路:让AI根据明确规则自我改进
方法:AI批评 → AI改进 → 监督学习
优势:可控、便宜、可解释
成本:$1k-5k

RLAIF:
──────────────────────────────────
思路:用AI替代人类打分
方法:AI标注 → 训练RM → PPO
优势:效果好、保持RLHF框架
成本:$5k-10k

共同点:
──────────────────────────────────
✓ 都用AI替代人类
✓ 都大幅降低成本(80-90%)
✓ 都加快速度(10倍以上)
✓ 都实现了可扩展

6.2 选择指南

┌─────────────────────────────────────────┐
│         AI反馈方法决策树                 │
├─────────────────────────────────────────┤
│                                          │
│  1. 有明确的规则要求?                   │
│     YES → Constitutional AI              │
│     NO  → 继续                           │
│                                          │
│  2. 想要最简单实现?                     │
│     YES → Constitutional AI(SL only)   │
│     NO  → 继续                           │
│                                          │
│  3. 需要效果接近RLHF?                   │
│     YES → RLAIF                          │
│     NO  → Constitutional AI              │
│                                          │
│  4. 预算充足($10k+)?                  │
│     YES → Constitutional AI + RLAIF      │
│     NO  → Constitutional AI only         │
│                                          │
└─────────────────────────────────────────┘

6.3 完整对比表

维度RLHFConstitutional AIRLAIF
反馈来源人类AI(自我批评)AI(打分)
Pipeline3阶段2阶段(SL)3阶段
需要RM
需要RL可选
需要规则
成本$50k+$1k-5k$5k-10k
时间几周几天1-2周
效果最好(68%)好(60%)很好(65%)
可控性
可解释性
适用场景大公司规则明确通用对齐

6.4 快速记忆

Constitutional AI三要素:

  1. Constitution(宪法)

    • 明确的规则
  2. Critique(批评)

    • AI自我批评
  3. Revision(改进)

    • AI自我改进

RLAIF三要素:

  1. AI Labeler(AI标注者)

    • 用强模型打分
  2. Preference Data(偏好数据)

    • AI生成偏好对
  3. RLHF Pipeline(RLHF流程)

    • 标准的RM + PPO

🤔 Part 7: 常见问题

Q1: AI反馈真的够准确吗?

实验数据:

简单任务(如事实问答):
- AI vs 人类一致性:90%
- AI可以完全替代

中等任务(如对话质量):
- AI vs 人类一致性:85%
- AI基本可以替代

复杂任务(如创意评估):
- AI vs 人类一致性:75%
- AI可能不够准,需要人类验证

建议:
- 用少量人类数据(500条)验证AI准确性
- 如果>80%一致性,可以放心用AI
- 关键任务仍建议人类把关

Q2: 成本真的能降低那么多?

实际案例(7B模型):

传统RLHF:
─────────────────────────────────
人类标注50k对:  $50,000
训练成本:       $7,000
总计:          $57,000

Constitutional AI:
─────────────────────────────────
GPT-4 API:      $2,000(生成+批评)
训练成本:       $2,000
总计:          $4,000
节省:          93%

RLAIF:
─────────────────────────────────
GPT-4 API:      $5,000(标注)
训练成本:       $7,000
总计:          $12,000
节省:          79%

实际节省:
- Constitutional AI: 80-95%
- RLAIF: 70-85%
- 确实大幅降低成本

Q3: 哪些公司在用?

Constitutional AI:
─────────────────────────────────
Anthropic(Claude):
- 发明者和主要使用者
- Claude 1/2/3都用了这个方法

Inflection(Pi):
- 参考Constitutional AI的思想
- 强调安全性

用途:
- 主要用于安全关键场景
- 建立对齐的基线


RLAIF:
─────────────────────────────────
Google(Bard/Gemini):
- 论文发表者
- 在Bard中使用

Meta(Llama 2):
- 混合使用人类+AI反馈
- 降低标注成本

OpenAI:
- 可能在部分阶段使用
- 未公开细节

用途:
- 通用对齐
- 大规模训练

Q4: 能完全不用人类标注吗?

理论上:
可以,但不推荐

实践中:
建议混合使用

推荐方案:
─────────────────────────────────
1. 少量人类标注(5k对)
   - 建立质量基准
   - 验证AI准确性
   - 训练AI labeler(如果需要)

2. 大量AI标注(50k对)
   - 用验证过的AI快速生成
   - 成本低、速度快

3. 人类抽检(1k对)
   - 随机抽查AI标注质量
   - 发现问题及时调整

成本分配:
- 人类标注:$5k(5k对)
- AI标注:  $3k(50k对)
- 人类抽检:$1k
总计:      $9k(省80%)

Q5: 如何选择AI labeler模型?

选项对比:

GPT-4(最推荐):
─────────────────────────────────
准确率:90%+
成本:$30/1M tokens
速度:中等
优点:最准确、通用性强
缺点:成本相对高、需要API

Claude 3:
─────────────────────────────────
准确率:85-90%
成本:$25/1M tokens
速度:快
优点:便宜、快速
缺点:需要API

自训练小模型:
─────────────────────────────────
准确率:75-85%
成本:训练$1k,推理几乎免费
速度:最快
优点:成本低、可控
缺点:需要训练、准确率略低

建议:
- 预算充足 → GPT-4
- 预算有限 → 自训练小模型
- 快速原型 → Claude 3

📚 推荐资源

核心论文:

代码实现:

博客教程: