LearnGenerative AI

Temperature and sampling

Turn the dials a chatbot uses to choose its next word, from completely predictable to wildly creative.

  • Generative AI
  • Language models

Move the sliders and press Sample. Bars show the chance of each word; the small triangles show how often it was actually picked. Click a word to see its formula. The example is in English.

How it works

Choosing, not just predicting.

A model gives every possible next word a score. A softmax turns the scores into probabilities, and the next word is drawn at random with those chances. The temperature T divides the scores first: below 1 it stretches the gaps, so the top word wins almost every time; above 1 it shrinks them, so unlikely words get a real chance.

Top-k keeps only the k most likely words. Top-p, or nucleus sampling, keeps the smallest group of top words whose chances add up to at least p. Words outside the group are cut, and the rest are rescaled to add up to 100%. Here the temperature is applied first, then top-k, then top-p.

Uncertainty, measured as entropy in bits, is 0 when one word is certain and log₂ 10 ≈ 3.32 when all ten words are equally likely. Low temperatures suit facts and code, higher ones suit brainstorming and stories, and the cut-offs stop very unlikely words from ever being picked.