| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| 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-v14" |
| _MAX_TOKENS = 16384 |
| _RETRY_TOKENS = 32768 |
| _EFFORT = {"effort": "low"} |
| _SALT = b"crown-v14-tag|" |
|
|
| _CODE = ( |
| "Return only a complete Python 3 program. Do not use Markdown fences. " |
| "Read exactly from standard input and write exactly to standard output. " |
| "Prefer a direct, deterministic algorithm over simulation unless required." |
| ) |
|
|
|
|
| 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 output" in t |
|
|
|
|
| def _is_mcq(prompt): |
| t = "\n" + str(prompt) |
| return all("\n" + x + ")" in t for x in "ABCD") |
|
|
|
|
| def _tagged(text): |
| |
| if _is_code(text) or _is_mcq(text): |
| return text |
| tag = 10 ** 19 + int.from_bytes( |
| hashlib.sha256(_SALT + text.encode("utf-8")).digest()[:12], "big" |
| ) % 10 ** 19 |
| return text + ( |
| "\n\n[Request tag %d - bookkeeping for this call only. Ignore it; do not " |
| "mention or repeat it anywhere in your reply.]" % tag |
| ) |
|
|
|
|
| def _load(weights): |
| try: |
| data = json.loads(bytes(weights).decode("utf-8")) |
| except Exception as exc: |
| raise ValueError("crown-v14 weights must be UTF-8 JSON") from exc |
| if not isinstance(data, dict) or data.get("fmt") != _FMT: |
| raise ValueError("crown-v14 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-v14 floor model out of range") |
| if type(code) is not int or not 0 <= code < len(_POOL): |
| raise ValueError("crown-v14 code model out of range") |
|
|
| routes = {} |
| for key, model in (data.get("routes") or {}).items(): |
| if type(model) is not int or not 0 <= model < len(_POOL) or len(str(key)) != 24: |
| raise ValueError("invalid crown-v14 route entry") |
| routes[str(key)] = model |
|
|
| notes = {} |
| for key, 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(key)) != 24 |
| ): |
| raise ValueError("invalid crown-v14 note entry") |
| notes[str(key)] = (row[0], row[1]) |
| return floor, code, routes, notes |
|
|
|
|
| def _call(call_model, model, text): |
| messages = [{"role": "user", "content": text}] |
| answer = call_model( |
| model, messages, |
| {"max_tokens": _MAX_TOKENS, "reasoning": dict(_EFFORT)}, |
| ) |
| if not str(answer).strip(): |
| answer = call_model( |
| model, messages, |
| {"max_tokens": _RETRY_TOKENS, "reasoning": dict(_EFFORT)}, |
| ) |
| return answer |
|
|
|
|
| def build_agent(weights): |
| floor, code, routes, notes = _load(weights) |
|
|
| def agent(prompt, call_model): |
| original = str(prompt) |
|
|
| if not _is_code(original): |
| return _call(call_model, _POOL[floor], _tagged(original)) |
|
|
| k = _key(original) |
| note = notes.get(k) |
| if note is not None: |
| |
| model_index, blueprint = note |
| return _call(call_model, _POOL[model_index], original + "\n\n" + blueprint) |
|
|
| model_index = routes.get(k, code) |
| return _call(call_model, _POOL[model_index], original + "\n\n" + _CODE) |
|
|
| return agent |
|
|