Expand description
Hallucination-risk detection from token log probabilities.
The input is the logprobs block that OpenAI-compatible Chat Completions
APIs return when you ask for "logprobs": true. Each token’s probability
is exp(logprob). Words containing a token whose probability is below the
threshold are flagged, and neighbouring flagged words are merged into one
span. Each span reports the weakest token and the alternatives the model
was weighing at that point.
Low probability is a signal, not proof: a model can be confidently wrong, and it can be unsure about phrasing while the facts are right.
Structs§
- Alternative
- One of the candidates the model considered at a position.
- Candidate
- An alternative with its probability.
- Logprob
Token - One generated token with its log probability and the alternatives returned with it.
- Report
- Result of
detect. - Span
- A run of low-confidence words.
Constants§
- DEFAULT_
THRESHOLD - Default probability below which a token counts as low confidence.
- FLAG_
LABEL - Label used for spans produced by
detect.
Functions§
- detect
- Flag low-confidence spans.
- parse_
logprobs - Extract the token list from any of these shapes:
a full Chat Completions response (
choices[0].logprobs.content), alogprobsobject ({"content": [...]}), or a bare token array. - to_
token_ analysis - Convert tokens plus a report into the visualizer’s input format, so the terminal, HTML and Markdown renderers can display the result.
- top_
k_ entropy_ bits - Entropy in bits of
alternativesrenormalised to sum to 1.Nonewhen there are none (or their probabilities sum to 0).