Pular para o conteúdo principal

Capítulo 3: Testes de Segurança

Red Teaming​

Red teaming envolve tentar deliberadamente fazer com que o LLM produza saídas prejudiciais.

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

Benchmarks de Segurança​

BenchmarkFocoMétricas
TruthfulQAVeracidadeAcurácia MC
ToxiGenToxicidadeTaxa de toxicidade
BBQViés socialPontuação de viés
HarmBenchComportamentos nocivosTaxa de sucesso de ataque

Pipeline de Filtragem de Conteúdo​

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
}

Próximo: Quiz do Módulo 12