"""Serve Reflex Reason 2B as a decision model on Jev's /v1/systemone wire format (TypeSafe-compatible).
pip install torch transformers peft fastapi uvicorn
python serve.py [--port 8766] [--temperature 3.176] [--max-think 384]
POST /v1/systemone {"state": ..., "questions": {name: {"type": "choice"|"noul"|"score", "instructions": ...,
"criteria": {...} | [...]}}}
-> {"model": "reflex-reason-2b", "answers": {name: {...}}, "usage": {"input_tokens": n, "output_tokens": m}}
The model thinks first (greedy, inside , at most --max-think tokens), as it was trained to. The options are
lettered A, B, C...; after "```" one forward pass gives its score for each letter, and a softmax at
--temperature turns them into a probability for every option. 3.176 was fitted (lowest log-loss) on JevBench's 231
public items; 1.0 is the raw model.
"""
import argparse
import torch
REPO = "Atlas-AI-research/reflex-reason-2b"
BASE = "Qwen/Qwen3-VL-2B-Instruct"
SYSTEM = ("You make one decision. Given the situation, a question and lettered options, think step by step about which "
"option is right, then give the letter of exactly one option in triple backticks.")
LETTERS = "ABCDEFGHIJ"
def options(q):
t, c = q.get("type", "choice"), q.get("criteria")
if t == "noul":
c = c or {}
return [("yes", f"yes: {c.get('true', 'yes')}"), ("no", f"no: {c.get('false', 'no')}")]
if t == "score":
return [(str(i), f"level {i}: {d}") for i, d in enumerate(c)]
if isinstance(c, dict):
return [(str(k), f"{k}: {v}") for k, v in c.items()]
return [(str(k), str(k)) for k in c]
def state_text(state):
if isinstance(state, str):
return state
import json
if isinstance(state, list):
return "\n".join(x if isinstance(x, str) else json.dumps(x, ensure_ascii=False) for x in state)
out = []
for k, v in state.items():
if isinstance(v, list):
v = "\n".join("- " + (x if isinstance(x, str) else json.dumps(x, ensure_ascii=False)) for x in v) or "none"
elif not isinstance(v, str):
v = json.dumps(v, ensure_ascii=False)
out.append(f"{k}:\n{v}" if "\n" in v else f"{k}: {v}")
return "\n".join(out)
@torch.no_grad()
def decide(m, tok, state, q, temperature, max_think):
opts = options(q)
body = (f"{state_text(state)}\n\nQuestion: {q.get('instructions', '')}\nOptions:\n"
+ "\n".join(f"{LETTERS[i]}. {text}" for i, (_, text) in enumerate(opts)))
prompt = f"<|im_start|>system\n{SYSTEM}<|im_end|>\n<|im_start|>user\n{body}<|im_end|>\n<|im_start|>assistant\n\n"
ids = tok(prompt, add_special_tokens=False, return_tensors="pt").input_ids.to(m.device)
out = m.generate(input_ids=ids, max_new_tokens=max_think, do_sample=False,
eos_token_id=tok.convert_tokens_to_ids("<|im_end|>"))
thought = tok.decode(out[0, ids.shape[1]:], skip_special_tokens=True).split("")[0].strip()
full = tok(prompt + thought + "\n\n\n```", add_special_tokens=False, return_tensors="pt").input_ids.to(m.device)
logits = m(input_ids=full).logits[0, -1].float()
letters = logits[[tok.convert_tokens_to_ids(LETTERS[i]) for i in range(len(opts))]]
probs = {k: round(v, 4) for (k, _), v in zip(opts, torch.softmax(letters / temperature, -1).tolist())}
t = q.get("type", "choice")
if t == "noul":
ans = {"type": "noul", "noul": probs["yes"]}
elif t == "score":
ans = {"type": "score", "score": round(sum(int(k) * v for k, v in probs.items()), 3), "probabilities": probs}
else:
ans = {"type": "choice", "choice": max(probs, key=probs.get), "probabilities": probs}
return ans, ids.shape[1], len(tok(thought, add_special_tokens=False).input_ids)
def main():
ap = argparse.ArgumentParser()
ap.add_argument("--port", type=int, default=8766)
ap.add_argument("--temperature", type=float, default=3.176)
ap.add_argument("--max-think", type=int, default=384)
ap.add_argument("--repo", default=REPO, help="Hugging Face repo id or a local folder with the adapter")
a = ap.parse_args()
import uvicorn
from fastapi import FastAPI
from peft import PeftModel
from transformers import AutoTokenizer, Qwen3VLForConditionalGeneration
tok = AutoTokenizer.from_pretrained(BASE)
dev = "cuda" if torch.cuda.is_available() else "cpu"
m = Qwen3VLForConditionalGeneration.from_pretrained(BASE, dtype=torch.bfloat16 if dev == "cuda" else torch.float32)
m = PeftModel.from_pretrained(m, a.repo).merge_and_unload().to(dev).eval()
app = FastAPI(title="reflex-reason-2b")
@app.post("/v1/systemone")
def systemone(req: dict):
answers, n_in, n_out = {}, 0, 0
for name, q in req["questions"].items():
answers[name], i, o = decide(m, tok, req["state"], q, a.temperature, a.max_think)
n_in, n_out = n_in + i, n_out + o
return {"model": "reflex-reason-2b", "answers": answers, "usage": {"input_tokens": n_in, "output_tokens": n_out}}
uvicorn.run(app, host="127.0.0.1", port=a.port, log_level="warning")
if __name__ == "__main__":
main()