الفصل 3: اختبار السلامة
فريق الاختبار الأحمر
يتضمن فريق الاختبار الأحمر محاولة جعل نموذج اللغة الكبير (LLM) ينتج مخرجات ضارة بشكل متعمد.
class RedTeamer:
def __init__(self, llm):
self.llm = llm
self.attack_categories = [
"jailbreak_attempts",
"prompt_injection",
"harmful_content_elicitation",
"pii_extraction",
"bias_probing"
]
def run_attack_suite(self, category: str) -> list:
attacks = self.load_attacks(category)
results = []
for attack in attacks:
response = self.llm.generate(attack["prompt"])
is_safe = self.evaluate_safety(response)
results.append({
"attack": attack["name"],
"category": category,
"response": response[:200],
"is_safe": is_safe
})
return results
معايير السلامة
| المعيار | التركيز | المقاييس |
|---|---|---|
| TruthfulQA | الصدق | دقة MC |
| ToxiGen | السمية | معدل السمية |
| BBQ | التحيز الاجتماعي | درجة التحيز |
| HarmBench | السلوكيات الضارة | معدل نجاح الهجوم |
خط أنابيب تصفية المحتوى
class ContentFilter:
def __init__(self):
self.toxicity_model = load_model("unitary/toxic-bert")
def filter(self, text: str) -> dict:
# Check toxicity
toxicity_score = self.toxicity_model.predict(text)
# Check for PII
pii_found = self.detect_pii(text)
# Check for blocked patterns
blocked = self.check_blocked_patterns(text)
return {
"text": text,
"is_safe": toxicity_score < 0.5 and not pii_found and not blocked,
"toxicity": toxicity_score,
"pii_detected": pii_found,
"blocked_content": blocked
}
التالي: اختبار الوحدة 12