Class: Legion::Extensions::Llm::Vertex::Helpers::Callable
- Inherits:
-
Object
- Object
- Legion::Extensions::Llm::Vertex::Helpers::Callable
- Defined in:
- lib/legion/extensions/llm/vertex/helpers/callable.rb
Overview
Callable wrapper for a Vertex AI provider instance. Delegates the
fleet dispatch operations to a per-instance Vertex::Provider (real
HTTP dispatch; provider/Faraday errors propagate so
normalize_dispatch_error classifies them) and implements the
disconnect and normalize_dispatch_error(error:) contracts
required by Inventory::CallableHandle and Routing::ProviderOutcome.
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; folded wire params
become Canonical::Params at this boundary (05 O4).
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 =
Named kwargs of Vertex's own embed dialect (task_type/title are Vertex wire spellings); anything else folds into params.
%i[dimensions task_type title headers].freeze
Instance Attribute Summary collapse
-
#provider ⇒ Object
readonly
Returns the value of attribute provider.
Instance Method Summary collapse
-
#chat(messages, model:, **rest) ⇒ Object
--- Fleet dispatch operations (Fleet::WorkerExecution contract) --.
- #count_tokens(messages:, model:, **rest) ⇒ Object
- #disconnect ⇒ Object
- #disconnected? ⇒ Boolean
- #embed(text:, model:, **rest) ⇒ Object
-
#initialize(instance_cfg:, logger:) ⇒ Callable
constructor
A new instance of Callable.
-
#normalize_dispatch_error(error:) ⇒ Object
--- Error normalization ------------------------------------------.
- #stream_chat(messages, model:, **rest) ⇒ Object
Constructor Details
#initialize(instance_cfg:, logger:) ⇒ Callable
Returns a new instance of Callable.
34 35 36 37 38 39 |
# File 'lib/legion/extensions/llm/vertex/helpers/callable.rb', line 34 def initialize(instance_cfg:, logger:) @instance_cfg = instance_cfg @logger = logger @provider = Provider.new(instance_cfg) @disconnected = false end |
Instance Attribute Details
#provider ⇒ Object (readonly)
Returns the value of attribute provider.
32 33 34 |
# File 'lib/legion/extensions/llm/vertex/helpers/callable.rb', line 32 def provider @provider end |
Instance Method Details
#chat(messages, model:, **rest) ⇒ Object
--- Fleet dispatch operations (Fleet::WorkerExecution contract) --
53 54 55 56 57 58 59 60 |
# File 'lib/legion/extensions/llm/vertex/helpers/callable.rb', line 53 def chat(, 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.() named, params = split_fleet_kwargs(rest, COMPLETION_NAMED_KEYS) provider.chat(, model: model, params: canonical_params(params), **named) end |
#count_tokens(messages:, model:, **rest) ⇒ Object
73 74 75 76 77 |
# File 'lib/legion/extensions/llm/vertex/helpers/callable.rb', line 73 def count_tokens(messages:, model:, **rest) provider.() _named, params = split_fleet_kwargs(rest, []) provider.count_tokens(messages: , model: model, params: params) end |
#disconnect ⇒ Object
45 46 47 48 49 |
# File 'lib/legion/extensions/llm/vertex/helpers/callable.rb', line 45 def disconnect @disconnected = true @provider.disconnect @logger.debug { '[vertex][callable] disconnected' } end |
#disconnected? ⇒ Boolean
41 42 43 |
# File 'lib/legion/extensions/llm/vertex/helpers/callable.rb', line 41 def disconnected? @disconnected end |
#embed(text:, model:, **rest) ⇒ Object
68 69 70 71 |
# File 'lib/legion/extensions/llm/vertex/helpers/callable.rb', line 68 def (text:, model:, **rest) named, params = split_fleet_kwargs(rest, EMBED_NAMED_KEYS) provider.(text: text, model: model, params: params, **named) end |
#normalize_dispatch_error(error:) ⇒ Object
--- Error normalization ------------------------------------------
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/legion/extensions/llm/vertex/helpers/callable.rb', line 81 def normalize_dispatch_error(error:) reason = error..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 errors map to provider_error. # Never escalate to instance_unavailable from a typed error alone. :provider_error end Legion::Extensions::Llm::Routing::ProviderOutcome.new( kind: kind, reason: reason.empty? ? 'unknown dispatch error' : reason ) end |
#stream_chat(messages, model:, **rest) ⇒ Object
62 63 64 65 66 |
# File 'lib/legion/extensions/llm/vertex/helpers/callable.rb', line 62 def stream_chat(, model:, **rest, &) provider.() named, params = split_fleet_kwargs(rest, COMPLETION_NAMED_KEYS) provider.stream_chat(, model: model, params: canonical_params(params), **named, &) end |