API Reference
Base Sampler
Beam Search Sampler
- class llm_samplers.beam.BeamSearchSampler[source]
Bases:
BaseSamplerBeam search sampler that maintains the top k most promising sequences.
- __init__(beam_width=5)[source]
Initialize the beam search sampler.
- Parameters:
beam_width (
int) – Number of sequences to keep at each step
Temperature Sampler
- class llm_samplers.temperature.TemperatureSampler[source]
Bases:
BaseSamplerTemperature sampling adjusts the “sharpness” of the probability distribution.
Low temperature (<1.0): More deterministic, picks high-probability tokens
High temperature (>1.0): More random, flatter distribution
- __init__(temperature=1.0)[source]
Initialize the temperature sampler.
- Parameters:
temperature (
float) – Temperature value to control sampling sharpness
Top-K Sampler
- class llm_samplers.top_k.TopKSampler[source]
Bases:
BaseSamplerTop-K sampling considers only the ‘k’ most probable tokens. All other tokens are set to have zero probability.
- __init__(k=50)[source]
Initialize the Top-K sampler.
- Parameters:
k (
int) – Number of top tokens to consider
Top-P (Nucleus) Sampler
- class llm_samplers.top_p.TopPSampler[source]
Bases:
BaseSamplerTop-P (Nucleus) sampling selects the smallest set of tokens whose cumulative probability exceeds threshold ‘p’.
- __init__(p=0.9)[source]
Initialize the Top-P sampler.
- Parameters:
p (
float) – Probability threshold for nucleus sampling
Min-P Sampler
- class llm_samplers.min_p.MinPSampler[source]
Bases:
BaseSamplerMin-P sampling dynamically adjusts the sampling pool size based on the probability of the most likely token.
- __init__(min_p=0.05)[source]
Initialize the Min-P sampler.
- Parameters:
min_p (
float) – Minimum probability threshold for tokens
Anti-Slop Sampler
- class llm_samplers.anti_slop.AntiSlopSampler[source]
Bases:
BaseSamplerAnti-Slop sampling down-weights probabilities at word & phrase level. Uses backtracking to retry with adjusted token probabilities if it encounters a disallowed word/phrase.
- __init__(disallowed_tokens=None, disallowed_phrases=None, penalty=0.5, max_retries=3)[source]
Initialize the Anti-Slop sampler.
XTC (Exclude Top Choices) Sampler
- class llm_samplers.xtc.XTCSampler[source]
Bases:
BaseSamplerXTC (Exclude Top Choices) sampling “turns truncation on its head.” Instead of pruning low-probability tokens, XTC targets the most probable ones under certain conditions to enhance creativity.
- __init__(top_k=5, exclusion_threshold=0.3, min_probability=0.1)[source]
Initialize the XTC sampler.
QAlign Sampler
- class llm_samplers.qalign.QAlignSampler[source]
Bases:
BaseSamplerQAlign sampler that uses MCMC to improve model outputs based on a reward model.
This implementation is based on the paper: “Sample, Don’t Search: Rethinking Test-Time Alignment for Language Models” by Faria et al. (2024) Paper: https://arxiv.org/abs/2504.03790
QAlign uses Markov Chain Monte Carlo (MCMC) to align model outputs at test time without requiring model fine-tuning. It converges to sampling from the optimal aligned distribution as test-time compute scales.
- __init__(reward_model, num_steps=10, temperature=1.0, beta=1.0, proposal_temp=1.0)[source]
Initialize QAlign sampler.
- Parameters:
reward_model (
Module) – The reward model to use for scoring outputsnum_steps (
int) – Number of MCMC steps to performtemperature (
float) – Temperature for sampling from the base modelbeta (
float) – Inverse temperature for the reward model (higher = more emphasis on reward)proposal_temp (
float) – Temperature for the proposal distribution