Release 12.0 (full): one browser harness that converses, writes from held sources and builds what you describe, keeping each request through corrections and restarts. No language model. (part 4)
04e89f2 verified Download workbench/converse/squircle_reply_v2.py from psiloceyeben/oracle7: direct link, hf CLI and curl.
- Browser
- Download file 8.35 kB
-
https://huggingface.co/psiloceyeben/oracle7/resolve/main/workbench/converse/squircle_reply_v2.py
- Command line
-
hf download hf://psiloceyeben/oracle7/workbench/converse/squircle_reply_v2.py
-
curl -L -o squircle_reply_v2.py https://huggingface.co/psiloceyeben/oracle7/resolve/main/workbench/converse/squircle_reply_v2.py
8.35 kB
| #!/usr/bin/env python3 | |
| """SQUIRCLE ROUTER v2 (round 3 of the open-conversation loop). Same circle, same selector (w = softmax(beta * phi)), same | |
| law. Three changes, each a class named by round 2's census and reading: | |
| 1. SELF-RECORDS (interior port `recall`): Oracle7's own written records in the release tree (changelogs, specialist | |
| cards, orbiter and agent READMEs, release records) are split into sentences and indexed by content word with IDF. | |
| A turn's content words, plus the words of whatever `ground` bound, retrieve the best sentence only if | |
| >= 2 distinct words overlap AND their IDF mass is >= RECALL_MIN. Every recalled sentence is CITED (file), so it passes | |
| the law as "cited". Nothing is written about any turn; the index is built from the tree as released. | |
| 2. COMMON GROUND as content: when no record fits, an earlier turn of THIS conversation that shares >= 2 rare words is | |
| related ("This connects to what you said earlier: ..."), the relational reply people give. | |
| 3. TYPED ACTION MATCH (commissive): an operation is offered only if the turn covers >= 50% of the IDF mass of the | |
| operation's own title words (IDF over the catalogue) with >= 2 words; otherwise the decline names the hole. | |
| The question port no longer echoes the turn with a frame: it names the hole (the turn's rare words with no record and | |
| no common ground) and asks for exactly that. | |
| usage: module (SquircleRouterV2(head_chat, ops, phi, act_of, tree))""" | |
| from __future__ import annotations | |
| import collections | |
| import math | |
| import os | |
| import re | |
| from squircle_reply import SquircleRouter, content_words | |
| SENT = re.compile(r"(?<=[.!?])\s+|\n+") | |
| RECALL_MIN = 9.0 | |
| COVER_MIN = 0.5 # the sentence must carry >= half of the turn's IDF mass (over words the records know) | |
| RARE = 3.0 # an overlap word counts only if it is not common in the records (idf >= 3) | |
| class Records: | |
| def __init__(self, tree): | |
| self.sents = [] | |
| for root, _, files in os.walk(tree): | |
| if "__pycache__" in root or "prosefold" in root: # voice/style files are not records of what Oracle7 is or did | |
| continue | |
| for f in files: | |
| if f.endswith(".md"): | |
| p = os.path.join(root, f) | |
| try: | |
| txt = open(p, encoding="utf-8", errors="ignore").read() | |
| except OSError: | |
| continue | |
| for s in SENT.split(txt): | |
| s = re.sub(r"[#*`|>_]+", " ", s) | |
| s = re.sub(r"\s+", " ", s).strip() | |
| w = set(content_words(s)) | |
| if 6 <= len(s.split()) <= 60 and len(w) >= 4: | |
| self.sents.append((s, os.path.relpath(p, tree), w)) | |
| df = collections.Counter(w for _, _, ws in self.sents for w in ws) | |
| n = max(1, len(self.sents)) | |
| self.idf = {w: math.log(n / k) for w, k in df.items()} | |
| self.inv = collections.defaultdict(list) | |
| for i, (_, _, ws) in enumerate(self.sents): | |
| for w in ws: | |
| self.inv[w].append(i) | |
| def best(self, words, k=2): | |
| q = {w for w in words if self.idf.get(w, 0) >= RARE} | |
| mass = sum(self.idf[w] for w in q) | |
| sc = collections.Counter() | |
| for w in q: | |
| for i in self.inv.get(w, ()): | |
| sc[i] += self.idf[w] | |
| out = [] | |
| for i, s in sc.most_common(40): | |
| ov = q & self.sents[i][2] | |
| if len(ov) >= 2 and s >= RECALL_MIN and s / max(mass, 1e-9) >= COVER_MIN: | |
| out.append((s, self.sents[i][0], self.sents[i][1], ov)) | |
| if len(out) >= k: | |
| break | |
| return out | |
| class SquircleRouterV2(SquircleRouter): | |
| def __init__(self, head_chat, ops, phi, act_of, tree, beta=4.0): | |
| self.ops = [o for o in ops if o.get("invocation") != "unavailable"] | |
| super().__init__(head_chat, self._ops_typed, phi, act_of, beta) | |
| self.rec = Records(tree) | |
| odf = collections.Counter() | |
| self._ow = [] | |
| for o in self.ops: | |
| ws = set(content_words((o.get("title", "") + " " + o["id"].replace(".", " ").replace("-", " ")))) | |
| self._ow.append(ws) | |
| odf.update(ws) | |
| n = max(1, len(self.ops)) | |
| self._oidf = {w: math.log(n / k) + 0.1 for w, k in odf.items()} | |
| self.turns = collections.defaultdict(list) | |
| # ---- commissive: typed match over the catalogue | |
| def _ops_typed(self, q): | |
| qw = set(q.split()) | |
| best = [] | |
| for o, ws in zip(self.ops, self._ow): | |
| ov = qw & ws | |
| if len(ov) < 2: | |
| continue | |
| cover = sum(self._oidf[w] for w in ov) / sum(self._oidf[w] for w in ws) | |
| if cover >= 0.5: | |
| best.append((cover, o)) | |
| best.sort(key=lambda x: -x[0]) | |
| return [o for _, o in best[:3]] | |
| def _words(self, words, bound): | |
| extra = content_words(bound.get("subject") or "") if bound else [] | |
| return list(dict.fromkeys(words + extra)) | |
| # ---- interior: recall from own records; relate to common ground | |
| def recall(self, words): | |
| return self.rec.best(words) | |
| def relate(self, session, words): | |
| q = {w for w in words if self.rec.idf.get(w, 6.0) >= 4.0} | |
| for t in reversed(self.turns[session][:-1]): | |
| ov = q & set(content_words(t)) | |
| if len(ov) >= 2: | |
| return t, ov | |
| return None | |
| # ---- boundary ports | |
| def p_inform(self, turn, head, words, bound, session=None): | |
| if self.check(head): | |
| return head["response"] | |
| ws = self._words(words, bound) | |
| hits = self.recall(ws) | |
| if hits: | |
| return " ".join("My records (%s): %s" % (f, s) for _, s, f, _ in hits) | |
| rel = self.relate(session, ws) if session else None | |
| if rel: | |
| return "This connects to what you said earlier in this conversation: \"%s\" (shared: %s)." % ( | |
| rel[0][:300], ", ".join(sorted(rel[1]))) | |
| return None | |
| def p_question(self, turn, words, bound, session=None): | |
| ws = self._words(words, bound) | |
| known = set().union(*[h[3] for h in self.recall(ws)]) if ws else set() | |
| hole = [w for w in ws if w not in known and self.rec.idf.get(w, 99) >= 5.0][:4] | |
| if not hole: | |
| return None | |
| return "I have no record of %s. What %s, or where is it recorded?" % ( | |
| ", ".join(hole), "is it" if len(hole) == 1 else "are they") | |
| def reply(self, turn, session="convo"): | |
| self.turns[session].append(turn) | |
| cg = self.common[session] | |
| bound = self.ground(cg, turn) | |
| words = self.receive(cg, turn) | |
| head = self.head_chat(turn, session) | |
| u = self.act_of(turn) | |
| cand = {"inform": lambda: self.p_inform(turn, head, words, bound, session), | |
| "question": lambda: self.p_question(turn, words, bound, session), | |
| "commissive": lambda: self.p_commissive(turn, words, bound, cg), | |
| "directive": lambda: self.p_directive(cg)} | |
| prior = self.phi.get(u) or {"inform": 1.0} | |
| scored = [] | |
| for port, make in cand.items(): | |
| p = prior.get(port, 0.0) | |
| if p <= 0: | |
| continue | |
| text = make() | |
| if text: | |
| scored.append((p, port, text)) | |
| if not scored: | |
| return {"status": "declined", "response": "I can't reply to that with anything I can check.", | |
| "path": "squircle2:none", "act_in": u, "head": head} | |
| z = sum(math.exp(self.beta * p) for p, _, _ in scored) | |
| w = sorted(((math.exp(self.beta * p) / z, port, text) for p, port, text in scored), key=lambda x: -x[0]) | |
| _, port, text = w[0] | |
| sub = "head" if port == "inform" and self.check(head) else ( | |
| "records" if text.startswith("My records") else "common" if text.startswith("This connects") else port) | |
| return {"status": "answered", "response": text, "path": "squircle2:%s>%s:%s" % (u, port, sub), "act_in": u, | |
| "port": port, "weights": {pt: round(x, 3) for x, pt, _ in w}} | |