Skip to main content

Module detect

Module detect 

Source
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.
LogprobToken
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), a logprobs object ({"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 alternatives renormalised to sum to 1. None when there are none (or their probabilities sum to 0).