OgaGenerator class is responsible for generating text using a loaded model. It provides methods for token-by-token generation, managing generation state, and accessing generated sequences.
Class Definition
~/workspace/source/src/ort_genai.h:446
Methods
Create()
Create a generator instance from a model and parameters.const OgaModel&
required
The model to use for generation
OgaGeneratorParams&
required
Generation parameters (search options, constraints, etc.)
std::unique_ptr<OgaGenerator> - A unique pointer to the created generator
Throws: std::runtime_error if generator creation fails
Example
AppendTokenSequences()
Append token sequences to the generator (typically the encoded prompt).const OgaSequences&
required
The token sequences to append (usually from tokenizer encoding)
std::runtime_error if appending fails
Example
~/workspace/source/examples/c/src/model_qa.cpp:128
AppendTokens()
Append individual tokens to the generator.const int32_t*
required
Pointer to the token IDs array
size_t
required
Number of tokens to append
Example
GenerateNextToken()
Generate the next token in the sequence.std::runtime_error if generation fails
This is the core method for token-by-token generation. Call this repeatedly until IsDone() returns true.
Example
~/workspace/source/examples/c/src/model_qa.cpp:136
IsDone()
Check if generation is complete.bool - True if generation has finished (reached max length or EOS token)
Example
GetSequenceData()
Get the raw token data for a sequence.size_t
required
The sequence index (typically 0 for single sequence generation)
const int32_t* - Pointer to the token data array
Example
GetSequenceCount()
Get the number of tokens in a sequence.size_t
required
The sequence index
size_t - Number of tokens in the sequence
GetSequence() (C++20)
Get a sequence as a span.size_t
required
The sequence index
std::span<const int32_t> - Span view of the token sequence
Example
GetNextTokens()
Get the most recently generated tokens.Example
TokenCount()
Get the total number of tokens in the current generation.size_t - Total number of tokens (prompt + generated)
Example
~/workspace/source/examples/c/src/model_qa.cpp:129
RewindTo()
Rewind the generator to a previous state.size_t
required
The token position to rewind to
Example
~/workspace/source/examples/c/src/model_chat.cpp:176
SetRuntimeOption()
Set runtime options for the generator.const char*
required
The option key (e.g., “terminate_session”)
const char*
required
The option value
Example
~/workspace/source/examples/c/src/model_qa.cpp:23
SetModelInput()
Set a custom model input tensor.const char*
required
The input name
OgaTensor&
required
The input tensor
SetInputs()
Set multiple model inputs at once.OgaNamedTensors&
required
Named collection of input tensors
GetInput()
Get a model input tensor.const char*
required
The input name
std::unique_ptr<OgaTensor> - The input tensor
GetOutput()
Get a model output tensor.const char*
required
The output name
std::unique_ptr<OgaTensor> - The output tensor
GetLogits()
Get the logits tensor from the last generation step.std::unique_ptr<OgaTensor> - The logits tensor
SetLogits()
Set custom logits for the next generation step.OgaTensor&
required
The logits tensor to use
IsSessionTerminated()
Check if the generation session has been terminated.bool - True if the session was terminated (e.g., via SetRuntimeOption)
SetActiveAdapter()
Set the active LoRA adapter for generation.OgaAdapters&
required
The adapters collection
const char*
required
Name of the adapter to activate
Complete Examples
Basic Generation
~/workspace/source/src/ort_genai.h:21
Streaming Generation
From~/workspace/source/examples/c/src/model_qa.cpp:132:
Chat with Context Management
From~/workspace/source/examples/c/src/model_chat.cpp:106:
See Also
- OgaGeneratorParams - Configure generation parameters
- OgaModel - Load models
- OgaTokenizer - Encode and decode text