function sample_next_token(probabilities, temperature=1.0, top_k=None):
   scaled = [p ** (1 / temperature) for p in probabilities] # low temp = more
 confident
   if top_k:

      scaled = keep_only_top_k(scaled, top_k) # ignore unlikely t
 okens
   normalized = normalize(scaled)
   return random_choice(weighted_by=normalized)
