"""Split a Actuate model's raw output into tool calls + the sentence to speak. from actuate_parse import parse out = parse("lfm3", raw_text) # kind: "lfm3" | "spark" | "inkling" out["calls"] -> [{"name": "find_patient", "arguments": {"phone": "512 555 0142"}}] out["say"] -> "Let me check that for you." Each model keeps its base model's native tool-call format, so this is the same parsing the base models need. """ import json import re def _val(v): v = v.strip() try: return json.loads(v) except (json.JSONDecodeError, ValueError): return v def parse(kind, text): calls, say = [], text if kind == "lfm3": # value for blk in re.findall(r"(.*?)(?:|$)", text, re.S): m = re.search(r"\s]+)>(.*?)(?:|$)", blk, re.S) if m: args = {k: _val(v) for k, v in re.findall(r"\s]+)>\n?(.*?)\n?", m.group(2), re.S)} calls.append({"name": m.group(1).strip(), "arguments": args}) say = re.sub(r".*?(|$)", "", text, flags=re.S) elif kind == "spark": # namekeyvalue for blk in re.findall(r"(.*?)(?:|$)", text, re.S): name = blk.split("")[0].strip() args = {k.strip(): _val(v) for k, v in re.findall(r"(.*?)\s*(.*?)", blk, re.S)} if name: calls.append({"name": name, "arguments": args}) say = re.sub(r".*?(|$)", "", text, flags=re.S) elif kind == "inkling": # <|message_model|>name<|content_invoke_tool_json|>{"name": ..., "args": {...}} for m in re.finditer(r"<\|content_invoke_tool_json\|>(\{.*?\})\s*(?:<\|end_message\|>|$)", text, re.S): try: j = json.loads(m.group(1)) calls.append({"name": j.get("name"), "arguments": j.get("args") or {}}) except json.JSONDecodeError: pass parts = re.split(r"<\|message_model\|>", text) say = " ".join(re.sub(r"<\|[a-z_]+\|>", " ", p) for p in parts if "<|content_invoke_tool_json|>" not in p) say = re.sub(r".*?", "", say, flags=re.S) say = re.sub(r"<\|[^|>]+\|>|", " ", say) return {"calls": calls, "say": re.sub(r"\s+", " ", say).strip()}