Class: Legion::Extensions::Llm::Mlx::Actor::MlxCallable

Inherits:
Object
  • Object
show all
Defined in:
lib/legion/extensions/llm/mlx/actors/discovery_refresh.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 coordinator's normalize_dispatch_error can classify them.

Constant Summary collapse

COMPLETION_NAMED_KEYS =

Keys the base Provider exposes as named kwargs for the completion operations. Anything else the fleet passes is folded into the payload params hash.

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

Instance Method Summary collapse

Constructor Details

#initialize(instance_cfg:, logger:) ⇒ MlxCallable

Returns a new instance of MlxCallable.



890
891
892
893
894
895
# File 'lib/legion/extensions/llm/mlx/actors/discovery_refresh.rb', line 890

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

Instance Method Details

#call_countObject



897
898
899
# File 'lib/legion/extensions/llm/mlx/actors/discovery_refresh.rb', line 897

def call_count
  @inference_calls
end

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

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



913
914
915
916
917
# File 'lib/legion/extensions/llm/mlx/actors/discovery_refresh.rb', line 913

def chat(messages:, model:, **rest)
  record_inference
  named, params = split_fleet_kwargs(rest, COMPLETION_NAMED_KEYS)
  provider.chat(messages: messages, model: model_info(model), params: params, **named)
end

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



931
932
933
934
935
# File 'lib/legion/extensions/llm/mlx/actors/discovery_refresh.rb', line 931

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

#disconnectObject



905
906
907
908
909
# File 'lib/legion/extensions/llm/mlx/actors/discovery_refresh.rb', line 905

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

#disconnected?Boolean

Returns:

  • (Boolean)


901
902
903
# File 'lib/legion/extensions/llm/mlx/actors/discovery_refresh.rb', line 901

def disconnected?
  @disconnected
end

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



925
926
927
928
929
# File 'lib/legion/extensions/llm/mlx/actors/discovery_refresh.rb', line 925

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

#normalize_dispatch_error(error:) ⇒ Object



937
938
939
940
941
942
943
944
945
# File 'lib/legion/extensions/llm/mlx/actors/discovery_refresh.rb', line 937

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



919
920
921
922
923
# File 'lib/legion/extensions/llm/mlx/actors/discovery_refresh.rb', line 919

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