Samples a category index from a categorical distribution, allowing text generation to choose among possible next tokens instead of always taking the most likely one. section 16.2.4
Sample the next token
random_sample <- function(preds, temperature = 1) { preds <- preds / temperature preds <- op_reshape(preds, c(1, -1)) random_categorical(preds, num_samples = 1) |> op_squeeze() } compiled_generate(prompt, random_sample)
Control the sampling behavior with:
- Temperature: lower values sharpen the distribution toward likely categories; higher values spread probability across more categories. section 16.2.4
- Top-k sampling: restricts random choice to a selected number of likely categories before sampling. section 16.2.4
