Documentation
¶
Overview ¶
Package confidence derives a decision-confidence signal from a grammar- constrained model's per-token logprobs. With a grammar active, llama.cpp reports the RAW (pre-mask) distribution, so at the decision-value position the top alternatives reveal the model's genuine preference among the legal options. We aggregate that raw mass by legal class (folding spelling variants like "Yes"/"yes", dropping grammar-illegal tokens) and return the normalized margin between the top two classes — a low margin means the model was genuinely torn (e.g. an "eager-YES" borderline call) and the pipeline should escalate to a larger tier rather than accept it.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Margin ¶
func Margin(toks []llamaclient.TokenLogprob, jsonKey string, classes []string) (float64, bool)
Margin returns a 0..1 separation between the model's top-1 and top-2 legal decision classes at the value position of jsonKey (e.g. "decision" / "label"), computed on the raw logprob distribution. ok=false when the position or a usable class distribution can't be resolved — the caller then falls back to other signals (self-reported confidence) rather than escalating blindly.
func MarginFull ¶ added in v0.206.0
func MarginFull(toks []llamaclient.TokenLogprob, jsonKey string, classes []string) (margin float64, declaredMass float64, ok bool)
MarginFull is the full-denominator margin and the declared (matched) mass share, for callers that need only those two numbers.
Types ¶
type Detail ¶ added in v0.206.0
Detail is the full view of one decision position. Margin and MatchedMargin are the SAME numerator over two different denominators, and they are NOT comparable to each other: a stored history of one scale must never be read as the other (register D-130).
MatchedMargin = (first - second) / mass on MATCHED class tokens (today's gate)
Margin = (first - second) / mass on EVERY alternative (full scale)
DeclaredMass = matched / all, in 0..1 — measured ~0.097 on llama.cpp, i.e.
the matched scale inflates the margin by about 10x.
Ambiguous = alternatives credited to NO class because they prefix more
than one (classOf returns -1). On taxonomy-shaped label sets
like {billing, billing_dispute} that silently zeroes real
mass; counting it is what makes the zeroing rate measurable.
func MarginDetail ¶ added in v0.206.0
func MarginDetail(toks []llamaclient.TokenLogprob, jsonKey string, classes []string) (Detail, bool)
MarginDetail returns the full decision-position view at the value position of jsonKey. ok=false when the position or a usable raw distribution can't be resolved (no alternatives, or every alternative clamped to the -inf sentinel) — the caller then falls back to other signals rather than escalating blindly, exactly as with Margin.
ok here means "the RAW distribution was usable", which is weaker than Margin's ok ("matched class mass was usable"): a position where the model put all of its mass outside the legal classes yields ok=true with DeclaredMass == 0 and both margins 0 — that is a fact worth recording, not an absence of data.