Skip to main content
The SamplingParams dataclass configures how tokens are sampled during text generation.

Class Definition

Fields

float
default:"0.0"
Controls randomness in sampling:
  • 0.0: Greedy decoding (always pick most likely token)
  • 0.0 < temperature < 1.0: Less random
  • 1.0: Sample according to model probabilities
  • > 1.0: More random
Lower values make output more deterministic and focused.
int
default:"-1"
Limits sampling to the K most likely tokens:
  • -1: Disabled (consider all tokens)
  • 1: Equivalent to greedy decoding
  • > 1: Sample from top K tokens only
Helps prevent sampling low-probability tokens.
float
default:"1.0"
Nucleus sampling - samples from smallest set of tokens whose cumulative probability exceeds P:
  • 1.0: Disabled (consider all tokens)
  • 0.0 < top_p < 1.0: Sample from top tokens with cumulative probability P
Dynamically adjusts vocabulary size based on probability distribution.
bool
default:"False"
Whether to ignore end-of-sequence tokens:
  • False: Stop generation when EOS token is sampled
  • True: Continue generating even after EOS token
Useful for forcing generation to reach max_tokens.
int
default:"1024"
Maximum number of tokens to generate (excluding input prompt).Generation stops when either:
  • max_tokens tokens have been generated, or
  • EOS token is sampled (unless ignore_eos=True)

Properties

is_greedy

Returns True if the configuration will result in greedy (deterministic) decoding. Conditions:
  • temperature <= 0.0 or top_k == 1
  • AND top_p == 1.0

Usage Examples

Greedy Decoding

Balanced Sampling

Creative Generation

Force Complete Generation

Constrained Sampling

Common Patterns

Notes

  • When both top_k and top_p are set, both filters are applied sequentially
  • temperature=0.0 is equivalent to top_k=1 (but more efficient)
  • For reproducible results, use temperature=0.0
  • Higher temperature values can lead to nonsensical outputs if too high (>1.5)