Module: Legion::LLM::Fleet::Dispatcher
- Extended by:
- Legion::Logging::Helper
- Defined in:
- lib/legion/llm/fleet/dispatcher.rb
Constant Summary collapse
- DEFAULT_TIMEOUT =
30- TIMEOUTS =
{ embed: 10, chat: 30, generate: 30, default: 30 }.freeze
Class Method Summary collapse
- .build_routing_key(provider:, request_type:, model:) ⇒ Object
-
.dispatch(model: nil, messages: nil, request: nil, message_context: {}, routing_key: nil, reply_to: nil, **opts) ⇒ Object
Backwards-compatible shim: supports old (model:, messages:) and new (request:, message_context:) callers.
- .error_result(reason, message_context: {}) ⇒ Object
- .fleet_available? ⇒ Boolean
- .fleet_enabled? ⇒ Boolean
- .fleet_timeout_from_settings(request_type) ⇒ Object
- .publish_request(**opts) ⇒ Object
- .resolve_timeout(request_type: :default, override: nil) ⇒ Object
- .sanitize_model(model) ⇒ Object
- .timeout_result(correlation_id, timeout, message_context: {}) ⇒ Object
- .transport_ready? ⇒ Boolean
- .wait_for_response(correlation_id, timeout:, message_context: {}) ⇒ Object
Class Method Details
.build_routing_key(provider:, request_type:, model:) ⇒ Object
64 65 66 |
# File 'lib/legion/llm/fleet/dispatcher.rb', line 64 def build_routing_key(provider:, request_type:, model:) "llm.request.#{provider}.#{request_type}.#{sanitize_model(model)}" end |
.dispatch(model: nil, messages: nil, request: nil, message_context: {}, routing_key: nil, reply_to: nil, **opts) ⇒ Object
Backwards-compatible shim: supports old (model:, messages:) and new (request:, message_context:) callers
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/legion/llm/fleet/dispatcher.rb', line 23 def dispatch(model: nil, messages: nil, request: nil, message_context: {}, routing_key: nil, reply_to: nil, **opts) return error_result('fleet_unavailable', message_context: ) unless fleet_available? # Old calling convention: build minimal params from model/messages if request.nil? && (model || ) provider = opts[:provider] || 'ollama' request_type = opts[:request_type] || 'chat' routing_key ||= build_routing_key(provider: provider, request_type: request_type, model: model) reply_to ||= ReplyDispatcher.agent_queue_name correlation_id = publish_request( routing_key: routing_key, reply_to: reply_to, provider: provider, model: model, request_type: request_type, messages: , message_context: , **opts ) timeout = resolve_timeout(request_type: request_type, override: opts[:timeout]) return wait_for_response(correlation_id, timeout: timeout, message_context: ) end # New calling convention request_opts = if request.respond_to?(:to_h) request.to_h.transform_keys(&:to_sym) else {} end request_opts = request_opts.merge(opts) provider = request_opts[:provider] || 'ollama' request_type = request_opts[:request_type] || 'chat' model = request_opts[:model] routing_key ||= build_routing_key(provider: provider, request_type: request_type, model: model) reply_to ||= ReplyDispatcher.agent_queue_name correlation_id = publish_request( routing_key: routing_key, reply_to: reply_to, provider: provider, model: model, request_type: request_type, message_context: , **request_opts.except(:provider, :model, :request_type, :timeout) ) timeout = resolve_timeout(request_type: request_type, override: request_opts[:timeout] || opts[:timeout]) wait_for_response(correlation_id, timeout: timeout, message_context: ) end |
.error_result(reason, message_context: {}) ⇒ Object
159 160 161 |
# File 'lib/legion/llm/fleet/dispatcher.rb', line 159 def error_result(reason, message_context: {}) { success: false, error: reason, message_context: } end |
.fleet_available? ⇒ Boolean
72 73 74 |
# File 'lib/legion/llm/fleet/dispatcher.rb', line 72 def fleet_available? transport_ready? && fleet_enabled? end |
.fleet_enabled? ⇒ Boolean
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/legion/llm/fleet/dispatcher.rb', line 81 def fleet_enabled? return true unless defined?(Legion::Settings) settings = begin Legion::Settings[:llm] rescue StandardError => e handle_exception(e, level: :debug, operation: 'llm.fleet.dispatcher.fleet_enabled') nil end return true unless settings.is_a?(Hash) routing = settings[:routing] return true unless routing.is_a?(Hash) routing.fetch(:use_fleet, true) end |
.fleet_timeout_from_settings(request_type) ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/legion/llm/fleet/dispatcher.rb', line 107 def fleet_timeout_from_settings(request_type) return unless defined?(Legion::Settings) settings = begin Legion::Settings[:llm] rescue StandardError => e handle_exception(e, level: :debug, operation: 'llm.fleet.dispatcher.resolve_timeout') nil end return unless settings.is_a?(Hash) routing = settings[:routing] return unless routing.is_a?(Hash) fleet_settings = routing.dig(:tiers, :fleet) fleet_settings = routing[:fleet] unless fleet_settings.is_a?(Hash) return unless fleet_settings.is_a?(Hash) fleet_settings.dig(:timeouts, request_type.to_sym) || fleet_settings[:timeout_seconds] end |
.publish_request(**opts) ⇒ Object
129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/legion/llm/fleet/dispatcher.rb', line 129 def publish_request(**opts) correlation_id = "req_#{SecureRandom.uuid}" opts[:fleet_correlation_id] = correlation_id if defined?(Legion::LLM::Fleet::Request) Legion::LLM::Fleet::Request.new(**opts).publish elsif defined?(Legion::Extensions::LLM::Gateway::Transport::Messages::InferenceRequest) Legion::Extensions::LLM::Gateway::Transport::Messages::InferenceRequest.new( reply_to: opts[:reply_to], **opts.except(:reply_to) ).publish end correlation_id end |
.resolve_timeout(request_type: :default, override: nil) ⇒ Object
98 99 100 101 102 103 104 105 |
# File 'lib/legion/llm/fleet/dispatcher.rb', line 98 def resolve_timeout(request_type: :default, override: nil) return override if override configured = fleet_timeout_from_settings(request_type) return configured if configured TIMEOUTS[request_type.to_sym] || TIMEOUTS[:default] end |
.sanitize_model(model) ⇒ Object
68 69 70 |
# File 'lib/legion/llm/fleet/dispatcher.rb', line 68 def sanitize_model(model) model.to_s.gsub(':', '.') end |
.timeout_result(correlation_id, timeout, message_context: {}) ⇒ Object
154 155 156 157 |
# File 'lib/legion/llm/fleet/dispatcher.rb', line 154 def timeout_result(correlation_id, timeout, message_context: {}) { success: false, error: 'fleet_timeout', correlation_id: correlation_id, timeout: timeout, message_context: } end |
.transport_ready? ⇒ Boolean
76 77 78 79 |
# File 'lib/legion/llm/fleet/dispatcher.rb', line 76 def transport_ready? !!(defined?(Legion::Settings) && Legion::Settings[:transport][:connected] == true) end |
.wait_for_response(correlation_id, timeout:, message_context: {}) ⇒ Object
144 145 146 147 148 149 150 151 152 |
# File 'lib/legion/llm/fleet/dispatcher.rb', line 144 def wait_for_response(correlation_id, timeout:, message_context: {}) future = ReplyDispatcher.register(correlation_id) result = future.value!(timeout) result || timeout_result(correlation_id, timeout, message_context: ) rescue Concurrent::CancelledOperationError timeout_result(correlation_id, timeout, message_context: ) ensure ReplyDispatcher.deregister(correlation_id) end |