Actuate-Inkling-2-Mini / actuate_parse.py
srinath-exe's picture
Rename to Actuate
a412486 verified
Raw History Blame Contribute Delete
2.57 kB
"""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": # <tool_call><function=name><parameter=key>value</parameter></function></tool_call>
for blk in re.findall(r"<tool_call>(.*?)(?:</tool_call>|$)", text, re.S):
m = re.search(r"<function=([^>\s]+)>(.*?)(?:</function>|$)", blk, re.S)
if m:
args = {k: _val(v) for k, v in re.findall(r"<parameter=([^>\s]+)>\n?(.*?)\n?</parameter>", m.group(2), re.S)}
calls.append({"name": m.group(1).strip(), "arguments": args})
say = re.sub(r"<tool_call>.*?(</tool_call>|$)", "", text, flags=re.S)
elif kind == "spark": # <tool_call>name<arg_key>key</arg_key><arg_value>value</arg_value></tool_call>
for blk in re.findall(r"<tool_call>(.*?)(?:</tool_call>|$)", text, re.S):
name = blk.split("<arg_key>")[0].strip()
args = {k.strip(): _val(v) for k, v in re.findall(r"<arg_key>(.*?)</arg_key>\s*<arg_value>(.*?)</arg_value>", blk, re.S)}
if name:
calls.append({"name": name, "arguments": args})
say = re.sub(r"<tool_call>.*?(</tool_call>|$)", "", 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"<think>.*?</think>", "", say, flags=re.S)
say = re.sub(r"<\|[^|>]+\|>|</?think>", " ", say)
return {"calls": calls, "say": re.sub(r"\s+", " ", say).strip()}