"""Tool menus and prompt rendering. Needs only a Hugging Face tokenizer (no MLX), so it can be smoke-tested without a GPU.""" import json import random from tasks import CATEGORIES, SYSTEM_PROMPT, TASKS from tools import ALL_BY_NAME, DECOY_TOOLS, REAL_BY_NAME, REAL_TOOLS GEMMA_TOOL_INSTRUCTIONS = """You have access to the following tools, described as JSON schema: {tools} To call a tool, reply with a fenced code block tagged tool_call that contains one JSON object with "name" and "arguments", like this: ```tool_call {{"name": "tool_name", "arguments": {{"argument_name": "value"}}}} ``` To make several independent calls, write one tool_call block per call. When you call tools, reply with only the tool_call block(s). If no tool is needed, answer the user directly in plain text.""" def menu(task, size): """size=8: the needed tool(s) + forced distractors + random real tools, shuffled per task. size=32: all 12 real tools + 20 decoys in one fixed order (identical for every task).""" if size == 32: names = sorted(ALL_BY_NAME) random.Random("menu32").shuffle(names) return [ALL_BY_NAME[n] for n in names] assert size == 8 rng = random.Random(f"menu8-{task['id']}") names = list(dict.fromkeys([c["name"] for c in task["expected"]] + task["distractors"])) others = [t["function"]["name"] for t in REAL_TOOLS if t["function"]["name"] not in names] rng.shuffle(others) names += others[: 8 - len(names)] rng.shuffle(names) return [REAL_BY_NAME[n] for n in names] def menu_key(tools): return "|".join(t["function"]["name"] for t in tools) def gemma_system(tools): listing = "\n".join(json.dumps(t["function"], ensure_ascii=False) for t in tools) return SYSTEM_PROMPT + "\n\n" + GEMMA_TOOL_INSTRUCTIONS.format(tools=listing) def render(tokenizer, family, user_prompt, tools, thinking=False): if family == "qwen": msgs = [{"role": "system", "content": SYSTEM_PROMPT}, {"role": "user", "content": user_prompt}] return tokenizer.apply_chat_template(msgs, tools=tools, add_generation_prompt=True, enable_thinking=thinking, tokenize=False) msgs = [{"role": "system", "content": gemma_system(tools)}, {"role": "user", "content": user_prompt}] return tokenizer.apply_chat_template(msgs, add_generation_prompt=True, tokenize=False) def encode(tokenizer, text): return tokenizer.encode(text, add_special_tokens=False) # templates already contain BOS if any def shared_prefix(tokenizer, family, tools, thinking=False): """Tokens shared by every prompt with this tool menu: the LCP of two unrelated user turns.""" a = encode(tokenizer, render(tokenizer, family, "Alpha request 1.", tools, thinking)) b = encode(tokenizer, render(tokenizer, family, "zz?", tools, thinking)) n = 0 while n < min(len(a), len(b)) and a[n] == b[n]: n += 1 return a[:n] def interleaved(tasks=TASKS): """Round-robin over categories so a condition cut short by its time budget stays balanced.""" by_cat = {c: [t for t in tasks if t["category"] == c] for c in CATEGORIES} out = [] for i in range(max(len(v) for v in by_cat.values())): out += [by_cat[c][i] for c in CATEGORIES if i < len(by_cat[c])] return out