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
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
HF Jobs script: Build Speculative Tool Actions datasets and push to Hub.
|
| 3 |
+
"""
|
| 4 |
+
import json
|
| 5 |
+
import re
|
| 6 |
+
from collections import Counter
|
| 7 |
+
from datasets import load_dataset, Dataset
|
| 8 |
+
from random import Random
|
| 9 |
+
|
| 10 |
+
ACTION_TYPES = [
|
| 11 |
+
"tool_call", "retrieval", "file_read", "file_write",
|
| 12 |
+
"repair", "verifier", "ask_clarification", "final_answer", "BLOCKED",
|
| 13 |
+
]
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
def classify_action(content, tool_calls=None):
|
| 17 |
+
c = content.lower()
|
| 18 |
+
tc = json.dumps(tool_calls).lower() if tool_calls else ""
|
| 19 |
+
combined = c + " " + tc
|
| 20 |
+
if re.search(r'\b(final answer|conclusion|summary:|in conclusion|the answer is)\b', combined):
|
| 21 |
+
return "final_answer"
|
| 22 |
+
if re.search(r'\b(ask for clarification|need more info|could you clarify|what do you mean)\b', combined):
|
| 23 |
+
return "ask_clarification"
|
| 24 |
+
if re.search(r'\b(blocked|unsafe|i cannot|i\'m sorry, but|refuse|not allowed|harmful)\b', combined):
|
| 25 |
+
return "BLOCKED"
|
| 26 |
+
if re.search(r'\b(write.*file|save.*file|edit.*file|patch|diff)\b', combined):
|
| 27 |
+
return "file_write"
|
| 28 |
+
if re.search(r'\b(read.*file|view.*file|cat |head |tail |open.*file|get_content)\b', combined):
|
| 29 |
+
return "file_read"
|
| 30 |
+
if re.search(r'\b(repair|fix.*bug|correct.*error|debug|resolve|try.*again with)\b', combined):
|
| 31 |
+
return "repair"
|
| 32 |
+
if re.search(r'\b(verify|check|validate|test|assert|review)\b', combined):
|
| 33 |
+
return "verifier"
|
| 34 |
+
if re.search(r'\b(search|retrieve|find|lookup|query|google|bing)\b', combined):
|
| 35 |
+
return "retrieval"
|
| 36 |
+
if tool_calls or re.search(r'\b(function call|tool call|invoke|execute)\b', combined):
|
| 37 |
+
return "tool_call"
|
| 38 |
+
return "tool_call"
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
def process_swe_smith(max_rows=5000):
|
| 42 |
+
print("Loading SWE-smith tool/train ...")
|
| 43 |
+
ds = load_dataset("SWE-bench/SWE-smith-trajectories", "tool", split="train", streaming=True)
|
| 44 |
+
rows_proposer, rows_verifier, rows_eval = [], [], []
|
| 45 |
+
count = 0
|
| 46 |
+
for example in ds:
|
| 47 |
+
count += 1
|
| 48 |
+
if count > max_rows:
|
| 49 |
+
break
|
| 50 |
+
messages = example.get("messages", [])
|
| 51 |
+
resolved = example.get("resolved", False)
|
| 52 |
+
state_so_far = []
|
| 53 |
+
for msg in messages:
|
| 54 |
+
role = msg.get("role", "")
|
| 55 |
+
content = msg.get("content", "")
|
| 56 |
+
tool_calls = msg.get("tool_calls", None)
|
| 57 |
+
if role in ("assistant", "agent"):
|
| 58 |
+
action_type = classify_action(content, tool_calls)
|
| 59 |
+
prompt_messages = state_so_far.copy()
|
| 60 |
+
completion_messages = [{"role": "assistant", "content": content}]
|
| 61 |
+
if tool_calls:
|
| 62 |
+
completion_messages[0]["tool_calls"] = tool_calls
|
| 63 |
+
rows_proposer.append({"prompt": prompt_messages, "completion": completion_messages, "action_type": action_type})
|
| 64 |
+
rows_verifier.append({"prompt": prompt_messages, "completion": completion_messages, "label": bool(resolved), "action_type": action_type})
|
| 65 |
+
rows_eval.append({"messages": prompt_messages + completion_messages, "resolved": resolved, "action_type": action_type})
|
| 66 |
+
state_so_far.append(msg)
|
| 67 |
+
print(f" -> proposer={len(rows_proposer)}, verifier={len(rows_verifier)}")
|
| 68 |
+
return rows_proposer, rows_verifier, rows_eval
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
def process_toolbench(max_rows=3000):
|
| 72 |
+
print("Loading toolbench/train ...")
|
| 73 |
+
ds = load_dataset("tuandunghcmut/toolbench-v1", split="train", streaming=True)
|
| 74 |
+
rows_proposer, rows_verifier, rows_eval = [], [], []
|
| 75 |
+
count = 0
|
| 76 |
+
for example in ds:
|
| 77 |
+
count += 1
|
| 78 |
+
if count > max_rows:
|
| 79 |
+
break
|
| 80 |
+
conv = example.get("conversations", {})
|
| 81 |
+
froms = conv.get("from", [])
|
| 82 |
+
values = conv.get("value", [])
|
| 83 |
+
state_so_far = []
|
| 84 |
+
for role, content in zip(froms, values):
|
| 85 |
+
msg = {"role": role, "content": content}
|
| 86 |
+
if role == "assistant":
|
| 87 |
+
action_type = classify_action(content)
|
| 88 |
+
rows_proposer.append({"prompt": state_so_far.copy(), "completion": [msg], "action_type": action_type})
|
| 89 |
+
rows_verifier.append({"prompt": state_so_far.copy(), "completion": [msg], "label": True, "action_type": action_type})
|
| 90 |
+
rows_eval.append({"messages": state_so_far + [msg], "resolved": True, "action_type": action_type})
|
| 91 |
+
state_so_far.append(msg)
|
| 92 |
+
print(f" -> proposer={len(rows_proposer)}, verifier={len(rows_verifier)}")
|
| 93 |
+
return rows_proposer, rows_verifier, rows_eval
|
| 94 |
+
|
| 95 |
+
|
| 96 |
+
def build_proposer(rows, hub_org):
|
| 97 |
+
def fmt(row):
|
| 98 |
+
system_msg = {"role": "system", "content": (
|
| 99 |
+
"You are an agent action predictor. Given the conversation state, predict the next action from: "
|
| 100 |
+
+ ", ".join(ACTION_TYPES) + ". Respond with exactly the action name and a brief justification.")}
|
| 101 |
+
prompt = [system_msg] + row["prompt"]
|
| 102 |
+
prompt[-1]["content"] += "\n\n[Next Action Prediction] Choose one: " + ", ".join(ACTION_TYPES)
|
| 103 |
+
completion = row["completion"]
|
| 104 |
+
completion[0]["content"] = f"Action: {row['action_type']}\n" + completion[0]["content"]
|
| 105 |
+
return {"prompt": prompt, "completion": completion}
|
| 106 |
+
ds = Dataset.from_list([fmt(r) for r in rows])
|
| 107 |
+
ds = ds.shuffle(seed=42).train_test_split(test_size=0.1)
|
| 108 |
+
ds.push_to_hub(f"{hub_org}/speculative-actions-proposer-sft")
|
| 109 |
+
print(f"Pushed proposer SFT to {hub_org}/speculative-actions-proposer-sft")
|
| 110 |
+
return ds
|
| 111 |
+
|
| 112 |
+
|
| 113 |
+
def build_verifier(rows, hub_org):
|
| 114 |
+
rng = Random(42)
|
| 115 |
+
good_rows = [r for r in rows if r["label"]]
|
| 116 |
+
bad_rows = [r for r in rows if not r["label"]]
|
| 117 |
+
if len(bad_rows) < len(good_rows) * 0.2:
|
| 118 |
+
for r in good_rows:
|
| 119 |
+
wrong_action = rng.choice([a for a in ACTION_TYPES if a != r["action_type"]])
|
| 120 |
+
bad_rows.append({
|
| 121 |
+
"prompt": r["prompt"],
|
| 122 |
+
"completion": [{"role": "assistant", "content": f"Action: {wrong_action}\n(synthetic incorrect action)"}],
|
| 123 |
+
"label": False,
|
| 124 |
+
"action_type": wrong_action,
|
| 125 |
+
})
|
| 126 |
+
pairs = []
|
| 127 |
+
for g in good_rows:
|
| 128 |
+
b = rng.choice(bad_rows)
|
| 129 |
+
pairs.append({"prompt": g["prompt"], "chosen": g["completion"], "rejected": b["completion"], "action_type": g["action_type"]})
|
| 130 |
+
ds = Dataset.from_list(pairs)
|
| 131 |
+
ds = ds.shuffle(seed=42).train_test_split(test_size=0.1)
|
| 132 |
+
ds.push_to_hub(f"{hub_org}/speculative-actions-verifier-pref")
|
| 133 |
+
print(f"Pushed verifier pref to {hub_org}/speculative-actions-verifier-pref")
|
| 134 |
+
return ds
|
| 135 |
+
|
| 136 |
+
|
| 137 |
+
def build_eval(rows, hub_org):
|
| 138 |
+
ds = Dataset.from_list(rows)
|
| 139 |
+
ds = ds.shuffle(seed=42).select(range(min(2000, len(rows))))
|
| 140 |
+
ds.push_to_hub(f"{hub_org}/speculative-actions-eval")
|
| 141 |
+
print(f"Pushed eval to {hub_org}/speculative-actions-eval")
|
| 142 |
+
return ds
|
| 143 |
+
|
| 144 |
+
|
| 145 |
+
def main():
|
| 146 |
+
hub_org = "narcolepticchicken"
|
| 147 |
+
p1, v1, e1 = process_swe_smith(5000)
|
| 148 |
+
p2, v2, e2 = process_toolbench(3000)
|
| 149 |
+
proposer_rows = p1 + p2
|
| 150 |
+
verifier_rows = v1 + v2
|
| 151 |
+
eval_rows = e1 + e2
|
| 152 |
+
print(f"\nTotal: proposer={len(proposer_rows)}, verifier={len(verifier_rows)}, eval={len(eval_rows)}")
|
| 153 |
+
print("\nAction distribution:")
|
| 154 |
+
for act, n in Counter(r["action_type"] for r in proposer_rows).most_common():
|
| 155 |
+
print(f" {act}: {n}")
|
| 156 |
+
build_proposer(proposer_rows, hub_org)
|
| 157 |
+
build_verifier(verifier_rows, hub_org)
|
| 158 |
+
build_eval(eval_rows, hub_org)
|
| 159 |
+
print("Done.")
|
| 160 |
+
|
| 161 |
+
|
| 162 |
+
if __name__ == "__main__":
|
| 163 |
+
main()
|