File size: 4,156 Bytes
1a7314a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
"""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()