Top-p (nucleus) sampling enhances creativity by selecting words from the smallest set of tokens whose cumulative probability exceeds a threshold. This allows the model to consider diverse and less likely options while maintaining coherence.
Here is the code reference below:
In the code above, we encourage more creative and varied outputs and avoid overly deterministic results from high-probability tokens only.