Class: Legion::Extensions::Llm::Openai::Helpers::Callable

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

Overview

Callable wrapper for an OpenAI provider instance. Implements the fleet dispatch operations by delegating to the per-instance Openai::Provider (errors propagate so normalize_dispatch_error can classify them), plus the disconnect and normalize_dispatch_error (error:) contracts required by Inventory::CallableHandle and Routing::ProviderOutcome.

Defined in its own file so the actor's runtime guard (the Every base is absent in a standalone load) does not prevent specs from loading it.

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:, provider: nil) ⇒ Callable

Returns a new instance of Callable.



28
29
30
31
32
33
# File 'lib/legion/extensions/llm/openai/helpers/callable.rb', line 28

def initialize(instance_cfg:, logger:, provider: nil)
  @instance_cfg = instance_cfg
  @logger = logger
  @injected_provider = provider
  @disconnected = false
end

Instance Method Details

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

-- Fleet dispatch operations ---------------------------------------- The fleet passes model: as the offering's raw model id (String). B4: the callable hands it to the wire unchanged — no wrapping, no default, no fallback. The 0.8.0 funnel renders model: verbatim into the payload, so a wrapped value would corrupt the request.



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

def chat(messages, model:, **rest)
  # 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



75
76
77
78
79
# File 'lib/legion/extensions/llm/openai/helpers/callable.rb', line 75

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

#disconnectObject



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

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

#disconnected?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/legion/extensions/llm/openai/helpers/callable.rb', line 35

def disconnected?
  @disconnected
end

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



70
71
72
73
# File 'lib/legion/extensions/llm/openai/helpers/callable.rb', line 70

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

#image(prompt:, model:, **rest) ⇒ Object



81
82
83
# File 'lib/legion/extensions/llm/openai/helpers/callable.rb', line 81

def image(prompt:, model:, **rest)
  provider.image(prompt: prompt, model: model, **rest)
end

#moderate(input:, model:, **rest) ⇒ Object



85
86
87
# File 'lib/legion/extensions/llm/openai/helpers/callable.rb', line 85

def moderate(input:, model:, **rest)
  provider.moderate(input: input, model: model, **rest)
end

#normalize_dispatch_error(error:) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/legion/extensions/llm/openai/helpers/callable.rb', line 89

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

  kind = case error
         when Faraday::ConnectionFailed
           :connection_failure
         when Faraday::TimeoutError
           :timeout
         when Faraday::ClientError
           classify_client_error(error: error)
         when Faraday::ServerError
           classify_server_error(error: error)
         when Legion::Extensions::Llm::OverloadedError
           :overloaded
         else
           # ServiceUnavailableError and all other error types map to
           # :provider_error. OpenAI does not produce a distinct flat
           # instance-down signal separate from overload; only an
           # authoritative explicit instance_unavailable condition
           # (which OpenAI does not emit via normal dispatch) would
           # map to :instance_unavailable.
           :provider_error
         end

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

#providerObject



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

def provider
  @provider ||= @injected_provider || Legion::Extensions::Llm::Openai::Provider.new(@instance_cfg)
end

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



64
65
66
67
68
# File 'lib/legion/extensions/llm/openai/helpers/callable.rb', line 64

def stream_chat(messages, model:, **rest, &)
  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