Highlight Low-Confidence Spans

easy
Unit 9 · KV Cache0/3 to milestone

Some products shade parts of an answer the model was unsure about, so readers know what to double-check. With per-token log-probabilities, find those spans.

Implement:

  • low_confidence_spans(logprobs, threshold, max_gap=0): a token is low-confidence if exp(logprob) < threshold. Return half-open (start, end) index ranges of consecutive low-confidence tokens, merging two spans separated by at most max_gap confident tokens.
  • span_texts(tokens, spans): the joined text of each span ("".join(tokens[start:end]))
  • answer_confidence(logprobs): the geometric-mean token probability, exp(mean(logprobs)) (1.0 for an empty answer)
#uncertainty#logprobs#ux2 visible + 1 hidden tests
solution.py · Python 3.13
Loading editor…
Run your code to see results. ⌘↵ to run · ⌘⇧↵ to submit
Highlight Low-Confidence Spans · TechGradify