agentmart-tooltune-data / src /generate_data.py
shantomathew's picture
ToolTune code, article and training notebook (Lexsi Track 3)
1a7314a verified
Raw History Blame Contribute Delete
4.16 kB
"""Generate the task suite + AgentTune-format training artifacts.
Outputs (under data/):
tasks.json -- {"tools": [...], "train": [...130 tasks], "test": [...40 tasks]}
sft_agenttune.jsonl -- SFT rows in AgentTune EventLog.as_dataset_rows('sft')
format: {"messages": [...], "segment_weights": [...],
"loss_mask": [...]}. Tool calls are emitted exactly as
AgentTune does: an assistant message whose content is
<tool_call>{"name": ..., "arguments": {...}}</tool_call>
with segment weight 2.0; user/tool messages weight 0.0
(masked); the final assistant answer weight 1.0.
Source: src/agenttune/agentic/events.py in AgentTune_ref/.
trajectories.jsonl -- TrajectoryDataset records in the format AgentTune's
trainer writes and auditkit.load_agenttune() reads:
{"task", "steps": [{"thought", "action":
{"tool_calls": [{"id","name","arguments"}]},
"observation"}], "final_response",
"metadata": {"conversation", ...}}.
Source: AgentTune README "Hand the run to AuditKIT".
"""
from __future__ import annotations
import json
import os
import sys
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from tasks import TOOL_SCHEMAS, build_tasks, run_tool # noqa: E402
SYSTEM = (
"You are a tool-using assistant. Call exactly one tool with valid JSON "
"arguments, then answer from the tool result."
)
def sft_row(task: dict) -> dict:
output = run_tool(task["tool"], task["arguments"])
call = {"name": task["tool"], "arguments": task["arguments"]}
messages = [
{"role": "user", "content": task["prompt"]},
{"role": "assistant", "content": f"<tool_call>{json.dumps(call)}</tool_call>"},
{"role": "tool", "content": output},
{"role": "assistant", "content": output},
]
weights = [0.0, 2.0, 0.0, 1.0] # AgentTune segment-aware weights
return {
"messages": messages,
"segment_weights": weights,
"loss_mask": [w == 0.0 for w in weights],
}
def trajectory_row(task: dict) -> dict:
output = run_tool(task["tool"], task["arguments"])
call = {"name": task["tool"], "arguments": task["arguments"]}
conversation = [
{"role": "system", "content": SYSTEM},
{"role": "user", "content": task["prompt"]},
{"role": "assistant", "content": f"<tool_call>{json.dumps(call)}</tool_call>"},
{"role": "tool", "content": output},
{"role": "assistant", "content": output},
]
return {
"task": task["prompt"],
"steps": [
{
"thought": "One tool call answers this task.",
"action": {"tool_calls": [{"id": "call_1", **call}]},
"observation": output,
}
],
"final_response": output,
"metadata": {
"conversation": conversation,
"split": "train",
"task_id": task["id"],
"student_model_target": "CohereLabs/aya-expanse-8b",
},
}
def main():
here = os.path.dirname(os.path.abspath(__file__))
data = os.path.join(here, "..", "data")
os.makedirs(data, exist_ok=True)
train, test = build_tasks()
with open(os.path.join(data, "tasks.json"), "w") as f:
json.dump({"tools": TOOL_SCHEMAS, "train": train, "test": test}, f, indent=2)
with open(os.path.join(data, "sft_agenttune.jsonl"), "w") as f:
for t in train:
f.write(json.dumps(sft_row(t)) + "\n")
with open(os.path.join(data, "trajectories.jsonl"), "w") as f:
for t in train:
f.write(json.dumps(trajectory_row(t)) + "\n")
print(f"train tasks: {len(train)} test tasks: {len(test)}")
print(f"SFT trajectories written: {len(train)} -> data/sft_agenttune.jsonl")
print(f"AgentTune trajectories written: {len(train)} -> data/trajectories.jsonl")
if __name__ == "__main__":
main()