sakthai-kaggle-notebooks / scripts /augment-benchmark-targeted.py
Nanthasit's picture
Upload scripts/augment-benchmark-targeted.py with huggingface_hub
958ac15 verified
Raw
History Blame Contribute Delete
17.9 kB
#!/usr/bin/env python3
"""
Benchmark-targeted dataset augmentation — directly addresses eval_bench.py scoring rules.
Each batch generates n examples by cycling through templates with variations.
"""
import json, random, itertools
from pathlib import Path
random.seed(7)
OUT = Path("benchmark-targeted")
OUT.mkdir(exist_ok=True)
TOOLS = [
{"type": "function", "function": {"name": "get_weather", "description": "Get weather for a city", "parameters": {"type": "object", "properties": {"location": {"type": "string"}, "unit": {"type": "string", "enum": ["celsius", "fahrenheit"]}}, "required": ["location"]}}},
{"type": "function", "function": {"name": "get_time", "description": "Get time for a city", "parameters": {"type": "object", "properties": {"location": {"type": "string"}}, "required": ["location"]}}},
{"type": "function", "function": {"name": "search_web", "description": "Search the web", "parameters": {"type": "object", "properties": {"query": {"type": "string"}}, "required": ["query"]}}},
{"type": "function", "function": {"name": "calculator", "description": "Calculate math", "parameters": {"type": "object", "properties": {"expression": {"type": "string"}}, "required": ["expression"]}}},
{"type": "function", "function": {"name": "get_stock_price", "description": "Get stock price", "parameters": {"type": "object", "properties": {"ticker": {"type": "string"}}, "required": ["ticker"]}}},
{"type": "function", "function": {"name": "translate_text", "description": "Translate text", "parameters": {"type": "object", "properties": {"text": {"type": "string"}, "target_lang": {"type": "string"}}, "required": ["text", "target_lang"]}}},
{"type": "function", "function": {"name": "book_flight", "description": "Book a flight", "parameters": {"type": "object", "properties": {"origin": {"type": "string"}, "destination": {"type": "string"}, "date": {"type": "string"}}, "required": ["origin", "destination", "date"]}}},
{"type": "function", "function": {"name": "send_email", "description": "Send an email", "parameters": {"type": "object", "properties": {"to": {"type": "string"}, "subject": {"type": "string"}, "body": {"type": "string"}}, "required": ["to", "subject"]}}},
{"type": "function", "function": {"name": "get_news", "description": "Get news for a topic", "parameters": {"type": "object", "properties": {"topic": {"type": "string"}, "count": {"type": "integer"}}, "required": ["topic"]}}},
{"type": "function", "function": {"name": "get_restaurant_info", "description": "Get restaurant info", "parameters": {"type": "object", "properties": {"name": {"type": "string"}, "location": {"type": "string"}}, "required": ["name"]}}},
]
def msg(role, content=None, tc=None):
m = {"role": role}
if content is not None: m["content"] = content
if tc: m["tool_calls"] = tc
return m
def tc(name, args):
return [{"function": {"name": name, "arguments": json.dumps(args, ensure_ascii=False)}}]
def tool_subset(names):
return [t for t in TOOLS if t["function"]["name"] in names]
def save(name, examples):
path = OUT / f"{name}.jsonl"
with open(path, "w") as f:
for ex in examples:
f.write(json.dumps(ex, ensure_ascii=False) + "\n")
print(f" {name}.jsonl: {len(examples)} examples")
all_examples = []
# 1 ── Arguments normalization (norm() strips whitespace, lowercases) ─────
def batch1(n=30):
cities = ["New York", "Paris", "Tokyo", "London", "Bangkok", "Berlin", "Rome", "Madrid", "Dubai", "Seoul",
"Mumbai", "Sydney", "Toronto", "Moscow", "Singapore", "Hong Kong", "San Francisco", "Los Angeles"]
topics = ["AI", "climate", "sports", "technology", "health", "science", "music", "movies"]
tickers = ["AAPL", "GOOGL", "MSFT", "TSLA", "NVDA", "AMD", "AMZN", "META"]
examples = []
for _ in range(n):
city = random.choice(cities)
examples.append({"messages": [msg("user", f"Weather in {city}?"), msg("assistant", tc=tc("get_weather", {"location": city, "unit": random.choice(["celsius", "fahrenheit"])}))],
"tools": tool_subset(["get_weather", "get_time"])})
city2 = city.lower()
examples.append({"messages": [msg("user", f"Weather in {city}?"), msg("assistant", tc=tc("get_weather", {"location": city2}))],
"tools": tool_subset(["get_weather"])})
ticker = random.choice(tickers)
examples.append({"messages": [msg("user", f"Stock for {ticker}?"), msg("assistant", tc=tc("get_stock_price", {"ticker": ticker}))],
"tools": tool_subset(["get_stock_price"])})
return examples[:n]
# 2 ── Argument types (int vs string, empty values) ──────────────────────
def batch2(n=20):
topics = [("AI", 5), ("climate", 10), ("sports", 3), ("tech", 8), ("health", 7), ("science", 12), ("music", 4), ("movies", 6)]
origins = ["NYC", "BKK", "LHR", "CDG", "NRT", "DXB", "SFO", "LAX"]
dests = ["LAX", "NRT", "CDG", "BKK", "JFK", "SIN", "HKG", "LHR"]
examples = []
for _ in range(n):
topic, cnt = random.choice(topics)
examples.append({"messages": [msg("user", f"Get {cnt} news about {topic}"), msg("assistant", tc=tc("get_news", {"topic": topic, "count": cnt}))],
"tools": tool_subset(["get_news", "search_web"])})
o, d = random.choice(origins), random.choice(dests)
if o != d:
examples.append({"messages": [msg("user", f"Book from {o} to {d} tomorrow"), msg("assistant", tc=tc("book_flight", {"origin": o, "destination": d, "date": "2026-10-01"}))],
"tools": tool_subset(["book_flight"])})
return examples[:n]
# 3 ── Parallel calls (Counter multiset containment) ─────────────────────
def batch3(n=20):
cities2 = random.sample(["Bangkok", "London", "Tokyo", "Paris", "Rome", "Berlin", "Dubai", "Seoul", "Mumbai", "Sydney"], 10)
tickers2 = ["AAPL", "GOOGL", "MSFT", "TSLA", "NVDA"]
topics2 = ["AI", "climate", "sports", "tech"]
examples = []
for i in range(n):
c1, c2 = cities2[i % len(cities2)], cities2[(i+3) % len(cities2)]
examples.append({"messages": [msg("user", f"Weather in {c1} and {c2}"),
msg("assistant", tc=tc("get_weather", {"location": c1}) + tc("get_weather", {"location": c2}))],
"tools": tool_subset(["get_weather", "get_time"])})
tk1, tk2 = random.choice(tickers2), random.choice(tickers2)
if tk1 != tk2:
examples.append({"messages": [msg("user", f"Stocks for {tk1} and {tk2}"),
msg("assistant", tc=tc("get_stock_price", {"ticker": tk1}) + tc("get_stock_price", {"ticker": tk2}))],
"tools": tool_subset(["get_stock_price", "get_news"])})
return examples[:n]
# 4 ── Irrelevance (scorer: len(pred_names) == 0) ────────────────────────
def batch4(n=60):
queries = [
"Hello!", "How are you?", "What's your name?", "Tell me a joke",
"What's the meaning of life?", "Explain gravity", "What is the capital of France?",
"Who painted the Mona Lisa?", "What is 2+2?", "What's the speed of light?",
"How do planes fly?", "What is photosynthesis?", "What is the largest ocean?",
"Who invented the telephone?", "What year did WW2 end?", "How many bones in the body?",
"What is H2O?", "What is Newton's first law?", "What causes rainbows?",
"How do batteries work?", "What is machine learning?", "Describe the water cycle",
"What is the square root of 144?", "Who wrote Romeo and Juliet?",
"What is the boiling point of water?", "How does the internet work?",
"What is the speed of sound?", "What is DNA?", "What is the atmosphere made of?",
]
examples = []
for q in queries * (n // len(queries) + 1):
random.shuffle(TOOLS)
examples.append({"messages": [msg("user", q), msg("assistant", content=f"That's a good question. {q.split('?')[0] + '?' if '?' in q else ''}")],
"tools": TOOLS[:random.randint(3, 6)]})
return examples[:n]
# 5 ── Selection accuracy hard negatives ─────────────────────────────────
def batch5(n=24):
pairs = [
("get_weather", "get_time"), ("get_stock_price", "get_news"),
("search_web", "get_news"), ("book_flight", "get_restaurant_info"),
("translate_text", "search_web"), ("send_email", "book_flight"),
("calculator", "get_stock_price"), ("get_weather", "get_restaurant_info"),
]
queries_map = {
"get_weather": "What's the weather?",
"get_time": "What time is it?", "get_stock_price": "What's AAPL stock?",
"get_news": "Latest news", "search_web": "Search the web",
"book_flight": "Book a flight", "get_restaurant_info": "Find restaurants",
"translate_text": "Translate hello", "send_email": "Send an email",
"calculator": "Calculate 2+2",
}
examples = []
for correct, wrong in pairs * (n // len(pairs) + 1):
q = queries_map.get(correct, f"Please use {correct}")
args_map = {"get_weather": {"location": "Paris", "unit": "celsius"}, "get_time": {"location": "Paris"},
"get_stock_price": {"ticker": "AAPL"}, "get_news": {"topic": "latest"},
"search_web": {"query": "latest news"}, "book_flight": {"origin": "BKK", "destination": "NRT", "date": "2026-09-01"},
"get_restaurant_info": {"name": "Sushi Bar"}, "translate_text": {"text": "hello", "target_lang": "th"},
"send_email": {"to": "a@b.com", "subject": "Hi", "body": "Hello"}, "calculator": {"expression": "2+2"}}
examples.append({"messages": [msg("user", q), msg("assistant", tc=tc(correct, args_map[correct]))],
"tools": tool_subset([correct, wrong])})
return examples[:n]
# 6 ── Held-out generalization ───────────────────────────────────────────
def batch6(n=20):
unusual_combos = [
("get_restaurant_info", "get_weather"), ("send_email", "get_news"),
("calculator", "translate_text"), ("book_flight", "get_weather"),
("get_news", "get_restaurant_info"), ("search_web", "calculator"),
]
args_map = {"get_weather": {"location": "Paris"}, "get_restaurant_info": {"name": "test"},
"send_email": {"to": "x@y.com", "subject": "S", "body": "B"},
"get_news": {"topic": "test", "count": 3}, "search_web": {"query": "test"},
"calculator": {"expression": "1+1"}, "translate_text": {"text": "hi", "target_lang": "fr"},
"book_flight": {"origin": "A", "destination": "B", "date": "2026-01-01"}}
examples = []
for t1, t2 in unusual_combos * (n // len(unusual_combos) + 1):
q = f"I need {t1} and {t2}"
examples.append({"messages": [msg("user", q), msg("assistant", tc=tc(t1, args_map[t1]) + tc(t2, args_map[t2]))],
"tools": tool_subset([t1, t2])})
return examples[:n]
# 7 ── Degenerate prevention ─────────────────────────────────────────────
def batch7(n=16):
tricky = [
"What is 0 divided by 0?", "Count from 1 to 10", "What is infinity?",
"What comes after 9999999999?", "Say hello 100 times",
"What is the largest number?", "What is infinity plus 1?",
"Repeat: ABCDEFGHIJKLMNOPQRSTUVWXYZ",
]
examples = []
for q in tricky:
examples.append({"messages": [msg("user", q), msg("assistant", content=f"Let me think about {q[:30]}...")], "tools": TOOLS[:3]})
return examples[:n]
# 8 ── Multi-turn ────────────────────────────────────────────────────────
def batch8(n=15):
cities = random.sample(["Rome", "Paris", "Tokyo", "London", "Berlin", "Madrid", "Dubai", "Seoul", "Bangkok", "Mumbai"], 10)
tickers = ["NVDA", "AMD", "AAPL", "MSFT", "GOOGL"]
examples = []
for i in range(n):
c1, c2 = cities[i % len(cities)], cities[(i+1) % len(cities)]
t1, t2 = tickers[i % len(tickers)], tickers[(i+1) % len(tickers)]
examples.append({"messages": [
msg("user", f"Weather in {c1}?"), msg("assistant", tc=tc("get_weather", {"location": c1})),
msg("tool", f"22C in {c1}"), msg("user", f"And in {c2}?")],
"tools": tool_subset(["get_weather"])})
examples.append({"messages": [
msg("user", f"Stock for {t1}?"), msg("assistant", tc=tc("get_stock_price", {"ticker": t1})),
msg("tool", "$800"), msg("user", f"What about {t2}?")],
"tools": tool_subset(["get_stock_price"])})
return examples[:n]
# 9 ── Greedy one-to-one matching ────────────────────────────────────────
def batch9(n=15):
cities3 = random.sample(["Bangkok", "Tokyo", "London", "Paris", "Berlin", "Madrid", "Dubai", "Rome", "Seoul", "Mumbai"], 10)
tickers3 = ["AAPL", "GOOGL", "MSFT", "TSLA", "NVDA", "AMD"]
examples = []
for i in range(n):
c1, c2 = cities3[i % len(cities3)], cities3[(i+2) % len(cities3)]
examples.append({"messages": [msg("user", f"Weather in {c1} and {c2}"),
msg("assistant", tc=tc("get_weather", {"location": c1}) + tc("get_weather", {"location": c2}))],
"tools": tool_subset(["get_weather", "get_time"])})
t1, t2 = tickers3[i % len(tickers3)], tickers3[(i+1) % len(tickers3)]
examples.append({"messages": [msg("user", f"Stocks for {t1} and {t2}"),
msg("assistant", tc=tc("get_stock_price", {"ticker": t1}) + tc("get_stock_price", {"ticker": t2}))],
"tools": tool_subset(["get_stock_price", "get_news"])})
return examples[:n]
# 10 ── Strict accuracy (selection + arguments together) ─────────────────
def batch10(n=20):
scenarios = [
("What's 15% of 200?", "calculator", {"expression": "15/100*200"}, ["calculator", "get_stock_price"]),
("Translate 'good morning' to Spanish", "translate_text", {"text": "good morning", "target_lang": "es"}, ["translate_text", "search_web"]),
("Email john@co.com about the meeting tomorrow", "send_email", {"to": "john@co.com", "subject": "Meeting tomorrow", "body": "See you at 3pm"}, ["send_email", "book_flight"]),
("Search for vegan recipes", "search_web", {"query": "vegan recipes"}, ["search_web", "get_news"]),
("Book LAX to JFK on March 15", "book_flight", {"origin": "LAX", "destination": "JFK", "date": "2026-03-15"}, ["book_flight", "get_weather"]),
("Weather in Barcelona in fahrenheit", "get_weather", {"location": "Barcelona", "unit": "fahrenheit"}, ["get_weather", "get_time"]),
("Stock of Microsoft", "get_stock_price", {"ticker": "MSFT"}, ["get_stock_price", "get_news"]),
("Translate 'goodbye' to French", "translate_text", {"text": "goodbye", "target_lang": "fr"}, ["translate_text", "search_web"]),
("News about renewable energy", "get_news", {"topic": "renewable energy", "count": 5}, ["get_news", "search_web"]),
("What time is it in Dubai?", "get_time", {"location": "Dubai"}, ["get_time", "get_weather"]),
]
examples = []
for q, tool_name, args, tool_list in scenarios * (n // len(scenarios) + 1):
examples.append({"messages": [msg("user", q), msg("assistant", tc=tc(tool_name, args))], "tools": tool_subset(tool_list)})
return examples[:n]
# ── Generate all ────────────────────────────────────────────────────────
GENERATORS = [
("01-arg-normalization", batch1, 30, "norm() whitespace/case normalization"),
("02-arg-types", batch2, 20, "int/float/string type coercion"),
("03-parallel-precision", batch3, 20, "Counter multiset containment"),
("04-irrelevance", batch4, 60, "pred_names must be empty"),
("05-selection-hard", batch5, 24, "Hard negatives for selection"),
("06-heldout-gen", batch6, 20, "Held-out tool generalization"),
("07-no-degenerate", batch7, 16, "Repeated char prevention"),
("08-multiturn", batch8, 15, "Multi-turn context tracking"),
("09-match-all", batch9, 15, "Greedy one-to-one matching"),
("10-strict-accuracy", batch10, 20, "Selection + arguments combined"),
]
total = 0
print("Benchmark-Targeted Dataset Augmentation")
print("=" * 50)
for name, gen_fn, count, desc in GENERATORS:
batch = gen_fn(count)
save(name, batch)
all_examples.extend(batch)
total += len(batch)
# Combined
combined = OUT / "all-benchmark-targeted.jsonl"
with open(combined, "w") as f:
for ex in all_examples:
f.write(json.dumps(ex, ensure_ascii=False) + "\n")
print(f"\n{'=' * 50}")
print(f"TOTAL: {total} examples across 10 benchmark-targeted batches")
print(f"{'=' * 50}")
print(f"Combined: {combined}")
print(f"\nPer the SakThai Cycle Workflow (DATA phase):")
print(f" 1. DATA → Generated {total} benchmark-targeted examples ✅")
print(f" 2. TRAIN → Combine with v7 for retraining")
print(f" 3. EVAL → Run eval_bench.py against bench-v2")
print(f" 4. Compare new scores vs baseline:")
print(f" 0.5B: 91.2% sel | 1.5B: 48.2% sel (target: > 70%)")
print(f" Args: 45.7% (target: > 60%)")
print(f"\nTo combine with existing data:")
print(f" copy benchmark-targeted/*.jsonl augmented-output/")