Generator class manages the text generation process. It handles the iterative generation of tokens and provides access to generated sequences.
Constructor
Generator(Model model, GeneratorParams generatorParams)
Creates a new generator instance.Model
required
The model to use for generation
GeneratorParams
required
Parameters controlling the generation process
Generator.cs:13-16
Generation Control Methods
GenerateNextToken()
Generates the next token in the sequence. This is the core method for the generation loop.Generator.cs:58-61
IsDone()
Checks if generation is complete (end-of-sequence token generated or max length reached). Returns:bool - true if generation is complete, false otherwise
Generator.cs:18-21
RewindTo(ulong newLength)
Rewinds the generator to a specific token length. Useful for chat scenarios where you want to keep the system prompt but remove the conversation history.ulong
required
The token length to rewind to
Generator.cs:68-71
Input Methods
AppendTokens(ReadOnlySpan<int> inputIDs)
Appends token IDs to the generator’s input.ReadOnlySpan<int>
required
The token IDs to append
Generator.cs:33-42
AppendTokenSequences(Sequences sequences)
Appends encoded sequences to the generator’s input.Sequences
required
The sequences to append (typically from
Tokenizer.Encode())Generator.cs:44-47
SetModelInput(string name, Tensor value)
Sets a specific model input tensor.string
required
The name of the input
Tensor
required
The tensor value to set
Generator.cs:23-26
SetInputs(NamedTensors namedTensors)
Sets multiple model inputs at once.NamedTensors
required
Collection of named tensors to set as inputs
Generator.cs:28-31
Output Methods
GetNextTokens()
Returns the tokens generated in the lastGenerateNextToken() call.
Returns: ReadOnlySpan<int> - The most recently generated tokens
Generator.cs:73-80
GetSequence(ulong index)
Returns the complete token sequence for a specific sequence index.ulong
required
The sequence index (0 for single sequence generation)
ReadOnlySpan<int> - The complete token sequence
Generator.cs:82-90
TokenCount()
Returns the total number of tokens in the generator (including input and generated tokens). Returns:ulong - The total token count
Generator.cs:53-56
Tensor Access Methods
GetInput(string inputName)
Retrieves an input tensor by name.string
required
The name of the input tensor
Tensor - The input tensor
Generator.cs:98-104
GetOutput(string outputName)
Retrieves an output tensor by name.string
required
The name of the output tensor
Tensor - The output tensor
Generator.cs:112-118
Adapter Methods
SetActiveAdapter(Adapters adapters, string adapterName)
Activates a previously loaded adapter (for LoRA/fine-tuned models).Adapters
required
The adapters container
string
required
The name of the adapter to activate
Generator.cs:126-131
Complete Generation Example
Here’s a complete example of the generation loop:examples/csharp/ModelChat/Program.cs:54-84
Streaming Generation Example
examples/csharp/ModelChat/Program.cs:200-214
Chat with Rewind Example
examples/csharp/ModelChat/Program.cs:314-382
See Also
- Model - Load models
- Tokenizer - Encode and decode text
- GeneratorParams - Configure generation parameters