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:
RxInferServer.Models.ModelsDispatcher
: Manages model discovery, loading, and accessRxInferServer.Models.LoadedModel
: Represents a loaded model with its configuration and implementation- Model registry: Maintains a collection of available models
- Hot-reloading system: Enables dynamic model updates during development
Model Discovery and Loading
The server discovers and loads models at startup using this process:
- It scans all directories specified in
RxInferServer.Models.RXINFER_SERVER_MODELS_LOCATIONS
- For each directory, it looks for subdirectories that might contain models
- In each subdirectory, it checks for
model.jl
andconfig.yaml
files - If both files exist, it loads the model's configuration and code
- 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:
- The server detects the changes automatically
- It reloads all models from their directories
- 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:
- Looks up the requested model by name using the dispatcher
- If found, returns the model's metadata
- Returns appropriate error responses if the model is not found or other issues occur
API Reference
RxInferServer.Models
— ModuleModels
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.
RxInferServer.Models.ModelsDispatcher
— TypeModelsDispatcher
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 storedmodels::M
: Dictionary mapping model names to loaded model instances
RxInferServer.Models.LoadedModel
— TypeLoadedModel
Represents a loaded RxInfer probabilistic model with its metadata and implementation.
Fields
path::String
: Path to the model directoryname::String
: Name of the modeldescription::String
: Description of the model's purpose and functionalityauthor::String
: Author or organization that created the modelroles::Vector{String}
: List of roles that can access the modelconfig::Dict{String, Any}
: Configuration parameters for the modelmod::Module
: Julia module containing the model's implementation
RxInferServer.Models.get_models
— Functionget_models(dispatcher::ModelsDispatcher; role = nothing)
Get all non-private models from the given dispatcher.
Arguments
dispatcher::ModelsDispatcher
: The dispatcher to get models fromroles::Union{Vector{String}, Nothing}
: The roles to filter models by (optional)
Returns
- A collection of all non-private loaded models
get_models()
Get all non-private models using the current dispatcher.
Returns
- A collection of all non-private loaded models
RxInferServer.Models.get_model
— Functionget_model(dispatcher::ModelsDispatcher, model_name::String)
Get a specific model by name from the dispatcher.
Arguments
dispatcher::ModelsDispatcher
: The dispatcher to get the model frommodel_name::String
: The name of the model to retrieve
Returns
LoadedModel
ornothing
: The requested model if found, otherwisenothing
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
ornothing
: The requested model if found, otherwisenothing
RxInferServer.Models.load_models!
— Functionload_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
RxInferServer.Models.reload!
— Functionreload!(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
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.
RxInferServer.Models.with_models
— Functionwith_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 contextlocations
: The locations to scan for models, defaults toRXINFER_SERVER_MODELS_LOCATIONS()
RxInferServer.Models.get_models_dispatcher
— Functionget_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 awith_models
context
RxInferServer.Models.serialize_parameters
— Functionserialize_parameters(parameters)
Serialize the given parameters to an opaque binary format.
RxInferServer.Models.serialize_state
— Functionserialize_state(state)
Serialize the given state to an opaque binary format.
RxInferServer.Models.deserialize_state
— Functiondeserialize_state(state_buffer)
Deserialize the given state from an opaque binary format.
RxInferServer.Models.deserialize_parameters
— Functiondeserialize_parameters(parameters_buffer)
Deserialize the given parameters from an opaque binary format.
RxInferServer.Models.validate_model_config_header
— Functionvalidate_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 stringdescription
must be a stringauthor
must be a stringroles
must be an array of strings
Arguments
config
: The model configuration
Returns
nothing
: If the model configuration is validRxInferServer.Models.ModelConfigurationValidationError
: If the model configuration is invalid
RxInferServer.Models.validate_model_config_arguments
— Functionvalidate_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 configurationarguments
: The arguments to validate
RxInferServer.Models.parse_model_config_default_arguments
— Functionparse_model_config_default_arguments(config)
Parse the default arguments from the model configuration.
Arguments
config
: The model configuration
Returns
Dict{String, Any}
: The default arguments
RxInferServer.Models.ModelConfigurationValidationError
— TypeModelConfigurationValidationError
A custom error type for model configuration validation errors.