Skip to main content

Overview

The select() 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 the choices parameter in gen() to achieve the same effect:

Choice Selection Methods

The choices_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

  1. Use descriptive choices: Make choices clear and unambiguous
  2. Keep temperature at 0.0: For deterministic selection
  3. Provide context: Give the model enough context to make the right choice
  4. Use meaningful variable names: Name selections based on what they represent

Advanced Example: Multi-step Classification

See Also