Class: Legion::Extensions::Llm::Mlx::Helpers::Callable

Inherits:
Object
  • Object
show all
Defined in:
lib/legion/extensions/llm/mlx/helpers/callable.rb

Overview

Callable wrapper for an MLX provider instance. It is the exact-execution dispatch target: it implements the fleet dispatch operations (chat, stream_chat, embed, count_tokens) by delegating to a per-instance Mlx::Provider built from the instance config, plus the disconnect and normalize_dispatch_error(error:) contracts required by Inventory::CallableHandle and Routing::ProviderOutcome. Provider and Faraday errors are NOT rescued here so the dispatch normalizer can classify them. 0.8.0 callable contract: chat/stream_chat take the rehydrated message array positionally (WorkerExecution dispatch shape) and the Selection-derived model as a bare String.

Constant Summary collapse

COMPLETION_NAMED_KEYS =

Keys the base Provider exposes as named kwargs for the completion operations. Anything else the fleet passes (sampling scalars, temperature — a Canonical::Params member, 05 O4) is folded into Canonical::Params at the dispatch boundary.

%i[tools schema thinking tool_prefs headers].freeze
EMBED_NAMED_KEYS =
%i[dimensions headers].freeze

Instance Method Summary collapse

Constructor Details

#initialize(instance_cfg:, logger:) ⇒ Callable

Returns a new instance of Callable.



33
34
35
36
37
38
# File 'lib/legion/extensions/llm/mlx/helpers/callable.rb', line 33

def initialize(instance_cfg:, logger:)
  @instance_cfg = instance_cfg
  @logger = logger
  @disconnected = false
  @inference_calls = 0
end

Instance Method Details

#call_countObject



40
41
42
# File 'lib/legion/extensions/llm/mlx/helpers/callable.rb', line 40

def call_count
  @inference_calls
end

#chat(messages, model:, **rest) ⇒ Object

── Fleet dispatch operations ───────────────────────────────────



56
57
58
59
60
61
62
63
64
# File 'lib/legion/extensions/llm/mlx/helpers/callable.rb', line 56

def chat(messages, model:, **rest)
  record_inference
  # Canonical boundary (N x N law): pipeline dispatch delivers
  # Canonical::Message objects only. Hash/legacy shapes are the
  # bypass class — reject loudly, never coerce.
  provider.enforce_canonical_messages!(messages)
  named, params = split_fleet_kwargs(rest, COMPLETION_NAMED_KEYS)
  provider.chat(messages, model: model, params: canonical_params(params), **named)
end

#count_tokens(messages:, model:, **rest) ⇒ Object



79
80
81
82
83
84
# File 'lib/legion/extensions/llm/mlx/helpers/callable.rb', line 79

def count_tokens(messages:, model:, **rest)
  record_inference
  provider.enforce_canonical_messages!(messages)
  _named, params = split_fleet_kwargs(rest, [])
  provider.count_tokens(messages: messages, model: model, params: params)
end

#disconnectObject



48
49
50
51
52
# File 'lib/legion/extensions/llm/mlx/helpers/callable.rb', line 48

def disconnect
  @disconnected = true
  @provider&.disconnect
  @logger.debug { '[mlx][callable] disconnected' }
end

#disconnected?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/legion/extensions/llm/mlx/helpers/callable.rb', line 44

def disconnected?
  @disconnected
end

#embed(text:, model:, **rest) ⇒ Object



73
74
75
76
77
# File 'lib/legion/extensions/llm/mlx/helpers/callable.rb', line 73

def embed(text:, model:, **rest)
  record_inference
  named, params = split_fleet_kwargs(rest, EMBED_NAMED_KEYS)
  provider.embed(text: text, model: model, params: params, **named)
end

#normalize_dispatch_error(error:) ⇒ Object



86
87
88
89
90
91
92
93
94
# File 'lib/legion/extensions/llm/mlx/helpers/callable.rb', line 86

def normalize_dispatch_error(error:)
  reason = error.message.to_s[0, 512]
  kind = classify_error_kind(error: error)

  Legion::Extensions::Llm::Routing::ProviderOutcome.new(
    kind: kind,
    reason: reason.empty? ? 'unknown dispatch error' : reason
  )
end

#stream_chat(messages, model:, **rest) ⇒ Object



66
67
68
69
70
71
# File 'lib/legion/extensions/llm/mlx/helpers/callable.rb', line 66

def stream_chat(messages, model:, **rest, &)
  record_inference
  provider.enforce_canonical_messages!(messages)
  named, params = split_fleet_kwargs(rest, COMPLETION_NAMED_KEYS)
  provider.stream_chat(messages, model: model, params: canonical_params(params), **named, &)
end