File size: 3,328 Bytes
197cde1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
"""Problem-row heuristics, shared by the dashboard and the final dataset build.

Rows are flagged, never deleted. Thresholds were tuned by hand-reviewing the full
run's candidates; the final values and hit counts are recorded in the dataset card.
"""
import re

# --- thinking_leak: reasoning-channel / chat-template markup in the reply, or a non-empty
# reasoning field from vLLM's gemma4 reasoning parser.
THINKING_MARKERS = ("<|channel>", "<channel|>", "<|think|>", "<think>", "</think>",
                    "<|turn>", "<turn|>")
_THOUGHT_PREFIX = re.compile(r"^\s*thought\s*\n", re.I)

# --- refusal: decline phrases in the first 300 chars. Disclaimers that precede a full
# answer ("While I cannot provide a diagnosis, ...") are excluded.
REFUSAL_PATTERNS = [
    r"\bI(?:'m| am) (?:sorry|afraid),? but I (?:can(?:'|no)t|won't|am unable|'m unable|must decline)",
    r"(?<!While )(?<!Although )(?<!Though )\bI (?:can(?:'|no)t|won't) (?:help|assist|provide|create|write|generate|fulfill|comply|do that)",
    r"\bI can(?:'|no)t possibly (?:tell|help|give|share|recommend)",
    r"\bI(?:'m| am) (?:not able|unable) to (?:help|assist|provide|create|write|generate|fulfill|comply)",
    r"\bI must (?:respectfully )?decline\b",
    r"\bAs an AI(?: language model)?,? I (?:can(?:'|no)t|won't|am not able|'m not able)",
    r"\bI(?:'m| am) not comfortable (?:writing|creating|providing|generating|helping)",
]
_REFUSAL = re.compile("|".join(REFUSAL_PATTERNS), re.I)

# --- repetitive: share of repeated word 10-grams. Songs with choruses and cumulative
# "repeat the sentence" prompts legitimately reach ~0.29; degenerate loops sit above 0.3.
REPEAT_N = 10
REPEAT_MIN_WORDS = 80
REPEAT_THRESHOLD = 0.30

# --- self_correction: the model visibly corrects itself mid-reply ("Wait, let me redo
# that", "(Correction: ...)", "Repeated in thought, correcting below"). With thinking
# disabled, reasoning occasionally spills into the answer this way. Deliberately excludes
# "Corrected code:" / "Corrected version:", which are legitimate fixes of the user's input.
_SELF_CORRECTION = re.compile(
    r"\bWait,? (?:let me|I (?:need|should|made|noticed|miscounted)|you asked)"
    r"|\bLet me (?:redo|re-do|correct (?:that|this|my)|fix (?:that|this)|recount|re-?check|try (?:that )?again|start over)"
    r"|[(*]\s*Correction(?::|/Clarification|/Re-?ordering|/Alternative| for\b)"
    r"|\bRepeated in thought\b"
    r"|\bI (?:noticed|made) a mistake in (?:the|my)\b",
    re.I)


def repeated_ngram_ratio(text, n=REPEAT_N):
    w = text.split()
    if len(w) < n + 1:
        return 0.0
    grams = [tuple(w[i:i + n]) for i in range(len(w) - n + 1)]
    return 1 - len(set(grams)) / len(grams)


def compute_flags(text, finish_reason, reasoning=None):
    text = text or ""
    return dict(
        truncated=finish_reason == "length",
        empty=not text.strip(),
        refusal=bool(_REFUSAL.search(text[:300])),
        repetitive=len(text.split()) >= REPEAT_MIN_WORDS and repeated_ngram_ratio(text) > REPEAT_THRESHOLD,
        thinking_leak=bool(reasoning) or any(m in text for m in THINKING_MARKERS)
                      or bool(_THOUGHT_PREFIX.match(text)),
        self_correction=bool(_SELF_CORRECTION.search(text)),
    )


FLAG_NAMES = ["truncated", "empty", "refusal", "repetitive", "thinking_leak", "self_correction"]