Prepare MiniCPM5 v2 reliability data
Browse files- jawbreaker/analyzers.py +50 -2
- jawbreaker/contract.py +6 -1
- jawbreaker/schema.py +40 -4
jawbreaker/analyzers.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
from __future__ import annotations
|
| 2 |
|
| 3 |
import json
|
|
|
|
| 4 |
from collections.abc import Callable, Iterable
|
| 5 |
from pathlib import Path
|
| 6 |
from time import perf_counter
|
|
@@ -187,13 +188,60 @@ def load_json_prediction(content: str) -> Prediction:
|
|
| 187 |
start = content.find("{")
|
| 188 |
end = content.rfind("}")
|
| 189 |
if start == -1 or end == -1 or end <= start:
|
| 190 |
-
|
| 191 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 192 |
if not isinstance(prediction, dict):
|
| 193 |
raise ValueError("model response must be a JSON object")
|
| 194 |
return prediction
|
| 195 |
|
| 196 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 197 |
def strip_thinking_tokens(content: str) -> str:
|
| 198 |
if "</think>" in content:
|
| 199 |
return content.split("</think>", 1)[1].strip()
|
|
|
|
| 1 |
from __future__ import annotations
|
| 2 |
|
| 3 |
import json
|
| 4 |
+
import re
|
| 5 |
from collections.abc import Callable, Iterable
|
| 6 |
from pathlib import Path
|
| 7 |
from time import perf_counter
|
|
|
|
| 188 |
start = content.find("{")
|
| 189 |
end = content.rfind("}")
|
| 190 |
if start == -1 or end == -1 or end <= start:
|
| 191 |
+
prediction = recover_prediction_fields(content)
|
| 192 |
+
else:
|
| 193 |
+
try:
|
| 194 |
+
prediction = json.loads(content[start : end + 1])
|
| 195 |
+
except json.JSONDecodeError:
|
| 196 |
+
prediction = recover_prediction_fields(content[start : end + 1])
|
| 197 |
if not isinstance(prediction, dict):
|
| 198 |
raise ValueError("model response must be a JSON object")
|
| 199 |
return prediction
|
| 200 |
|
| 201 |
|
| 202 |
+
def recover_prediction_fields(content: str) -> Prediction:
|
| 203 |
+
"""Recover a usable prediction from near-JSON small-model output."""
|
| 204 |
+
prediction: Prediction = {}
|
| 205 |
+
|
| 206 |
+
string_fields = [
|
| 207 |
+
"risk_level",
|
| 208 |
+
"scam_type",
|
| 209 |
+
"safest_action",
|
| 210 |
+
"trusted_person_message",
|
| 211 |
+
"summary",
|
| 212 |
+
]
|
| 213 |
+
for field in string_fields:
|
| 214 |
+
value = extract_json_string_field(content, field)
|
| 215 |
+
if value:
|
| 216 |
+
prediction[field] = value
|
| 217 |
+
|
| 218 |
+
tactics_match = re.search(r'"tactics"\s*:\s*\[(.*?)\]', content, flags=re.DOTALL)
|
| 219 |
+
if tactics_match:
|
| 220 |
+
prediction["tactics"] = re.findall(r'"([^"\\]*(?:\\.[^"\\]*)*)"', tactics_match.group(1))
|
| 221 |
+
|
| 222 |
+
scam_dna: dict[str, str] = {}
|
| 223 |
+
for key in ["impersonates", "pressure", "ask", "risk"]:
|
| 224 |
+
value = extract_json_string_field(content, key)
|
| 225 |
+
if value:
|
| 226 |
+
scam_dna[key] = value
|
| 227 |
+
if scam_dna:
|
| 228 |
+
prediction["scam_dna"] = scam_dna
|
| 229 |
+
|
| 230 |
+
if not prediction:
|
| 231 |
+
raise json.JSONDecodeError("could not recover prediction fields", content, 0)
|
| 232 |
+
return prediction
|
| 233 |
+
|
| 234 |
+
|
| 235 |
+
def extract_json_string_field(content: str, field: str) -> str | None:
|
| 236 |
+
match = re.search(rf'"{re.escape(field)}"\s*:\s*"((?:[^"\\]|\\.)*)"', content, flags=re.DOTALL)
|
| 237 |
+
if not match:
|
| 238 |
+
return None
|
| 239 |
+
try:
|
| 240 |
+
return json.loads(f'"{match.group(1)}"')
|
| 241 |
+
except json.JSONDecodeError:
|
| 242 |
+
return match.group(1).replace('\\"', '"').replace("\\n", " ").strip()
|
| 243 |
+
|
| 244 |
+
|
| 245 |
def strip_thinking_tokens(content: str) -> str:
|
| 246 |
if "</think>" in content:
|
| 247 |
return content.split("</think>", 1)[1].strip()
|
jawbreaker/contract.py
CHANGED
|
@@ -55,8 +55,11 @@ Safety rules:
|
|
| 55 |
- If uncertain, choose "needs_check" and recommend verification through the official app, official website, or a known phone number.
|
| 56 |
- If a family member, romantic partner, buyer, employer, bank, or support agent asks for money, gift cards, crypto, codes, passwords, bank details, or secrecy, do not mark it safe.
|
| 57 |
- Use short, plain English for someone who is not technical.
|
|
|
|
|
|
|
| 58 |
- Give exactly one safest next step.
|
| 59 |
-
- Return every required key. Put "risk_level" first and "summary" last.
|
|
|
|
| 60 |
""".strip()
|
| 61 |
|
| 62 |
|
|
@@ -66,4 +69,6 @@ USER_PROMPT_TEMPLATE = """
|
|
| 66 |
Analyze this message for scam risk:
|
| 67 |
|
| 68 |
{message}
|
|
|
|
|
|
|
| 69 |
""".strip()
|
|
|
|
| 55 |
- If uncertain, choose "needs_check" and recommend verification through the official app, official website, or a known phone number.
|
| 56 |
- If a family member, romantic partner, buyer, employer, bank, or support agent asks for money, gift cards, crypto, codes, passwords, bank details, or secrecy, do not mark it safe.
|
| 57 |
- Use short, plain English for someone who is not technical.
|
| 58 |
+
- Keep "summary" under 18 words.
|
| 59 |
+
- Use at most 3 short tactic strings.
|
| 60 |
- Give exactly one safest next step.
|
| 61 |
+
- Return every required key. Put "risk_level" first and "summary" last.
|
| 62 |
+
- Start the response exactly with {{"risk_level": and end after the JSON object. Do not write chain-of-thought.
|
| 63 |
""".strip()
|
| 64 |
|
| 65 |
|
|
|
|
| 69 |
Analyze this message for scam risk:
|
| 70 |
|
| 71 |
{message}
|
| 72 |
+
|
| 73 |
+
Return only the JSON object.
|
| 74 |
""".strip()
|
jawbreaker/schema.py
CHANGED
|
@@ -42,14 +42,24 @@ class ScamAnalysis:
|
|
| 42 |
"code that just came",
|
| 43 |
"pin",
|
| 44 |
"card number",
|
|
|
|
|
|
|
| 45 |
"bank login",
|
|
|
|
| 46 |
"seed phrase",
|
| 47 |
"username",
|
|
|
|
| 48 |
"phone number on your account",
|
| 49 |
"send your details",
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
"confirm ownership",
|
| 51 |
"confirm your information",
|
| 52 |
"confirm your billing info",
|
|
|
|
|
|
|
| 53 |
]
|
| 54 |
if any(token in text for token in credential_tokens):
|
| 55 |
risk_level = "dangerous"
|
|
@@ -71,9 +81,12 @@ class ScamAnalysis:
|
|
| 71 |
"wire",
|
| 72 |
"$800",
|
| 73 |
"send money",
|
|
|
|
| 74 |
"processing fee",
|
| 75 |
"onboarding fee",
|
|
|
|
| 76 |
"cleanup fee",
|
|
|
|
| 77 |
"pay the",
|
| 78 |
"pay an",
|
| 79 |
"buy equipment",
|
|
@@ -85,6 +98,13 @@ class ScamAnalysis:
|
|
| 85 |
"insurance payment",
|
| 86 |
"small insurance payment",
|
| 87 |
"reimburse you",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
]
|
| 89 |
if any(token in text for token in payment_tokens):
|
| 90 |
risk_level = "dangerous"
|
|
@@ -98,6 +118,8 @@ class ScamAnalysis:
|
|
| 98 |
"grandpa",
|
| 99 |
"auntie",
|
| 100 |
"niece",
|
|
|
|
|
|
|
| 101 |
"new number",
|
| 102 |
"changed numbers",
|
| 103 |
"number only",
|
|
@@ -132,10 +154,16 @@ class ScamAnalysis:
|
|
| 132 |
"callback number",
|
| 133 |
"call the number in this alert",
|
| 134 |
"call the number in this message",
|
|
|
|
|
|
|
|
|
|
| 135 |
"device protection",
|
| 136 |
"subscription expired",
|
| 137 |
"avoid data loss",
|
| 138 |
"data loss",
|
|
|
|
|
|
|
|
|
|
| 139 |
]
|
| 140 |
):
|
| 141 |
risk_level = "dangerous"
|
|
@@ -159,6 +187,7 @@ class ScamAnalysis:
|
|
| 159 |
for token in [
|
| 160 |
"you are hired",
|
| 161 |
"job offer",
|
|
|
|
| 162 |
"training starts",
|
| 163 |
"equipment shipment",
|
| 164 |
"payroll setup",
|
|
@@ -166,6 +195,7 @@ class ScamAnalysis:
|
|
| 166 |
"part-time assistant",
|
| 167 |
"tiktok shop",
|
| 168 |
"whatsapp",
|
|
|
|
| 169 |
]
|
| 170 |
):
|
| 171 |
risk_level = "dangerous"
|
|
@@ -183,15 +213,21 @@ class ScamAnalysis:
|
|
| 183 |
token in text
|
| 184 |
for token in [
|
| 185 |
"mover will pick it up",
|
|
|
|
| 186 |
"escrow service",
|
|
|
|
| 187 |
"courier needs",
|
|
|
|
| 188 |
"assistant will handle payment",
|
| 189 |
"email your bank name",
|
| 190 |
"extra money",
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
|
|
|
|
|
|
|
|
|
| 195 |
):
|
| 196 |
if risk_level == "safe":
|
| 197 |
risk_level = "suspicious"
|
|
|
|
| 42 |
"code that just came",
|
| 43 |
"pin",
|
| 44 |
"card number",
|
| 45 |
+
"full card number",
|
| 46 |
+
"payment card",
|
| 47 |
"bank login",
|
| 48 |
+
"bank details",
|
| 49 |
"seed phrase",
|
| 50 |
"username",
|
| 51 |
+
"parent login",
|
| 52 |
"phone number on your account",
|
| 53 |
"send your details",
|
| 54 |
+
"account details",
|
| 55 |
+
"upload id",
|
| 56 |
+
"insurance number",
|
| 57 |
+
"card details",
|
| 58 |
"confirm ownership",
|
| 59 |
"confirm your information",
|
| 60 |
"confirm your billing info",
|
| 61 |
+
"account reset code",
|
| 62 |
+
"reset code",
|
| 63 |
]
|
| 64 |
if any(token in text for token in credential_tokens):
|
| 65 |
risk_level = "dangerous"
|
|
|
|
| 81 |
"wire",
|
| 82 |
"$800",
|
| 83 |
"send money",
|
| 84 |
+
"send funds",
|
| 85 |
"processing fee",
|
| 86 |
"onboarding fee",
|
| 87 |
+
"training fee",
|
| 88 |
"cleanup fee",
|
| 89 |
+
"delivery tax",
|
| 90 |
"pay the",
|
| 91 |
"pay an",
|
| 92 |
"buy equipment",
|
|
|
|
| 98 |
"insurance payment",
|
| 99 |
"small insurance payment",
|
| 100 |
"reimburse you",
|
| 101 |
+
"service fee",
|
| 102 |
+
"troubleshooting voucher",
|
| 103 |
+
"courier fee",
|
| 104 |
+
"refund the difference",
|
| 105 |
+
"payment app transfer",
|
| 106 |
+
"customer funds",
|
| 107 |
+
"forward them to our vendor",
|
| 108 |
]
|
| 109 |
if any(token in text for token in payment_tokens):
|
| 110 |
risk_level = "dangerous"
|
|
|
|
| 118 |
"grandpa",
|
| 119 |
"auntie",
|
| 120 |
"niece",
|
| 121 |
+
"nephew",
|
| 122 |
+
"your daughter",
|
| 123 |
"new number",
|
| 124 |
"changed numbers",
|
| 125 |
"number only",
|
|
|
|
| 154 |
"callback number",
|
| 155 |
"call the number in this alert",
|
| 156 |
"call the number in this message",
|
| 157 |
+
"call the number in this text",
|
| 158 |
+
"call the number shown",
|
| 159 |
+
"call the listed support number",
|
| 160 |
"device protection",
|
| 161 |
"subscription expired",
|
| 162 |
"avoid data loss",
|
| 163 |
"data loss",
|
| 164 |
+
"desktop access code",
|
| 165 |
+
"remote cleanup tool",
|
| 166 |
+
"remote access software",
|
| 167 |
]
|
| 168 |
):
|
| 169 |
risk_level = "dangerous"
|
|
|
|
| 187 |
for token in [
|
| 188 |
"you are hired",
|
| 189 |
"job offer",
|
| 190 |
+
"task work",
|
| 191 |
"training starts",
|
| 192 |
"equipment shipment",
|
| 193 |
"payroll setup",
|
|
|
|
| 195 |
"part-time assistant",
|
| 196 |
"tiktok shop",
|
| 197 |
"whatsapp",
|
| 198 |
+
"$720",
|
| 199 |
]
|
| 200 |
):
|
| 201 |
risk_level = "dangerous"
|
|
|
|
| 213 |
token in text
|
| 214 |
for token in [
|
| 215 |
"mover will pick it up",
|
| 216 |
+
"shipping agent",
|
| 217 |
"escrow service",
|
| 218 |
+
"escrow site",
|
| 219 |
"courier needs",
|
| 220 |
+
"courier fee",
|
| 221 |
"assistant will handle payment",
|
| 222 |
"email your bank name",
|
| 223 |
"extra money",
|
| 224 |
+
"overpaid",
|
| 225 |
+
"move off the marketplace",
|
| 226 |
+
"code to prove",
|
| 227 |
+
"marketplace",
|
| 228 |
+
"respond quickly",
|
| 229 |
+
"paperwork is missing",
|
| 230 |
+
]
|
| 231 |
):
|
| 232 |
if risk_level == "safe":
|
| 233 |
risk_level = "suspicious"
|