Skip to main content
The Generator class manages the token generation loop and state.

Constructor

Create a generator from a model and parameters.
Model
required
The Model object to generate with
GeneratorParams
required
Generation parameters including search options

Methods

append_tokens()

Add input tokens to the generator. Can accept either a numpy array or OgaTensor.
numpy.ndarray | OgaTensor
required
Token IDs to add to the input sequence

generate_next_token()

Generate the next token in the sequence.
This method runs one iteration of the generation loop, including:
  • Model forward pass
  • Sampling from logits
  • Updating internal state

is_done()

Check if generation is complete.
bool
True if generation has finished for all sequences in the batch

get_next_tokens()

Get the most recently generated token for each sequence in the batch.
numpy.ndarray
Array of int32 token IDs, one per sequence in the batch

get_sequence()

Get the complete token sequence for a specific batch index.
int
required
Batch index of the sequence to retrieve
numpy.ndarray
Complete array of token IDs including input and generated tokens

token_count()

Get the total number of tokens processed so far.
int
Total number of tokens in the sequence

set_inputs()

Set model inputs from a NamedTensors object (typically from multimodal processor).
NamedTensors
required
Named tensor inputs from a processor

get_logits()

Get the current logits (pre-softmax scores) for the next token.
numpy.ndarray
Float array of shape [batch_size, vocab_size]

set_logits()

Manually set the logits before sampling the next token.
numpy.ndarray
required
Float array of shape [batch_size, vocab_size]

rewind_to()

Rewind the generator to a previous token position.
int
required
Token position to rewind to

get_input()

Get a model input tensor by name.
str
required
Name of the input tensor
numpy.ndarray
The requested input tensor as a numpy array

get_output()

Get a model output tensor by name.
str
required
Name of the output tensor
numpy.ndarray
The requested output tensor as a numpy array

set_model_input()

Manually set a model input tensor.
str
required
Name of the input tensor
numpy.ndarray
required
Tensor data as a numpy array

set_active_adapter()

Switch to a different LoRA adapter.
Adapters
required
Adapters object containing loaded adapters
str
required
Name of the adapter to activate

set_runtime_option()

Set a runtime option for the generator.
str
required
Option key
str
required
Option value

Example Usage

Basic generation:
Streaming generation:
Batch generation:
Manipulating logits:
Rewinding for chat: