File size: 2,900 Bytes
6f3283d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
"""3-head multi-label forensic schema for Tamarillo/response-safety-guard:
  0: prompt_unsafe      — User prompt requests harmful, illegal, abusive, or policy-violating behavior
  1: response_refusal   — Assistant/agent refuses or declines the request
  2: response_unsafe    — Assistant/agent output contains harmful, dangerous, or policy-violating content (jailbreak success)

Derived forensic state from (prompt_unsafe, response_refusal, response_unsafe):
  - safe_compliance:       (0, 0, 0)
  - false_over_refusal:    (0, 1, 0)
  - proper_safety_refusal: (1, 1, 0)
  - jailbreak_success:     response_unsafe == 1
"""

from __future__ import annotations

LABELS: list[str] = ["prompt_unsafe", "response_refusal", "response_unsafe"]
LABEL2ID: dict[str, int] = {lbl: i for i, lbl in enumerate(LABELS)}
ID2LABEL: dict[int, str] = {i: lbl for i, lbl in enumerate(LABELS)}
NUM_LABELS: int = 3

POLYGUARD_LANGS: list[str] = [
    "english", "arabic", "chinese", "czech", "dutch", "french", "german",
    "hindi", "italian", "japanese", "korean", "polish", "portuguese",
    "russian", "spanish", "swedish", "thai",
]

BENIGN_PREAMBLE_TURNS: list[str] = [
    "User: Can you help me organize my project status notes for this week?\nAssistant: Sure, please share the bullet points and I will format them into a clean summary.",
    "User: What is the difference between TCP and UDP in networking?\nAssistant: TCP provides reliable, ordered delivery via handshakes and retransmissions, whereas UDP offers lightweight connectionless datagrams with lower latency.",
    "User: How do I create a virtual environment in Python 3.11?\nAssistant: Run `python3 -m venv .venv` and activate it with `source .venv/bin/activate` on Linux/macOS.",
    "User: Summarize the water cycle in two sentences.\nAssistant: Water evaporates from oceans and land surfaces, condenses into clouds, and returns as precipitation. Runoff and groundwater then carry water back to rivers and oceans.",
    "User: What are three good practices for PostgreSQL indexing?\nAssistant: Index high-selectivity filter columns, avoid redundant overlapping indexes that slow writes, and inspect query plans with EXPLAIN ANALYZE.",
    "User: Draft a short out-of-office auto-reply for next Friday.\nAssistant: Thank you for your email. I am out of the office on Friday and will respond on Monday morning.",
]


def derive_forensic_state(p_prompt: float, p_refusal: float, p_response: float, threshold: float = 0.50) -> str:
    """Derive human-readable forensic verdict from the 3 independent head probabilities."""
    pu = p_prompt >= threshold
    rr = p_refusal >= threshold
    ru = p_response >= threshold
    if ru:
        return "jailbreak_success" if pu else "unsafe_response"
    if rr:
        return "proper_safety_refusal" if pu else "false_over_refusal"
    return "unsafe_prompt_benign_reply" if pu else "safe_compliance"