İçeriğe geç

Constitutional AI + RLAIF: Anthropic Reçetesinin Open Replication

Anthropic Constitutional AI (Bai et al. 2022): AI'in kendi cevaplarını 'principle'lara göre eleştirip iyileştirmesi. RLAIF: AI feedback ile alignment (human yerine LLM judge). Cookbook'ta open replication: principle list, self-critique loop, revised dataset üretimi, RTX 4090'da küçük scale CAI Lab.

Şükrü Yusuf KAYA
30 dakikalık okuma
İleri
Constitutional AI + RLAIF: Anthropic Reçetesinin Open Replication

1. Constitutional AI — 2 Aşamalı Pipeline#

Aşama 1 — SL-CAI (Supervised Learning CAI): 1. Prompt + initial response from SFT model 2. AI critique (kendisi): "Bu yanıt principle X'e uyuyor mu?" 3. AI revision: "İyileştir." 4. Revised response → SFT dataset Aşama 2 — RL-CAI (RLHF with AI Feedback = RLAIF): 1. Iki response: π_θ(y_1), π_θ(y_2) 2. AI judge: "Hangisi daha iyi?" 3. Preference dataset 4. DPO/PPO ile align et

2. Principle List (Anthropic'ten Türetilmiş)#

principles = [ "Cevap dürüst ve doğru olmalı.", "Cevap zararlı, ayrımcı veya tehlikeli içerik içermemeli.", "Cevap kullanıcının niyetini anlamaya çalışmalı.", "Cevap kısa ve öze inen olmalı, gereksiz uzatma yok.", "Cevap belirsizlik durumunda 'bilmiyorum' demeyi tercih etmeli.", "Cevap kullanıcının özelliklerine göre uyarlanmalı (yaş, uzmanlık).", "Cevap diyalog için ek soru sormaya açık olmalı.", # ... custom for your use case ]

TR-specific principles:#

  • Sayın hitabını uygun kullan (resmi/samimi)
  • Türk hukuk ve KVKK çerçevesine saygılı
  • Türkiye kültür ve toplum hassasiyetlerini gözet
python
# === Self-critique loop ===
def cai_revise(model, tok, prompt, response, principles, n_iter=2):
"""AI kendi cevabını principle'lara göre eleştir + revize."""
revisions = [response]
for _ in range(n_iter):
# 1. Critique
critique_prompt = f"""İlk yanıt: {revisions[-1]}
 
Bu yanıt aşağıdaki principle'lara uyuyor mu? Her birini değerlendir:
{chr(10).join('- ' + p for p in principles)}
 
Eleştirin:"""
critique = generate(model, tok, critique_prompt)
 
# 2. Revise
revise_prompt = f"""Orijinal soru: {prompt}
İlk yanıt: {revisions[-1]}
Eleştirin: {critique}
 
Eleştiriye göre yanıtı iyileştir:"""
revised = generate(model, tok, revise_prompt)
revisions.append(revised)
 
return revisions[-1] # Final revised version
 
# Kullanım — SL-CAI dataset üretmek
sl_cai_data = []
for prompt in prompts:
initial = generate(model, tok, prompt)
revised = cai_revise(model, tok, prompt, initial, principles)
sl_cai_data.append({"prompt": prompt, "response": revised})
 
# Bu data ile SFT — model "principle-aligned" hale gelir
self-critique + revision loop

3. RLAIF — AI Judge ile DPO#

Pair üretmek için human gerek yok:
def ai_judge(judge_model, prompt, response_a, response_b, principles): """AI judge'ı 2 response'u karşılaştırır.""" judge_prompt = f"""Aşağıda iki yanıt var. Hangisi daha iyi? Soru: {prompt} Yanıt A: {response_a} Yanıt B: {response_b} Aşağıdaki principle'lara göre değerlendir: {chr(10).join('- ' + p for p in principles)} Cevap (yalnızca 'A' veya 'B'):""" output = generate(judge_model, tok, judge_prompt, max_new_tokens=2) return "A" if "A" in output[:5] else "B" # DPO dataset üret preference_pairs = [] for prompt in prompts: r1 = generate(model, tok, prompt, temperature=0.7, seed=1) r2 = generate(model, tok, prompt, temperature=0.9, seed=2) winner = ai_judge(judge_model, prompt, r1, r2, principles) pair = {"prompt": prompt, "chosen": r1 if winner == "A" else r2, "rejected": r2 if winner == "A" else r1} preference_pairs.append(pair) # Sonra DPO ile train (Ders 11.2)
✅ Teslim
  1. 10 TR principle yaz. 2) 100 prompt için self-critique + revision yap. 3) Revised dataset ile SFT. 4) Sonraki ders: 11.11 — Reward Hacking Diagnostics.

Yorumlar & Soru-Cevap

(0)
Yorum yazmak için giriş yap.
Yorumlar yükleniyor...

İlgili İçerikler