admesh/agentic-intent-classifier
254
1from __future__ import annotations2 3import json4import sys5from pathlib import Path6 7BASE_DIR = Path(__file__).resolve().parent.parent8if str(BASE_DIR) not in sys.path:9 sys.path.insert(0, str(BASE_DIR))10 11from config import IAB_BENCHMARK_PATH, IAB_DIFFICULTY_DATA_DIR12 13 14def write_jsonl(path: Path, rows: list[dict]) -> None:15 path.parent.mkdir(parents=True, exist_ok=True)16 with path.open("w", encoding="utf-8") as handle:17 for row in rows:18 handle.write(json.dumps(row, sort_keys=True) + "\n")19 20 21def shopping_prompts(fields: dict[str, str]) -> dict[str, tuple[str, ...]]:22 return {23 "easy": (24 f"best {fields['item_plural']}",25 f"which {fields['item']} should i buy in {fields['year']}",26 f"{fields['provider_a']} vs {fields['provider_b']}",27 f"{fields['item']} buying guide",28 ),29 "medium": (30 f"best {fields['item']} for {fields['audience']}",31 f"compare {fields['provider_a']} and {fields['provider_b']} before buying",32 f"affordable {fields['item_plural']} for {fields['audience']}",33 f"what {fields['item_plural']} are worth considering for {fields['audience']}",34 ),35 "hard": (36 f"i am replacing my current {fields['item']} and need the right option for {fields['audience']}",37 f"help me narrow down {fields['item_plural']} for {fields['audience']} without wasting money",38 f"which option makes more sense between {fields['provider_a']} and {fields['provider_b']} for {fields['audience']}",39 f"i need a shortlist of {fields['item_plural']} that fit {fields['constraint']}",40 ),41 }42 43 44def software_prompts(fields: dict[str, str]) -> dict[str, tuple[str, ...]]:45 return {46 "easy": (47 f"best {fields['item_plural']} for {fields['audience']}",48 f"what is {fields['item']}",49 f"{fields['provider_a']} vs {fields['provider_b']}",50 f"{fields['item']} for {fields['goal']}",51 ),52 "medium": (53 f"compare {fields['provider_a']} and {fields['provider_b']} for {fields['audience']}",54 f"best {fields['item_plural']} for {fields['goal']}",55 f"how does {fields['item']} work for {fields['audience']}",56 f"which {fields['item']} should a {fields['audience']} choose",57 ),58 "hard": (59 f"i am evaluating software for {fields['goal']} and need the right category",60 f"what tools should i shortlist before picking between {fields['provider_a']} and {fields['provider_b']}",61 f"we need a platform for {fields['goal']} and are not sure which branch this falls into",62 f"help me assess {fields['provider_a']} versus other options for {fields['audience']}",63 ),64 }65 66 67def business_it_prompts(fields: dict[str, str]) -> dict[str, tuple[str, ...]]:68 return {69 "easy": (70 "how do i reset my password",71 "business login security tools",72 "identity management software",73 f"{fields['provider_a']} vs {fields['provider_b']} for access management",74 ),75 "medium": (76 "best software for employee password resets",77 "how does single sign-on work for a small company",78 "access management platform for remote employees",79 f"compare {fields['provider_a']} and {fields['provider_b']} for workforce identity",80 ),81 "hard": (82 "our team keeps getting locked out and we need better identity software",83 "what category covers employee account security and access provisioning",84 "we need business software for login, permissions, and access control",85 "help me evaluate identity tooling for company account security",86 ),87 }88 89 90def dining_prompts(fields: dict[str, str]) -> dict[str, tuple[str, ...]]:91 return {92 "easy": (93 "book a table for dinner",94 "best restaurants for date night",95 "where should i eat tonight",96 "reserve a table for two",97 ),98 "medium": (99 f"{fields['area']} restaurant options for a birthday dinner",100 "family friendly restaurants near me",101 "compare brunch spots for a weekend meetup",102 "where can i book dinner for four tonight",103 ),104 "hard": (105 "i need a place to eat and want something i can reserve tonight",106 "what category covers restaurants and booking a table",107 "help me find a dinner spot for a client meeting",108 "i want dining options, not recipes",109 ),110 }111 112 113def beverage_prompts(fields: dict[str, str]) -> dict[str, tuple[str, ...]]:114 return {115 "easy": (116 "best vodka drink to try",117 "whiskey cocktail ideas",118 "what is a martini",119 "bourbon vs rye for beginners",120 ),121 "medium": (122 "best whiskey cocktail for a dinner party",123 "vodka drinks for beginners",124 "compare bourbon and scotch flavor profiles",125 "how does gin differ from vodka in cocktails",126 ),127 "hard": (128 "i want alcoholic drink recommendations, not restaurant suggestions",129 "help me understand beginner-friendly cocktails with bourbon",130 "what should i try if i want a spirit-forward drink",131 "compare vodka cocktails with tequila cocktails",132 ),133 }134 135 136def ai_prompts(fields: dict[str, str]) -> dict[str, tuple[str, ...]]:137 return {138 "easy": (139 "what is intent classification in nlp",140 "machine learning basics",141 "how does natural language processing work",142 "what are large language models",143 ),144 "medium": (145 "best ai methods for text classification",146 "nlp model comparison for intent detection",147 "how do llms handle classification tasks",148 "ai tools for labeling text data",149 ),150 "hard": (151 "i want the ai concept behind intent models, not software shopping",152 "help me understand the machine learning side of nlp classification",153 "compare transformer-based approaches for intent detection",154 "what branch covers language-model research topics",155 ),156 }157 158 159KIND_TO_BUILDER = {160 "shopping": shopping_prompts,161 "software": software_prompts,162 "business_it": business_it_prompts,163 "dining": dining_prompts,164 "beverage": beverage_prompts,165 "ai": ai_prompts,166}167 168 169AUGMENTATION_SCENARIOS = {170 "Automotive > Auto Buying and Selling": [171 {172 "kind": "shopping",173 "item": "car",174 "item_plural": "cars",175 "provider_a": "Toyota Corolla",176 "provider_b": "Honda Civic",177 "audience": "a commuter",178 "constraint": "a practical budget",179 "year": "2026",180 },181 {182 "kind": "shopping",183 "item": "suv",184 "item_plural": "suvs",185 "provider_a": "Toyota RAV4",186 "provider_b": "Honda CR-V",187 "audience": "a growing family",188 "constraint": "daily driving and storage needs",189 "year": "2026",190 },191 {192 "kind": "shopping",193 "item": "electric car",194 "item_plural": "electric cars",195 "provider_a": "Tesla Model 3",196 "provider_b": "Hyundai Ioniq 5",197 "audience": "a first-time ev buyer",198 "constraint": "reasonable range and price",199 "year": "2026",200 },201 ],202 "Business and Finance > Business > Sales": [203 {204 "kind": "software",205 "item": "crm software",206 "item_plural": "crm tools",207 "provider_a": "HubSpot",208 "provider_b": "Zoho",209 "audience": "small sales teams",210 "goal": "lead management",211 },212 {213 "kind": "software",214 "item": "sales engagement software",215 "item_plural": "sales platforms",216 "provider_a": "Apollo",217 "provider_b": "Outreach",218 "audience": "outbound teams",219 "goal": "pipeline generation",220 },221 {222 "kind": "software",223 "item": "customer relationship management software",224 "item_plural": "crm systems",225 "provider_a": "Pipedrive",226 "provider_b": "Freshsales",227 "audience": "growing startups",228 "goal": "deal tracking",229 },230 ],231 "Business and Finance > Business > Marketing and Advertising": [232 {233 "kind": "software",234 "item": "marketing software",235 "item_plural": "marketing tools",236 "provider_a": "Semrush",237 "provider_b": "Ahrefs",238 "audience": "content teams",239 "goal": "organic growth",240 },241 {242 "kind": "software",243 "item": "seo platform",244 "item_plural": "seo tools",245 "provider_a": "Surfer",246 "provider_b": "Clearscope",247 "audience": "editorial teams",248 "goal": "content optimization",249 },250 {251 "kind": "software",252 "item": "advertising analytics software",253 "item_plural": "marketing analytics tools",254 "provider_a": "Triple Whale",255 "provider_b": "Northbeam",256 "audience": "performance marketers",257 "goal": "campaign measurement",258 },259 ],260 "Business and Finance > Business > Business I.T.": [261 {"kind": "business_it", "provider_a": "Okta", "provider_b": "Microsoft Entra"},262 {"kind": "business_it", "provider_a": "JumpCloud", "provider_b": "Okta"},263 {"kind": "business_it", "provider_a": "Duo", "provider_b": "OneLogin"},264 ],265 "Food & Drink > Dining Out": [266 {"kind": "dining", "area": "downtown"},267 {"kind": "dining", "area": "midtown"},268 {"kind": "dining", "area": "the waterfront"},269 ],270 "Food & Drink > Alcoholic Beverages": [271 {"kind": "beverage"},272 {"kind": "beverage"},273 {"kind": "beverage"},274 ],275 "Technology & Computing > Artificial Intelligence": [276 {"kind": "ai"},277 {"kind": "ai"},278 {"kind": "ai"},279 ],280 "Technology & Computing > Computing > Computer Software and Applications": [281 {282 "kind": "software",283 "item": "software platform",284 "item_plural": "software applications",285 "provider_a": "Notion",286 "provider_b": "Airtable",287 "audience": "operations teams",288 "goal": "workflow management",289 },290 {291 "kind": "software",292 "item": "project management software",293 "item_plural": "software tools",294 "provider_a": "Asana",295 "provider_b": "ClickUp",296 "audience": "remote teams",297 "goal": "project planning",298 },299 {300 "kind": "software",301 "item": "business software",302 "item_plural": "software products",303 "provider_a": "Monday.com",304 "provider_b": "Notion",305 "audience": "startup operators",306 "goal": "team coordination",307 },308 ],309 "Technology & Computing > Computing > Computer Software and Applications > Communication": [310 {311 "kind": "software",312 "item": "communication software",313 "item_plural": "communication tools",314 "provider_a": "Slack",315 "provider_b": "Microsoft Teams",316 "audience": "remote teams",317 "goal": "team communication",318 },319 {320 "kind": "software",321 "item": "team chat software",322 "item_plural": "messaging platforms",323 "provider_a": "Slack",324 "provider_b": "Discord",325 "audience": "distributed startups",326 "goal": "internal collaboration",327 },328 {329 "kind": "software",330 "item": "workplace communication platform",331 "item_plural": "communication apps",332 "provider_a": "Google Chat",333 "provider_b": "Microsoft Teams",334 "audience": "cross-functional teams",335 "goal": "company messaging",336 },337 ],338 "Technology & Computing > Computing > Internet > Web Hosting": [339 {340 "kind": "software",341 "item": "web hosting",342 "item_plural": "hosting providers",343 "provider_a": "Vercel",344 "provider_b": "Netlify",345 "audience": "startup launch teams",346 "goal": "site hosting",347 },348 {349 "kind": "software",350 "item": "hosting platform",351 "item_plural": "hosting services",352 "provider_a": "Cloudflare Pages",353 "provider_b": "Render",354 "audience": "developers",355 "goal": "website deployment",356 },357 {358 "kind": "software",359 "item": "managed hosting",360 "item_plural": "hosting options",361 "provider_a": "WP Engine",362 "provider_b": "Kinsta",363 "audience": "content teams",364 "goal": "site performance",365 },366 ],367 "Technology & Computing > Computing > Laptops": [368 {369 "kind": "shopping",370 "item": "laptop",371 "item_plural": "laptops",372 "provider_a": "MacBook Air",373 "provider_b": "Dell XPS 13",374 "audience": "work and study",375 "constraint": "battery life and portability",376 "year": "2026",377 },378 {379 "kind": "shopping",380 "item": "gaming laptop",381 "item_plural": "gaming laptops",382 "provider_a": "Asus ROG Zephyrus",383 "provider_b": "Lenovo Legion Slim",384 "audience": "gamers",385 "constraint": "performance under a reasonable budget",386 "year": "2026",387 },388 {389 "kind": "shopping",390 "item": "student laptop",391 "item_plural": "student laptops",392 "provider_a": "Acer Swift Go",393 "provider_b": "HP Pavilion Aero",394 "audience": "college students",395 "constraint": "price and portability",396 "year": "2026",397 },398 ],399 "Technology & Computing > Computing > Desktops": [400 {401 "kind": "shopping",402 "item": "desktop",403 "item_plural": "desktops",404 "provider_a": "iMac",405 "provider_b": "Dell Inspiron Desktop",406 "audience": "home offices",407 "constraint": "everyday productivity",408 "year": "2026",409 },410 {411 "kind": "shopping",412 "item": "gaming desktop",413 "item_plural": "gaming desktops",414 "provider_a": "Alienware Aurora",415 "provider_b": "Lenovo Legion Tower",416 "audience": "pc gamers",417 "constraint": "strong graphics performance",418 "year": "2026",419 },420 {421 "kind": "shopping",422 "item": "desktop pc",423 "item_plural": "desktop computers",424 "provider_a": "HP Envy Desktop",425 "provider_b": "Acer Aspire TC",426 "audience": "families",427 "constraint": "value for money",428 "year": "2026",429 },430 ],431 "Technology & Computing > Consumer Electronics > Smartphones": [432 {433 "kind": "shopping",434 "item": "smartphone",435 "item_plural": "smartphones",436 "provider_a": "iPhone 17",437 "provider_b": "Samsung Galaxy S26",438 "audience": "everyday users",439 "constraint": "camera quality and battery life",440 "year": "2026",441 },442 {443 "kind": "shopping",444 "item": "budget phone",445 "item_plural": "budget smartphones",446 "provider_a": "Pixel 10a",447 "provider_b": "Galaxy A57",448 "audience": "budget-conscious buyers",449 "constraint": "under midrange pricing",450 "year": "2026",451 },452 {453 "kind": "shopping",454 "item": "android phone",455 "item_plural": "android phones",456 "provider_a": "OnePlus 15",457 "provider_b": "Pixel 10",458 "audience": "power users",459 "constraint": "performance and clean software",460 "year": "2026",461 },462 ],463}464 465 466BENCHMARK_SCENARIOS = {467 "Automotive > Auto Buying and Selling": {468 "kind": "shopping",469 "item": "car",470 "item_plural": "vehicles",471 "provider_a": "Mazda CX-5",472 "provider_b": "Subaru Forester",473 "audience": "a first-time buyer",474 "constraint": "safety and price",475 "year": "2026",476 },477 "Business and Finance > Business > Sales": {478 "kind": "software",479 "item": "crm platform",480 "item_plural": "sales tools",481 "provider_a": "Copper",482 "provider_b": "Salesforce Essentials",483 "audience": "small revenue teams",484 "goal": "managing leads",485 },486 "Business and Finance > Business > Marketing and Advertising": {487 "kind": "software",488 "item": "marketing platform",489 "item_plural": "marketing tools",490 "provider_a": "Moz",491 "provider_b": "SE Ranking",492 "audience": "brand teams",493 "goal": "search visibility",494 },495 "Business and Finance > Business > Business I.T.": {496 "kind": "business_it",497 "provider_a": "Rippling",498 "provider_b": "JumpCloud",499 },500 "Food & Drink > Dining Out": {"kind": "dining", "area": "uptown"},501 "Food & Drink > Alcoholic Beverages": {"kind": "beverage"},502 "Technology & Computing > Artificial Intelligence": {"kind": "ai"},503 "Technology & Computing > Computing > Computer Software and Applications": {504 "kind": "software",505 "item": "workflow software",506 "item_plural": "productivity apps",507 "provider_a": "Basecamp",508 "provider_b": "Asana",509 "audience": "small teams",510 "goal": "organizing work",511 },512 "Technology & Computing > Computing > Computer Software and Applications > Communication": {513 "kind": "software",514 "item": "communication platform",515 "item_plural": "team messaging tools",516 "provider_a": "Mattermost",517 "provider_b": "Slack",518 "audience": "engineering teams",519 "goal": "workplace communication",520 },521 "Technology & Computing > Computing > Internet > Web Hosting": {522 "kind": "software",523 "item": "web hosting service",524 "item_plural": "hosting platforms",525 "provider_a": "Fly.io",526 "provider_b": "Render",527 "audience": "product builders",528 "goal": "deploying websites",529 },530 "Technology & Computing > Computing > Laptops": {531 "kind": "shopping",532 "item": "laptop",533 "item_plural": "portable computers",534 "provider_a": "Surface Laptop",535 "provider_b": "Framework Laptop",536 "audience": "knowledge workers",537 "constraint": "portability and repairability",538 "year": "2026",539 },540 "Technology & Computing > Computing > Desktops": {541 "kind": "shopping",542 "item": "desktop computer",543 "item_plural": "desktop pcs",544 "provider_a": "Mac Studio",545 "provider_b": "HP Omen 45L",546 "audience": "creators",547 "constraint": "performance and reliability",548 "year": "2026",549 },550 "Technology & Computing > Consumer Electronics > Smartphones": {551 "kind": "shopping",552 "item": "smartphone",553 "item_plural": "mobile phones",554 "provider_a": "Nothing Phone 4",555 "provider_b": "Pixel 10 Pro",556 "audience": "everyday buyers",557 "constraint": "camera and battery performance",558 "year": "2026",559 },560}561 562 563def build_rows(label: str, scenarios: list[dict], include_difficulty: bool) -> list[dict]:564 rows = []565 seen = set()566 for scenario in scenarios:567 prompts_by_difficulty = KIND_TO_BUILDER[scenario["kind"]](scenario)568 for difficulty, prompts in prompts_by_difficulty.items():569 for text in prompts:570 normalized = " ".join(text.strip().lower().split())571 key = (label, normalized)572 if key in seen:573 continue574 seen.add(key)575 row = {"text": normalized, "iab_path": label}576 if include_difficulty:577 row["difficulty"] = difficulty578 rows.append(row)579 return rows580 581 582def split_rows(rows: list[dict]) -> tuple[list[dict], list[dict], list[dict]]:583 total = len(rows)584 val_count = max(1, total // 6)585 test_count = max(1, total // 6)586 test_rows = rows[:test_count]587 val_rows = rows[test_count : test_count + val_count]588 train_rows = rows[test_count + val_count :]589 return train_rows, val_rows, test_rows590 591 592def main() -> None:593 train_rows: list[dict] = []594 val_rows: list[dict] = []595 test_rows: list[dict] = []596 benchmark_rows: list[dict] = []597 598 for label, scenarios in AUGMENTATION_SCENARIOS.items():599 rows = build_rows(label, scenarios, include_difficulty=True)600 train_split, val_split, test_split = split_rows(rows)601 train_rows.extend(train_split)602 val_rows.extend(val_split)603 test_rows.extend(test_split)604 605 for label, scenario in BENCHMARK_SCENARIOS.items():606 benchmark_rows.extend(build_rows(label, [scenario], include_difficulty=True))607 608 write_jsonl(IAB_DIFFICULTY_DATA_DIR / "train.jsonl", train_rows)609 write_jsonl(IAB_DIFFICULTY_DATA_DIR / "val.jsonl", val_rows)610 write_jsonl(IAB_DIFFICULTY_DATA_DIR / "test.jsonl", test_rows)611 write_jsonl(IAB_BENCHMARK_PATH, benchmark_rows)612 613 print(f"train: {len(train_rows)} rows")614 print(f"val: {len(val_rows)} rows")615 print(f"test: {len(test_rows)} rows")616 print(f"benchmark: {len(benchmark_rows)} rows")617 618 619if __name__ == "__main__":620 main()621 