mcpField
C
constitutional-ai

constitutional-ai

github.com/orchestra-research開発14.1k950 回インストール19 週間前に更新
リポジトリ

constitutional-ai について

Constitutional AIは、安全性の学習や研究に使うモデル調整手法です。原則に基づく自己批評と修正、AIフィードバックを用い、有害な出力への人手ラベルを必要としません。

主な機能

  1. 01原則に基づく自己批評と修正
  2. 02修正回答を使った教師ありファインチューニング
  3. 03RLAIFによる選好モデルの学習
  4. 04思考連鎖による回答の批評

活用シーン

  1. 01人手の安全性ラベルなしでの学習
  2. 02明示した原則に沿ったモデル調整
  3. 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フィードバックを通じてモデルを無害にするよう学習させます。

主要な考え方: モデルは「憲法」(原則の集合)を用いて、自身の応答を批評し修正することを学習します。

2つのフェーズ:

  1. 教師あり学習(SL): 自己批評 + 修正
  2. 強化学習(RL): RLAIF(AIフィードバックによる強化学習)

憲法の例:

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フィードバックによる強化学習)

ステップ 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)]
)

使用すべき場合と代替手法

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 と RLHF の比較: 性能比較、コスト分析、AIフィードバックと人間のフィードバックをいつ使い分けるかについては references/rlaif-comparison.md を参照してください。

思考連鎖による推論: 批評のためのプロンプトエンジニアリング、多段階推論、透明性の向上については references/cot-critique.md を参照してください。

ハードウェア要件

  • GPU: NVIDIA A100/H100 を推奨
  • VRAM:
    • SLフェーズ(7B): A100 40GB × 1
    • RLフェーズ(7B): A100 40GB × 2(ポリシー + 報酬モデル)
  • シングルノード: ほとんどのユースケースに十分
  • 混合精度: BF16 を推奨

計算要件:

  • SLフェーズ: 標準的な SFT と同程度
  • RLフェーズ: PPO と同程度(DPO より高い)
  • AI評価: 批評・選好生成のための追加の推論

リソース

コメント