tool format hardening
This commit is contained in:
@@ -71,9 +71,25 @@ def shell(cmd, t=120):
|
||||
if isinstance(err, bytes): err = err.decode("utf-8", "replace")
|
||||
return f"{(out + err).strip()}\n\n[shell timeout after {t}s]\nexit: -1"
|
||||
|
||||
def sanitize_msgs(msgs):
|
||||
for m in msgs:
|
||||
if isinstance(m, dict) and m.get("role") == "assistant" and "tool_calls" in m:
|
||||
tcs = m.get("tool_calls") or []
|
||||
for tc in tcs:
|
||||
if isinstance(tc, dict) and "function" in tc:
|
||||
fn = tc.get("function") or {}
|
||||
astr = fn.get("arguments", "{}")
|
||||
try:
|
||||
p = json.loads(astr) if astr else {}
|
||||
if not isinstance(p, dict):
|
||||
fn["arguments"] = json.dumps({"invalid_raw": astr} if astr else {})
|
||||
except Exception:
|
||||
fn["arguments"] = json.dumps({"invalid_raw": astr} if astr else {})
|
||||
|
||||
fib = [1, 1, 2, 3, 5, 8, 13, 21, 34]
|
||||
|
||||
def llm(cfg, msgs, tools):
|
||||
sanitize_msgs(msgs)
|
||||
url = cfg["endpoint"].rstrip("/") + "/chat/completions"
|
||||
h = {"Content-Type": "application/json", "User-Agent": "Mozilla/5.0 (compatible; Bantam/1.0)"}
|
||||
k = cfg.get("api_key", "").strip()
|
||||
@@ -126,6 +142,11 @@ def llm(cfg, msgs, tools):
|
||||
return m
|
||||
except Exception as e:
|
||||
if _COL: sys.stdout.write("\r\033[K"); sys.stdout.flush()
|
||||
if isinstance(e, urllib.error.HTTPError):
|
||||
code = e.code
|
||||
body = e.read().decode("utf-8", "replace").strip() if hasattr(e, "read") else str(e)
|
||||
if 400 <= code < 500 and code not in (408, 429):
|
||||
raise RuntimeError(f"HTTP {code}: {body}")
|
||||
if i < len(fib): print(c(f"[network error: {e}, retrying in {dly}s...]", 31)); time.sleep(dly)
|
||||
else: raise
|
||||
|
||||
@@ -148,7 +169,9 @@ def AL(cfg, msgs, sp, depth=0):
|
||||
a = json.loads(astr) if astr else {}
|
||||
if not isinstance(a, dict): raise ValueError("args must be a JSON object")
|
||||
err = ""
|
||||
except Exception as e: err, a = str(e), {}
|
||||
except Exception as e:
|
||||
err, a = str(e), {}
|
||||
tc["function"]["arguments"] = json.dumps({"invalid_raw": astr} if astr else {})
|
||||
if err: res, sty = f"[tool error: invalid JSON args for {fn}: {err}. Raw: {astr!r}]", 31
|
||||
elif fn == "shell_exec": res, sty = shell(a.get("command", ""), stime), 2
|
||||
elif fn == "run_subagent":
|
||||
|
||||
Reference in New Issue
Block a user