File size: 1,145 Bytes
2778af7 | 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 | """Prompt rendering. Training and inference share this exact format."""
from __future__ import annotations
from .schema import Question, normalize_state
SPECIAL_TOKENS = [
"<|state|>",
"<|question|>",
"<|type|>",
"<|options|>",
"<|answer|>",
"<|opt|>",
]
OPTION_MARKER = "<|opt|>"
def option_texts(q: Question) -> list[str]:
if q.kind == "noul":
return ["false", "true"]
if q.kind == "choice":
out = []
for key, desc in zip(q.keys, q.descriptions):
out.append(key if desc == key else f"{key} - {desc}")
return out
return list(q.descriptions)
def render_prompt(state: str, q: Question) -> str:
state = normalize_state(state)
if q.kind == "score":
instruction = q.instructions + " Options are ordered from lowest to highest."
else:
instruction = q.instructions
lines = "\n".join(f"{i}. {t} {OPTION_MARKER}" for i, t in enumerate(option_texts(q)))
return (
f"<|state|>\n{state}\n"
f"<|question|>\n{instruction}\n"
f"<|type|>\n{q.kind}\n"
f"<|options|>\n{lines}\n"
f"<|answer|>"
)
|