Skip to main content
The model functions provide the core capabilities for loading and configuring ONNX Runtime GenAI models.

Model Creation

function
Creates a model from a configuration directory.Parameters:
  • config_path: Path to the model configuration directory (UTF-8 encoded)
  • out: Pointer to store the created model
Returns: NULL on success, or OgaResult* containing error message on failureExample:
function
Creates a model from an OgaConfig object.Parameters:
  • config: Configuration object to use for the model
  • out: Pointer to store the created model
Returns: NULL on success, or OgaResult* containing error message on failure
function
Creates a model with runtime settings and device configuration.Parameters:
  • config_path: Path to the model configuration directory (UTF-8 encoded)
  • settings: Runtime settings to use for the model
  • out: Pointer to store the created model
Returns: NULL on success, or OgaResult* containing error message on failure
function
Destroys a model and frees its resources.Parameters:
  • model: The model to destroy

Model Properties

function
Returns the type of the model (e.g., “gpt2”, “llama”).Parameters:
  • model: The model to query
  • out: Pointer to store the model type string
Returns: NULL on success, or OgaResult* containing error message on failureNote: The returned string must be destroyed with OgaDestroyString()Example:
function
Returns the device type of the model (e.g., “CPU”, “CUDA”, “DML”).Parameters:
  • model: The model to query
  • out: Pointer to store the device type string
Returns: NULL on success, or OgaResult* containing error message on failureNote: The returned string must be destroyed with OgaDestroyString()

Configuration Objects

OgaConfig

function
Creates a configuration object from a configuration directory.Parameters:
  • config_path: Path to the configuration directory (UTF-8 encoded)
  • out: Pointer to store the created config
Returns: NULL on success, or OgaResult* containing error message on failure
function
Destroys a configuration object.Parameters:
  • config: The config to destroy
function
Overlays JSON configuration on top of the existing config.Parameters:
  • config: The config to modify
  • json: JSON string to overlay
Returns: NULL on success, or OgaResult* containing error message on failureExample:

Provider Configuration

function
Clears the list of execution providers in the configuration.Parameters:
  • config: The config to modify
Returns: NULL on success, or OgaResult* containing error message on failure
function
Adds a provider to the end of the provider list. If the provider already exists, this function does nothing.Parameters:
  • config: The config to modify
  • provider: The provider name to add (e.g., “CPU”, “CUDA”, “DML”)
Returns: NULL on success, or OgaResult* containing error message on failureExample:
function
Sets a provider-specific option.Parameters:
  • config: The config to modify
  • provider: The provider name
  • key: The option key
  • value: The option value
Returns: NULL on success, or OgaResult* containing error message on failure

Hardware Device Filtering

function
Filters execution provider devices by hardware device type (e.g., “CPU”, “GPU”, “NPU”).Parameters:
  • config: The config to modify
  • provider: The provider name
  • hardware_device_type: The hardware device type
Returns: NULL on success, or OgaResult* containing error message on failure
function
Filters execution provider devices by hardware device ID.Parameters:
  • config: The config to modify
  • provider: The provider name
  • hardware_device_id: The hardware device ID
Returns: NULL on success, or OgaResult* containing error message on failure
function
Filters execution provider devices by hardware vendor ID.Parameters:
  • config: The config to modify
  • provider: The provider name
  • hardware_vendor_id: The hardware vendor ID
Returns: NULL on success, or OgaResult* containing error message on failure
function
Clears the hardware device type filter.
function
Clears the hardware device ID filter.
function
Clears the hardware vendor ID filter.

Model Data from Memory

function
Adds model data to load the model from memory instead of from disk.Parameters:
  • config: The config to modify
  • model_filename: The name of the model file as defined in the config
  • model_data: Pointer to the model data (must remain valid until model is created)
  • model_data_length: Length of the model data in bytes
Returns: NULL on success, or OgaResult* containing error message on failureNote: The model data must remain valid at least until the model is created. If using session.use_ort_model_bytes_directly, the data must remain valid until the model is destroyed.
function
Removes previously added model data.Parameters:
  • config: The config to modify
  • model_filename: The name of the model file to remove
Returns: NULL on success, or OgaResult* containing error message on failure

Runtime Settings

function
Creates a runtime settings object.Parameters:
  • out: Pointer to store the created runtime settings
Returns: NULL on success, or OgaResult* containing error message on failure
function
Destroys a runtime settings object.Parameters:
  • settings: The runtime settings to destroy
function
Sets a runtime handle (e.g., device context) for the runtime settings.Parameters:
  • settings: The runtime settings to modify
  • handle_name: The name of the handle to set
  • handle: Pointer to the handle value
Returns: NULL on success, or OgaResult* containing error message on failure

Complete Example

See Also

C API Overview

Learn about memory management and error handling

Generator Functions

Generate text with your loaded model