Overview
Thegen() function generates text from the language model at the current position in your prompt program.
Syntax
Parameters
Variable name to store the generated text. Access with
state[name].Maximum number of tokens to generate.
Minimum number of tokens to generate.
Sampling temperature. Higher values (e.g., 1.5) make output more random, lower values (e.g., 0.2) make it more deterministic.
Nucleus sampling threshold. Only tokens with cumulative probability up to
top_p are considered.Top-k sampling. Only the top
k most likely tokens are considered. -1 means disabled.Minimum probability threshold for token sampling.
Stop sequences. Generation stops when any of these strings are generated.
Token IDs that trigger generation to stop.
Regular expressions that trigger generation to stop when matched.
Penalty for token frequency. Positive values reduce repetition.
Penalty for token presence. Positive values encourage topic diversity.
Whether to ignore end-of-sequence tokens.
Regular expression constraint. Generated text must match this pattern.
JSON schema constraint. Generated text must be valid JSON matching this schema.
If provided,
gen() behaves like select() and chooses from these options.Whether to return log probabilities for generated tokens.
Start position for computing log probabilities.
Number of top log probabilities to return per token.
Usage
Basic Generation
With Stop Sequences
Temperature Control
Constrained Generation with Regex
JSON Schema Constraint
Specialized Variants
gen_int()
Generates an integer value.gen_string()
Generates a string value.Accessing Generated Content
The generated text is stored in the state object and can be accessed by name:See Also
- @sglang.function - Define prompt programs
- select() - Choose from predefined options
- Sampling Parameters - Detailed parameter documentation
