File size: 12,420 Bytes
0fda8e6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
"""AINode's prompt renderer, copied VERBATIM from the repo so training and serving render the
same bytes. Do not edit by hand: regenerate with train/make_verbatim.py.

source commit: e5c089386e0239c9eb270eeb490d181722b8da5b
files: ainode/api/decide.py (SYSTEM_PROMPT, ANSWER_INSTRUCTION, MAX_OPTIONS, TOP_LOGPROBS, BOOLEAN_OPTIONS, DecideError, option_label, option_labels, serialize_state, build_messages)
       ainode/api/systemone.py (CHOICE, NOUL, SCORE, QUESTION_TYPES, NOUL_OPTIONS, MIN_SCORE_LEVELS, MAX_SCORE_LEVELS, MAX_CRITERIA, Translated, option_text, criteria_pairs, choice_options, noul_options, score_options, translate_one, translate_questions)
prompt_source_sha256: d2660ebec28bd3f1704235bda88d24a397c1c62475e740519cb8ef2d08f25fdd  (sha256 of the copied definitions, in this order)

The served path is: systemone.translate_questions -> decide.normalize_questions (shape checks only)
-> decide.build_messages(serialize_state(state), None, question, options) -> the model's chat
template with add_generation_prompt=True and enable_thinking=False -> one label token.
"""
from __future__ import annotations

import json
from typing import Any, NamedTuple, Optional

PROMPT_SOURCE_COMMIT = "e5c089386e0239c9eb270eeb490d181722b8da5b"
PROMPT_SOURCE_SHA256 = "d2660ebec28bd3f1704235bda88d24a397c1c62475e740519cb8ef2d08f25fdd"

SYSTEM_PROMPT = ("You are a decision function. Answer with the single letter "
                 "of the best option and nothing else.")


ANSWER_INSTRUCTION = "Answer with the label of one option and nothing else."


MAX_OPTIONS = 255


TOP_LOGPROBS = 20


BOOLEAN_OPTIONS = ("yes", "no")


class DecideError(Exception):
    """A bad request shape. Carries the message the caller gets in the 4xx.

    ``/v1/decide`` answers it as a 400 and ``/v1/systemone`` as the 422 the Jev
    format specifies, so the message says what is wrong and never which status
    somebody is about to put it in.
    """


def option_label(index: int) -> str:
    """Zero-based option index to its letter label: A..Z, AA, AB, ... IU.

    Bijective base-26 (spreadsheet columns), so the scheme keeps going past Z
    without a separator and without ever colliding.
    """
    if index < 0:
        raise ValueError("option index cannot be negative")
    n = index + 1
    out = ""
    while n > 0:
        n, rem = divmod(n - 1, 26)
        out = chr(ord("A") + rem) + out
    return out


def option_labels(count: int) -> list[str]:
    """The labels for a question with `count` options, in option order."""
    return [option_label(i) for i in range(count)]


def serialize_state(state: Any) -> str:
    """The state as the model sees it: a string verbatim, anything else compact JSON."""
    if state is None:
        return ""
    if isinstance(state, str):
        return state
    try:
        return json.dumps(state, separators=(",", ":"), sort_keys=True,
                          ensure_ascii=False)
    except (TypeError, ValueError) as exc:
        raise DecideError(f"'state' is not JSON-serializable: {exc}") from exc


def build_messages(state: str, instructions: Optional[str], question: str,
                   options: list[str]) -> list[dict]:
    """The chat messages for one question. Pure, so the tests can pin the text.

    The state comes BEFORE the question on purpose: every question in a request
    then shares a byte-identical prefix (system message plus state), so the
    engine's prefix cache prefills the shared part once no matter how many
    questions are asked against it.
    """
    system = SYSTEM_PROMPT
    extra = (instructions or "").strip()
    if extra:
        system = f"{SYSTEM_PROMPT}\n\n{extra}"
    lines = ["STATE:", state, "", f"QUESTION: {question}", "", "OPTIONS:"]
    lines += [f"{option_label(i)}. {opt}" for i, opt in enumerate(options)]
    lines += ["", ANSWER_INSTRUCTION]
    return [
        {"role": "system", "content": system},
        {"role": "user", "content": "\n".join(lines)},
    ]


CHOICE = "choice"


NOUL = "noul"


SCORE = "score"


QUESTION_TYPES = (CHOICE, NOUL, SCORE)


NOUL_OPTIONS = ("true", "false")


MIN_SCORE_LEVELS = 2


MAX_SCORE_LEVELS = 10


MAX_CRITERIA = TOP_LOGPROBS


class Translated(NamedTuple):
    """One Jev question as the decision core sees it, plus the way back out.

    ``options`` is what the model reads, one line per option. ``names`` is what
    each of those options answers to on the wire, in the same order: a choice
    criteria key verbatim, ``true`` / ``false``, or a score level's position.
    Keeping the pair here is what lets the answer name the caller's own key
    rather than the letter the engine was constrained to.
    """

    kind: str
    question: str
    options: list[str]
    names: list[str]


def option_text(name: str, description: Any) -> str:
    """One option line: the name the answer will carry, then what it means.

    The name comes first and VERBATIM because it is the string the caller's
    client compares against, and a model that has read it beside its description
    is choosing between meanings rather than between labels. The description is
    flattened to one line, because the prompt renders one option per line and a
    description with a newline in it would read as two options.
    """
    if isinstance(description, str) and description.strip():
        return f"{name}: {' '.join(description.split())}"
    return name


def criteria_pairs(key: str, criteria: Any, kind: str) -> list[tuple[str, Any]]:
    """The ``(name, description)`` pairs of an object ``criteria``, in order.

    Insertion order is the rubric order for a score, and JSON parsing preserves
    it, so nothing here sorts. A name is validated stripped and kept as written:
    the answer has to carry the caller's own key back, byte for byte, because the
    caller's code looks that key up.
    """
    if not isinstance(criteria, dict) or not criteria:
        raise DecideError(
            f"question '{key}': a {kind} question needs a non-empty 'criteria' "
            "object of {name: description}")
    pairs: list[tuple[str, Any]] = []
    for name, description in criteria.items():
        if not isinstance(name, str) or not name.strip():
            raise DecideError(f"question '{key}': every 'criteria' name must be a "
                              f"non-empty string (got {name!r})")
        if description is not None and not isinstance(description, str):
            raise DecideError(f"question '{key}': the 'criteria' description for "
                              f"'{name}' must be a string")
        pairs.append((name.strip(), description))
    return pairs


def choice_options(key: str, criteria: Any) -> tuple[list[str], list[str]]:
    """A choice question's options and the criteria keys they answer to."""
    pairs = criteria_pairs(key, criteria, "choice")
    if len(pairs) < 2:
        raise DecideError(f"question '{key}': a choice needs at least 2 'criteria' "
                          f"options, got {len(pairs)}")
    if len(pairs) > MAX_CRITERIA:
        raise DecideError(
            f"question '{key}': {len(pairs)} 'criteria' options is more than the "
            f"{MAX_CRITERIA} this node can report a probability for. One engine "
            f"call carries back the top {TOP_LOGPROBS} labels, so a wider option "
            "set would answer with a distribution missing its tail")
    return ([option_text(name, desc) for name, desc in pairs],
            [name for name, _ in pairs])


def noul_options(key: str, criteria: Any) -> tuple[list[str], list[str]]:
    """A noul's two options, always ``true`` then ``false``.

    The criteria block is optional here, and may describe one side only: some
    clients send both, some send neither, and a yes-or-no question is still
    answerable from its instructions alone. What a caller may not do is rename
    the sides, because the answer is P(true) and nothing else can stand in for
    it.
    """
    described: dict[str, Any] = {}
    if criteria is not None:
        if not isinstance(criteria, dict):
            raise DecideError(f"question '{key}': 'criteria' must be an object of "
                              "{true: description, false: description}")
        for name, description in criteria.items():
            flat = name.strip() if isinstance(name, str) else name
            if flat not in NOUL_OPTIONS:
                raise DecideError(f"question '{key}': a noul's 'criteria' names only "
                                  f"'true' and 'false' (got {name!r})")
            if description is not None and not isinstance(description, str):
                raise DecideError(f"question '{key}': the 'criteria' description for "
                                  f"'{flat}' must be a string")
            described[flat] = description
    return ([option_text(name, described.get(name)) for name in NOUL_OPTIONS],
            list(NOUL_OPTIONS))


def score_options(key: str, criteria: Any) -> tuple[list[str], list[str]]:
    """A score's levels in rubric order: a list by position, an object by insertion.

    Both spellings are accepted because both are in the wild: the list form names
    the levels and nothing else, the object form names them and says what each
    one means. Either way position 0 is the first level the caller wrote, which
    is what the legend and the expected score are counted against.
    """
    if isinstance(criteria, list):
        names: list[str] = []
        for level in criteria:
            if not isinstance(level, str) or not level.strip():
                raise DecideError(f"question '{key}': every 'criteria' level must be "
                                  f"a non-empty string (got {level!r})")
            names.append(level.strip())
        options = list(names)
    elif isinstance(criteria, dict):
        pairs = criteria_pairs(key, criteria, "score")
        names = [name for name, _ in pairs]
        options = [option_text(name, desc) for name, desc in pairs]
    else:
        raise DecideError(
            f"question '{key}': a score question needs 'criteria', either an ordered "
            "list of levels or an object of {level: description}")
    if not MIN_SCORE_LEVELS <= len(names) <= MAX_SCORE_LEVELS:
        raise DecideError(
            f"question '{key}': a score's 'criteria' needs {MIN_SCORE_LEVELS} to "
            f"{MAX_SCORE_LEVELS} ordered levels, got {len(names)}")
    if len(set(names)) != len(names):
        raise DecideError(f"question '{key}': 'criteria' repeats a level name. Every "
                          "level must be distinct so a score names one of them")
    return options, names


def translate_one(key: str, spec: Any) -> Translated:
    """One question off the wire. Reads three fields and ignores the rest.

    ``type``, ``instructions`` and ``criteria`` are the whole question as far as
    this route is concerned, which is also exactly what JDE's
    ``questionsForWire`` sends. A field beyond them belongs to the caller's own
    code, so it is neither read nor echoed.
    """
    if not isinstance(spec, dict):
        raise DecideError(f"question '{key}' must be an object")
    kind = spec.get("type")
    if kind not in QUESTION_TYPES:
        raise DecideError(f"question '{key}': 'type' must be one of "
                          f"{', '.join(QUESTION_TYPES)} (got {kind!r})")
    instructions = spec.get("instructions")
    if not isinstance(instructions, str) or not instructions.strip():
        raise DecideError(f"question '{key}' needs a non-empty 'instructions' string")
    criteria = spec.get("criteria")
    if kind == NOUL:
        options, names = noul_options(key, criteria)
    elif kind == SCORE:
        options, names = score_options(key, criteria)
    else:
        options, names = choice_options(key, criteria)
    return Translated(kind, instructions.strip(), options, names)


def translate_questions(raw: Any) -> dict[str, Translated]:
    """Every question in the body, in the order the caller wrote them."""
    if not isinstance(raw, dict) or not raw:
        raise DecideError("'questions' must be a non-empty object of {id: question}")
    out: dict[str, Translated] = {}
    for key, spec in raw.items():
        if not isinstance(key, str) or not key.strip():
            raise DecideError("every question id must be a non-empty string")
        out[key] = translate_one(key, spec)
    return out