gizml:grammar
the texts a model may write, for constrained decoding. A Grammar is compiled once from a JSON schema (or is any JSON object); a Recognizer reads a text byte by byte and says whether the text so far can still become one the grammar allows, and whether it is one already. A sampler masks each token whose bytes the recognizer refuses (gizml:llm), so the answer is JSON of the schema. The recognizer keeps each reading that is still possible (anyOf, enum values and object keys with a common start), so it needs no lookahead.
import * as Grammar from "gizml:grammar";Types
Node
type Node = { kind: i64; keys: Vec<string>; values: Vec<i64>; required: Vec<bool>; extra: bool; items: i64; least: i64; most: i64; texts: Vec<bytes>; options: Vec<i64>; }A node of a grammar: its kind and, by kind, its keys and their nodes, the keys it needs and whether other keys may come (OBJECT); its items' node and bounds (ARRAY); its texts as compact JSON (LITERAL: true, null, an enum's values); its options (CHOICE).
kind:i64keys:Vec<string>values:Vec<i64>required:Vec<bool>extra:boolitems:i64least:i64most:i64texts:Vec<bytes>options:Vec<i64>
Grammar
type Grammar = { nodes: Vec<Node>; }A grammar: its nodes, the first the whole text's.
nodes:Vec<M22.Node>
Frame
type Frame = { node: i64; kind: i64; phase: i64; seen: Vec<bool>; keys: Vec<i64>; free: bool; pos: i64; count: i64; texts: Vec<i64>; sub: i64; }One step of a reading: the grammar node, the state of the value that it reads, and what it has seen.
node:i64kind:i64phase:i64seen:Vec<bool>keys:Vec<i64>free:boolpos:i64count:i64texts:Vec<i64>sub:i64
Reading
type Reading = { frames: Vec<Frame>; space: i64; }One reading of a text so far: its stack of frames and the space that the grammar allows next.
frames:Vec<M22.Frame>space:i64
Recognizer
type Recognizer = { readings: Vec<Reading>; }The readings of a text so far by a grammar: none when the grammar allows no text that starts so.
readings:Vec<M22.Reading>
Functions
anyObject
function anyObject(): GrammarAny JSON object, as OpenAI's response_format json_object asks.
Effects: fail.
fromSchema
function fromSchema(schema: JsonValue): Grammar throws stringThe grammar of a JSON schema: type (one or a list), properties, required, additionalProperties (false when not given, as vLLM's strict mode), items, minItems, maxItems, enum, const, anyOf, oneOf and $ref to #/$defs or #/definitions. Other keywords (pattern, format, lengths, bounds of numbers) are not enforced.
Effects: fail.
Methods
Grammar.start
function Grammar.start(self: Grammar): RecognizerThe recognizer of g before any byte. The text starts with its value: no white space before it.
Effects: fail.
Recognizer.live
function Recognizer.live(self: Recognizer): boolWhether the text so far can still become one the grammar allows.
Recognizer.done
function Recognizer.done(self: Recognizer): boolWhether the text so far is one the grammar allows (white space may still follow).
Effects: fail.
Recognizer.feed
function Recognizer.feed(self: Recognizer, g: Grammar, text: bytes): RecognizerThe recognizer of grammar g after the bytes of text; not live when g refuses one.
Effects: fail.
Recognizer.allows
function Recognizer.allows(self: Recognizer, g: Grammar, text: bytes): boolWhether text may come next by grammar g: the recognizer stays live after it.
Effects: fail.