Module: Legion::Extensions::Ollama
- Extended by:
- Core
- Defined in:
- lib/legion/extensions/ollama.rb,
lib/legion/extensions/ollama/client.rb,
lib/legion/extensions/ollama/version.rb,
lib/legion/extensions/ollama/transport.rb,
lib/legion/extensions/ollama/runners/chat.rb,
lib/legion/extensions/ollama/helpers/usage.rb,
lib/legion/extensions/ollama/runners/blobs.rb,
lib/legion/extensions/ollama/runners/fleet.rb,
lib/legion/extensions/ollama/helpers/client.rb,
lib/legion/extensions/ollama/helpers/errors.rb,
lib/legion/extensions/ollama/runners/models.rb,
lib/legion/extensions/ollama/runners/version.rb,
lib/legion/extensions/ollama/actors/model_sync.rb,
lib/legion/extensions/ollama/runners/s3_models.rb,
lib/legion/extensions/ollama/runners/embeddings.rb,
lib/legion/extensions/ollama/actors/model_worker.rb,
lib/legion/extensions/ollama/runners/completions.rb,
lib/legion/extensions/ollama/actors/endpoint_puller.rb,
lib/legion/extensions/ollama/transport/exchanges/llm_request.rb,
lib/legion/extensions/ollama/transport/messages/llm_response.rb,
lib/legion/extensions/ollama/transport/exchanges/llm_registry.rb,
lib/legion/extensions/ollama/transport/messages/registry_event.rb
Defined Under Namespace
Modules: Actor, Helpers, Runners, Transport Classes: Client
Constant Summary collapse
- VERSION =
'0.3.10'
Class Method Summary collapse
- .actor_suffix(value) ⇒ Object
-
.build_actors ⇒ Object
Called by the framework during autobuild.
- .context_window_for(subscription) ⇒ Object
- .default_settings ⇒ Object
- .fleet_offering_lane_setting(key) ⇒ Object
- .fleet_scheduler ⇒ Object
- .model_worker_actor_name(request_type:, model:, lane_style:, offering_instance_id:) ⇒ Object
- .nested_setting(hash, *keys) ⇒ Object
- .offering_instance_for(subscription) ⇒ Object
- .offering_lanes_enabled? ⇒ Boolean
- .register_model_worker(request_type:, model:, context_window:, lane_style: :shared, offering_instance_id: nil) ⇒ Object
- .setting_value(hash, key) ⇒ Object
- .sorted_subscriptions(subscriptions) ⇒ Object
- .valid_fleet_subscriptions(subscriptions) ⇒ Object
Class Method Details
.actor_suffix(value) ⇒ Object
187 188 189 |
# File 'lib/legion/extensions/ollama.rb', line 187 def self.actor_suffix(value) value.to_s.downcase.gsub(/[^a-z0-9]+/, '_').gsub(/\A_+|_+\z/, '') end |
.build_actors ⇒ Object
Called by the framework during autobuild. Runs normal actor discovery, then replaces the single ModelWorker entry with one concrete subclass per subscription entry in settings (each has a zero-arg initialize).
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/legion/extensions/ollama.rb', line 70 def self.build_actors super @actors.delete(:model_worker) subs = setting_value(settings, :subscriptions) valid_subscriptions = valid_fleet_subscriptions(subs) endpoint_configured = fleet_scheduler == :basic_get && nested_setting(settings, :fleet, :endpoint, :enabled) == true && valid_subscriptions.any? @actors.delete(:endpoint_puller) unless endpoint_configured return unless subs.is_a?(Array) return if fleet_scheduler == :basic_get sorted_subscriptions(subs).each do |sub| request_type = setting_value(sub, :type)&.to_s model = setting_value(sub, :model)&.to_s context_window = context_window_for(sub) next unless request_type && model register_model_worker(request_type: request_type, model: model, context_window: context_window) offering_instance_id = offering_instance_for(sub) next unless offering_instance_id register_model_worker(request_type: request_type, model: model, context_window: context_window, lane_style: :offering, offering_instance_id: offering_instance_id) end end |
.context_window_for(subscription) ⇒ Object
111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/legion/extensions/ollama.rb', line 111 def self.context_window_for(subscription) limits = setting_value(subscription, :limits) || {} raw = setting_value(subscription, :context_window) || setting_value(subscription, :max_context) || setting_value(subscription, :max_input_tokens) || setting_value(limits, :context_window) || setting_value(limits, :max_input_tokens) return nil if raw.nil? || raw.to_s.empty? Integer(raw) rescue ArgumentError, TypeError nil end |
.default_settings ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/legion/extensions/ollama.rb', line 41 def self.default_settings { s3: {}, fleet: { consumer_priority: 0, scheduler: :basic_get, queue_expires_ms: 60_000, message_ttl_ms: 120_000, queue_max_length: 100, delivery_limit: 3, consumer_ack_timeout_ms: 300_000, endpoint: { enabled: false, empty_lane_backoff_ms: 250, idle_backoff_ms: 1_000, max_consecutive_pulls_per_lane: 0, accept_when: [] }, offering_lanes: { enabled: false, instance_id: nil } } } end |
.fleet_offering_lane_setting(key) ⇒ Object
175 176 177 178 |
# File 'lib/legion/extensions/ollama.rb', line 175 def self.fleet_offering_lane_setting(key) offering_lanes = nested_setting(settings, :fleet, :offering_lanes) || {} setting_value(offering_lanes, key) end |
.fleet_scheduler ⇒ Object
191 192 193 |
# File 'lib/legion/extensions/ollama.rb', line 191 def self.fleet_scheduler (nested_setting(settings, :fleet, :scheduler) || :basic_get).to_sym end |
.model_worker_actor_name(request_type:, model:, lane_style:, offering_instance_id:) ⇒ Object
180 181 182 183 184 185 |
# File 'lib/legion/extensions/ollama.rb', line 180 def self.model_worker_actor_name(request_type:, model:, lane_style:, offering_instance_id:) return :"model_worker_#{request_type}_#{model.tr(':.', '__')}" if lane_style.to_s == 'shared' suffix = [lane_style, request_type, model, offering_instance_id].compact.join('_') :"model_worker_#{actor_suffix(suffix)}" end |
.nested_setting(hash, *keys) ⇒ Object
212 213 214 215 216 217 218 |
# File 'lib/legion/extensions/ollama.rb', line 212 def self.nested_setting(hash, *keys) keys.reduce(hash) do |current, key| return nil unless current.respond_to?(:key?) setting_value(current, key) end end |
.offering_instance_for(subscription) ⇒ Object
154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/legion/extensions/ollama.rb', line 154 def self.offering_instance_for(subscription) return nil unless offering_lanes_enabled? raw = setting_value(subscription, :offering_instance_id) || setting_value(subscription, :provider_instance) || setting_value(subscription, :instance_id) || fleet_offering_lane_setting(:instance_id) || fleet_offering_lane_setting(:provider_instance) || fleet_offering_lane_setting(:offering_instance_id) normalized = raw&.to_s return nil if normalized.nil? || normalized.empty? normalized end |
.offering_lanes_enabled? ⇒ Boolean
169 170 171 172 173 |
# File 'lib/legion/extensions/ollama.rb', line 169 def self.offering_lanes_enabled? fleet_offering_lane_setting(:enabled) == true rescue StandardError false end |
.register_model_worker(request_type:, model:, context_window:, lane_style: :shared, offering_instance_id: nil) ⇒ Object
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/legion/extensions/ollama.rb', line 125 def self.register_model_worker(request_type:, model:, context_window:, lane_style: :shared, offering_instance_id: nil) actor_name = model_worker_actor_name( request_type: request_type, model: model, lane_style: lane_style, offering_instance_id: offering_instance_id ) worker_class = Class.new(Legion::Extensions::Ollama::Actor::ModelWorker) do define_method(:initialize) do super( request_type: request_type, model: model, context_window: context_window, lane_style: lane_style, offering_instance_id: offering_instance_id ) end end @actors[actor_name] = { extension: 'lex-ollama', extension_name: :ollama, actor_name: actor_name, actor_class: worker_class, type: 'literal' } end |
.setting_value(hash, key) ⇒ Object
203 204 205 206 207 208 209 210 |
# File 'lib/legion/extensions/ollama.rb', line 203 def self.setting_value(hash, key) return nil unless hash.respond_to?(:key?) string_key = key.to_s return hash[string_key] if hash.key?(string_key) hash[key] if hash.key?(key) end |
.sorted_subscriptions(subscriptions) ⇒ Object
100 101 102 103 104 105 106 107 108 109 |
# File 'lib/legion/extensions/ollama.rb', line 100 def self.sorted_subscriptions(subscriptions) subscriptions.sort_by do |sub| type = setting_value(sub, :type).to_s [ type == 'embed' ? 0 : 1, context_window_for(sub) || Float::INFINITY, setting_value(sub, :model).to_s ] end end |
.valid_fleet_subscriptions(subscriptions) ⇒ Object
195 196 197 198 199 200 201 |
# File 'lib/legion/extensions/ollama.rb', line 195 def self.valid_fleet_subscriptions(subscriptions) return [] unless subscriptions.is_a?(Array) subscriptions.select do |sub| setting_value(sub, :type) && setting_value(sub, :model) end end |