Automation foundation model for tiny devices: 2-bit, 8-29 MB, tool calls, ASR, structured extraction and embeddings on phones, wearables, smart homes, robots, cars and microcontrollers.
fix tool calls
HenryNdubuakue36b1f181ffe
diff --git a/scripts/tools_data.py b/scripts/tools_data.pyindex 53ccd4c..8308b9b 100755--- a/scripts/tools_data.py+++ b/scripts/tools_data.py@@ -22,7 +22,7 @@ Datasets and their native formats:3. glaiveai/glaive-function-calling-v2Tool definitions embedded in the system prompt; chat is a flat string withUSER:/A: role markers and <functioncall> tags. Examples with empty answers- (answers=[]) are filtered out in the final combine step.+ (answers=[]) are kept so the model learns when NOT to call functions.4. Team-ACE/ToolACETool definitions as a JSON array in the system prompt; function calls use a@@ -227,6 +227,26 @@ def _parse_toolace_calls(text):return answers+def compact_tools(tools_json):+ """Strip verbose schema descriptions, keep only function name + param name/type."""+ try:+ tools = json.loads(tools_json)+ except (json.JSONDecodeError, TypeError):+ return tools_json+ compact = []+ for t in tools:+ params = {}+ raw_params = t.get("parameters", {})+ props = raw_params.get("properties", raw_params) if isinstance(raw_params, dict) else {}+ for pname, pdef in props.items():+ if isinstance(pdef, dict):+ params[pname] = pdef.get("type", "string")+ else:+ params[pname] = str(pdef)+ compact.append({"name": t["name"], "parameters": params})+ return json.dumps(compact)++CONVERTERS = {"xlam-function-calling-60k": convert_xlam,"APIGen-MT-5k": convert_apigen_mt,@@ -252,15 +272,17 @@ def load_and_combine():parts.append(converted)combined = concatenate_datasets(parts)- pre_filter = len(combined)- # Filter out examples with empty answers- combined = combined.filter(- lambda ex: json.loads(ex["answers"]) != [],- desc="Filtering empty answers",+ # Compact tool schemas: strip descriptions, keep only name + param name/type+ print(f"\nCompacting tool schemas...")+ combined = combined.map(+ lambda ex: {"tools": compact_tools(ex["tools"])},+ desc="Compacting tools",)++ empty_count = sum(1 for ex in combined if json.loads(ex["answers"]) == [])print(f"\n{'='*60}")- print(f"Combined dataset: {len(combined)} examples (filtered {pre_filter - len(combined)} empty answers)")+ print(f"Combined dataset: {len(combined)} examples ({empty_count} with empty answers = no-call examples)")print(f"Columns: {combined.column_names}")counts = Counter(combined["source"])diff --git a/src/data.py b/src/data.pyindex 319dbcc..4ebc294 100644--- a/src/data.py+++ b/src/data.py@@ -173,27 +173,6 @@ def _cache_key(prefix, n_samples, max_enc_len, max_dec_len):return hashlib.md5(key.encode()).hexdigest()[:12]-def _compact_tools(tools_json):- """Strip verbose schema descriptions, keep only function name + param name/type."""- import json- try:- tools = json.loads(tools_json)- except (json.JSONDecodeError, TypeError):- return tools_json- compact = []- for t in tools:- params = {}- raw_params = t.get("parameters", {})- props = raw_params.get("properties", raw_params) if isinstance(raw_params, dict) else {}- for pname, pdef in props.items():- if isinstance(pdef, dict):- params[pname] = pdef.get("type", "string")- else:- params[pname] = str(pdef)- compact.append({"name": t["name"], "parameters": params})- return json.dumps(compact)--def prepare_tool_call_pairs(ds, tokenizer, max_enc_len=256, max_dec_len=1024):"""Prepare tool-call encoder-decoder pairs with <tool_call> task token.@@ -221,9 +200,9 @@ def prepare_tool_call_pairs(ds, tokenizer, max_enc_len=256, max_dec_len=1024):eos_id = tokenizer.eos_token_idtool_call_id = tokenizer.tool_call_token_id- # Encoder: query only. Decoder prefix: compact tools. Decoder answer: answers.+ # Encoder: query only.enc_texts = [ex["query"] for ex in ds]- tools_texts = [_compact_tools(ex["tools"]) for ex in ds]+ tools_texts = [ex["tools"] for ex in ds]ans_texts = [ex["answers"] for ex in ds]num_workers = min(os.cpu_count() or 1, 8)diff --git a/src/run.py b/src/run.pyindex 87b2b1d..0dba654 100644--- a/src/run.py+++ b/src/run.py@@ -30,8 +30,6 @@ def generate(model, params, tokenizer, query, tools="[]", max_gen_len=512, seed=Encoder: query only.Decoder: prefilled with [BOS, <tool_call>, tools_tokens...], then greedy decode."""- from .data import _compact_tools-enc_tokens = tokenizer.encode(query)enc_input = jnp.array([enc_tokens])@@ -44,8 +42,8 @@ def generate(model, params, tokenizer, query, tools="[]", max_gen_len=512, seed={"params": params}, enc_input, src_mask=src_mask, method="encode")- # Build decoder prefix: [BOS, <tool_call>, compact_tools_tokens...]- tools_tokens = tokenizer.encode(_compact_tools(tools))+ # Build decoder prefix: [BOS, <tool_call>, tools_tokens...]+ tools_tokens = tokenizer.encode(tools)prefix = [eos_id, tool_call_id] + tools_tokensprefix_len = len(prefix)