Overview
Thegen() function generates text from the language model at the current position in your prompt program.
Syntax
Parameters
str
Variable name to store the generated text. Access with
state[name].int
default:"128"
Maximum number of tokens to generate.
int
Minimum number of tokens to generate.
float
default:"1.0"
Sampling temperature. Higher values (e.g., 1.5) make output more random, lower values (e.g., 0.2) make it more deterministic.
float
default:"1.0"
Nucleus sampling threshold. Only tokens with cumulative probability up to
top_p are considered.int
default:"-1"
Top-k sampling. Only the top
k most likely tokens are considered. -1 means disabled.float
default:"0.0"
Minimum probability threshold for token sampling.
str | List[str]
Stop sequences. Generation stops when any of these strings are generated.
List[int]
Token IDs that trigger generation to stop.
str | List[str]
Regular expressions that trigger generation to stop when matched.
float
default:"0.0"
Penalty for token frequency. Positive values reduce repetition.
float
default:"0.0"
Penalty for token presence. Positive values encourage topic diversity.
bool
default:"false"
Whether to ignore end-of-sequence tokens.
str
Regular expression constraint. Generated text must match this pattern.
str
JSON schema constraint. Generated text must be valid JSON matching this schema.
List[str]
If provided,
gen() behaves like select() and chooses from these options.bool
Whether to return log probabilities for generated tokens.
int
Start position for computing log probabilities.
int
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
