"""HumpBack v2 server: local API + web UI for the belief memory. Endpoints: GET / web UI (chat + memory viewer) POST /api/ask {question} -> {answer, confidence, ...} POST /api/learn {claim, subject?, predicate?, obj?, source_type?, source_ref?} GET /api/memory -> active beliefs + stats POST /api/forget {query} -> delete matching beliefs POST /api/expire -> run expiry sweep GET /api/status -> memory + tools stats GET /api/calibration -> ECE on a held-out set """ from __future__ import annotations from fastapi import FastAPI, HTTPException from fastapi.responses import HTMLResponse, JSONResponse from .config import INITIAL_CONFIDENCE, VOLATILITY from .memory import Memory from .tools import Calculator, FileLookup, SandboxRun app = FastAPI(title="HumpBack v2 โ belief memory server") _m = Memory("humpback2.db") @app.get("/", response_class=HTMLResponse) def index(): return HTMLResponse(content=UI, status_code=200) @app.post("/api/ask") def ask(req: dict): question = (req.get("question") or "").strip() if not question: raise HTTPException(400, "question required") res = _m.ask(question, tools=_tools()) return JSONResponse({ "answer": res["answer"], "confidence": res["confidence"], "method": res["method"], "tool_calls": res["tool_calls"], "logprob": round(res["logprob"], 3), "sources": res["sources"], }) @app.post("/api/learn") def learn(req: dict): claim = (req.get("claim") or "").strip() if not claim: raise HTTPException(400, "claim required") meta = { "subject": req.get("subject"), "predicate": req.get("predicate"), "obj": req.get("obj"), "source_type": req.get("source_type") or "user", "source_ref": req.get("source_ref"), "confidence": req.get("confidence"), "volatility": req.get("volatility"), } sid = _m.learn(claim, **{k: v for k, v in meta.items() if v is not None}) return JSONResponse({"id": sid, "memory_size": _m.size()}) @app.get("/api/memory") def memory(): rows = _m.store.conn.execute( "SELECT id, claim, subject, predicate, object, source_type, volatility, " "confidence, support_count, contradict_count, status, last_used_at " "FROM beliefs WHERE status='active' ORDER BY last_confirmed_at DESC LIMIT 200" ).fetchall() return JSONResponse([dict(r) for r in rows] + [{"memory_size": _m.size()}]) @app.get("/api/status") def status(): return JSONResponse({ "memory_size": _m.size(), "active": _m.store.count_active(), "retracted": _m.store.conn.execute( "SELECT COUNT(*) c FROM beliefs WHERE status='retracted'").fetchone()["c"], "superseded": _m.store.conn.execute( "SELECT COUNT(*) c FROM beliefs WHERE status='superseded'").fetchone()["c"], "volatility": dict(VOLATILITY), "priors": INITIAL_CONFIDENCE, "tools": [t.name for t in _tools()], }) @app.post("/api/forget") def forget(req: dict): query = (req.get("query") or "").strip() if not query: raise HTTPException(400, "query required") n = _m.forget(query) return JSONResponse({"removed": n, "memory_size": _m.size()}) @app.post("/api/expire") def expire(): n = _m.expiry_sweep() return JSONResponse({"retracted": n, "memory_size": _m.size()}) @app.get("/api/calibration") def calibration(): from .evaluate import calibration_report rep = calibration_report(_m) return JSONResponse(rep) def _tools(): return [Calculator(), FileLookup(), SandboxRun()] # --- single-file web UI ------------------------------------------------- UI = r"""