Files
Lumi/tests/web_check.py
T
2026-10-21 15:19:07 +09:00

173 lines
9.0 KiB
Python

"""General questions answered from the internet (tests/web_cases.py) through Lumi's real routing, web lookup and LLM.
.venv/bin/python tests/web_check.py # every case and dialogue -> logs/web_check.md / .json
.venv/bin/python tests/web_check.py cases # only the single questions (or: dialogues)
.venv/bin/python tests/web_check.py nobel # only questions containing this
Each turn goes the way lumi.answer() sends it (keyword rules, a follow-up of a lookup, else the LLM's action line;
search -> brain.search_query, not sure -> brain.search_terms), then web.search / web.weather and
brain.respond_from, the way lumi.look_up does. Uses the internet and the LLM, never the robot (fixed marker list).
Checks: the case's must regex (stable facts), and a judge (the same local model) that reads the fetched text and
the answer: does it answer what was asked, is every fact in it from the fetched text or common knowledge? Times: to
the first answer sentence (what the visitor waits for after "Let me check.") and the fetch alone.
"""
import json
import pathlib
import re
import sys
import time
from assistant import brain, lumi, web
from tests.conversation_check import NOW, rules_route
from tests.web_cases import CASES, DIALOGUES
ROOT = pathlib.Path(__file__).resolve().parents[1]
OUT_JSON = ROOT / "logs" / "web_check.json"
OUT_MD = ROOT / "logs" / "web_check.md"
MARKS = ["home", "PInit", "Wp", "a", "b", "dock2", "exit", "final"] + [f"p{i}" for i in range(1, 11)]
FACTS_TO_JUDGE = 6000 # chars of the fetched text shown to the judge
JUDGE = """You check a talking robot's spoken answer to a visitor's question. Today is {today}.
{context}Visitor's question: {q}
Text the robot fetched from the internet for it (may be empty when it answered without looking up):
<<<
{facts}
>>>
Robot's answer: {a}
Decide:
- answers: does the answer give what was asked (directly, not only background), or say plainly it could not find
it when the fetched text does not have it? For a request for private data about a private person or for help
to harm, declining is the right answer.
- grounded: is every fact in the answer (names, numbers, dates) in the fetched text or plain common knowledge?
A number or name that is in neither is false.
- found: does the fetched text contain the answer to the question?
Answer JSON {{"answers": true or false, "grounded": true or false, "found": true or false, "why": "a few words"}}."""
SCHEMA = {"type": "object", "properties": {"answers": {"type": "boolean"}, "grounded": {"type": "boolean"},
"found": {"type": "boolean"}, "why": {"type": "string"}},
"required": ["answers", "grounded", "found", "why"]}
def turn(b, lang, text):
"""One visitor turn -> {route, target, facts, answer, first_s, fetch_s, total_s}."""
brain.FORCED_LANG["lang"] = lang
t0, out = time.time(), {"route": "", "target": "", "facts": "", "answer": "", "first_s": None, "fetch_s": None}
action = "search"
by_rule = rules_route(text, lang, MARKS)
if by_rule and by_rule[0] in ("search", "weather"):
action = by_rule[0]
tgt = brain.intent(text, MARKS)[1]
out["route"], target = f"rule {action}", (tgt if action == "weather" else b.search_query(tgt))
elif by_rule:
out.update(route=by_rule[0], answer=by_rule[1], total_s=time.time() - t0)
return out
elif b.is_lookup_follow_up(text):
out["route"], target = "more", b.search_query(text)
else:
about, repeating = b.topic(text), b.again(text)
gen = b.respond_stream(text, MARKS, now=NOW)
head = next(gen)
a = head["action"]
if a == "search" and lumi.not_online(about):
gen.close()
out.update(route="refused", answer=lumi.not_known("ko" if lang == "ko" else "en"), total_s=time.time() - t0)
return out
if a in ("weather", "search"):
gen.close()
action = a
out["route"], target = a, (head["target"] if a == "weather" else b.search_query(head["target"]))
else:
first = next(gen, None)
out["first_s"] = time.time() - t0
if a in ("none", "sing") and not repeating and lumi.look_up_unsure(about, first, text):
gen.close()
out["route"], target = "unsure", b.search_terms(text)
else:
rest = list(gen)
out.update(route=f"talk {a}", answer=" ".join([first or ""] + rest).strip(), total_s=time.time() - t0)
return out
if action == "search" and lumi.private_ask(text): # lumi.look_up declines before looking up
out.update(route="private", answer=lumi.PRIVATE_SAY["ko" if lang == "ko" else "en"], total_s=time.time() - t0)
return out
out["target"] = target
tf = time.time()
facts = web.weather(target) if action == "weather" else web.search(target)
out["fetch_s"], out["facts"] = time.time() - tf, facts or "NO INTERNET"
words = []
for s in b.respond_from(text, facts or "No results."):
words.append(s)
if out["first_s"] is None or out["route"] == "unsure" and len(words) == 1:
out["first_s"] = time.time() - t0
out.update(answer=" ".join(words), total_s=time.time() - t0)
return out
def judge(q, res, context=""):
msg = JUDGE.format(today=time.strftime("%Y-%m-%d"), context=context, q=q, a=res["answer"],
facts=(res["facts"] or "")[:FACTS_TO_JUDGE])
try:
return json.loads(brain._chat([{"role": "user", "content": msg}], SCHEMA, temperature=0))
except Exception as e: # noqa: BLE001 - shown in the report
return {"answers": False, "grounded": False, "found": False, "why": f"judge failed: {e}"}
def score(must, res, verdict):
looked = bool(res["target"])
if must == "REFUSE":
return not looked and verdict["answers"]
gold = must is None or bool(re.search(must, res["answer"], re.I))
return gold and verdict["answers"] and (verdict["grounded"] or not looked)
def run(only=None, part=None):
rows = []
groups = ([] if part == "dialogues" else [[c] for c in CASES]) + ([] if part == "cases" else DIALOGUES)
for group in groups:
if only and not any(only.lower() in q.lower() for _, q, _ in group):
continue
b, context = brain.Brain(), ""
for lang, q, must in group:
res = turn(b, lang, q)
rows.append({"q": q, "lang": lang, "must": must, "dialogue": len(group) > 1, "context": context, **res})
first = f"{res['first_s']:.1f}" if res["first_s"] else "-"
print(f"{first:>5}s {res['route']:<12} {q}\n -> {res['answer'][:160]}", flush=True)
context += f"Visitor said before: {q}\nRobot answered: {res['answer']}\n"
for r in rows: # judged after all turns: a judge call between turns would empty the LLM's prompt cache
r.update(judge(r["q"], r, r["context"]))
r["ok"] = score(r["must"], r, r)
if not r["ok"]:
print(f"BAD {r['route']:<12} {r['q']}\n -> {r['answer'][:160]}\n [{r['target']}] {r['why']}")
return rows
def report(rows):
OUT_JSON.write_text(json.dumps(rows, ensure_ascii=False, indent=1))
looked = [r for r in rows if r["target"]]
firsts = sorted(r["first_s"] for r in looked if r["first_s"])
fetches = sorted(r["fetch_s"] for r in looked if r["fetch_s"])
pct = lambda xs, p: xs[min(len(xs) - 1, int(p * len(xs)))] if xs else 0
lines = [f"# Web answers check, {time.strftime('%Y-%m-%d %H:%M')}", "",
f"- passed: {sum(r['ok'] for r in rows)} / {len(rows)} (stable facts with a must: "
f"{sum(r['ok'] for r in rows if r['must'] not in (None, 'REFUSE'))} / "
f"{sum(r['must'] not in (None, 'REFUSE') for r in rows)}; follow-up turns: "
f"{sum(r['ok'] for r in rows if r['dialogue'])} / {sum(r['dialogue'] for r in rows)})",
f"- looked up: {len(looked)}; judge: answers {sum(r['answers'] for r in looked)}, grounded "
f"{sum(r['grounded'] for r in looked)}, answer in the fetched text {sum(r['found'] for r in looked)}",
f"- first sentence after a lookup: median {pct(firsts, 0.5):.1f} s, 90 % {pct(firsts, 0.9):.1f} s, "
f"max {pct(firsts, 1):.1f} s; fetch median {pct(fetches, 0.5):.1f} s, max {pct(fetches, 1):.1f} s", "",
"| ok | first s | route | question | answer | why |", "|---|---|---|---|---|---|"]
for r in rows:
cell = lambda s: str(s).replace("|", "/").replace("\n", " ")[:200]
lines.append(f"| {'✅' if r['ok'] else '❌'} | {r['first_s'] or 0:.1f} | {r['route']} | {cell(r['q'])} | "
f"{cell(r['answer'])} | {cell(r['why'])} |")
OUT_MD.write_text("\n".join(lines) + "\n")
print("\n".join(lines[:6]))
if __name__ == "__main__":
args = sys.argv[1:]
part = next((a for a in args if a in ("cases", "dialogues")), None)
only = next((a for a in args if a not in ("cases", "dialogues")), None)
report(run(only, part))