Skip to content

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.

Add finetune data format docs and per-tool warning; align tool schema across codebase (#26)

Jakub Mroz34861f39ae29
diff --git a/README.md b/README.md
index 351ddfc..b2f4560 100644
--- a/README.md
+++ b/README.md
@@ -82,7 +82,7 @@ tokenizer = get_tokenizer()
result = generate(
model, params, tokenizer,
query="What's the weather in San Francisco?",
- tools='[{"name":"get_weather","parameters":{"location":"string"}}]',
+ tools='[{"name":"get_weather","description":"Get current weather for a city.","parameters":{"location":{"type":"string","description":"City name.","required":true}}}]',
stream=False,
)
print(result)
@@ -99,6 +99,49 @@ needle playground
needle finetune data.jsonl
```
+### Data format
+
+Each line in the JSONL file has three fields: `query`, `tools`, and `answers`.
+
+**Tool schema:**
+```json
+{
+ "name": "get_weather",
+ "description": "Get current weather for a city.",
+ "parameters": {
+ "location": { "type": "string", "description": "City name.", "required": true }
+ }
+}
+```
+
+**Answer schema:**
+```json
+{ "name": "get_weather", "arguments": { "location": "Paris" } }
+```
+
+**Full JSONL example** (each line is one training example, `tools` and `answers` are JSON-encoded strings):
+```jsonl
+{"query": "What's the weather in Paris?", "tools": "[{\"name\":\"get_weather\",\"description\":\"Get current weather for a city.\",\"parameters\":{\"location\":{\"type\":\"string\",\"description\":\"City name.\",\"required\":true}}}]", "answers": "[{\"name\":\"get_weather\",\"arguments\":{\"location\":\"Paris\"}}]"}
+{"query": "Turn off the lights", "tools": "[{\"name\":\"get_weather\",\"description\":\"Get current weather for a city.\",\"parameters\":{\"location\":{\"type\":\"string\",\"description\":\"City name.\",\"required\":true}}},{\"name\":\"toggle_lights\",\"description\":\"Toggle smart lights on or off.\",\"parameters\":{\"state\":{\"type\":\"string\",\"description\":\"on or off.\",\"required\":true}}}]", "answers": "[{\"name\":\"toggle_lights\",\"arguments\":{\"state\":\"off\"}}]"}
+```
+
+Provide at least **120 examples per tool** (100 train / 10 val / 10 test). Fewer examples will overfit — you'll see perfect training metrics but the model won't generalize. Vary query phrasing and include examples with multiple tools available.
+
+### Using a finetuned model
+
+Finetuning saves the best checkpoint as `checkpoints/needle_finetuned_<id>_best.pkl`:
+
+```bash
+needle run --checkpoint checkpoints/needle_finetuned_*_best.pkl \
+ --query "What's the weather?" --tools '[{"name":"get_weather","description":"Get current weather for a city.","parameters":{"location":{"type":"string","description":"City name.","required":true}}}]'
+```
+
+```python
+params, config = load_checkpoint("checkpoints/needle_finetuned_<id>_best.pkl")
+model = SimpleAttentionNetwork(config)
+result = generate(model, params, get_tokenizer(), query="...", tools='[...]', stream=False)
+```
+
## CLI
```
diff --git a/docs/hf_model_card.md b/docs/hf_model_card.md
index c0e9975..e356505 100644
--- a/docs/hf_model_card.md
+++ b/docs/hf_model_card.md
@@ -98,7 +98,7 @@ tokenizer = get_tokenizer()
result = generate(
model, params, tokenizer,
query="What's the weather in San Francisco?",
- tools='[{"name":"get_weather","parameters":{"location":"string"}}]',
+ tools='[{"name":"get_weather","description":"Get current weather for a city.","parameters":{"location":{"type":"string","description":"City name.","required":true}}}]',
stream=False,
)
print(result)
diff --git a/needle/model/run.py b/needle/model/run.py
index 9216045..954022e 100644
--- a/needle/model/run.py
+++ b/needle/model/run.py
@@ -284,9 +284,9 @@ def main(args):
queries = [(query, tools)]
else:
queries = [
- ('What is the weather in San Francisco?', '[{"name": "get_weather", "parameters": {"location": "string"}}]'),
- ('Send an email to [email protected] saying hello', '[{"name": "send_email", "parameters": {"to": "string", "body": "string"}}]'),
- ('Get the current stock price of AAPL', '[{"name": "get_stock_price", "parameters": {"symbol": "string"}}]'),
+ ('What is the weather in San Francisco?', '[{"name": "get_weather", "description": "Get current weather for a city.", "parameters": {"location": {"type": "string", "description": "City name.", "required": true}}}]'),
+ ('Send an email to [email protected] saying hello', '[{"name": "send_email", "description": "Send an email to a recipient.", "parameters": {"to": {"type": "string", "description": "The recipient email address.", "required": true}, "body": {"type": "string", "description": "The email body text.", "required": true}}}]'),
+ ('Get the current stock price of AAPL', '[{"name": "get_stock_price", "description": "Get the current stock price.", "parameters": {"symbol": {"type": "string", "description": "Ticker symbol.", "required": true}}}]'),
]
for i, (q, t) in enumerate(queries):
diff --git a/needle/training/finetune.py b/needle/training/finetune.py
index e494308..3f02645 100644
--- a/needle/training/finetune.py
+++ b/needle/training/finetune.py
@@ -6,6 +6,7 @@ import random
import shutil
import tempfile
import time
+from collections import Counter
from ..dataset import dataset as data_mod
from ..dataset.dataset import _cache_key, _save_cache_metadata, get_tokenizer, pack_sequences, prepare_tool_call_pairs
@@ -236,6 +237,23 @@ def finetune_local(args):
print(f"Loaded {len(examples)} examples from {args.jsonl_path}")
if len(examples) < 3:
raise ValueError("finetune requires at least 3 examples for train/val/test splits")
+ tool_counts = Counter()
+ for ex in examples:
+ for call in json.loads(ex.get("answers", "[]")):
+ if call.get("name"):
+ tool_counts[call["name"]] += 1
+ low_tools = {t: c for t, c in tool_counts.items() if c < 120}
+ if low_tools:
+ print(
+ f"\n⚠ WARNING: The following tools have fewer than 120 examples "
+ f"(need at least 120 per tool: 100 train / 10 val / 10 test):"
+ )
+ for tool, count in sorted(low_tools.items()):
+ print(f" • {tool}: {count}")
+ print(
+ f" Finetuning with too few examples will overfit (perfect training "
+ f"metrics but poor real-world performance). Add more diverse examples.\n"
+ )
from ..dataset.tokenizer import DEFAULT_MAX_ENC_LEN, DEFAULT_MAX_DEC_LEN
max_enc_len = getattr(args, "max_enc_len", None) or DEFAULT_MAX_ENC_LEN