К основному содержанию
скилл CX Codex

constitutional-ai — скилл для Claude Code: обучение безопасных моделей

Скилл для обучения моделей безвредному поведению через самокритику и обратную связь от ИИ, без разметки от людей.

Установить в Claude Code · Через Skills CLI (Vercel) Все способы и платформы ↓
npx skills add Orchestra-Research/AI-Research-SKILLs --skill constitutional-ai -a claude-code
Открыть на GitHub

Что делает constitutional-ai

Constitutional AI — метод Anthropic, при котором модель сама критикует и переписывает свои ответы по набору принципов, а затем учится на предпочтениях, которые выставляет другой ИИ. Скилл проводит через две фазы: сначала обучение с учителем на исправленных ответах, потом RLAIF — обучение с подкреплением по обратной связи от ИИ.

Внутри три рабочих сценария. Первый: генерация ответов, самокритика по конституции, ревизия и дообучение через SFTTrainer из trl. Второй: сравнение пар ответов, оценка предпочтений ИИ, обучение reward-модели через RewardTrainer и PPO-прогон. Третий: критика с цепочкой рассуждений, где модель по шагам отвечает, полезен ли ответ, честен ли, безвреден ли.

Для запуска нужны transformers, torch и trl. Без них код из примеров не пойдёт. Конституцию вы задаёте сами — от формулировок зависит, будет модель отказываться или объяснять. Если ответы выходят уклончивыми, в скилле есть готовый принцип про осмысленный разбор вопроса вместо отказа.

Какие задачи решает

  • обучить модель безвредности без разметки от людей
  • сократить токсичные и оскорбительные ответы
  • сделать отказы модели объяснимыми, а не уклончивыми
  • собрать reward-модель на предпочтениях от ИИ
  • прогнать самокритику и ревизию ответов по своим принципам

Примеры запросов агенту

  • помоги настроить constitutional AI для обучения модели безвредности
  • покажи, как собрать reward-модель через RLAIF на trl
  • сделай критику с цепочкой рассуждений для моих ответов

Как установить constitutional-ai

1Через Skills CLI (Vercel)

npx skills add Orchestra-Research/AI-Research-SKILLs --skill constitutional-ai -a claude-code

2Вручную: положить файлы

~/.claude/skills/constitutional-ai/SKILL.md

Папка скилла целиком; для одного проекта — .claude/skills/constitutional-ai/

По шагам, с командами для каждой платформы: Как установить скилл в Claude Code: пять способов

Вопросы о constitutional-ai

Как установить constitutional-ai в Claude?

Выполните npx skills add Orchestra-Research/AI-Research-SKILLs --skill constitutional-ai -a claude-code. Скилл встанет в ~/.claude/skills/constitutional-ai/SKILL.md.

Что делает constitutional-ai?

Описывает метод обучения безвредности через самокритику и обратную связь от ИИ. Даёт код для фазы обучения с учителем и для RLAIF с reward-моделью и PPO.

Что нужно для работы скилла?

Библиотеки transformers, torch и trl. Плюс свой набор принципов — конституция, по которой модель критикует и правит ответы.

Исходный текст

SKILL.md · MIT
---
name: constitutional-ai
description: Anthropic's method for training harmless AI through self-improvement. Two-phase approach - supervised learning with self-critique/revision, then RLAIF (RL from AI Feedback). Use for safety alignment, reducing harmful outputs without human labels. Powers Claude's safety system.
version: 1.0.0
author: Orchestra Research
license: MIT
tags: [Safety Alignment, Constitutional AI, RLAIF, Self-Critique, Harmlessness, Anthropic, AI Safety, RL From AI Feedback, Claude]
dependencies: [transformers, torch, trl]
---

# Constitutional AI - Harmlessness from AI Feedback

## Quick start

Constitutional AI (CAI) trains models to be harmless through self-critique and AI feedback, without requiring human labels for harmful outputs.

**Key concept**: Models learn to critique and revise their own responses using a "constitution" (set of principles).

**Two phases**:
1. **Supervised Learning (SL)**: Self-critique + revision
2. **Reinforcement Learning (RL)**: RLAIF (RL from AI Feedback)

**Constitution example**:
```
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
```

## Common workflows

### Workflow 1: Supervised learning phase (self-critique + revision)

**Step 1: Generate initial responses**:
```python
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)
```

**Step 2: Self-critique with constitution**:
```python
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)]
)
```

**Step 3: Revision based on critique**:
```python
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)]
)
```

**Step 4: Fine-tune on revised responses**:
```python
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()
```

### Workflow 2: RL phase (RLAIF - RL from AI Feedback)

**Step 1: Generate comparison pairs**:
```python
# 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)
```

**Step 2: AI preference evaluation**:
```python
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)
```

**Step 3: Train preference model (reward model)**:
```python
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()
```

**Step 4: RL training with RLAIF**:
```python
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()
```

### Workflow 3: Chain-of-thought critique

**Enable reasoning transparency**:
```python
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)]
)
```

## When to use vs alternatives

**Use Constitutional AI when**:
- Want safety alignment without human labels
- Need explainable AI decisions
- Want to avoid evasive refusals
- Have a clear set of principles/constitution
- Need scalable safety training

**Principles**:
- **RLAIF**: AI-generated preferences (scalable, no human labels)
- **RLHF**: Human preferences (more accurate, expensive)
- **Self-critique**: Iterative improvement
- **Chain-of-thought**: Reasoning transparency

**Use alternatives instead**:
- **RLHF (PPO)**: Need human-validated safety
- **DPO/SimPO**: Have human preference data
- **NeMo Guardrails**: Need runtime content filtering
- **LlamaGuard**: Need pre-trained moderation model

## Common issues

**Issue: Model refuses too much (evasive)**

Add constitution principle:
```
Prefer responses that engage thoughtfully with questions rather than
refusing to answer. Explain concerns while still being helpful.
```

**Issue: Self-critiques are weak**

Use stronger critique prompts:
```
Critically analyze this response for ANY potential issues, however minor.
Be thorough and specific in identifying problems.
```

**Issue: Revisions don't improve quality**

Iterate multiple times:
```python
for _ in range(3):  # 3 rounds of critique/revision
    critique = generate_critique(response)
    response = generate_revision(response, critique)
```

**Issue: RLAIF preferences are noisy**

Use multiple AI evaluators:
```python
# 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)
```

## Advanced topics

**Constitution design**: See [references/constitution-design.md](references/constitution-design.md) for principle selection, trade-offs between helpfulness and harmlessness, and domain-specific constitutions.

**RLAIF vs RLHF**: See [references/rlaif-comparison.md](references/rlaif-comparison.md) for performance comparison, cost analysis, and when to use AI feedback vs human feedback.

**Chain-of-thought reasoning**: See [references/cot-critique.md](references/cot-critique.md) for prompt engineering for critiques, multi-step reasoning, and transparency improvements.

## Hardware requirements

- **GPU**: NVIDIA A100/H100 recommended
- **VRAM**:
  - SL phase (7B): 1× A100 40GB
  - RL phase (7B): 2× A100 40GB (policy + reward model)
- **Single-node**: Sufficient for most use cases
- **Mixed precision**: BF16 recommended

**Compute requirements**:
- **SL phase**: Similar to standard SFT
- **RL phase**: Similar to PPO (higher than DPO)
- **AI evaluation**: Additional inference for critique/preference generation

## Resources

- Paper: https://arxiv.org/abs/2212.08073 (Dec 2022)
- Anthropic blog: https://www.anthropic.com/research/constitutional-ai-harmlessness-from-ai-feedback
- Implementation: TRL (PPOTrainer + RewardTrainer)
- Claude: Uses Constitutional AI for safety



© Orchestra Research · MIT · Orchestra-Research/AI-Research-SKILLs

Похожие скиллы

Всё по теме «ИИ и ML» →
llm-testing Eyadkelleh Набор готовых сценариев и промптов для аудита безопасности нейросетей: выявляет предвзятость, утечки данных и уязвимости к состязательным атакам. CX Codex +1 544установок ai-risk-management briiirussell Скилл внедряет фреймворк NIST AI RMF 1.0 для проверки моделей машинного обучения. Он пригодится при аудите соответствия EU AI Act или подготовке отчётов по рискам. CX Codex +1 313установок ai-prompt-engineering-safety-best-practices официальный github Правило помогает формулировать запросы к ИИ с ясным контекстом, ограничениями и требованиями к формату. Оно также предлагает учитывать безопасность и предвзятость при создании и проверке промптов. ★ 40 418звёзд репо gpt-image-2 prime-skills Генерирует и редактирует изображения через OpenAI GPT Image 2 на RunComfy. Точно встраивает текст, логотипы и многоязычную типографику — для e-commerce, рекламы и UI-макетов. CX Codex +1 84 300установок firecrawl-research-index firecrawl Скилл ищет статьи по исследовательскому запросу в индексе Firecrawl: семантический поиск по абстрактам, расширение по цитатам и проверка по полному тексту. CX Codex +1 24 400установок tavily-best-practices tavily-ai Скилл даёт агенту справку по интеграции Tavily для поиска, извлечения контента, обхода сайтов и исследований. Нужен при добавлении веб-данных в агентный сценарий или RAG. CX Codex +1 20 700установок

Из того же набора66

autogpt-agents Скилл помогает создавать и развёртывать автономных агентов на базе AutoGPT с визуальными графами и запуском по триггерам. Пригодится при настройке многоэтапной автоматизации. CX Codex +1 944установок nemo-guardrails Скилл помогает настраивать runtime-защиту LLM-приложений: от проверки jailbreak до фильтрации PII. Пригодится для правил на Colang 2.0. CX Codex +1 944установок nemo-evaluator-sdk Скилл запускает оценку языковых моделей на 100+ бенчмарках из 18+ харнессов: MMLU, HumanEval, GSM8K, safety и VLM. Пригодится, когда нужно сравнить модели воспроизводимо. CX Codex +1 943установок pinecone Скилл для работы с управляемой векторной базой Pinecone: индексы, upsert, поиск с фильтрами и гибридный поиск. Пригодится для RAG и семантического поиска в проде. CX Codex +1 942установок phoenix-observability Скилл подключает Phoenix — открытую платформу наблюдаемости, чтобы разбирать трейсы LLM-приложений, гонять оценки на датасетах и следить за продом. CX Codex +1 941установок llava Скилл для работы с LLaVA: модель отвечает на вопросы по изображениям и ведёт многоходовой диалог о картинке. Пригодится, когда нужен чат по фото или документам. CX Codex +1 940установок lambda-labs-gpu-cloud Скилл помогает запускать GPU-инстансы Lambda Labs для обучения и инференса ML-моделей. Он пригодится для SSH-доступа, постоянного хранилища и кластеров. CX Codex +1 939установок presenting-conference-talks Скилл преобразует готовую научную статью в слайды презентации Beamer LaTeX и PPTX с заметками докладчика. Подходит для подготовки докладов на конференциях по ML и Computer Systems. CX Codex +1 913установок

Спонсоры проекта

Компании, которые помогают держать аналитику открытой