Models work numbers out in their heads and get multi-digit products and powers wrong (qwen3:8b: 3847 x 2963 as 11,378,261; in the reply loop it searched the web for it and gave no answer). The calculator evaluates a whitelisted ast (numbers, operators, a few math functions, pi/e), never code, with a 200 character cap and a power size limit so nothing hangs. Idea from OpenJarvis's calculator tool (Apache 2.0). Eval (qwen3:8b, v2 setup): 0/2 before, 12/12 over four runs after; the unrelated question never calls it. Router evals unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
69 lines
2.9 KiB
Python
69 lines
2.9 KiB
Python
"""
|
|
Eval: the model works out arithmetic with the calculator tool and answers with its exact value
|
|
instead of computing it in its head (src/jarvis/tools/builtin/calculator.spec.md).
|
|
|
|
Cases (numbers chosen so a model guessing in its head gets them wrong; qwen3:8b without the tool
|
|
answered 3847 x 2963 as 11,378,261 and 1.07^12 as 3.84 on 2026-11-23):
|
|
|
|
- a product of two four-digit numbers uses the calculator and the answer carries the exact value,
|
|
- a power with a fraction does the same,
|
|
- an unrelated question does not use the calculator.
|
|
|
|
Run: EVAL_JUDGE_MODEL=qwen3:8b python -m pytest evals/test_calculator_tool.py -v
|
|
(v2 brain setup: EVAL_COMPACT_PROMPT=1 EVAL_INTENT_JUDGE=1)
|
|
"""
|
|
|
|
import re
|
|
|
|
import pytest
|
|
|
|
from conftest import requires_judge_llm
|
|
|
|
|
|
def _run(mock_config, eval_db, eval_dialogue_memory, query):
|
|
from helpers import JUDGE_BASE_URL, JUDGE_MODEL
|
|
from jarvis.memory.history import HistoryContext
|
|
from jarvis.reply import turn_state
|
|
from jarvis.reply.engine import run_reply_engine
|
|
from jarvis.tools.builtin import calculator
|
|
|
|
mock_config.ollama_base_url = JUDGE_BASE_URL
|
|
mock_config.ollama_chat_model = JUDGE_MODEL
|
|
calls = []
|
|
original = calculator.calculate
|
|
calculator.calculate = lambda expression: calls.append(expression) or original(expression)
|
|
turn_state.reset()
|
|
turn_state.set_user("100")
|
|
try:
|
|
reply = run_reply_engine(db=eval_db, cfg=mock_config, tts=None, text=query,
|
|
dialogue_memory=eval_dialogue_memory, history=HistoryContext(),
|
|
legacy_memory=False, default_language="Korean") or ""
|
|
finally:
|
|
turn_state.reset()
|
|
calculator.calculate = original
|
|
print(f"\n calculator [{JUDGE_MODEL}] {query!r}: calls={calls} reply={reply[:200]!r}")
|
|
return calls, reply
|
|
|
|
|
|
def _digits(text):
|
|
"""The reply with digit-group separators and spaces removed ("11,398,661" -> "11398661")."""
|
|
return re.sub(r"(?<=\d)[,\s](?=\d)", "", text).replace(" ", "")
|
|
|
|
|
|
@pytest.mark.eval
|
|
@requires_judge_llm
|
|
class TestCalculator:
|
|
def test_a_large_product_is_computed_exactly(self, mock_config, eval_db, eval_dialogue_memory):
|
|
calls, reply = _run(mock_config, eval_db, eval_dialogue_memory, "3847 곱하기 2963은 얼마야?")
|
|
assert calls, "the calculator was not used"
|
|
assert any(v in _digits(reply) for v in ("11398661", "1139만8661")), reply
|
|
|
|
def test_a_fractional_power_is_computed_exactly(self, mock_config, eval_db, eval_dialogue_memory):
|
|
calls, reply = _run(mock_config, eval_db, eval_dialogue_memory, "1.07의 12제곱은 얼마야?")
|
|
assert calls, "the calculator was not used"
|
|
assert "2.25" in reply, reply
|
|
|
|
def test_an_unrelated_question_does_not_calculate(self, mock_config, eval_db, eval_dialogue_memory):
|
|
calls, _ = _run(mock_config, eval_db, eval_dialogue_memory, "고양이는 하루에 몇 시간 자?")
|
|
assert not calls, calls
|