import jsonimport torchfrom transformers import AutoModelForCausalLM, AutoTokenizer MODEL = "samadpls/onepass-qwen3-0.6b"TEMPERATURE = 1.0 # calibration temperature for this modelSYSTEM = "You are a decision model. Read the state, then answer every question with the letter of one option."LETTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"KIND = {"noul": "true or false", "choice": "choose one", "score": "pick a level"} def options(q): c = q.get("criteria") or {} if q["type"] == "noul": # yes/no: always true, then false return [("true", c.get("true", "")), ("false", c.get("false", ""))] if q["type"] == "score": # levels 0..n-1, in order return [(str(i), d) for i, d in enumerate(c)] return list(c.items()) def build_prompt(state, questions, tok): enc = lambda s: tok.encode(s, add_special_tokens=False) # encode piece by piece, never the whole string text = state if isinstance(state, str) else json.dumps(state, indent=1, ensure_ascii=False) ids = enc(f"<|im_start|>system\n{SYSTEM}<|im_end|>\n<|im_start|>user\nState:\n{text}\n") slots = {} for n, (qid, q) in enumerate(questions.items(), 1): opts = options(q) lines = "".join(f"{LETTERS[i]}) {label}: {desc}\n" for i, (label, desc) in enumerate(opts)) ids += enc(f"\nQuestion {n} ({KIND[q['type']]}): {q['instructions']}\n{lines}Answer:") slots[qid] = (len(ids) - 1, [label for label, _ in opts]) # the next token here is the answer letter ids += enc(" ?\n") return ids + enc("<|im_end|>\n"), slots tok = AutoTokenizer.from_pretrained(MODEL)model = AutoModelForCausalLM.from_pretrained(MODEL)state = "My card was charged twice for one order. Please refund the extra charge today."questions = { "refund": {"type": "noul", "instructions": "The customer wants a refund.", "criteria": {"true": "Yes.", "false": "No."}}, "team": {"type": "choice", "instructions": "Which team should handle this?", "criteria": {"billing": "Payments and refunds.", "tech_support": "Bugs and setup.", "sales": "New orders."}},}ids, slots = build_prompt(state, questions, tok)letters = [tok.encode(" " + L, add_special_tokens=False)[0] for L in LETTERS]with torch.no_grad(): logits = model(torch.tensor([ids])).logits[0] # one forward pass, nothing generatedfor qid, (pos, labels) in slots.items(): p = (logits[pos, letters[: len(labels)]].float() / TEMPERATURE).softmax(-1) print(qid, dict(zip(labels, p.tolist())))