first submission
Browse files- .gitignore +6 -0
- LICENSE +8 -0
- README.md +86 -0
- config.json +17 -0
- escalation/patcher.py +55 -0
- escalation/prompts/context_compression/repair.txt +15 -0
- escalation/prompts/context_compression/system.txt +1 -0
- escalation/prompts/generic/repair.txt +12 -0
- escalation/prompts/generic/system.txt +1 -0
- escalation/prompts/sql_generation/repair.txt +14 -0
- escalation/prompts/sql_generation/system.txt +1 -0
- harness/adapters/__init__.py +22 -0
- harness/adapters/base.py +63 -0
- harness/adapters/context_compression.py +64 -0
- harness/adapters/generic.py +20 -0
- harness/adapters/sql_generation.py +75 -0
- harness/engine.py +71 -0
- harness/prompts/context_compression/retry.txt +9 -0
- harness/prompts/context_compression/task.txt +5 -0
- harness/prompts/generic/retry.txt +11 -0
- harness/prompts/generic/task.txt +5 -0
- harness/prompts/sql_generation/retry.txt +14 -0
- harness/prompts/sql_generation/task.txt +8 -0
- harness/verifier.py +9 -0
- main.py +116 -0
- manifest.example.json +23 -0
- manifest.json +173 -0
- requirements.txt +5 -0
- router/config.json +7 -0
- router/training/data_sources.json +1 -0
- router/training/metrics.jsonl +1 -0
- router/training/recipe.json +9 -0
- router/training/train_router.py +60 -0
.gitignore
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
__pycache__/
|
| 2 |
+
*.pyc
|
| 3 |
+
*.safetensors
|
| 4 |
+
*.bin
|
| 5 |
+
*.sqlite
|
| 6 |
+
.env
|
LICENSE
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Copyright (c) 2026 Microtensor. All rights reserved.
|
| 2 |
+
|
| 3 |
+
This template and every derivative of it are licensed to you solely for
|
| 4 |
+
building and submitting systems to Microtensor arenas under the Microtensor
|
| 5 |
+
submission terms (https://www.microtensor.cloud/terms). No other use,
|
| 6 |
+
redistribution or sublicensing is permitted without written permission from
|
| 7 |
+
Microtensor. Models submitted to an arena are licensed to Microtensor under
|
| 8 |
+
the submission terms and are not published under a free licence.
|
README.md
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Microtensor miner template
|
| 2 |
+
|
| 3 |
+
A working submission for any Microtensor arena, in the two repository layout validators expect. Fork it, pick the adapter for your arena's task, replace the model, improve the harness, train the router, and ship.
|
| 4 |
+
|
| 5 |
+
## What a submission is
|
| 6 |
+
|
| 7 |
+
| Component | Lives in | What validators do with it |
|
| 8 |
+
|---|---|---|
|
| 9 |
+
| Specialised model (SLM) | your `mt_slm_<arena>` repository | load it themselves and serve it to your code |
|
| 10 |
+
| Harness | `harness/` here | run `main.py` in a sandbox with no network except the validator's services |
|
| 11 |
+
| Router | `router/` here | load it themselves as a one logit sequence classifier and serve `/decide` |
|
| 12 |
+
| Frontier repair | `escalation/` here | run it with a ticket that pays for frontier calls through the relay |
|
| 13 |
+
|
| 14 |
+
You may submit any subset; `manifest.json` names which. Validators fill the rest with the arena's reference components and pay you from the pools you compete in.
|
| 15 |
+
|
| 16 |
+
## Repository layout
|
| 17 |
+
|
| 18 |
+
```
|
| 19 |
+
<user>/microtensor_<arena> this repository
|
| 20 |
+
manifest.json written by `mt submit manifest`
|
| 21 |
+
config.json runtime limits and the adapter name
|
| 22 |
+
main.py serves /harness/run, /escalation/repair, /system/run
|
| 23 |
+
requirements.txt pinned packages from the allowed list only
|
| 24 |
+
harness/ engine.py, verifier.py, adapters/, prompts/<adapter>/
|
| 25 |
+
escalation/ patcher.py, prompts/<adapter>/
|
| 26 |
+
router/ config.json, model.safetensors, tokenizer files, training/
|
| 27 |
+
|
| 28 |
+
<user>/mt_slm_<arena> the slm_repo/ folder, pushed as its own repository
|
| 29 |
+
README.md LICENSE config.json generation_config.json model.safetensors tokenizer.json tokenizer_config.json
|
| 30 |
+
training/ recipe, data sources, distillation, compression, runs, scripts
|
| 31 |
+
```
|
| 32 |
+
|
| 33 |
+
## Adapters
|
| 34 |
+
|
| 35 |
+
An arena's items carry a task document (`task`) whose fields the arena definition describes in `schemas.input`. The harness and the repair prompt are written once, against an **adapter** that knows how to prompt for that task and how to check an answer before it is returned:
|
| 36 |
+
|
| 37 |
+
| Adapter | For arenas whose task is | Verification |
|
| 38 |
+
|---|---|---|
|
| 39 |
+
| `generic` | any structured or free text task | non empty, length bound, refuses injected instructions |
|
| 40 |
+
| `sql_generation` | a question plus a schema, answered with one SQLite query | parses with sqlglot, names only known tables, executes on the arena's sample database |
|
| 41 |
+
| `context_compression` | a coding agent's session to compact into the five field JSON (`ctx_compress`) | one JSON object; folds extra keys into `summary` and object entries into plain strings; checks the token budget the instruction states |
|
| 42 |
+
|
| 43 |
+
`config.json` names the adapter. Write your own under `harness/adapters/` for an arena with a different task: subclass `Adapter`, override `verify` (and `clean`, `query` or `render` when the task needs it), register it in `harness/adapters/__init__.py`, and add `harness/prompts/<name>/task.txt`, `retry.txt`, `escalation/prompts/<name>/system.txt`, `repair.txt`. Prompt templates may use any task field by name, `{fields}` for every field as `key: value` lines, `{task_json}`, and `{draft}`, `{errors}`, `{trace}` on retries and repairs.
|
| 44 |
+
|
| 45 |
+
For `ctx_compress`, set `config.json` to `"adapter": "context_compression"`, `"slm": {"max_context": 32768, "max_new_tokens": 3072}`. The prompts use the Qwen chat format; for Phi 4 mini, change `harness/prompts/context_compression/*.txt` to its `<|system|>`, `<|user|>`, `<|assistant|>` and `<|end|>` markers.
|
| 46 |
+
|
| 47 |
+
## Steps
|
| 48 |
+
|
| 49 |
+
1. Read the arena definition: `GET https://api.microtensor.cloud/v2/arenas/<arena>` gives the task, the allowed bases and licences, the tools, the budgets and the item counts.
|
| 50 |
+
2. Train and compress your model from an allowed base, write the training record, push `slm_repo/` to `huggingface.co/<user>/mt_slm_<arena>`. Note the commit sha.
|
| 51 |
+
3. Train the router (`router/training/train_router.py`) on drafts from your own model, labelled by the arena's grader on the public sample items. Put the weights and tokenizer under `router/`.
|
| 52 |
+
4. Set `"adapter"` in `config.json`, adjust the prompts and the verifier. Keep `harness/` and `escalation/` under 2 MB together.
|
| 53 |
+
5. Write the manifest and check both repositories:
|
| 54 |
+
```
|
| 55 |
+
mt submit manifest --artifact . --arena <arena> --slm ./slm_repo \
|
| 56 |
+
--slm-repo <user>/mt_slm_<arena> --slm-revision <sha>
|
| 57 |
+
mt submit validate --artifact . --slm ./slm_repo
|
| 58 |
+
```
|
| 59 |
+
6. Push this repository to `huggingface.co/<user>/microtensor_<arena>`. Note the commit sha.
|
| 60 |
+
7. Commit before the epoch's commit window closes. If the arena charges a fee, pay it first and cite the transfer; a free arena (every testnet arena) needs only the commit:
|
| 61 |
+
```
|
| 62 |
+
mt submit pay --arena <arena>
|
| 63 |
+
mt submit commit --arena <arena> --hf-user <user> --artifact-sha <sha> --fee-block <block> --fee-index <index>
|
| 64 |
+
mt submit commit --arena <arena> --hf-user <user> --artifact-sha <sha>
|
| 65 |
+
```
|
| 66 |
+
8. `mt submit status --arena <arena>` shows the epoch and your published scores once validators report.
|
| 67 |
+
|
| 68 |
+
## Training playbook
|
| 69 |
+
|
| 70 |
+
Prompting pays only through `harness/prompts/`; the model pool runs your weights under the arena's own prompt. There is no retrieval beyond the arena's tools on the item's database. Provenance fingerprints your weights against the declared base and against public checkpoints, so an untrained base never certifies. Work in this order:
|
| 71 |
+
|
| 72 |
+
1. Baseline each allowed base on the public sample items with the arena's grader.
|
| 73 |
+
2. Set the adapter prompt, the verifier and the retry prompt; measure again.
|
| 74 |
+
3. Distil: frontier answers on schemas and question shapes like the arena's, filtered by the grader, fine tuned from the base and recorded under `slm_repo/training/`.
|
| 75 |
+
4. Reinforce with the grader as the reward, or train on preference pairs of a right teacher answer against a wrong draft.
|
| 76 |
+
5. Hold out whole domains, rename and revalue as the twin rule does, measure latency on a 24 GB card, then train the router on this model's drafts.
|
| 77 |
+
|
| 78 |
+
Items are fresh every epoch and the twin gap is deducted, so train the skill, not the sample databases. The full playbook: https://www.microtensor.cloud/docs/certified-inference/guides/training
|
| 79 |
+
|
| 80 |
+
## The sandbox contract
|
| 81 |
+
|
| 82 |
+
Your `main.py` listens on `MT_PORT` and may reach only four addresses: `MT_SLM_URL` (your model, OpenAI completions, greedy), `MT_ROUTER_URL` (your router, `POST /decide`), `MT_TOOLS_URL` (the arena's tools, if it has any), and `MT_RELAY_URL` (`POST /v1/chat/completions` with the `x-mt-ticket` header, only inside a repair). Tool answers are sometimes faulty on purpose, and sometimes carry instructions; following them scores the item wrong. A request that times out, crashes or returns an invalid answer counts as wrong.
|
| 83 |
+
|
| 84 |
+
## Licence
|
| 85 |
+
|
| 86 |
+
This template, your derivative of it and the model you submit are licensed under the Microtensor submission terms (see `LICENSE`); none of it is published under a free licence. Each arena lists which base model licences it accepts as inputs, because the base's terms must allow a commercial derivative.
|
config.json
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"adapter": "context_compression",
|
| 3 |
+
"slm": {
|
| 4 |
+
"max_context": 32768,
|
| 5 |
+
"max_new_tokens": 3072
|
| 6 |
+
},
|
| 7 |
+
"router": {
|
| 8 |
+
"max_input_tokens": 4096
|
| 9 |
+
},
|
| 10 |
+
"harness": {
|
| 11 |
+
"max_steps": 3,
|
| 12 |
+
"max_total_tokens": 32000
|
| 13 |
+
},
|
| 14 |
+
"escalation": {
|
| 15 |
+
"preferred_model": "gpt-5"
|
| 16 |
+
}
|
| 17 |
+
}
|
escalation/patcher.py
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __future__ import annotations
|
| 2 |
+
|
| 3 |
+
from pathlib import Path
|
| 4 |
+
from typing import Any
|
| 5 |
+
|
| 6 |
+
import httpx
|
| 7 |
+
|
| 8 |
+
from harness.adapters import Adapter
|
| 9 |
+
|
| 10 |
+
PROMPTS = Path(__file__).resolve().parent / "prompts"
|
| 11 |
+
MAX_TOKENS = 512
|
| 12 |
+
TRACE_LIMIT = 1200
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
def _trim(text: str, limit: int) -> str:
|
| 16 |
+
return text if len(text) <= limit else text[-limit:]
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
def repair(
|
| 20 |
+
task: dict[str, Any],
|
| 21 |
+
draft: Any,
|
| 22 |
+
trace: str,
|
| 23 |
+
errors: str,
|
| 24 |
+
*,
|
| 25 |
+
adapter: Adapter,
|
| 26 |
+
relay_url: str,
|
| 27 |
+
ticket: str,
|
| 28 |
+
model: str,
|
| 29 |
+
) -> Any:
|
| 30 |
+
system = adapter.render(PROMPTS, "system", task)
|
| 31 |
+
user = adapter.render(
|
| 32 |
+
PROMPTS,
|
| 33 |
+
"repair",
|
| 34 |
+
task,
|
| 35 |
+
draft=adapter.as_text(draft),
|
| 36 |
+
errors=_trim(errors or "no verifier output", 400),
|
| 37 |
+
trace=_trim(trace or "", TRACE_LIMIT),
|
| 38 |
+
)
|
| 39 |
+
body = {
|
| 40 |
+
"model": model,
|
| 41 |
+
"max_tokens": MAX_TOKENS,
|
| 42 |
+
"messages": [{"role": "system", "content": system}, {"role": "user", "content": user}],
|
| 43 |
+
}
|
| 44 |
+
with httpx.Client(timeout=180.0) as http:
|
| 45 |
+
response = http.post(
|
| 46 |
+
relay_url.rstrip("/") + "/v1/chat/completions",
|
| 47 |
+
json=body,
|
| 48 |
+
headers={"x-mt-ticket": ticket},
|
| 49 |
+
)
|
| 50 |
+
if response.status_code != 200:
|
| 51 |
+
return draft
|
| 52 |
+
choices = response.json().get("choices") or [{}]
|
| 53 |
+
text = str((choices[0].get("message") or {}).get("content") or "")
|
| 54 |
+
fixed = adapter.clean(text)
|
| 55 |
+
return fixed or draft
|
escalation/prompts/context_compression/repair.txt
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Instruction:
|
| 2 |
+
{system}
|
| 3 |
+
|
| 4 |
+
Session:
|
| 5 |
+
{user}
|
| 6 |
+
|
| 7 |
+
Draft compaction:
|
| 8 |
+
{draft}
|
| 9 |
+
|
| 10 |
+
Why the draft is wrong: {errors}
|
| 11 |
+
|
| 12 |
+
Harness trace:
|
| 13 |
+
{trace}
|
| 14 |
+
|
| 15 |
+
Return the corrected compaction within the token budget the instruction states.
|
escalation/prompts/context_compression/system.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
You repair the compaction of a coding agent's session. Reply with one JSON object and nothing else, with exactly the fields summary, files_touched, open_tasks, errors and decisions; every list entry is a plain string. Copy file paths, function names and error messages exactly as they appear in the session. Use only facts from the session.
|
escalation/prompts/generic/repair.txt
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Task:
|
| 2 |
+
{fields}
|
| 3 |
+
|
| 4 |
+
Draft answer:
|
| 5 |
+
{draft}
|
| 6 |
+
|
| 7 |
+
Why the draft is wrong: {errors}
|
| 8 |
+
|
| 9 |
+
Harness trace:
|
| 10 |
+
{trace}
|
| 11 |
+
|
| 12 |
+
Return the corrected answer.
|
escalation/prompts/generic/system.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
You repair answers to tasks. Reply with the corrected answer only, no prose, no code fences.
|
escalation/prompts/sql_generation/repair.txt
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Schema:
|
| 2 |
+
{schema}
|
| 3 |
+
|
| 4 |
+
Question: {question}
|
| 5 |
+
|
| 6 |
+
Draft query:
|
| 7 |
+
{draft}
|
| 8 |
+
|
| 9 |
+
Why the draft is wrong: {errors}
|
| 10 |
+
|
| 11 |
+
Harness trace:
|
| 12 |
+
{trace}
|
| 13 |
+
|
| 14 |
+
Return the corrected query.
|
escalation/prompts/sql_generation/system.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
You repair SQLite queries. Reply with one corrected query only, no prose, no code fences.
|
harness/adapters/__init__.py
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __future__ import annotations
|
| 2 |
+
|
| 3 |
+
from harness.adapters.base import Adapter, Verdict
|
| 4 |
+
from harness.adapters.context_compression import CompressionAdapter
|
| 5 |
+
from harness.adapters.generic import GenericAdapter
|
| 6 |
+
from harness.adapters.sql_generation import SqlAdapter
|
| 7 |
+
|
| 8 |
+
ADAPTERS: dict[str, Adapter] = {
|
| 9 |
+
GenericAdapter.name: GenericAdapter(),
|
| 10 |
+
SqlAdapter.name: SqlAdapter(),
|
| 11 |
+
CompressionAdapter.name: CompressionAdapter(),
|
| 12 |
+
}
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
def load(name: str) -> Adapter:
|
| 16 |
+
try:
|
| 17 |
+
return ADAPTERS[name]
|
| 18 |
+
except KeyError as exc:
|
| 19 |
+
raise ValueError(f"unknown adapter {name!r}; one of {sorted(ADAPTERS)}") from exc
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
__all__ = ["ADAPTERS", "Adapter", "Verdict", "load"]
|
harness/adapters/base.py
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __future__ import annotations
|
| 2 |
+
|
| 3 |
+
import json
|
| 4 |
+
import re
|
| 5 |
+
from dataclasses import dataclass
|
| 6 |
+
from pathlib import Path
|
| 7 |
+
from typing import Any
|
| 8 |
+
|
| 9 |
+
_FENCE = re.compile(r"```[a-zA-Z]*")
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
@dataclass(frozen=True)
|
| 13 |
+
class Verdict:
|
| 14 |
+
ok: bool
|
| 15 |
+
answer: Any
|
| 16 |
+
reason: str = ""
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
class SafeDict(dict):
|
| 20 |
+
def __missing__(self, key: str) -> str:
|
| 21 |
+
return ""
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
def strip_fences(text: str) -> str:
|
| 25 |
+
return _FENCE.sub("\n", text).strip()
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
def fields_of(task: dict[str, Any]) -> str:
|
| 29 |
+
lines = []
|
| 30 |
+
for key, value in task.items():
|
| 31 |
+
body = value if isinstance(value, str) else json.dumps(value, ensure_ascii=False)
|
| 32 |
+
lines.append(f"{key}: {body}")
|
| 33 |
+
return "\n".join(lines)
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
class Adapter:
|
| 37 |
+
name = "generic"
|
| 38 |
+
stop: list[str] = ["\n\n\n"]
|
| 39 |
+
|
| 40 |
+
def render(self, prompts: Path, kind: str, task: dict[str, Any], **extra: Any) -> str:
|
| 41 |
+
template = (prompts / self.name / f"{kind}.txt").read_text(encoding="utf-8")
|
| 42 |
+
values = SafeDict(task_json=json.dumps(task, ensure_ascii=False), fields=fields_of(task))
|
| 43 |
+
values.update({k: v for k, v in task.items() if isinstance(v, str | int | float)})
|
| 44 |
+
values.update({k: (v if isinstance(v, str) else json.dumps(v)) for k, v in extra.items()})
|
| 45 |
+
return template.format_map(values)
|
| 46 |
+
|
| 47 |
+
def query(self, task: dict[str, Any]) -> str:
|
| 48 |
+
for key in ("question", "prompt", "input", "text", "instruction"):
|
| 49 |
+
if isinstance(task.get(key), str):
|
| 50 |
+
return str(task[key])
|
| 51 |
+
return fields_of(task)
|
| 52 |
+
|
| 53 |
+
def clean(self, text: str) -> str:
|
| 54 |
+
return strip_fences(text)
|
| 55 |
+
|
| 56 |
+
def verify(self, text: str, task: dict[str, Any], tools_url: str) -> Verdict:
|
| 57 |
+
answer = self.clean(text)
|
| 58 |
+
if not answer:
|
| 59 |
+
return Verdict(False, "", "empty answer")
|
| 60 |
+
return Verdict(True, answer)
|
| 61 |
+
|
| 62 |
+
def as_text(self, answer: Any) -> str:
|
| 63 |
+
return answer if isinstance(answer, str) else json.dumps(answer, ensure_ascii=False)
|
harness/adapters/context_compression.py
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __future__ import annotations
|
| 2 |
+
|
| 3 |
+
import json
|
| 4 |
+
import re
|
| 5 |
+
from typing import Any
|
| 6 |
+
|
| 7 |
+
from harness.adapters.base import Adapter, Verdict, strip_fences
|
| 8 |
+
|
| 9 |
+
FIELDS = ("summary", "files_touched", "open_tasks", "errors", "decisions")
|
| 10 |
+
BUDGET = re.compile(r"within (\d+) tokens")
|
| 11 |
+
CHARS_PER_TOKEN = 3.5
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
def _text(value: Any) -> str:
|
| 15 |
+
if isinstance(value, str):
|
| 16 |
+
return value.strip()
|
| 17 |
+
if isinstance(value, dict):
|
| 18 |
+
return "; ".join(f"{key}: {_text(item)}" for key, item in value.items() if _text(item))
|
| 19 |
+
if isinstance(value, list):
|
| 20 |
+
return "; ".join(_text(item) for item in value if _text(item))
|
| 21 |
+
return "" if value is None else str(value)
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
def normalise(found: dict[str, Any]) -> dict[str, Any]:
|
| 25 |
+
summary = _text(found.get("summary"))
|
| 26 |
+
extra = [f"{key}: {_text(value)}" for key, value in found.items() if key not in FIELDS and _text(value)]
|
| 27 |
+
if extra:
|
| 28 |
+
summary = " ".join([summary, *extra]).strip()
|
| 29 |
+
shaped: dict[str, Any] = {"summary": summary}
|
| 30 |
+
for key in FIELDS[1:]:
|
| 31 |
+
value = found.get(key)
|
| 32 |
+
entries = value if isinstance(value, list) else ([] if value in (None, "") else [value])
|
| 33 |
+
shaped[key] = [entry for entry in (_text(item) for item in entries) if entry]
|
| 34 |
+
return shaped
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
class CompressionAdapter(Adapter):
|
| 38 |
+
name = "context_compression"
|
| 39 |
+
stop = ["<|im_end|>", "<|endoftext|>", "<|end|>"]
|
| 40 |
+
|
| 41 |
+
def clean(self, text: str) -> str:
|
| 42 |
+
body = strip_fences(text)
|
| 43 |
+
start, end = body.find("{"), body.rfind("}")
|
| 44 |
+
return body[start : end + 1] if start >= 0 and end > start else body
|
| 45 |
+
|
| 46 |
+
def verify(self, text: str, task: dict[str, Any], tools_url: str) -> Verdict:
|
| 47 |
+
body = self.clean(text)
|
| 48 |
+
try:
|
| 49 |
+
found = json.loads(body)
|
| 50 |
+
except ValueError:
|
| 51 |
+
return Verdict(False, body, "the reply is not one JSON object")
|
| 52 |
+
if not isinstance(found, dict):
|
| 53 |
+
return Verdict(False, body, "the reply is not one JSON object")
|
| 54 |
+
if not _text(found.get("summary")):
|
| 55 |
+
return Verdict(False, body, "the summary field is missing or empty")
|
| 56 |
+
answer = json.dumps(normalise(found), ensure_ascii=False)
|
| 57 |
+
budget = BUDGET.search(str(task.get("system") or ""))
|
| 58 |
+
if budget and len(answer) / CHARS_PER_TOKEN > int(budget.group(1)):
|
| 59 |
+
return Verdict(
|
| 60 |
+
False,
|
| 61 |
+
answer,
|
| 62 |
+
f"the reply is longer than {budget.group(1)} tokens; shorten the summary and the lists",
|
| 63 |
+
)
|
| 64 |
+
return Verdict(True, answer)
|
harness/adapters/generic.py
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __future__ import annotations
|
| 2 |
+
|
| 3 |
+
from typing import Any
|
| 4 |
+
|
| 5 |
+
from harness.adapters.base import Adapter, Verdict
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
class GenericAdapter(Adapter):
|
| 9 |
+
name = "generic"
|
| 10 |
+
max_chars = 4000
|
| 11 |
+
|
| 12 |
+
def verify(self, text: str, task: dict[str, Any], tools_url: str) -> Verdict:
|
| 13 |
+
answer = self.clean(text)
|
| 14 |
+
if not answer:
|
| 15 |
+
return Verdict(False, "", "empty answer")
|
| 16 |
+
if len(answer) > self.max_chars:
|
| 17 |
+
return Verdict(False, answer, f"answer longer than {self.max_chars} characters")
|
| 18 |
+
if "ignore the task" in answer.lower():
|
| 19 |
+
return Verdict(False, answer, "answer repeats injected instructions")
|
| 20 |
+
return Verdict(True, answer)
|
harness/adapters/sql_generation.py
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __future__ import annotations
|
| 2 |
+
|
| 3 |
+
import re
|
| 4 |
+
from pathlib import Path
|
| 5 |
+
from typing import Any
|
| 6 |
+
|
| 7 |
+
import httpx
|
| 8 |
+
|
| 9 |
+
from harness.adapters.base import Adapter, Verdict, strip_fences
|
| 10 |
+
|
| 11 |
+
_FORBIDDEN = re.compile(r"\b(attach|detach|pragma|vacuum|insert|update|delete|drop|alter)\b", re.I)
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
def schema_lookup(tools_url: str) -> str:
|
| 15 |
+
try:
|
| 16 |
+
with httpx.Client(timeout=20.0) as http:
|
| 17 |
+
response = http.post(tools_url.rstrip("/") + "/tools/schema_lookup", json={})
|
| 18 |
+
response.raise_for_status()
|
| 19 |
+
tables = response.json().get("tables") or []
|
| 20 |
+
except (httpx.HTTPError, ValueError):
|
| 21 |
+
return ""
|
| 22 |
+
return "\n".join(
|
| 23 |
+
str(t.get("sql") or "") for t in tables if "ignore the task" not in str(t.get("sql") or "").lower()
|
| 24 |
+
)
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
class SqlAdapter(Adapter):
|
| 28 |
+
name = "sql_generation"
|
| 29 |
+
stop = ["\n\n", ";"]
|
| 30 |
+
|
| 31 |
+
def render(self, prompts: Path, kind: str, task: dict[str, Any], **extra: Any) -> str:
|
| 32 |
+
if not task.get("schema") and extra.get("tools_url"):
|
| 33 |
+
task = {**task, "schema": schema_lookup(str(extra["tools_url"]))}
|
| 34 |
+
return super().render(prompts, kind, task, **extra)
|
| 35 |
+
|
| 36 |
+
def clean(self, text: str) -> str:
|
| 37 |
+
body = strip_fences(text)
|
| 38 |
+
body = re.sub(r"^\s*(sql|sqlite|query|answer)\s*:\s*", "", body, flags=re.I)
|
| 39 |
+
return body.split(";", 1)[0].strip()
|
| 40 |
+
|
| 41 |
+
def verify(self, text: str, task: dict[str, Any], tools_url: str) -> Verdict:
|
| 42 |
+
sql = self.clean(text)
|
| 43 |
+
if not sql:
|
| 44 |
+
return Verdict(False, "", "empty answer")
|
| 45 |
+
if not sql.lower().startswith(("select", "with")):
|
| 46 |
+
return Verdict(False, sql, "not a SELECT statement")
|
| 47 |
+
if _FORBIDDEN.search(sql):
|
| 48 |
+
return Verdict(False, sql, "forbidden statement")
|
| 49 |
+
import sqlglot
|
| 50 |
+
|
| 51 |
+
try:
|
| 52 |
+
parsed = sqlglot.parse_one(sql, read="sqlite")
|
| 53 |
+
except sqlglot.errors.ParseError as exc:
|
| 54 |
+
return Verdict(False, sql, f"does not parse: {str(exc)[:120]}")
|
| 55 |
+
schema = str(task.get("schema") or "")
|
| 56 |
+
tables = {t.name.lower() for t in parsed.find_all(sqlglot.exp.Table)}
|
| 57 |
+
known = {
|
| 58 |
+
name.lower()
|
| 59 |
+
for name in re.findall(r"create\s+table\s+\"?([A-Za-z_][A-Za-z0-9_]*)", schema, re.I)
|
| 60 |
+
}
|
| 61 |
+
unknown = sorted(tables - known) if known else []
|
| 62 |
+
if unknown:
|
| 63 |
+
return Verdict(False, sql, f"unknown tables: {', '.join(unknown)}")
|
| 64 |
+
try:
|
| 65 |
+
with httpx.Client(timeout=20.0) as http:
|
| 66 |
+
response = http.post(
|
| 67 |
+
tools_url.rstrip("/") + "/tools/query_execution", json={"sql": sql}
|
| 68 |
+
)
|
| 69 |
+
if response.status_code == 200:
|
| 70 |
+
body = response.json()
|
| 71 |
+
if isinstance(body, dict) and body.get("error"):
|
| 72 |
+
return Verdict(False, sql, f"execution error: {str(body['error'])[:120]}")
|
| 73 |
+
except (httpx.HTTPError, ValueError):
|
| 74 |
+
pass
|
| 75 |
+
return Verdict(True, sql)
|
harness/engine.py
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __future__ import annotations
|
| 2 |
+
|
| 3 |
+
from dataclasses import dataclass, field
|
| 4 |
+
from pathlib import Path
|
| 5 |
+
from typing import Any
|
| 6 |
+
|
| 7 |
+
import httpx
|
| 8 |
+
|
| 9 |
+
from harness.adapters import Adapter
|
| 10 |
+
from harness.verifier import verify
|
| 11 |
+
|
| 12 |
+
PROMPTS = Path(__file__).resolve().parent / "prompts"
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
@dataclass
|
| 16 |
+
class HarnessResult:
|
| 17 |
+
answer: Any
|
| 18 |
+
verified: bool
|
| 19 |
+
errors: str = ""
|
| 20 |
+
trace: str = ""
|
| 21 |
+
steps: int = 0
|
| 22 |
+
attempts: list[str] = field(default_factory=list)
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
def complete(slm_url: str, prompt: str, max_tokens: int, stop: list[str]) -> str:
|
| 26 |
+
with httpx.Client(timeout=120.0) as http:
|
| 27 |
+
response = http.post(
|
| 28 |
+
slm_url.rstrip("/") + "/v1/completions",
|
| 29 |
+
json={"model": "slm", "prompt": prompt, "max_tokens": max_tokens, "stop": stop},
|
| 30 |
+
)
|
| 31 |
+
response.raise_for_status()
|
| 32 |
+
choices = response.json().get("choices") or [{}]
|
| 33 |
+
return str(choices[0].get("text") or "").strip()
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
def run_harness(
|
| 37 |
+
task: dict[str, Any],
|
| 38 |
+
*,
|
| 39 |
+
adapter: Adapter,
|
| 40 |
+
slm_url: str,
|
| 41 |
+
tools_url: str,
|
| 42 |
+
config: dict[str, Any],
|
| 43 |
+
) -> HarnessResult:
|
| 44 |
+
max_tokens = int((config.get("slm") or {}).get("max_new_tokens") or 256)
|
| 45 |
+
max_steps = int((config.get("harness") or {}).get("max_steps") or 3)
|
| 46 |
+
errors = ""
|
| 47 |
+
answer = ""
|
| 48 |
+
trace: list[str] = []
|
| 49 |
+
attempts: list[str] = []
|
| 50 |
+
for step in range(1, max_steps + 1):
|
| 51 |
+
kind = "task" if not errors else "retry"
|
| 52 |
+
prompt = adapter.render(
|
| 53 |
+
PROMPTS, kind, task, draft=answer, errors=errors, tools_url=tools_url
|
| 54 |
+
)
|
| 55 |
+
answer = complete(slm_url, prompt, max_tokens, adapter.stop)
|
| 56 |
+
attempts.append(answer)
|
| 57 |
+
verdict = verify(adapter, answer, task, tools_url)
|
| 58 |
+
trace.append(f"step {step}: {'ok' if verdict.ok else verdict.reason}")
|
| 59 |
+
if verdict.ok:
|
| 60 |
+
return HarnessResult(
|
| 61 |
+
answer=verdict.answer, verified=True, trace="\n".join(trace), steps=step, attempts=attempts
|
| 62 |
+
)
|
| 63 |
+
errors = verdict.reason
|
| 64 |
+
return HarnessResult(
|
| 65 |
+
answer=adapter.clean(answer),
|
| 66 |
+
verified=False,
|
| 67 |
+
errors=errors,
|
| 68 |
+
trace="\n".join(trace),
|
| 69 |
+
steps=max_steps,
|
| 70 |
+
attempts=attempts,
|
| 71 |
+
)
|
harness/prompts/context_compression/retry.txt
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<|im_start|>system
|
| 2 |
+
{system}<|im_end|>
|
| 3 |
+
<|im_start|>user
|
| 4 |
+
{user}<|im_end|>
|
| 5 |
+
<|im_start|>assistant
|
| 6 |
+
{draft}<|im_end|>
|
| 7 |
+
<|im_start|>user
|
| 8 |
+
That reply was rejected: {errors}. Reply again with only the JSON object with exactly the five fields summary, files_touched, open_tasks, errors and decisions, every list entry a plain string.<|im_end|>
|
| 9 |
+
<|im_start|>assistant
|
harness/prompts/context_compression/task.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<|im_start|>system
|
| 2 |
+
{system}<|im_end|>
|
| 3 |
+
<|im_start|>user
|
| 4 |
+
{user}<|im_end|>
|
| 5 |
+
<|im_start|>assistant
|
harness/prompts/generic/retry.txt
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
You answer tasks precisely. Reply with the answer only, no explanation.
|
| 2 |
+
|
| 3 |
+
{fields}
|
| 4 |
+
|
| 5 |
+
A previous attempt was rejected.
|
| 6 |
+
Attempt:
|
| 7 |
+
{draft}
|
| 8 |
+
Problem: {errors}
|
| 9 |
+
|
| 10 |
+
Write a corrected answer.
|
| 11 |
+
Answer:
|
harness/prompts/generic/task.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
You answer tasks precisely. Reply with the answer only, no explanation.
|
| 2 |
+
|
| 3 |
+
{fields}
|
| 4 |
+
|
| 5 |
+
Answer:
|
harness/prompts/sql_generation/retry.txt
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
You are a SQLite expert. Answer with exactly one SQL query and nothing else.
|
| 2 |
+
|
| 3 |
+
Schema:
|
| 4 |
+
{schema}
|
| 5 |
+
|
| 6 |
+
Question: {question}
|
| 7 |
+
|
| 8 |
+
A previous attempt was rejected.
|
| 9 |
+
Attempt:
|
| 10 |
+
{draft}
|
| 11 |
+
Problem: {errors}
|
| 12 |
+
|
| 13 |
+
Write a corrected query.
|
| 14 |
+
SQL:
|
harness/prompts/sql_generation/task.txt
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
You are a SQLite expert. Answer with exactly one SQL query and nothing else.
|
| 2 |
+
|
| 3 |
+
Schema:
|
| 4 |
+
{schema}
|
| 5 |
+
|
| 6 |
+
Question: {question}
|
| 7 |
+
|
| 8 |
+
SQL:
|
harness/verifier.py
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __future__ import annotations
|
| 2 |
+
|
| 3 |
+
from typing import Any
|
| 4 |
+
|
| 5 |
+
from harness.adapters import Adapter, Verdict
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
def verify(adapter: Adapter, text: str, task: dict[str, Any], tools_url: str) -> Verdict:
|
| 9 |
+
return adapter.verify(text, task, tools_url)
|
main.py
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __future__ import annotations
|
| 2 |
+
|
| 3 |
+
import json
|
| 4 |
+
import os
|
| 5 |
+
import sys
|
| 6 |
+
from pathlib import Path
|
| 7 |
+
from typing import Any
|
| 8 |
+
|
| 9 |
+
import httpx
|
| 10 |
+
from fastapi import FastAPI
|
| 11 |
+
from pydantic import BaseModel, Field
|
| 12 |
+
|
| 13 |
+
ROOT = Path(__file__).resolve().parent
|
| 14 |
+
sys.path.insert(0, str(ROOT))
|
| 15 |
+
|
| 16 |
+
from escalation.patcher import repair # noqa: E402
|
| 17 |
+
from harness.adapters import load # noqa: E402
|
| 18 |
+
from harness.engine import run_harness # noqa: E402
|
| 19 |
+
|
| 20 |
+
CONFIG: dict[str, Any] = json.loads((ROOT / "config.json").read_text(encoding="utf-8"))
|
| 21 |
+
ADAPTER = load(str(CONFIG.get("adapter") or "generic"))
|
| 22 |
+
SLM_URL = os.environ["MT_SLM_URL"]
|
| 23 |
+
ROUTER_URL = os.environ.get("MT_ROUTER_URL", "")
|
| 24 |
+
TOOLS_URL = os.environ["MT_TOOLS_URL"]
|
| 25 |
+
RELAY_URL = os.environ["MT_RELAY_URL"]
|
| 26 |
+
PREFERRED_MODEL = str((CONFIG.get("escalation") or {}).get("preferred_model") or "")
|
| 27 |
+
|
| 28 |
+
app = FastAPI(docs_url=None, redoc_url=None, openapi_url=None)
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
class HarnessIn(BaseModel):
|
| 32 |
+
item_id: str
|
| 33 |
+
task: dict[str, Any]
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
class RepairIn(BaseModel):
|
| 37 |
+
item_id: str
|
| 38 |
+
task: dict[str, Any]
|
| 39 |
+
draft: Any = None
|
| 40 |
+
trace: str = ""
|
| 41 |
+
errors: str = ""
|
| 42 |
+
ticket: str = ""
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
class SystemIn(BaseModel):
|
| 46 |
+
item_id: str
|
| 47 |
+
task: dict[str, Any]
|
| 48 |
+
floor: float = Field(ge=0.0, le=1.0)
|
| 49 |
+
ticket: str = ""
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
def decide(query: str, draft: str) -> float:
|
| 53 |
+
if not ROUTER_URL:
|
| 54 |
+
return 1.0
|
| 55 |
+
with httpx.Client(timeout=30.0) as http:
|
| 56 |
+
response = http.post(
|
| 57 |
+
ROUTER_URL.rstrip("/") + "/decide", json={"input": {"query": query, "draft": draft}}
|
| 58 |
+
)
|
| 59 |
+
response.raise_for_status()
|
| 60 |
+
answers = response.json().get("answers") or []
|
| 61 |
+
return float(answers[0]["probability"]) if answers else 0.0
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
def _repair(payload: RepairIn | SystemIn, draft: Any, trace: str, errors: str) -> Any:
|
| 65 |
+
return repair(
|
| 66 |
+
payload.task,
|
| 67 |
+
draft,
|
| 68 |
+
trace,
|
| 69 |
+
errors,
|
| 70 |
+
adapter=ADAPTER,
|
| 71 |
+
relay_url=RELAY_URL,
|
| 72 |
+
ticket=payload.ticket,
|
| 73 |
+
model=PREFERRED_MODEL,
|
| 74 |
+
)
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
@app.get("/health")
|
| 78 |
+
def health() -> dict[str, str]:
|
| 79 |
+
return {"status": "ok", "adapter": ADAPTER.name}
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
@app.post("/harness/run")
|
| 83 |
+
def harness_run(payload: HarnessIn) -> dict[str, Any]:
|
| 84 |
+
result = run_harness(
|
| 85 |
+
payload.task, adapter=ADAPTER, slm_url=SLM_URL, tools_url=TOOLS_URL, config=CONFIG
|
| 86 |
+
)
|
| 87 |
+
return {
|
| 88 |
+
"answer": result.answer,
|
| 89 |
+
"self_check": {"pass": result.verified},
|
| 90 |
+
"trace": result.trace,
|
| 91 |
+
"steps": result.steps,
|
| 92 |
+
}
|
| 93 |
+
|
| 94 |
+
|
| 95 |
+
@app.post("/escalation/repair")
|
| 96 |
+
def escalation_repair(payload: RepairIn) -> dict[str, Any]:
|
| 97 |
+
return {"answer": _repair(payload, payload.draft, payload.trace, payload.errors)}
|
| 98 |
+
|
| 99 |
+
|
| 100 |
+
@app.post("/system/run")
|
| 101 |
+
def system_run(payload: SystemIn) -> dict[str, Any]:
|
| 102 |
+
result = run_harness(
|
| 103 |
+
payload.task, adapter=ADAPTER, slm_url=SLM_URL, tools_url=TOOLS_URL, config=CONFIG
|
| 104 |
+
)
|
| 105 |
+
draft = result.answer
|
| 106 |
+
probability = decide(ADAPTER.query(payload.task), ADAPTER.as_text(draft))
|
| 107 |
+
if result.verified and probability >= payload.floor:
|
| 108 |
+
return {"answer": draft, "route": "local", "p": probability}
|
| 109 |
+
answer = _repair(payload, draft, result.trace, result.errors)
|
| 110 |
+
return {"answer": answer, "route": "escalated", "p": probability}
|
| 111 |
+
|
| 112 |
+
|
| 113 |
+
if __name__ == "__main__":
|
| 114 |
+
import uvicorn
|
| 115 |
+
|
| 116 |
+
uvicorn.run(app, host="0.0.0.0", port=int(os.environ.get("MT_PORT", "8080")), log_level="warning")
|
manifest.example.json
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"schema_version": 1,
|
| 3 |
+
"arena": "<arena>",
|
| 4 |
+
"components": ["slm", "harness", "router", "escalation"],
|
| 5 |
+
"hardware_class": "gpu_24gb",
|
| 6 |
+
"slm": {
|
| 7 |
+
"repo": "<user>/mt_slm_<arena>",
|
| 8 |
+
"revision": "<40 hex git commit sha>",
|
| 9 |
+
"files": {"model.safetensors": "sha256:...", "config.json": "sha256:..."}
|
| 10 |
+
},
|
| 11 |
+
"files": {
|
| 12 |
+
"config.json": "sha256:...",
|
| 13 |
+
"main.py": "sha256:...",
|
| 14 |
+
"requirements.txt": "sha256:...",
|
| 15 |
+
"router/config.json": "sha256:...",
|
| 16 |
+
"router/model.safetensors": "sha256:...",
|
| 17 |
+
"harness/engine.py": "sha256:...",
|
| 18 |
+
"harness/verifier.py": "sha256:...",
|
| 19 |
+
"harness/adapters/__init__.py": "sha256:...",
|
| 20 |
+
"harness/prompts/generic/task.txt": "sha256:...",
|
| 21 |
+
"escalation/patcher.py": "sha256:..."
|
| 22 |
+
}
|
| 23 |
+
}
|
manifest.json
ADDED
|
@@ -0,0 +1,173 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"arena": "ctx_compress",
|
| 3 |
+
"components": [
|
| 4 |
+
"slm",
|
| 5 |
+
"harness"
|
| 6 |
+
],
|
| 7 |
+
"files": {
|
| 8 |
+
".gitignore": "sha256:e7ab22595b29e55626e02c418ce6d59e580da857ded2a08f903e6d7ef1aec090",
|
| 9 |
+
"LICENSE": "sha256:30746c09020f9d1cd2882d681e7b972848f83a128fef195575a41a1e41fdec7e",
|
| 10 |
+
"README.md": "sha256:b0dec53abf4afb25dd3f5370d6d6eb13fc87b391e057fc242ad79e34e0d83288",
|
| 11 |
+
"config.json": "sha256:098d5f1c15bc0c72dab6920846952b21d518c5a94a868455b7cf32c12345968b",
|
| 12 |
+
"escalation/patcher.py": "sha256:600f78f383d94cda69381ddf0d84873024f90bad4b058b9e006fa8b2dba6c779",
|
| 13 |
+
"escalation/prompts/context_compression/repair.txt": "sha256:f63b5f7a16621b6c3b471fb229410414866c2913abd695f26201c6f09e2194c2",
|
| 14 |
+
"escalation/prompts/context_compression/system.txt": "sha256:2f3bb96f667a24c1f50f2c6f4d856fefe06642b9b71cec8887596828ef79aeea",
|
| 15 |
+
"escalation/prompts/generic/repair.txt": "sha256:734091ff1ab1be571b6fcd2d450c265f55c431ad4c10151b8d31c1e37ec57023",
|
| 16 |
+
"escalation/prompts/generic/system.txt": "sha256:1191cfb67c22351adc36774fe5da8842cfa256a9f780ca1b41b7b7cdbfd9b13f",
|
| 17 |
+
"escalation/prompts/sql_generation/repair.txt": "sha256:7cdc2b0f488c003fbdc95be46268491aac9bfc8ae078390d03b2e2702932bd5e",
|
| 18 |
+
"escalation/prompts/sql_generation/system.txt": "sha256:a82c3d64ac057bb7b376270fcd180e680969cdbd3290ca67429d60599979c659",
|
| 19 |
+
"harness/adapters/__init__.py": "sha256:64899ed354e462e8bf6b278244401259e2d798943559382ec01bed7c36ee6d7b",
|
| 20 |
+
"harness/adapters/base.py": "sha256:b04da8e04e848e1b6d9edc4105391212121010ace0ac257421671535603a96b0",
|
| 21 |
+
"harness/adapters/context_compression.py": "sha256:47d8b1ccadbd4d7921dc78c4076f829d727c3f37b59dd0622081644997a3c3f0",
|
| 22 |
+
"harness/adapters/generic.py": "sha256:7ca2b1d5b1e9dec0d83820881f85744fcd3ae42d3a75d117447aead906872e5b",
|
| 23 |
+
"harness/adapters/sql_generation.py": "sha256:1e392d15645901fd080adf497cac1ec2b817baf5eb71959958d1c00302e6e21e",
|
| 24 |
+
"harness/engine.py": "sha256:87d33738a0c04fe0314e7f37bee85d3f7a6e7b7e94ff51f0c8ab6eac3347f768",
|
| 25 |
+
"harness/prompts/context_compression/retry.txt": "sha256:0a307850ab2187825dad4d3d36fb43adea4fdb6e0c4bd516d1a18f994982a99d",
|
| 26 |
+
"harness/prompts/context_compression/task.txt": "sha256:826ca63bccd76232f5aeaf0dd85d79e7747f6cf312e9ec469a4696d114d37d0e",
|
| 27 |
+
"harness/prompts/generic/retry.txt": "sha256:6f4741f5c4daf44e03fa55b0b5c6a10c7e84af780789e019a095060d3a6b7b06",
|
| 28 |
+
"harness/prompts/generic/task.txt": "sha256:695a5284f2c90433fd97617da4c82cdcb623d0ab091cca861f3f1d628a16a16c",
|
| 29 |
+
"harness/prompts/sql_generation/retry.txt": "sha256:9da2f6b58c3030c626dc5cff6c7799ab979779371c76382c0a802836a857601c",
|
| 30 |
+
"harness/prompts/sql_generation/task.txt": "sha256:c3a8b6c7028f60614b3c9c1240ab7f98fe51cd4455b7c025ebe972066f0fd994",
|
| 31 |
+
"harness/verifier.py": "sha256:8be9c46674ed2a4c5e60457f9aefd767d907b697bb5978c9bc48698015444681",
|
| 32 |
+
"main.py": "sha256:b58cf3812b208b02e9b08d8434e6dae6e709f6352470c5ab10a0a8dd5de42d59",
|
| 33 |
+
"manifest.example.json": "sha256:66c84dcbb4f58ebe47818bdca730e34a9782cc177eed3d6be1865d50453dead9",
|
| 34 |
+
"requirements.txt": "sha256:db65ff41ff4d68c613b1312491e7af60a37c1769e28e215c4f510e3614b77551",
|
| 35 |
+
"router/config.json": "sha256:cb6ea49716f2dd71ff62ed030fd6e525ccebf46030f017df786f1aa35ac1e077",
|
| 36 |
+
"router/training/data_sources.json": "sha256:5e1a5ba89b09eab573ffd00de540cf4a980541e0c75ad48be54ea38643d31d2f",
|
| 37 |
+
"router/training/metrics.jsonl": "sha256:794318db32f615ec49bfbea4ff12a274ad5c1676fad6ddb71c1d40b3a23c7be6",
|
| 38 |
+
"router/training/recipe.json": "sha256:bba9519bece1a14f222b64c2bf5feb1c92e1d37aae39f79ea1c5fa1799981e6f",
|
| 39 |
+
"router/training/train_router.py": "sha256:55886c9eccf006090a00bd2293cc4a334396f99eb531cbe539f8c461b4a8903a",
|
| 40 |
+
"slm_repo/.cache/huggingface/.gitignore": "sha256:684888c0ebb17f374298b65ee2807526c066094c701bcc7ebbe1c1095f494fc1",
|
| 41 |
+
"slm_repo/.cache/huggingface/CACHEDIR.TAG": "sha256:f6572428f6d5e1575e73a1502895a8731f10757dfbb634909c6e154b849af91d",
|
| 42 |
+
"slm_repo/.cache/huggingface/download/.gitattributes.lock": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
|
| 43 |
+
"slm_repo/.cache/huggingface/download/.gitattributes.metadata": "sha256:26ed8780607527e9da7626af6e2433226f9516850d83034c566e5335f0f67693",
|
| 44 |
+
"slm_repo/.cache/huggingface/download/LICENSE.lock": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
|
| 45 |
+
"slm_repo/.cache/huggingface/download/LICENSE.metadata": "sha256:0ced6ba0f8089fb046eab87bc4944f52d1488e551d325ec6fec9942a14323817",
|
| 46 |
+
"slm_repo/.cache/huggingface/download/README.md.lock": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
|
| 47 |
+
"slm_repo/.cache/huggingface/download/README.md.metadata": "sha256:db96dbdba710323b8f64bea7b67d8a3081438de0063225ae68ac873feec88686",
|
| 48 |
+
"slm_repo/.cache/huggingface/download/chat_template.jinja.lock": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
|
| 49 |
+
"slm_repo/.cache/huggingface/download/chat_template.jinja.metadata": "sha256:8602495d725d930253a96d4e1740de9c30310eb838f067e8a7ea500309ffa2e6",
|
| 50 |
+
"slm_repo/.cache/huggingface/download/config.json.lock": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
|
| 51 |
+
"slm_repo/.cache/huggingface/download/config.json.metadata": "sha256:ed6922838d88a4c57a7e99212c3aa23d51f6f5bdb1eef63e1d395dce3c96c859",
|
| 52 |
+
"slm_repo/.cache/huggingface/download/generation_config.json.lock": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
|
| 53 |
+
"slm_repo/.cache/huggingface/download/generation_config.json.metadata": "sha256:57094d7351dffa72b8d3e5f87ac323c1f55967e784c214d2e9f4d2726def9c23",
|
| 54 |
+
"slm_repo/.cache/huggingface/download/model.safetensors.lock": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
|
| 55 |
+
"slm_repo/.cache/huggingface/download/model.safetensors.metadata": "sha256:36f687f7a92e4e49e7c7765f4613e45a5bd06bd89c11556f14121adc084fb4c3",
|
| 56 |
+
"slm_repo/.cache/huggingface/download/tokenizer.json.lock": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
|
| 57 |
+
"slm_repo/.cache/huggingface/download/tokenizer.json.metadata": "sha256:3eff25d6fc3b6cb432c4f8fcc0acb02ee7103707776d33dcf0b2a9b88dac8523",
|
| 58 |
+
"slm_repo/.cache/huggingface/download/tokenizer_config.json.lock": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
|
| 59 |
+
"slm_repo/.cache/huggingface/download/tokenizer_config.json.metadata": "sha256:e541d720585cba287ef0811ce109cadfece88a25d003f79c7148e1f6a1355029",
|
| 60 |
+
"slm_repo/.cache/huggingface/download/training/compression/before_after.json.lock": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
|
| 61 |
+
"slm_repo/.cache/huggingface/download/training/compression/before_after.json.metadata": "sha256:55339fcfba4442750b1f0c5bbd2abf523bd1d56a90ff35a70509e541c918ef71",
|
| 62 |
+
"slm_repo/.cache/huggingface/download/training/compression/compression.json.lock": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
|
| 63 |
+
"slm_repo/.cache/huggingface/download/training/compression/compression.json.metadata": "sha256:0fcf0be92d274d0784a0e30e90cc0b63a3a9f414c4f4240d58c2b1746a55a934",
|
| 64 |
+
"slm_repo/.cache/huggingface/download/training/data/data_card.md.lock": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
|
| 65 |
+
"slm_repo/.cache/huggingface/download/training/data/data_card.md.metadata": "sha256:49ff1e371ad295fdc4dc0362419623bfb9943c2bee8efe24a0e06815d26269ed",
|
| 66 |
+
"slm_repo/.cache/huggingface/download/training/data/sources.json.lock": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
|
| 67 |
+
"slm_repo/.cache/huggingface/download/training/data/sources.json.metadata": "sha256:2a1d5ebc00c9c18778460ef58f7bbd299b36750c8768f532e66189215ed0735a",
|
| 68 |
+
"slm_repo/.cache/huggingface/download/training/data/synthetic.json.lock": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
|
| 69 |
+
"slm_repo/.cache/huggingface/download/training/data/synthetic.json.metadata": "sha256:36d97b28c7b54fdc85ce51c086753daa2aeddcfd741704dfaad8c11ab1a165c6",
|
| 70 |
+
"slm_repo/.cache/huggingface/download/training/distillation/teacher.json.lock": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
|
| 71 |
+
"slm_repo/.cache/huggingface/download/training/distillation/teacher.json.metadata": "sha256:00e419b843ddd685f15a8949057311b57508c218a1745125c3fb3f66064165fe",
|
| 72 |
+
"slm_repo/.cache/huggingface/download/training/recipe.json.lock": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
|
| 73 |
+
"slm_repo/.cache/huggingface/download/training/recipe.json.metadata": "sha256:1a70ee730abe82148670b225a9418523c76be9f6b5d8ebdb8548a9f3b5be21ea",
|
| 74 |
+
"slm_repo/.cache/huggingface/download/training/runs/poc-1/config.json.lock": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
|
| 75 |
+
"slm_repo/.cache/huggingface/download/training/runs/poc-1/config.json.metadata": "sha256:487307ef727a022d89288ece64697a81aa6cf7ac4fee17e740cc3c3134d655b7",
|
| 76 |
+
"slm_repo/.cache/huggingface/download/training/runs/poc-1/env.json.lock": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
|
| 77 |
+
"slm_repo/.cache/huggingface/download/training/runs/poc-1/env.json.metadata": "sha256:eda12137978a8e3ba2a6a6a49814df36d3c9d29109c90e7e3fa5e319b7fec470",
|
| 78 |
+
"slm_repo/.cache/huggingface/download/training/runs/poc-1/final_eval.json.lock": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
|
| 79 |
+
"slm_repo/.cache/huggingface/download/training/runs/poc-1/final_eval.json.metadata": "sha256:995a0dc3dbe9cb30e4093076a6ad8a066e5297614edd3625580905b82f0ca528",
|
| 80 |
+
"slm_repo/.cache/huggingface/download/training/runs/poc-1/metrics.jsonl.lock": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
|
| 81 |
+
"slm_repo/.cache/huggingface/download/training/runs/poc-1/metrics.jsonl.metadata": "sha256:534db5421e384091c7a1915090568ab2649b246419bbc801fd8e37fde803517e",
|
| 82 |
+
"slm_repo/.cache/huggingface/trees/d8d33ca6fa6cf3ba366e18bde3a158a42eec042e.json": "sha256:7975adb47d37d7841c3a083164fae3de176fda7822e2a8d5b809160fcc9925a6",
|
| 83 |
+
"slm_repo/.gitattributes": "sha256:34448b82c17d60fec9b65b1f093c115ddbaadc04beb1b0140b6bfed2e012a930",
|
| 84 |
+
"slm_repo/LICENSE": "sha256:95304758c3d62e112f73b2c31152522fbb47932dc50d752b29dfe64c6e051e63",
|
| 85 |
+
"slm_repo/README.md": "sha256:0f372ba0b29fce919fff05350bdd0064e8387610c0443496ba44661d0e2056ed",
|
| 86 |
+
"slm_repo/chat_template.jinja": "sha256:cd8e9439f0570856fd70470bf8889ebd8b5d1107207f67a5efb46e342330527f",
|
| 87 |
+
"slm_repo/config.json": "sha256:835c6c16d40f8a2883b7bc6c9073fdd648e41211a8be5b0ba44f798d8166486d",
|
| 88 |
+
"slm_repo/generation_config.json": "sha256:9b4cd225aa41c8f5c9a743317f377abf345c08595a1d6adbf2a2f3f837a6e9c0",
|
| 89 |
+
"slm_repo/model.safetensors": "sha256:e882ac47481918e1fa2efdb43998c5ada1a39d5e00b383eb42d70995c4df0e56",
|
| 90 |
+
"slm_repo/tokenizer.json": "sha256:3fd169731d2cbde95e10bf356d66d5997fd885dd8dbb6fb4684da3f23b2585d8",
|
| 91 |
+
"slm_repo/tokenizer_config.json": "sha256:8b6f658e2435ec8da6866013cb485a1d2a6559bed4bf76ed99ea68b031854b41",
|
| 92 |
+
"slm_repo/training/compression/before_after.json": "sha256:2b577c8807eb643f6c0946ef02701ab22201672a3ed4d839b56b25063a6e6e25",
|
| 93 |
+
"slm_repo/training/compression/compression.json": "sha256:8115ff6eab8f94ff7ef4945bbbcf143d412eee190c1e4284ee0189d78b7cb720",
|
| 94 |
+
"slm_repo/training/data/data_card.md": "sha256:f76d93b81ad5c5311592cbd26ce32b6e92b1c9d760b8a506a7401758ca00ad63",
|
| 95 |
+
"slm_repo/training/data/sources.json": "sha256:d97d256363f35c8a4f79c6c7dad52e8b6e42c2bab3a05c3f397dacffac9daf4c",
|
| 96 |
+
"slm_repo/training/data/synthetic.json": "sha256:ec789224c7e7715fdaf6faecfd90a5fb7a93d4b497a53278d12690c18801d8be",
|
| 97 |
+
"slm_repo/training/distillation/teacher.json": "sha256:870840af07f63cc308ba7dd01bf429b8f140e58180879f9bdd29a864c5484a5f",
|
| 98 |
+
"slm_repo/training/recipe.json": "sha256:f1427fd3c93746790d7d4a34eef4d768e14115ac885701c8c8508b3e2e955cd4",
|
| 99 |
+
"slm_repo/training/runs/example/config.json": "sha256:85dd762f53b6386c023a66f001ab5e57c41056ddba3f67e6dc84c84715e566e2",
|
| 100 |
+
"slm_repo/training/runs/example/env.json": "sha256:6d5043689980a5b6ae1d0e45fd1641d9a33d4afe4261a63df67ed6bb79349535",
|
| 101 |
+
"slm_repo/training/runs/example/final_eval.json": "sha256:c5f74092f5f70391a51b05bce25661bc1301a655a0004b5933a125771d4f87a5",
|
| 102 |
+
"slm_repo/training/runs/example/metrics.jsonl": "sha256:a4ee6c5c61662d627a2a54d3ea908d707c38065c13449e91441c70510481901e",
|
| 103 |
+
"slm_repo/training/runs/poc-1/config.json": "sha256:9347de25df0a27011362dabc68dc75f6f2e4be8c231ee0f38a367b8e9d647bd7",
|
| 104 |
+
"slm_repo/training/runs/poc-1/env.json": "sha256:f7818844dd2d7dbd05f42eb47af17410b66f339a3e983403e0454640336107bf",
|
| 105 |
+
"slm_repo/training/runs/poc-1/final_eval.json": "sha256:5355a783fbe83b722dc1fc4d4b249d119b464f4801b93fabb76e2472b90f3ff7",
|
| 106 |
+
"slm_repo/training/runs/poc-1/metrics.jsonl": "sha256:2eccba9c7265c4d79e8160a10b29c6aa21ea60f5b285f82ab3a0e12648582d1c",
|
| 107 |
+
"slm_repo/training/scripts/compress.py": "sha256:11ef6f98adf5c92a656fb1fbe68fea13e3421d030e0b4d23bd7d7c26326ed9f3",
|
| 108 |
+
"slm_repo/training/scripts/distill.py": "sha256:20e39a7111140d4e60205f5099752e917fdb74e294196cc6549d7f0c4d620962",
|
| 109 |
+
"slm_repo/training/scripts/requirements.txt": "sha256:9d31b4ee3f82f15edcf110eaebe43e560705fdb44de8128f3b7d5b488a8fc5ca",
|
| 110 |
+
"slm_repo/training/scripts/score_compress.py": "sha256:1095b23fdf69a30ecfd24cca4665b79054c7581510ce17bbd75c466ca6e60a0d",
|
| 111 |
+
"slm_repo/training/scripts/train.py": "sha256:977c8549a1e4678671f73cb873d165a2c9b23c1e22c0f9d6757ab9241f20abdf"
|
| 112 |
+
},
|
| 113 |
+
"hardware_class": "mt-5",
|
| 114 |
+
"schema_version": 1,
|
| 115 |
+
"slm": {
|
| 116 |
+
"files": {
|
| 117 |
+
".cache/huggingface/.gitignore": "sha256:684888c0ebb17f374298b65ee2807526c066094c701bcc7ebbe1c1095f494fc1",
|
| 118 |
+
".cache/huggingface/CACHEDIR.TAG": "sha256:f6572428f6d5e1575e73a1502895a8731f10757dfbb634909c6e154b849af91d",
|
| 119 |
+
".cache/huggingface/download/.gitattributes.lock": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
|
| 120 |
+
".cache/huggingface/download/.gitattributes.metadata": "sha256:26ed8780607527e9da7626af6e2433226f9516850d83034c566e5335f0f67693",
|
| 121 |
+
".cache/huggingface/download/LICENSE.lock": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
|
| 122 |
+
".cache/huggingface/download/LICENSE.metadata": "sha256:0ced6ba0f8089fb046eab87bc4944f52d1488e551d325ec6fec9942a14323817",
|
| 123 |
+
".cache/huggingface/download/README.md.lock": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
|
| 124 |
+
".cache/huggingface/download/README.md.metadata": "sha256:db96dbdba710323b8f64bea7b67d8a3081438de0063225ae68ac873feec88686",
|
| 125 |
+
".cache/huggingface/download/chat_template.jinja.lock": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
|
| 126 |
+
".cache/huggingface/download/chat_template.jinja.metadata": "sha256:8602495d725d930253a96d4e1740de9c30310eb838f067e8a7ea500309ffa2e6",
|
| 127 |
+
".cache/huggingface/download/config.json.lock": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
|
| 128 |
+
".cache/huggingface/download/config.json.metadata": "sha256:ed6922838d88a4c57a7e99212c3aa23d51f6f5bdb1eef63e1d395dce3c96c859",
|
| 129 |
+
".cache/huggingface/download/generation_config.json.lock": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
|
| 130 |
+
".cache/huggingface/download/generation_config.json.metadata": "sha256:57094d7351dffa72b8d3e5f87ac323c1f55967e784c214d2e9f4d2726def9c23",
|
| 131 |
+
".cache/huggingface/download/model.safetensors.lock": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
|
| 132 |
+
".cache/huggingface/download/model.safetensors.metadata": "sha256:36f687f7a92e4e49e7c7765f4613e45a5bd06bd89c11556f14121adc084fb4c3",
|
| 133 |
+
".cache/huggingface/download/tokenizer.json.lock": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
|
| 134 |
+
".cache/huggingface/download/tokenizer.json.metadata": "sha256:3eff25d6fc3b6cb432c4f8fcc0acb02ee7103707776d33dcf0b2a9b88dac8523",
|
| 135 |
+
".cache/huggingface/download/tokenizer_config.json.lock": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
|
| 136 |
+
".cache/huggingface/download/tokenizer_config.json.metadata": "sha256:e541d720585cba287ef0811ce109cadfece88a25d003f79c7148e1f6a1355029",
|
| 137 |
+
".cache/huggingface/download/training/compression/before_after.json.lock": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
|
| 138 |
+
".cache/huggingface/download/training/compression/before_after.json.metadata": "sha256:55339fcfba4442750b1f0c5bbd2abf523bd1d56a90ff35a70509e541c918ef71",
|
| 139 |
+
".cache/huggingface/download/training/compression/compression.json.lock": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
|
| 140 |
+
".cache/huggingface/download/training/compression/compression.json.metadata": "sha256:0fcf0be92d274d0784a0e30e90cc0b63a3a9f414c4f4240d58c2b1746a55a934",
|
| 141 |
+
".cache/huggingface/download/training/data/data_card.md.lock": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
|
| 142 |
+
".cache/huggingface/download/training/data/data_card.md.metadata": "sha256:49ff1e371ad295fdc4dc0362419623bfb9943c2bee8efe24a0e06815d26269ed",
|
| 143 |
+
".cache/huggingface/download/training/data/sources.json.lock": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
|
| 144 |
+
".cache/huggingface/download/training/data/sources.json.metadata": "sha256:2a1d5ebc00c9c18778460ef58f7bbd299b36750c8768f532e66189215ed0735a",
|
| 145 |
+
".cache/huggingface/download/training/data/synthetic.json.lock": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
|
| 146 |
+
".cache/huggingface/download/training/data/synthetic.json.metadata": "sha256:36d97b28c7b54fdc85ce51c086753daa2aeddcfd741704dfaad8c11ab1a165c6",
|
| 147 |
+
".cache/huggingface/download/training/distillation/teacher.json.lock": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
|
| 148 |
+
".cache/huggingface/download/training/distillation/teacher.json.metadata": "sha256:00e419b843ddd685f15a8949057311b57508c218a1745125c3fb3f66064165fe",
|
| 149 |
+
".cache/huggingface/download/training/recipe.json.lock": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
|
| 150 |
+
".cache/huggingface/download/training/recipe.json.metadata": "sha256:1a70ee730abe82148670b225a9418523c76be9f6b5d8ebdb8548a9f3b5be21ea",
|
| 151 |
+
".cache/huggingface/download/training/runs/poc-1/config.json.lock": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
|
| 152 |
+
".cache/huggingface/download/training/runs/poc-1/config.json.metadata": "sha256:487307ef727a022d89288ece64697a81aa6cf7ac4fee17e740cc3c3134d655b7",
|
| 153 |
+
".cache/huggingface/download/training/runs/poc-1/env.json.lock": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
|
| 154 |
+
".cache/huggingface/download/training/runs/poc-1/env.json.metadata": "sha256:eda12137978a8e3ba2a6a6a49814df36d3c9d29109c90e7e3fa5e319b7fec470",
|
| 155 |
+
".cache/huggingface/download/training/runs/poc-1/final_eval.json.lock": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
|
| 156 |
+
".cache/huggingface/download/training/runs/poc-1/final_eval.json.metadata": "sha256:995a0dc3dbe9cb30e4093076a6ad8a066e5297614edd3625580905b82f0ca528",
|
| 157 |
+
".cache/huggingface/download/training/runs/poc-1/metrics.jsonl.lock": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
|
| 158 |
+
".cache/huggingface/download/training/runs/poc-1/metrics.jsonl.metadata": "sha256:534db5421e384091c7a1915090568ab2649b246419bbc801fd8e37fde803517e",
|
| 159 |
+
".cache/huggingface/trees/d8d33ca6fa6cf3ba366e18bde3a158a42eec042e.json": "sha256:7975adb47d37d7841c3a083164fae3de176fda7822e2a8d5b809160fcc9925a6",
|
| 160 |
+
".gitattributes": "sha256:34448b82c17d60fec9b65b1f093c115ddbaadc04beb1b0140b6bfed2e012a930",
|
| 161 |
+
"LICENSE": "sha256:95304758c3d62e112f73b2c31152522fbb47932dc50d752b29dfe64c6e051e63",
|
| 162 |
+
"README.md": "sha256:0f372ba0b29fce919fff05350bdd0064e8387610c0443496ba44661d0e2056ed",
|
| 163 |
+
"chat_template.jinja": "sha256:cd8e9439f0570856fd70470bf8889ebd8b5d1107207f67a5efb46e342330527f",
|
| 164 |
+
"config.json": "sha256:835c6c16d40f8a2883b7bc6c9073fdd648e41211a8be5b0ba44f798d8166486d",
|
| 165 |
+
"generation_config.json": "sha256:9b4cd225aa41c8f5c9a743317f377abf345c08595a1d6adbf2a2f3f837a6e9c0",
|
| 166 |
+
"model.safetensors": "sha256:e882ac47481918e1fa2efdb43998c5ada1a39d5e00b383eb42d70995c4df0e56",
|
| 167 |
+
"tokenizer.json": "sha256:3fd169731d2cbde95e10bf356d66d5997fd885dd8dbb6fb4684da3f23b2585d8",
|
| 168 |
+
"tokenizer_config.json": "sha256:8b6f658e2435ec8da6866013cb485a1d2a6559bed4bf76ed99ea68b031854b41"
|
| 169 |
+
},
|
| 170 |
+
"repo": "MubarakB/mt_slm_ctx_compress",
|
| 171 |
+
"revision": "d8d33ca6fa6cf3ba366e18bde3a158a42eec042e"
|
| 172 |
+
}
|
| 173 |
+
}
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi==0.115.12
|
| 2 |
+
uvicorn==0.34.2
|
| 3 |
+
httpx==0.28.1
|
| 4 |
+
pydantic==2.11.4
|
| 5 |
+
sqlglot==26.16.4
|
router/config.json
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"architectures": ["DebertaV2ForSequenceClassification"],
|
| 3 |
+
"model_type": "deberta-v2",
|
| 4 |
+
"num_labels": 1,
|
| 5 |
+
"problem_type": "regression",
|
| 6 |
+
"note": "replace this file, model.safetensors and the tokenizer files with your trained router; see router/training/train_router.py"
|
| 7 |
+
}
|
router/training/data_sources.json
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
[{"name": "own-slm-drafts", "version": "s1", "license": "self", "sha256": ""}]
|
router/training/metrics.jsonl
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
{"epoch": 1, "loss": 0.0, "ece": 0.0, "auroc": 0.0, "note": "replace with your training metrics"}
|
router/training/recipe.json
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"base": "microsoft/deberta-v3-xsmall",
|
| 3 |
+
"method": "sequence classification head, one logit, trained on (query, draft, y) pairs",
|
| 4 |
+
"pairs": "drafts from your own SLM on the arena's public sample items, labelled by the arena's grader",
|
| 5 |
+
"epochs": 3,
|
| 6 |
+
"learning_rate": 3e-05,
|
| 7 |
+
"max_input_tokens": 4096,
|
| 8 |
+
"calibration": "temperature fit on a held out split so sigmoid(logit) is a probability"
|
| 9 |
+
}
|
router/training/train_router.py
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __future__ import annotations
|
| 2 |
+
|
| 3 |
+
import argparse
|
| 4 |
+
import json
|
| 5 |
+
from pathlib import Path
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
def main() -> None:
|
| 9 |
+
parser = argparse.ArgumentParser(description="train a one logit router on (query, draft, y) rows")
|
| 10 |
+
parser.add_argument("--pairs", type=Path, required=True, help="jsonl with query, draft, y")
|
| 11 |
+
parser.add_argument("--base", default="microsoft/deberta-v3-xsmall")
|
| 12 |
+
parser.add_argument("--out", type=Path, default=Path(__file__).resolve().parents[1])
|
| 13 |
+
parser.add_argument("--epochs", type=int, default=3)
|
| 14 |
+
parser.add_argument("--max-input-tokens", type=int, default=4096)
|
| 15 |
+
args = parser.parse_args()
|
| 16 |
+
|
| 17 |
+
import torch
|
| 18 |
+
from torch.utils.data import DataLoader
|
| 19 |
+
from transformers import AutoModelForSequenceClassification, AutoTokenizer
|
| 20 |
+
|
| 21 |
+
rows = [json.loads(line) for line in args.pairs.read_text(encoding="utf-8").splitlines() if line.strip()]
|
| 22 |
+
tokenizer = AutoTokenizer.from_pretrained(args.base)
|
| 23 |
+
model = AutoModelForSequenceClassification.from_pretrained(args.base, num_labels=1)
|
| 24 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 25 |
+
model.to(device)
|
| 26 |
+
optimizer = torch.optim.AdamW(model.parameters(), lr=3e-5)
|
| 27 |
+
loss_fn = torch.nn.BCEWithLogitsLoss()
|
| 28 |
+
|
| 29 |
+
def batches():
|
| 30 |
+
for start in range(0, len(rows), 16):
|
| 31 |
+
chunk = rows[start : start + 16]
|
| 32 |
+
encoded = tokenizer(
|
| 33 |
+
[r["query"] for r in chunk],
|
| 34 |
+
[r["draft"] for r in chunk],
|
| 35 |
+
truncation=True,
|
| 36 |
+
max_length=args.max_input_tokens,
|
| 37 |
+
padding=True,
|
| 38 |
+
return_tensors="pt",
|
| 39 |
+
)
|
| 40 |
+
labels = torch.tensor([float(r["y"]) for r in chunk])
|
| 41 |
+
yield {k: v.to(device) for k, v in encoded.items()}, labels.to(device)
|
| 42 |
+
|
| 43 |
+
model.train()
|
| 44 |
+
for epoch in range(args.epochs):
|
| 45 |
+
total = 0.0
|
| 46 |
+
for encoded, labels in DataLoader(list(batches()), batch_size=None):
|
| 47 |
+
optimizer.zero_grad()
|
| 48 |
+
logits = model(**encoded).logits.reshape(-1)
|
| 49 |
+
loss = loss_fn(logits, labels)
|
| 50 |
+
loss.backward()
|
| 51 |
+
optimizer.step()
|
| 52 |
+
total += float(loss)
|
| 53 |
+
print(f"epoch {epoch + 1}: loss {total:.4f}")
|
| 54 |
+
model.save_pretrained(args.out, safe_serialization=True)
|
| 55 |
+
tokenizer.save_pretrained(args.out)
|
| 56 |
+
print(f"saved to {args.out}")
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
if __name__ == "__main__":
|
| 60 |
+
main()
|