Skip to main content
The generator functions provide the core text generation capabilities, allowing you to generate tokens iteratively or process complete sequences.

Generator Creation

function
Creates generator parameters from a model.Parameters:
  • model: The model to use for generation
  • out: Pointer to store the created generator params
Returns: NULL on success, or OgaResult* containing error message on failure
function
Destroys generator parameters.Parameters:
  • params: The generator params to destroy
function
Creates a generator from a model and parameters.Parameters:
  • model: The model to use for generation
  • params: The parameters to use for generation
  • out: Pointer to store the created generator
Returns: NULL on success, or OgaResult* containing error message on failureExample:
function
Destroys a generator.Parameters:
  • generator: The generator to destroy

Generator Parameters

Search Parameters

function
Sets a numerical search parameter.Parameters:
  • params: The generator params to modify
  • name: The parameter name (e.g., “max_length”, “temperature”, “top_p”, “top_k”)
  • value: The numerical value to set
Returns: NULL on success, or OgaResult* containing error message on failureExample:
function
Sets a boolean search parameter.Parameters:
  • params: The generator params to modify
  • name: The parameter name
  • value: The boolean value to set
Returns: NULL on success, or OgaResult* containing error message on failure
function
Gets a numerical search parameter value.Parameters:
  • params: The generator params to query
  • name: The parameter name
  • value: Pointer to store the parameter value
Returns: NULL on success, or OgaResult* containing error message on failure
function
Gets a boolean search parameter value.Parameters:
  • params: The generator params to query
  • name: The parameter name
  • value: Pointer to store the parameter value
Returns: NULL on success, or OgaResult* containing error message on failure

Guided Generation

function
Sets guidance for constrained generation.Parameters:
  • params: The generator params to modify
  • type: The guidance type (“json_schema”, “regex”, or “lark_grammar”)
  • data: The guidance specification
  • enable_ff_tokens: Whether to enable force-forward tokens (only valid with batch_size=1 and beam_size=1)
Returns: NULL on success, or OgaResult* containing error message on failureExample:

Input Management

function
Adds input token sequences to the generator.Parameters:
  • generator: The generator to add tokens to
  • p_sequences: The input token sequences
Returns: NULL on success, or OgaResult* containing error message on failure
function
Adds input tokens directly to the generator.Parameters:
  • generator: The generator to add tokens to
  • input_ids: Array of token IDs
  • input_ids_count: Number of tokens (batch_size × sequence_length)
Returns: NULL on success, or OgaResult* containing error message on failure
function
Returns the total number of tokens that have been added to the generator.Parameters:
  • generator: The generator to query
Returns: Number of tokens
function
Sets additional model inputs that GenAI doesn’t handle automatically (e.g., LoRA inputs).Parameters:
  • generator: The generator to set inputs on
  • name: Name of the model input (must match the model’s input name)
  • tensor: The tensor containing the input data
Returns: NULL on success, or OgaResult* containing error message on failure
function
Sets multiple model inputs at once.Parameters:
  • generator: The generator to set inputs on
  • named_tensors: Collection of named tensors
Returns: NULL on success, or OgaResult* containing error message on failure

Generation Loop

function
Generates the next token based on the current state. This computes logits from the model and updates the generator’s internal state.Parameters:
  • generator: The generator to advance
Returns: NULL on success, or OgaResult* containing error message on failureExample:
function
Returns the most recently generated tokens. The count matches the batch size.Parameters:
  • generator: The generator to query
  • out: Pointer to store the token array (valid until next generator call)
  • out_count: Pointer to store the number of tokens
Returns: NULL on success, or OgaResult* containing error message on failure
function
Returns true if the generator has finished generating all sequences.Parameters:
  • generator: The generator to check
Returns: true if generation is complete, false otherwise
function
Returns true if the session has been terminated.Parameters:
  • generator: The generator to check
Returns: true if session is terminated, false otherwise

Sequence Access

function
Returns the number of tokens in the sequence at the given index.Parameters:
  • generator: The generator to query
  • index: The sequence index
Returns: Number of tokens in the sequence
function
Returns a pointer to the sequence data at the given index.Parameters:
  • generator: The generator to query
  • index: The sequence index
Returns: Pointer to the sequence data (owned by generator, valid until generator is destroyed)Example:

Logits Access and Modification

function
Returns a copy of the logits from the model as a CPU tensor. Only contains the last token logits.Parameters:
  • generator: The generator to get logits from
  • out: Pointer to store the logits tensor
Returns: NULL on success, or OgaResult* containing error message on failureNote: The returned tensor must be destroyed with OgaDestroyTensor()
function
Sets the logits for the generator. Useful for guided generation.Parameters:
  • generator: The generator to set logits on
  • tensor: The logits tensor (must have same shape as GetLogits output)
Returns: NULL on success, or OgaResult* containing error message on failure
function
Returns a copy of a model input as a CPU tensor.Parameters:
  • generator: The generator to query
  • name: Name of the input tensor
  • out: Pointer to store the input tensor
Returns: NULL on success, or OgaResult* containing error message on failure
function
Returns a copy of a model output as a CPU tensor.Parameters:
  • generator: The generator to query
  • name: Name of the output tensor
  • out: Pointer to store the output tensor
Returns: NULL on success, or OgaResult* containing error message on failure

Advanced Features

function
Rewinds the generator to a specific token length. Useful for backtracking during generation.Parameters:
  • generator: The generator to rewind
  • new_length: The desired token length after rewinding
Returns: NULL on success, or OgaResult* containing error message on failure
function
Sets a runtime option for the generator.Parameters:
  • generator: The generator to configure
  • key: The runtime option name
  • value: The runtime option value
Returns: NULL on success, or OgaResult* containing error message on failure

Complete Generation Example

Streaming Generation Example

See Also

C API Overview

Learn about memory management and error handling

Model Functions

Create and configure models