Models

RxInferServer provides a flexible system for loading, managing, and exposing RxInfer probabilistic models through the API. This section explains the technical implementation details of how models work in the server, including the model dispatcher, loading process, and API integration.

For information about how to create and add new models, please refer to the How to Add a Model manual.

Model System Overview

The model system in RxInferServer consists of several key components:

Model Discovery and Loading

The server discovers and loads models at startup using this process:

  1. It scans all directories specified in RxInferServer.Models.RXINFER_SERVER_MODELS_LOCATIONS
  2. For each directory, it looks for subdirectories that might contain models
  3. In each subdirectory, it checks for model.jl and config.yaml files
  4. If both files exist, it loads the model's configuration and code
  5. The model is added to the server's model registry if successful

Models are accessed through a RxInferServer.Models.ModelsDispatcher which provides methods to retrieve models by name or list all available (non-private) models.

Hot-Reloading System

RxInferServer supports hot-reloading of models during development. When model files are modified:

  1. The server detects the changes automatically
  2. It reloads all models from their directories
  3. The updated models become immediately available through the API

This feature is enabled by default during development and can be disabled through the server's configuration. See Hot-Reloading for more details.

API Integration

Models are exposed through the API endpoints defined in the OpenAPI specification. When a client requests model information or executes a model, the server:

  1. Looks up the requested model by name using the dispatcher
  2. If found, returns the model's metadata
  3. Returns appropriate error responses if the model is not found or other issues occur

API Reference

RxInferServer.ModelsModule
Models

Module responsible for loading, managing, and accessing RxInfer probabilistic models in the server. Handles model discovery, loading, and provides access to models through a dispatcher.

source
RxInferServer.Models.ModelsDispatcherType
ModelsDispatcher

A dispatcher that manages loaded models from specified locations. Responsible for model discovery, loading, and providing access to models.

Fields

  • locations::L: The locations where models are stored
  • models::M: Dictionary mapping model names to loaded model instances
source
RxInferServer.Models.LoadedModelType
LoadedModel

Represents a loaded RxInfer probabilistic model with its metadata and implementation.

Fields

  • path::String: Path to the model directory
  • name::String: Name of the model
  • description::String: Description of the model's purpose and functionality
  • author::String: Author or organization that created the model
  • roles::Vector{String}: List of roles that can access the model
  • config::Dict{String, Any}: Configuration parameters for the model
  • mod::Module: Julia module containing the model's implementation
source
RxInferServer.Models.get_modelsFunction
get_models(dispatcher::ModelsDispatcher; role = nothing)

Get all non-private models from the given dispatcher.

Arguments

  • dispatcher::ModelsDispatcher: The dispatcher to get models from
  • roles::Union{Vector{String}, Nothing}: The roles to filter models by (optional)

Returns

  • A collection of all non-private loaded models
source
get_models()

Get all non-private models using the current dispatcher.

Returns

  • A collection of all non-private loaded models
source
RxInferServer.Models.get_modelFunction
get_model(dispatcher::ModelsDispatcher, model_name::String)

Get a specific model by name from the dispatcher.

Arguments

  • dispatcher::ModelsDispatcher: The dispatcher to get the model from
  • model_name::String: The name of the model to retrieve

Returns

  • LoadedModel or nothing: The requested model if found, otherwise nothing
source
get_model(model_name::String)

Get a specific model by name from the current dispatcher.

Arguments

  • model_name::String: The name of the model to retrieve

Returns

  • LoadedModel or nothing: The requested model if found, otherwise nothing
source
RxInferServer.Models.load_models!Function
load_models!(models, locations)

Load models from the specified locations into the models dictionary.

Arguments

  • models: Dictionary to populate with loaded models (name => LoadedModel)
  • locations: List of directories to scan for models

Throws

  • ErrorException: If a location does not exist or if duplicate model names are found
source
RxInferServer.Models.reload!Function
reload!(dispatcher::ModelsDispatcher)

Reload all models from the dispatcher's locations, updating the dispatcher's models. Used for hot-reloading models when their files change.

Arguments

  • dispatcher::ModelsDispatcher: The dispatcher to reload models for
Warning

This function completely replaces the models dictionary with newly loaded models, allowing for model updates, additions, and removals to be recognized. Indented for interactive use only.

source
RxInferServer.Models.with_modelsFunction
with_models(f::Function; locations = RXINFER_SERVER_MODELS_LOCATIONS())

Execute function f with an initialized models dispatcher for the given locations. Creates a scoped context where models can be accessed via the dispatcher.

Arguments

  • f::Function: The function to execute within the models context
  • locations: The locations to scan for models, defaults to RXINFER_SERVER_MODELS_LOCATIONS()
source
RxInferServer.Models.get_models_dispatcherFunction
get_models_dispatcher()::ModelsDispatcher

Get the current active models dispatcher. Must be called within a with_models context.

Returns

  • ModelsDispatcher: The active models dispatcher

Throws

  • ErrorException: If called outside of a with_models context
source
RxInferServer.Models.validate_model_config_headerFunction
validate_model_config_header(config)

Validate the model config header. This includes checking that the config satisfies the model config schema. The function checks the existence of the following named keys:

  • name must be a string
  • description must be a string
  • author must be a string
  • roles must be an array of strings

Arguments

  • config: The model configuration

Returns

source
RxInferServer.Models.validate_model_config_argumentsFunction
validate_model_config_required_arguments(config, arguments)

Validate the arguments from the model configuration. This includes checking that

  • the required arguments are present

Arguments

  • config: The model configuration
  • arguments: The arguments to validate
source