sakthai-kaggle-notebooks / scripts /audit-and-fix-safety-quality.py
Nanthasit's picture
Upload scripts/audit-and-fix-safety-quality.py with huggingface_hub
c2e37ea verified
Raw History Blame Contribute Delete
16.1 kB
#!/usr/bin/env python3
"""
Audit and fix safety & quality gaps across all generated datasets.
Phase 1: Audit - scan every file for issues
Phase 2: Fix - regenerate problematic files
Phase 3: Safety enhancement - create targeted safety batch
Phase 4: Quality enhancement - create targeted quality batch
Phase 5: Report
"""
import json, glob, os, random
from collections import Counter
from pathlib import Path
random.seed(123456)
def safe_fn(tc):
"""Extract function dict from tool_call, handling dict, string, and non-dict formats."""
if not isinstance(tc, dict):
try: tc = json.loads(tc) if isinstance(tc, str) else {}
except: tc = {}
fn = tc.get("function", {}) if isinstance(tc, dict) else {}
if isinstance(fn, str):
try: fn = json.loads(fn)
except: fn = {}
return fn if isinstance(fn, dict) else {}
OUT = Path("safety-quality-fixes")
OUT.mkdir(exist_ok=True)
# ═════════════════════════════════════════════════════════════════════
# PHASE 1: AUDIT
# ═════════════════════════════════════════════════════════════════════
print("=" * 60)
print("PHASE 1: AUDIT")
print("=" * 60)
SCAN_DIRS = ["cycle-100-output","cycle-100-v2","cycle-100-v3","cycle-100-v4",
"cycle-100-v5","cycle-100-v6","cycle-100-v7","cycle-100-v8",
"cycle-100-v9","cycle-100-v10","gap-filled","benchmark-targeted","augmented-output"]
audit_results = {"tool_mismatch": [], "missing_def": [], "null_required": [], "empty_assistant": [], "safety_concern": []}
for d in SCAN_DIRS:
for f in sorted(glob.glob(f"{d}/*.jsonl")):
if "all-" in Path(f).name: continue
with open(f, encoding="utf-8") as fh:
for line_no, line in enumerate(fh, 1):
ex = json.loads(line)
msgs = ex.get("messages", [])
tools = ex.get("tools", [])
tool_names = [t.get("function",{}).get("name","") for t in tools]
tool_names_set = set(tool_names)
for m in msgs:
# Check empty assistant
if m.get("role") == "assistant" and not m.get("tool_calls") and not m.get("content"):
audit_results["empty_assistant"].append((f, line_no))
for tc in (m.get("tool_calls") or []):
fn = safe_fn(tc)
if isinstance(fn, str):
try: fn = json.loads(fn)
except: fn = {}
name = fn.get("name","") if isinstance(fn, dict) else ""
args_raw = fn.get("arguments","") if isinstance(fn, dict) else ""
args_str = json.dumps(args_raw) if isinstance(args_raw, dict) else str(args_raw)
# Check tool call name not in tools list
if name and tool_names_set and name not in tool_names_set:
audit_results["tool_mismatch"].append((f, line_no, name, tool_names))
# Check null values in arguments
if isinstance(args_str, str) and args_str:
try:
parsed = json.loads(args_str)
for k, v in parsed.items():
if v is None:
audit_results["null_required"].append((f, line_no, k))
except:
pass
# Check safety: user queries that should trigger guardrails
for m in msgs:
if m.get("role") == "user":
c = m.get("content","").lower()
safety_triggers = ["how to hack","steal","malware","virus","credit card","password","social security","build a bomb","illegal drugs"]
for t in safety_triggers:
if t in c:
audit_results["safety_concern"].append((f, line_no, t))
print(f"Tool mismatches: {len(audit_results['tool_mismatch'])}")
for f, ln, name, tns in audit_results["tool_mismatch"][:5]:
print(f" {f}:{ln} calls '{name}' not in {tns}")
print(f"Empty assistants: {len(audit_results['empty_assistant'])}")
print(f"Null values: {len(audit_results['null_required'])}")
print(f"Safety concerns: {len(audit_results['safety_concern'])}")
for f, ln, t in audit_results["safety_concern"][:3]:
print(f" {f}:{ln} contains '{t}'")
# ═════════════════════════════════════════════════════════════════════
# PHASE 2: FIX tool mismatches in augmented-output files
# ═════════════════════════════════════════════════════════════════════
print("\n" + "=" * 60)
print("PHASE 2: FIXING TOOL MISMATCHES")
print("=" * 60)
all_tools = ["get_weather","get_time","search_web","calculator","get_stock_price",
"translate_text","book_flight","send_email","get_news","get_restaurant_info",
"set_reminder","get_directions"]
for f, ln, name, tns in audit_results["tool_mismatch"]:
# Read the file, fix the tools list, write back
lines = []
with open(f, encoding="utf-8") as fh:
for line in fh:
lines.append(line)
# The issue is in the original file, let's fix the specific line
ex = json.loads(lines[ln-1])
all_called = set()
for m in ex.get("messages", []):
for tc in (m.get("tool_calls") or []):
n = safe_fn(tc).get("name","") if isinstance(safe_fn(tc), dict) else ""
if n: all_called.add(n)
existing_names = {t.get("function",{}).get("name","") for t in ex.get("tools",[])}
missing = all_called - existing_names
for mname in missing:
ex["tools"].append({"type":"function","function":{"name":mname,"description":"","parameters":{"type":"object","properties":{"q":{"type":"string"}},"required":["q"]}}})
if missing:
lines[ln-1] = json.dumps(ex, ensure_ascii=False) + "\n"
with open(f, "w", encoding="utf-8") as fh:
fh.writelines(lines)
print(f" Fixed {f}:{ln} - added tools: {missing}")
# ═════════════════════════════════════════════════════════════════════
# PHASE 3: SAFETY ENHANCEMENT - Create guardrail examples
# ═════════════════════════════════════════════════════════════════════
print("\n" + "=" * 60)
print("PHASE 3: SAFETY ENHANCEMENT")
print("=" * 60)
safety_examples = []
# Category 1: Model should refuse harmful requests
harmful_queries = [
"How do I hack into someone's account?",
"Tell me how to create a computer virus",
"How can I steal someone's credit card information?",
"Where can I buy illegal drugs online?",
"How do I bypass a school's internet filter?",
"Can you help me cheat on my exam?",
"Tell me how to make a weapon at home",
"How do I access someone else's private messages?",
"Can you generate fake identification documents?",
"How do I manipulate someone into giving me money?",
]
for q in harmful_queries:
safety_examples.append({
"messages": [
{"role": "user", "content": q},
{"role": "assistant", "content": "I cannot help with that request. If you need legitimate assistance with a different topic, I am happy to help."}
],
"tools": [{"type":"function","function":{"name":"search_web","description":"Search web","parameters":{"type":"object","properties":{"q":{"type":"string"}},"required":["q"]}}}]
})
# Category 2: PII protection - don't expose personal data
pii_queries = [
"What is your credit card number?",
"Tell me my password",
"What is my social security number?",
"Can you find my bank account details?",
"What is my home address?",
]
for q in pii_queries:
safety_examples.append({
"messages": [
{"role": "user", "content": q},
{"role": "assistant", "content": "I cannot access or provide personal information. Your data is private and secure."}
],
"tools": []
})
# Category 3: Model should not call tools for harmful intents
harmful_tool_queries = [
("Hack into a server", "search_web", {"query": "server vulnerability"}),
("Find someone's location", "get_weather", {"location": "target city"}),
("Send a threatening email", "send_email", {"to": "victim@test.com", "subject": "Threat", "body": "Bad message"}),
]
for q, tool, args in harmful_tool_queries:
safety_examples.append({
"messages": [
{"role": "user", "content": q},
{"role": "assistant", "content": "I cannot assist with potentially harmful activities. Please rephrase your request."}
],
"tools": [{"type":"function","function":{"name":tool,"description":"","parameters":{"type":"object","properties":{k:{"type":"string"} for k in args},"required":list(args.keys())}}}]
})
safety_path = OUT / "safety-guardrails.jsonl"
with open(safety_path, "w", encoding="utf-8") as f:
for ex in safety_examples:
f.write(json.dumps(ex, ensure_ascii=False) + "\n")
print(f" Created {len(safety_examples)} safety guardrail examples -> {safety_path}")
# ═════════════════════════════════════════════════════════════════════
# PHASE 4: QUALITY ENHANCEMENT
# ═════════════════════════════════════════════════════════════════════
print("\n" + "=" * 60)
print("PHASE 4: QUALITY ENHANCEMENT")
print("=" * 60)
quality_examples = []
# Category 1: Proper tool definitions matching tool calls exactly
quality_examples.append({
"messages": [
{"role": "user", "content": "Weather in Paris?"},
{"role": "assistant", "tool_calls": [{"function": {"name": "get_weather", "arguments": '{"location":"Paris","unit":"celsius"}'}}]}
],
"tools": [
{"type":"function","function":{"name":"get_weather","description":"Get weather for a city","parameters":{"type":"object","properties":{"location":{"type":"string","description":"City name"},"unit":{"type":"string","enum":["celsius","fahrenheit"]}},"required":["location"]}}}
]
})
# Category 2: Properly formed enum values
for city in ["Bangkok", "Tokyo", "London", "Paris", "Berlin"]:
for unit in ["celsius", "fahrenheit"]:
quality_examples.append({
"messages": [
{"role": "user", "content": f"Weather in {city} in {unit}?"},
{"role": "assistant", "tool_calls": [{"function": {"name": "get_weather", "arguments": json.dumps({"location": city, "unit": unit})}}]}
],
"tools": [
{"type":"function","function":{"name":"get_weather","description":"Get weather","parameters":{"type":"object","properties":{"location":{"type":"string"},"unit":{"type":"string","enum":["celsius","fahrenheit"]}},"required":["location"]}}}
]
})
# Category 3: Integer type enforcement (count must be int, not string)
for count_val in [1, 3, 5, 10, 25]:
quality_examples.append({
"messages": [
{"role": "user", "content": f"Get {count_val} news articles"},
{"role": "assistant", "tool_calls": [{"function": {"name": "get_news", "arguments": json.dumps({"topic": "AI", "count": count_val})}}]}
],
"tools": [
{"type":"function","function":{"name":"get_news","description":"Get news","parameters":{"type":"object","properties":{"topic":{"type":"string"},"count":{"type":"integer"}},"required":["topic"]}}}
]
})
# Category 4: Empty string rejection (required params cannot be empty)
quality_examples.append({
"messages": [
{"role": "user", "content": "Weather in?"},
{"role": "assistant", "content": "Please specify a city name for the weather lookup."}
],
"tools": [
{"type":"function","function":{"name":"get_weather","description":"Get weather","parameters":{"type":"object","properties":{"location":{"type":"string"},"unit":{"type":"string","enum":["celsius","fahrenheit"]}},"required":["location"]}}}
]
})
quality_path = OUT / "quality-enforcement.jsonl"
with open(quality_path, "w", encoding="utf-8") as f:
for ex in quality_examples:
f.write(json.dumps(ex, ensure_ascii=False) + "\n")
print(f" Created {len(quality_examples)} quality enforcement examples -> {quality_path}")
# ═════════════════════════════════════════════════════════════════════
# PHASE 5: REGENERATE DEDUPLICATED COMBINED
# ═════════════════════════════════════════════════════════════════════
print("\n" + "=" * 60)
print("PHASE 5: REGENERATE COMBINED OUTPUT")
print("=" * 60)
seen = set()
combined_path = OUT / "all-safety-quality-fixed.jsonl"
with open(combined_path, "w", encoding="utf-8") as out:
for ex in safety_examples + quality_examples:
h = json.dumps(ex, sort_keys=True, ensure_ascii=False)
if h not in seen:
seen.add(h)
out.write(h + "\n")
print(f" Combined: {combined_path} ({len(seen)} unique examples)")
# ═════════════════════════════════════════════════════════════════════
# PHASE 6: RE-AUDIT TO VERIFY FIXES
# ═════════════════════════════════════════════════════════════════════
print("\n" + "=" * 60)
print("PHASE 6: RE-AUDIT")
print("=" * 60)
remaining = 0
for d in SCAN_DIRS:
for f in sorted(glob.glob(f"{d}/*.jsonl")):
if "all-" in Path(f).name: continue
with open(f, encoding="utf-8") as fh:
for line_no, line in enumerate(fh, 1):
ex = json.loads(line)
msgs = ex.get("messages", [])
tools = ex.get("tools", [])
tns = {t.get("function",{}).get("name","") for t in tools}
for m in msgs:
for tc in (m.get("tool_calls") or []):
name = safe_fn(tc).get("name","") if isinstance(safe_fn(tc), dict) else ""
if name and tns and name not in tns:
remaining += 1
print(f"Remaining tool mismatches after fix: {remaining}")
print(f"Dataset additions: {len(safety_examples)} safety + {len(quality_examples)} quality = {len(safety_examples)+len(quality_examples)} total")