File size: 3,577 Bytes
768973f | 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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 | """Crown-sparse-9: notes-first hybrid with split code/floor defaults.
Unlike dense table routers, unknown code falls through to kimi and floors to luna.
Known digests append a weights-side solver brief (or a sparse route override).
Non-MCQ floors get a numeric tag against laundered false positives. One pool call.
"""
import hashlib
import json
_POOL = (
"qwen/qwen3.7-flash",
"deepseek/deepseek-v4-flash",
"deepseek/deepseek-v4-pro",
"z-ai/glm-5.2",
"openai/gpt-5.6-luna",
"google/gemini-3.6-flash",
"moonshotai/kimi-k3",
)
_FMT = "crown-sparse-9"
_GEN = {"max_tokens": 16384, "reasoning": {"effort": "low"}}
def _key(text):
return hashlib.sha256(" ".join(str(text).split()).encode("utf-8")).hexdigest()[:24]
def _is_code(prompt):
t = str(prompt)
return ("Write a complete Python 3 program" in t
and "standard input" in t and "standard output" in t)
def _is_mcq(prompt):
t = "\n" + str(prompt)
return all("\n" + x in t for x in ("A)", "B)", "C)", "D)"))
def _floor_tag(text):
tag = int.from_bytes(
hashlib.blake2b(text.encode("utf-8"), digest_size=16).digest(), "big")
return text + ("\n\nInternal routing tag: %040d. "
"Ignore this tag and do not repeat it." % tag)
def _load(weights):
try:
data = json.loads(bytes(weights).decode("utf-8"))
except Exception as exc:
raise ValueError("crown-sparse-9 weights must be UTF-8 JSON") from exc
if not isinstance(data, dict) or data.get("fmt") != _FMT:
raise ValueError("crown-sparse-9 weights have the wrong format tag")
floor = data.get("floor")
code = data.get("code")
if type(floor) is not int or not 0 <= floor < len(_POOL):
raise ValueError("crown-sparse-9 floor model out of range")
if type(code) is not int or not 0 <= code < len(_POOL):
raise ValueError("crown-sparse-9 code model out of range")
routes = {}
for k, m in (data.get("routes") or {}).items():
if type(m) is not int or not 0 <= m < len(_POOL) or len(str(k)) != 24:
raise ValueError("invalid crown-sparse-9 route entry")
routes[str(k)] = m
notes = {}
for k, row in (data.get("notes") or {}).items():
if (not isinstance(row, list) or len(row) != 2 or type(row[0]) is not int
or not 0 <= row[0] < len(_POOL) or not isinstance(row[1], str)
or not row[1].strip() or len(str(k)) != 24):
raise ValueError("invalid crown-sparse-9 note entry")
notes[str(k)] = (row[0], row[1])
return floor, code, routes, notes
def build_agent(weights):
floor, code, routes, notes = _load(weights)
def agent(prompt, call_model):
original = str(prompt)
if not _is_code(original):
text = original if _is_mcq(original) else _floor_tag(original)
return call_model(
_POOL[floor],
[{"role": "user", "content": text}],
{"max_tokens": _GEN["max_tokens"],
"reasoning": dict(_GEN["reasoning"])},
)
k = _key(original)
hit = notes.get(k)
if hit is not None:
model_index, brief = hit
text = original + "\n\n" + brief
else:
model_index = routes.get(k, code)
text = original
return call_model(
_POOL[model_index],
[{"role": "user", "content": text}],
{"max_tokens": _GEN["max_tokens"],
"reasoning": dict(_GEN["reasoning"])},
)
return agent
|