Skip to main content
The 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

Defined in: ~/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.)
Returns: 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)
Throws: std::runtime_error if appending fails

Example

From ~/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.
Throws: std::runtime_error if generation fails This is the core method for token-by-token generation. Call this repeatedly until IsDone() returns true.

Example

From ~/workspace/source/examples/c/src/model_qa.cpp:136

IsDone()

Check if generation is complete.
Returns: 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)
Returns: 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
Returns: size_t - Number of tokens in the sequence

GetSequence() (C++20)

Get a sequence as a span.
size_t
required
The sequence index
Returns: std::span<const int32_t> - Span view of the token sequence

Example

GetNextTokens()

Get the most recently generated tokens.
Returns: The newly generated tokens (one per batch element)

Example

TokenCount()

Get the total number of tokens in the current generation.
Returns: size_t - Total number of tokens (prompt + generated)

Example

From ~/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
This is useful for chat scenarios where you want to maintain context but remove recent messages.

Example

From ~/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

From ~/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
Returns: std::unique_ptr<OgaTensor> - The input tensor

GetOutput()

Get a model output tensor.
const char*
required
The output name
Returns: std::unique_ptr<OgaTensor> - The output tensor

GetLogits()

Get the logits tensor from the last generation step.
Returns: 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.
Returns: 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

Based on: ~/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