File size: 3,453 Bytes
b1aa83e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | #!/usr/bin/env python3
"""check_agent - check a custom Oracle7 agent before you use it.
python agents/check_agent.py agents/kitchen (run from the release folder, next to oracle7.py)
1. agent.json is valid and names only STANDING specialists (each passed its own held-out test).
2. Coverage: up to 12 development questions from each chosen specialist's own panel are asked of the AGENT; it reports
how many its specialists answered, declined in-domain, or left to the core. (Development questions only - the held-out
questions that admitted each specialist are never used here.)
3. Scope: a few questions from outside every chosen specialist are asked; with "core": "never" each must get the
out-of-scope reply, and with "fallback" none may come back through a specialist the agent did not choose.
Exit code 0 = the agent is usable as written."""
import json
import os
import sys
from pathlib import Path
HERE = Path(__file__).resolve().parent.parent
sys.path.insert(0, str(HERE))
sys.path.insert(0, str(HERE / "agents"))
OFF_TOPIC = ["Who wrote Leaves of Grass?", "What is the derivative of x^3?", "How long does a stone take to fall 80 m?",
"A van gets 25 mpg. How many kg of CO2 does a 200-mile trip produce?", "What is the capital of Malta?"]
def main():
if len(sys.argv) != 2:
sys.exit(__doc__)
import oracle7
import oracle7_agent as A
root = oracle7.ROOT
try:
spec = A.load_spec(sys.argv[1], root)
except A.AgentError as e:
print("agent.json: FAIL -", e)
sys.exit(1)
print("agent.json: ok - %s, %d specialists, core=%s" % (spec["name"], len(spec["specialists"]), spec["core"]))
oracle7.setup()
core = None
if spec["core"] == "fallback":
_, core = oracle7.load()
agent = A.Agent(sys.argv[1], core, root)
bad = 0
for sid in spec["specialists"]:
m = json.load(open(A.spec_root(root) / sid / "manifest.json", encoding="utf-8"))
ppath = str(m["panel"]["path"]).replace("__ORACLE7_ROOT__", str(root))
items = json.load(open(ppath, encoding="utf-8"))
items = items["items"] if isinstance(items, dict) else items
dev = [x for x in items if x.get("split") == "dev" and x.get("valid", True)][:12]
c = {"specialist": 0, "agent_core": 0, "declined": 0}
for x in dev:
r = agent.chat(x["q"], session_id="check:%s" % sid)
p = str(r.get("path", ""))
if ">specialist:" in p or ">combo:" in p:
c["specialist"] += 1
elif r.get("status") == "answered":
c["agent_core"] += 1
else:
c["declined"] += 1
print(" %-22s dev %2d: answered by its specialists %2d | by the core %2d | declined %2d" % (
sid, len(dev), c["specialist"], c["agent_core"], c["declined"]))
print("scope:")
for q in OFF_TOPIC:
r = agent.chat(q, session_id="check:scope")
p = str(r.get("path", ""))
foreign = A._foreign(p.split(">", 1)[1] if ">" in p else p, set(spec["specialists"]))
ok = (not foreign) and (spec["core"] == "fallback" or p.endswith(">out_of_scope") or ">specialist:" in p)
bad += not ok
print(" [%s] %-70s %s" % ("ok" if ok else "FAIL", q[:70], p[:90]))
print("RESULT", "usable" if bad == 0 else "NOT usable (%d scope failures)" % bad)
sys.exit(1 if bad else 0)
if __name__ == "__main__":
main()
|