Command Palette

Search for a command to run...

Hectal
Phase 1: How LLMs Work (for Engineers)
LLM Engineering · Exercise 4 of 5 PreviousNext
Intermediate

Top-p (nucleus) filtering

Write top_p(probs, p) where probs maps tokens to probabilities. Sort tokens by probability, highest first (ties by token name). Keep tokens in that order until their running total is at least p, including the token that reaches it.

Return the kept tokens as a list, then renormalise: return a second value, a dictionary of kept token to probability divided by the kept total. So top_p returns (tokens, dist).

What Check tests

  1. p = 0.7 keeps a and b
  2. The kept probabilities are renormalised
  3. A very high top token alone is enough
  4. p = 1.0 keeps everything
  5. Ties are ordered by name

Stuck? One hint at a time

The solution unlocks after your first Check. Try first: that is where the learning happens.

main.py
Sign in to run your code.

Output

Press Run to see what your program prints.