Class: Legion::Extensions::Llm::Vllm::VllmCallable

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

Overview

Callable wrapper for a vLLM provider instance. Implements the disconnect and normalize_dispatch_error(error:) contracts required by Inventory::CallableHandle and Routing::ProviderOutcome, plus the fleet dispatch operations the coordinator invokes (chat, stream_chat, embed, count_tokens). Each dispatch delegates to a per-instance Vllm::Provider built lazily from the instance config; 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:) ⇒ VllmCallable

Returns a new instance of VllmCallable.



25
26
27
28
29
30
# File 'lib/legion/extensions/llm/vllm/callable.rb', line 25

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

Instance Method Details

#call_countObject



32
33
34
# File 'lib/legion/extensions/llm/vllm/callable.rb', line 32

def call_count
  @inference_calls
end

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

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



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

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

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



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

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



40
41
42
43
44
# File 'lib/legion/extensions/llm/vllm/callable.rb', line 40

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

#disconnected?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/legion/extensions/llm/vllm/callable.rb', line 36

def disconnected?
  @disconnected
end

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



60
61
62
63
64
# File 'lib/legion/extensions/llm/vllm/callable.rb', line 60

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



72
73
74
75
76
77
78
# File 'lib/legion/extensions/llm/vllm/callable.rb', line 72

def normalize_dispatch_error(error:)
  reason = error.message.to_s[0, 512]
  kind = classify_dispatch_error(error: error)
  Legion::Extensions::Llm::Routing::ProviderOutcome.new(
    kind: kind, reason: reason.empty? ? 'unknown dispatch error' : reason
  )
end

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



54
55
56
57
58
# File 'lib/legion/extensions/llm/vllm/callable.rb', line 54

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