fix(reply): never deliver the tool-less answer when the planned call cannot run
When the skipped-tool guard could not run the planned call (not a literal call, or arguments the tool does not take) it delivered the answer it had held back, which was given without the tool and could be a made-up value. The planned call is now recorded as a failed tool result and the model answers from that, so the user hears that it could not check or do it. M0 kit: a case passes only when the final answer uses the tool result (13 degrees, not a made-up 24) or, for the no-tool request, says it cannot do it; the no-tool request now counts. The kit mirrors the engine's failed-result path, and a failing model moves on to the next candidate on these stricter checks. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -33,26 +33,39 @@ from m0lib.llm_client import ChatClient
|
||||
_KIT = Path(__file__).resolve().parent.parent
|
||||
_PKG = "m0_jarvis"
|
||||
|
||||
# (prompt, expected tool or None, argument check, canned tool result)
|
||||
FIX_CASES: list[tuple[str, str | None, Any, dict | None]] = [
|
||||
# (prompt, expected tool or None, argument check, canned tool result, check of the final answer)
|
||||
# The final check is what makes a case pass: the right tool call is not enough
|
||||
# if the spoken answer then ignores the result or claims something not done.
|
||||
REFUSAL = "refusal"
|
||||
FIX_CASES: list[tuple[str, str | None, Any, dict | None, str | None]] = [
|
||||
("서울 날씨 어때?", "get_weather", lambda a: "서울" in str(a.get("city", "")) or "Seoul" in str(a.get("city", "")),
|
||||
{"city": "서울", "temp_c": 13, "sky": "흐리고 비", "rain_mm": 2}),
|
||||
{"city": "서울", "temp_c": 13, "sky": "흐리고 비", "rain_mm": 2}, "13"),
|
||||
("부산 지금 비 와?", "get_weather", lambda a: "부산" in str(a.get("city", "")) or "Busan" in str(a.get("city", "")),
|
||||
{"city": "부산", "temp_c": 17, "sky": "맑음", "rain_mm": 0}),
|
||||
{"city": "부산", "temp_c": 17, "sky": "맑음", "rain_mm": 0}, "17"),
|
||||
("나 지금 서울인데 우산 챙겨야 할까?", "get_weather",
|
||||
lambda a: "서울" in str(a.get("city", "")) or "Seoul" in str(a.get("city", "")),
|
||||
{"city": "서울", "temp_c": 13, "sky": "흐리고 비", "rain_mm": 2}),
|
||||
("5분 타이머 맞춰 줘.", "set_timer", lambda a: int(a.get("minutes", 0)) == 5, {"ok": True, "minutes": 5}),
|
||||
("10분 뒤에 알려 줘.", "set_timer", lambda a: int(a.get("minutes", 0)) == 10, {"ok": True, "minutes": 10}),
|
||||
{"city": "서울", "temp_c": 13, "sky": "흐리고 비", "rain_mm": 2}, "비"),
|
||||
("5분 타이머 맞춰 줘.", "set_timer", lambda a: int(a.get("minutes", 0)) == 5, {"ok": True, "minutes": 5}, "5"),
|
||||
("10분 뒤에 알려 줘.", "set_timer", lambda a: int(a.get("minutes", 0)) == 10, {"ok": True, "minutes": 10}, "10"),
|
||||
("두 번째 모니터에 뭐 떠 있어?", "view_screen", lambda a: int(a.get("monitor", 0)) == 2,
|
||||
{"monitor": 2, "seen": "유튜브 재생 화면, 제목 '오늘의 뉴스'"}),
|
||||
("안녕, 오늘 기분 어때?", None, None, None),
|
||||
# No tool can do this: the answer must not say it was done (read the text).
|
||||
("거실 불 꺼 줘.", None, None, None),
|
||||
{"monitor": 2, "seen": "유튜브 재생 화면, 제목 '오늘의 뉴스'"}, "뉴스"),
|
||||
("안녕, 오늘 기분 어때?", None, None, None, None),
|
||||
# No tool can do this: passing means saying so, not claiming it was done.
|
||||
("거실 불 꺼 줘.", None, None, None, REFUSAL),
|
||||
]
|
||||
|
||||
# A value from each canned result that a grounded final answer repeats.
|
||||
_GROUNDING = {"get_weather": "temp_c", "set_timer": "minutes", "view_screen": None}
|
||||
# Words a Korean answer uses to say it cannot do something. The kit's prompts
|
||||
# are fixed Korean sentences, so a word check is enough here (the product
|
||||
# itself stays language-agnostic).
|
||||
_CANNOT = ("수 없", "수가 없", "못 ", "못해", "못합", "못했", "어렵", "지원하지", "기능이 없", "기능은 없", "불가")
|
||||
|
||||
|
||||
def case_for(prompt: str) -> tuple:
|
||||
return next(c for c in FIX_CASES if c[0] == prompt)
|
||||
|
||||
|
||||
def is_refusal(text: str) -> bool:
|
||||
return any(w in text for w in _CANNOT)
|
||||
|
||||
|
||||
def jarvis_src() -> Path | None:
|
||||
@@ -109,8 +122,9 @@ def _call_ok(call: dict, want: str, check) -> bool:
|
||||
return False
|
||||
|
||||
|
||||
def run_case(c: ChatClient, system: str, planner, prompt: str, want: str | None, check, result: dict | None) -> dict:
|
||||
"""One try through prompt layer -> planner -> guard -> final answer."""
|
||||
def run_case(c: ChatClient, system: str, planner, case: tuple) -> dict:
|
||||
"""One try through prompt layer -> planner -> guard -> final answer, as the engine does it."""
|
||||
prompt, want, check, result, final_check = case
|
||||
tool_names = [t["function"]["name"] for t in TOOLS]
|
||||
cfg = types.SimpleNamespace(planner_enabled=True, ollama_base_url="m0", planner_model=c.model,
|
||||
ollama_chat_model=c.model, planner_timeout_sec=60.0)
|
||||
@@ -125,35 +139,51 @@ def run_case(c: ChatClient, system: str, planner, prompt: str, want: str | None,
|
||||
row["text_with_call"] = bool(calls and a["text"].strip())
|
||||
row["model_ok"] = (not calls) if want is None else bool(calls) and _call_ok(calls[0], want, check)
|
||||
|
||||
call = calls[0] if calls else None
|
||||
call, tool_content = (calls[0], None) if calls else (None, None)
|
||||
row["guard_ran"] = False
|
||||
if not calls:
|
||||
steps = planner.named_tool_steps(plan, tool_names)
|
||||
parsed = planner.parse_concrete_tool_step(steps[0], TOOLS) if steps else None
|
||||
steps = [] if calls else planner.named_tool_steps(plan, tool_names)
|
||||
if steps:
|
||||
# Skipped-tool guard, as in the engine: run the literal planned call, or
|
||||
# if it cannot run, answer from a failed tool result (never the guess).
|
||||
parsed = planner.parse_concrete_tool_step(steps[0], TOOLS)
|
||||
if parsed:
|
||||
call = {"name": parsed[0], "arguments": json.dumps(parsed[1], ensure_ascii=False)}
|
||||
row["guard_ran"] = True
|
||||
row["guard_call"] = call
|
||||
if want is None:
|
||||
row["ok"] = call is None
|
||||
row["final_text"] = a["text"][:200]
|
||||
return row
|
||||
row["ok"] = bool(call) and _call_ok(call, want, check)
|
||||
if call and result is not None:
|
||||
else:
|
||||
call = {"name": steps[0].split()[0], "arguments": "{}"}
|
||||
tool_content = (f"Error: the planned call `{steps[0]}` could not be run, so nothing was fetched or "
|
||||
"done. Tell the user plainly that you could not check or do it right now; do not "
|
||||
"guess a value or claim it was done.")
|
||||
row["guard_failed_call"] = steps[0]
|
||||
|
||||
final = a["text"]
|
||||
if call is not None:
|
||||
if tool_content is None:
|
||||
if result is not None and want is not None and _call_ok(call, want, check):
|
||||
tool_content = json.dumps(result, ensure_ascii=False)
|
||||
else:
|
||||
tool_content = f"Error: {call['name']} cannot do that request."
|
||||
msgs += [{"role": "assistant", "content": "", "tool_calls": [
|
||||
{"id": "call_1", "type": "function", "function": {"name": call["name"], "arguments": call["arguments"]}}]},
|
||||
{"role": "tool", "tool_call_id": "call_1", "content": json.dumps(result, ensure_ascii=False)}]
|
||||
{"role": "tool", "tool_call_id": "call_1", "content": tool_content}]
|
||||
b = c.chat(msgs, max_tokens=120, tools=TOOLS, extra=NO_THINK)
|
||||
row["final_text"] = b["text"][:200]
|
||||
key = _GROUNDING.get(call["name"])
|
||||
row["final_uses_result"] = (str(result.get(key)) in b["text"]) if key and key in result else None
|
||||
final = b["text"]
|
||||
row["final_text"] = final[:200]
|
||||
|
||||
if want is not None:
|
||||
row["final_uses_result"] = bool(final_check) and final_check in final
|
||||
row["ok"] = bool(call) and _call_ok(call, want, check) and row["final_uses_result"]
|
||||
elif final_check == REFUSAL:
|
||||
row["ok"] = not calls and is_refusal(final)
|
||||
else:
|
||||
row["final_text"] = a["text"][:200]
|
||||
row["ok"] = not calls
|
||||
return row
|
||||
|
||||
|
||||
def tools_passed(summary: dict | None) -> bool:
|
||||
"""Every scored case ended with the right tool call (by the model or the guard)."""
|
||||
"""Every case passed: the right tool call (by the model or the guard) and a final
|
||||
answer that uses the tool's result, or an honest refusal where no tool can help."""
|
||||
return bool(summary) and summary["total"] > 0 and summary["ok"] == summary["total"]
|
||||
|
||||
|
||||
@@ -167,16 +197,17 @@ def test_tools_fixed(r: StageResult, c: ChatClient, tries: int = 3, key: str = "
|
||||
names = [t["function"]["name"] for t in TOOLS]
|
||||
system = f"{VOICE_SYSTEM}\n\n{sp.TOOL_HONESTY_RULES}\n\n{sp.tool_honesty_reminder(names)}"
|
||||
rows = []
|
||||
for prompt, want, check, result in FIX_CASES:
|
||||
for case in FIX_CASES:
|
||||
prompt, want = case[0], case[1]
|
||||
for i in range(tries):
|
||||
try:
|
||||
row = run_case(c, system, planner, prompt, want, check, result)
|
||||
row = run_case(c, system, planner, case)
|
||||
except Exception as e:
|
||||
row = {"prompt": prompt, "expected": want, "ok": False, "error": str(e)[:300]}
|
||||
row["try"] = i + 1
|
||||
rows.append(row)
|
||||
r.save()
|
||||
scored = [x for x in rows if x["prompt"] != "거실 불 꺼 줘."]
|
||||
scored = rows # every case counts, the no-tool refusal too
|
||||
summary = {"model_ok": sum(1 for x in scored if x.get("model_ok")),
|
||||
"ok": sum(1 for x in scored if x.get("ok")),
|
||||
"guard_ran": sum(1 for x in rows if x.get("guard_ran")),
|
||||
|
||||
Reference in New Issue
Block a user