"""Tool-call calibration conversations: NousResearch/hermes-function-calling-v1 (func-calling.json, rev dae3e1d), parsed into structured tool_calls and rendered through the model's own chat template with tools=, so the Hessians see the served system prompt and turns. No BFCL data. Import render(tok, path) -> list of rendered strings, in file order.""" import json, re CALL = re.compile(r"\s*(\{.*?\})\s*", re.S) RESP = re.compile(r"\s*(.*?)\s*", re.S) def convert(r): tools = json.loads(r["tools"]) if isinstance(r["tools"], str) else r["tools"] msgs = [] for m in r["conversations"]: v = m["value"] if m["from"] == "system": continue # the template writes its own tools system prompt if m["from"] == "human": msgs.append({"role": "user", "content": v}) elif m["from"] == "gpt": calls = [json.loads(c) for c in CALL.findall(v)] if calls: msgs.append({"role": "assistant", "content": CALL.sub("", v).strip(), "tool_calls": [{"type": "function", "function": {"name": c["name"], "arguments": c.get("arguments", {})}} for c in calls]}) else: msgs.append({"role": "assistant", "content": v}) elif m["from"] == "tool": for body in RESP.findall(v) or [v]: msgs.append({"role": "tool", "content": body}) return tools, msgs def render(tok, path, **kw): out = [] for r in json.load(open(path)): try: tools, msgs = convert(r) out.append(tok.apply_chat_template(msgs, tools=tools, tokenize=False, **kw)) except (ValueError, KeyError, TypeError, json.JSONDecodeError): continue return out