Module: Legion::LLM::API::Native::Models

Extended by:
Legion::Logging::Helper
Defined in:
lib/legion/llm/api/native/models.rb

Constant Summary collapse

AUTO_ROUTING_MODEL_ID =
'legionio'
AUTO_ROUTING_MODEL_DISPLAY =
'LegionIO'
AUTO_ROUTING_OFFERING_ID =
'legionio:auto:inference:legionio'
AUTO_ROUTING_CAPABILITIES =
%w[auto_routing chat completion json_schema tools].freeze

Class Method Summary collapse

Class Method Details

.auto_routing_model?(model) ⇒ Boolean

Returns:

  • (Boolean)


182
183
184
# File 'lib/legion/llm/api/native/models.rb', line 182

def self.auto_routing_model?(model)
  model.to_s.strip.downcase == AUTO_ROUTING_MODEL_ID
end

.auto_routing_offeringObject



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/legion/llm/api/native/models.rb', line 135

def self.auto_routing_offering
  {
    id:                    AUTO_ROUTING_OFFERING_ID,
    offering_id:           AUTO_ROUTING_OFFERING_ID,
    model:                 AUTO_ROUTING_MODEL_ID,
    display_name:          AUTO_ROUTING_MODEL_DISPLAY,
    model_family:          'legionio',
    canonical_model_alias: AUTO_ROUTING_MODEL_ID,
    type:                  :inference,
    provider_family:       'legionio',
    provider_instance:     'auto',
    instance_id:           'auto',
    tier:                  :auto,
    transport:             :internal,
    enabled:               true,
    capabilities:          AUTO_ROUTING_CAPABILITIES,
    limits:                {},
    health:                { circuit_state: 'available' },
    metadata:              { auto_route: true, placeholder: true, display_name: AUTO_ROUTING_MODEL_DISPLAY },
    routing_metadata:      { strategy: 'auto' },
    source:                'static'
  }
end

.auto_routing_offering_matches?(filters) ⇒ Boolean

Returns:

  • (Boolean)


159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/legion/llm/api/native/models.rb', line 159

def self.auto_routing_offering_matches?(filters)
  normalized = request_filters(filters)
  type = normalized[:type]
  return false if type && !type.to_s.empty? && type.to_s != 'inference' && type.to_s != 'chat'

  provider = normalized[:provider]
  return false if provider && !provider.to_s.empty? && !%w[legionio auto].include?(provider.to_s.downcase)

  instance = normalized[:instance_id]
  return false if instance && !instance.to_s.empty? && !%w[auto legionio].include?(instance.to_s.downcase)

  model = normalized[:model] || normalized[:offering_id]
  return false if model && !model.to_s.empty? && !auto_routing_model?(model) && model.to_s != AUTO_ROUTING_OFFERING_ID

  family = normalized[:model_family]
  return false if family && !family.to_s.empty? && family.to_s.downcase != 'legionio'

  capability = normalized[:capability]
  return false if capability && !AUTO_ROUTING_CAPABILITIES.include?(capability.to_s)

  true
end

.model_summaries(offerings) ⇒ Object



91
92
93
94
95
96
# File 'lib/legion/llm/api/native/models.rb', line 91

def self.model_summaries(offerings)
  summaries = offerings.group_by { |offering| offering[:model] }.map do |model, rows|
    summarize_model(model, rows)
  end
  summaries.sort_by { |model| [auto_routing_model?(model[:id]) ? 0 : 1, model[:id]] }
end

.registered(app) ⇒ Object



17
18
19
20
21
22
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/legion/llm/api/native/models.rb', line 17

def self.registered(app)
  log.debug('[llm][api][models] registering model inventory routes')

  app.get '/api/llm/models' do
    log.debug('[llm][api][models] action=list_models')
    require_llm!

    filters = Legion::LLM::API::Native::Models.request_filters(params)
    offerings = Legion::LLM::Inventory.offerings(filters)
    offerings = Legion::LLM::API::Native::Models.with_auto_routing_offering(offerings, filters)

    json_response({
                    models:    Legion::LLM::API::Native::Models.model_summaries(offerings),
                    offerings: offerings,
                    summary:   Legion::LLM::API::Native::Models.summary(offerings)
                  })
  rescue StandardError => e
    handle_exception(e, level: :error, handled: true, operation: 'llm.api.models.list')
    json_error('model_inventory_error', e.message, status_code: 500)
  end

  app.get '/api/llm/models/:id' do
    model_id = params[:id]
    log.debug("[llm][api][models] action=get_model id=#{model_id}")
    require_llm!

    filters = { model: model_id }
    offerings = Legion::LLM::Inventory.offerings(filters)
    offerings = Legion::LLM::API::Native::Models.with_auto_routing_offering(offerings, filters)
    halt json_error('model_not_found', "Model '#{model_id}' not found", status_code: 404) unless offerings.any?

    json_response({
                    model:     Legion::LLM::API::Native::Models.summarize_model(model_id, offerings),
                    offerings: offerings
                  })
  rescue StandardError => e
    handle_exception(e, level: :error, handled: true, operation: 'llm.api.models.get')
    json_error('model_inventory_error', e.message, status_code: 500)
  end

  app.get '/api/llm/providers/:name/models' do
    provider = params[:name]
    log.debug("[llm][api][models] action=list_provider_models provider=#{provider}")
    require_llm!

    filters = Legion::LLM::API::Native::Models.request_filters(params).merge(provider: provider)
    offerings = Legion::LLM::Inventory.offerings(filters)

    json_response({
                    provider:  provider,
                    models:    Legion::LLM::API::Native::Models.model_summaries(offerings),
                    offerings: offerings,
                    summary:   Legion::LLM::API::Native::Models.summary(offerings)
                  })
  rescue StandardError => e
    handle_exception(e, level: :error, handled: true, operation: 'llm.api.models.provider')
    json_error('model_inventory_error', e.message, status_code: 500)
  end

  log.debug('[llm][api][models] model inventory routes registered')
end

.request_filters(params) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
# File 'lib/legion/llm/api/native/models.rb', line 79

def self.request_filters(params)
  {
    provider:     params[:provider],
    instance_id:  params[:instance_id] || params[:instance],
    type:         params[:type] || params[:purpose],
    model:        params[:model],
    offering_id:  params[:offering_id],
    model_family: params[:model_family],
    capability:   params[:capability]
  }
end

.summarize_model(model, offerings) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/legion/llm/api/native/models.rb', line 98

def self.summarize_model(model, offerings)
  summary = {
    id:             model.to_s,
    types:          offerings.map { |offering| offering[:type].to_s }.uniq.sort,
    providers:      offerings.map { |offering| offering[:provider_family] }.uniq.sort,
    model_families: offerings.filter_map { |offering| offering[:model_family] }.uniq.sort,
    offering_ids:   offerings.filter_map { |offering| offering[:offering_id] }.uniq.sort,
    instances:      offerings.map { |offering| offering[:instance_id] }.uniq.sort,
    capabilities:   offerings.flat_map { |offering| offering[:capabilities] }.uniq.sort,
    max_context:    offerings.filter_map { |offering| offering.dig(:limits, :context_window) }.max,
    enabled:        offerings.any? { |offering| offering[:enabled] != false }
  }
  if auto_routing_model?(model)
    summary[:display_name] = AUTO_ROUTING_MODEL_DISPLAY
    summary[:auto_route] = true
    summary[:default] = true
  end
  summary
end

.summary(offerings) ⇒ Object



118
119
120
121
122
123
124
125
126
# File 'lib/legion/llm/api/native/models.rb', line 118

def self.summary(offerings)
  {
    total_offerings: offerings.size,
    models:          offerings.map { |offering| offering[:model] }.uniq.size,
    providers:       offerings.map { |offering| offering[:provider_family] }.uniq.size,
    by_type:         offerings.group_by { |offering| offering[:type].to_s }
                              .transform_values(&:size)
  }
end

.with_auto_routing_offering(offerings, filters = {}) ⇒ Object



128
129
130
131
132
133
# File 'lib/legion/llm/api/native/models.rb', line 128

def self.with_auto_routing_offering(offerings, filters = {})
  return offerings unless auto_routing_offering_matches?(filters)
  return offerings if offerings.any? { |offering| auto_routing_model?(offering[:model]) }

  [auto_routing_offering, *offerings]
end