Download humpback2/core.py from gregrsea/HumpBack: direct link, hf CLI and curl.
- Browser
- Download file 4.28 kB
-
https://huggingface.co/gregrsea/HumpBack/resolve/main/humpback2/core.py
- Command line
-
hf download hf://gregrsea/HumpBack/humpback2/core.py
-
curl -L -o core.py https://huggingface.co/gregrsea/HumpBack/resolve/main/humpback2/core.py
4.28 kB
| """Deterministic frozen-core emulator for the demo. | |
| v2 is designed around a frozen, quantized LLM core (0.5B-3B, llama.cpp). That | |
| model is not bundled here, so we ship a `RuleCore` that answers deterministically | |
| from the belief store. It keeps the same interface as a real core so the server | |
| and metacognition can be exercised end-to-end and swapped for a real model later. | |
| """ | |
| from __future__ import annotations | |
| import re | |
| import numpy as np | |
| from .belief_store import BeliefStore | |
| from .tools import Calculator | |
| _PR = re.compile( | |
| r"^(?:what|how)\s+(?:is|are|was)\s+the\s+(?:price|cost|value)\s+of\s+(.+?)\s*\??$", | |
| re.I, | |
| ) | |
| _CONCEPT = re.compile(r"^(?:what|who)\s+(?:is|are|was)\s+(.+?)\s*\??$", re.I) | |
| _FACT = re.compile(r"^(.+?)\s*\??$", re.I) | |
| _INTRO = {"static": "My stored memory says: ", "slow": "My stored memory says: ", "fast": "My stored memory says: "} | |
| class Core: | |
| """Interface a real frozen LLM core can implement later.""" | |
| def ask(self, question: str, store: BeliefStore, tools=None) -> dict: | |
| raise NotImplementedError | |
| class RuleCore(Core): | |
| """Deterministic emulator: answer grounded in retrieved beliefs + tools.""" | |
| def __init__(self): | |
| self.calculator = Calculator() | |
| def ask(self, question: str, store: BeliefStore, tools=None) -> dict: | |
| beliefs = store.retrieve(question, k=5) | |
| top_eff = max((b["effective_confidence"] for b in beliefs), default=0.0) | |
| answer, logprob, method = self._synthesize(question, beliefs, store, tools) | |
| # Calibration-friendly signal. | |
| logprob = max(logprob, -5.0) + top_eff * 1.6 | |
| return { | |
| "answer": answer, | |
| "confidence": round(top_eff, 3), | |
| "effective_confidence": round(top_eff, 3), | |
| "method": method, | |
| "tool_calls": [], | |
| "logprob": logprob, | |
| "question": question, | |
| "sources": [b["claim"] for b in beliefs[:3]], | |
| } | |
| def _synthesize(self, question: str, beliefs, store, tools): | |
| calc_ans = self._try_calc(question) | |
| if calc_ans is not None: | |
| return calc_ans, 0.2, "tool" | |
| kw = self._keyword(question) | |
| if kw: | |
| for b in beliefs: | |
| claim = b["claim"].lower() | |
| sub = (b.get("subject") or "").lower() | |
| if kw in claim or (sub and kw in sub): | |
| if b.get("effective_confidence", 1.0) < 0.25: | |
| return "I don't have a confident memory for that (expired or disputed).", -2.0, "hedge" | |
| return self._answer(b), 0.0, "memory" | |
| if beliefs: | |
| b = beliefs[0] | |
| if b.get("effective_confidence", 1.0) >= 0.25: | |
| return self._answer(b), -0.5, "memory" | |
| return "I'm not sure — my memory is too thin to answer well.", -2.0, "hedge" | |
| def _answer(self, b): | |
| if b.get("subject") and b.get("object"): | |
| return f"The {b['subject']} is {b['object']}" | |
| return f"{_INTRO.get(b.get('volatility'), '')}{b['claim']}" | |
| def _try_calc(self, question: str): | |
| q = question.lower().strip().rstrip("?").strip() | |
| m = re.search(r"(?:what\s+is\s+|calculate\s+)?([\d\s+\-*/().x×times]+)", q) | |
| if not m: | |
| return None | |
| raw = m.group(1).replace("times", "*").replace("x", "*").replace("×", "*").strip() | |
| if not any(op in raw for op in "+-*/"): | |
| return None | |
| try: | |
| val = self.calculator.run(expr=raw)["result"] | |
| return f"{val}" | |
| except Exception: | |
| return None | |
| def _keyword(self, question: str): | |
| q = question.lower().strip().rstrip("?").strip() | |
| if "capital" in q and "france" in q: | |
| return "paris" | |
| m = _PR.match(q) | |
| if m: | |
| return self._clean(m.group(1)) | |
| m = _CONCEPT.match(q) | |
| if m: | |
| return self._clean(m.group(1)) | |
| m = _FACT.match(q) | |
| if m and self._not_pronoun(m.group(1)): | |
| return self._clean(m.group(1)) | |
| return None | |
| def _not_pronoun(s): | |
| return s.lower() not in ("what", "who", "how") | |
| def _clean(s: str): | |
| return re.sub(r"\s+", " ", s).strip().rstrip("?").strip() | |