Class: Legion::Extensions::Llm::Mlx::Provider

Inherits:
Provider
  • Object
show all
Includes:
CapabilityConfig, CapabilityResolution, HealthHelpers, Provider::OpenAICompatible
Defined in:
lib/legion/extensions/llm/mlx/provider.rb

Overview

MLX provider implementation for local OpenAI-compatible servers.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.capabilitiesObject



177
# File 'lib/legion/extensions/llm/mlx/provider.rb', line 177

def capabilities = Capabilities

.configuration_optionsObject



175
# File 'lib/legion/extensions/llm/mlx/provider.rb', line 175

def configuration_options = %i[mlx_api_base mlx_api_key]

.configuration_requirementsObject



176
# File 'lib/legion/extensions/llm/mlx/provider.rb', line 176

def configuration_requirements = []

.default_tierObject



174
# File 'lib/legion/extensions/llm/mlx/provider.rb', line 174

def default_tier = :local

.default_transportObject



173
# File 'lib/legion/extensions/llm/mlx/provider.rb', line 173

def default_transport = :http

.local?Boolean

Returns:

  • (Boolean)


172
# File 'lib/legion/extensions/llm/mlx/provider.rb', line 172

def local? = true

.slugObject



171
# File 'lib/legion/extensions/llm/mlx/provider.rb', line 171

def slug = 'mlx'

Instance Method Details

#api_baseObject



184
185
186
# File 'lib/legion/extensions/llm/mlx/provider.rb', line 184

def api_base
  normalize_url(config.mlx_api_base || settings[:instances][:default][:endpoint])
end

#headersObject



188
189
190
191
192
193
# File 'lib/legion/extensions/llm/mlx/provider.rb', line 188

def headers
  hdrs = identity_headers
  token = config.mlx_api_key
  hdrs['Authorization'] = "Bearer #{token}" unless token.nil? || token.to_s.empty?
  hdrs
end

#health(live: false) ⇒ Object



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/legion/extensions/llm/mlx/provider.rb', line 197

def health(live: false)
  log.info("Checking MLX health live=#{live} at #{api_base}#{health_url}")
  raw = connection.get(health_url).body
  health_payload(raw)
rescue StandardError => e
  handle_exception(e, level: :warn, handled: true, operation: 'mlx.provider.health')
  {
    provider: :mlx,
    instance_id: provider_instance_id,
    status: 'unhealthy',
    ready: false,
    circuit_state: 'open',
    error: e.class.name,
    message: e.message
  }
end

#health_urlObject



195
# File 'lib/legion/extensions/llm/mlx/provider.rb', line 195

def health_url = '/health'

#list_modelsObject



219
220
221
222
223
224
# File 'lib/legion/extensions/llm/mlx/provider.rb', line 219

def list_models(**)
  log.info('Listing available MLX models')
  models = super
  log.info("Discovered #{Array(models).size} MLX models")
  models
end

#offering_from_model(model_info, health: {}) ⇒ Object



226
227
228
229
230
231
# File 'lib/legion/extensions/llm/mlx/provider.rb', line 226

def offering_from_model(model_info, health: {})
  policy = resolve_capability_policy(model_info)
  Legion::Extensions::Llm::Routing::ModelOffering.new(**build_offering_kwargs(
    model_info: model_info, policy: policy, health: health
  ))
end

#readiness(live: false) ⇒ Object



214
215
216
217
# File 'lib/legion/extensions/llm/mlx/provider.rb', line 214

def readiness(live: false)
  log.info("Checking MLX readiness (live=#{live})")
  super
end

#settingsObject



180
181
182
# File 'lib/legion/extensions/llm/mlx/provider.rb', line 180

def settings
  Mlx.default_settings
end