Overview
Theselect() function makes the language model choose from a predefined list of options. This is useful for classification, multiple-choice questions, and structured decision-making.
Syntax
Parameters
str
Variable name to store the selected choice. Access with
state[name].List[str]
required
List of possible choices. The model will select one of these options.
float
default:"0.0"
Sampling temperature for selection. Usually kept at 0.0 for deterministic selection.
ChoicesSamplingMethod
default:"token_length_normalized"
Method for computing choice probabilities:
token_length_normalized: Normalizes by token length (default, recommended)- Other methods available in
sglang.lang.choices
Usage
Basic Selection
Multiple Choice Question
Classification with Confidence
Yes/No Questions
Using select() with gen()
You can also use thechoices parameter in gen() to achieve the same effect:
Choice Selection Methods
Thechoices_method parameter controls how probabilities are computed:
token_length_normalized (Default)
Normalizes choice probabilities by token length. This is the recommended method as it prevents bias toward longer/shorter choices.Best Practices
-
Use descriptive choices: Make choices clear and unambiguous
-
Keep temperature at 0.0: For deterministic selection
-
Provide context: Give the model enough context to make the right choice
-
Use meaningful variable names: Name selections based on what they represent
Advanced Example: Multi-step Classification
See Also
- @sglang.function - Define prompt programs
- gen() - Generate free-form text
- Choices Module - Advanced choice selection methods
