Module: Legion::LLM::API::Native::Offerings

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

Class Method Summary collapse

Class Method Details

.compact_offering(offering) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/legion/llm/api/native/offerings.rb', line 80

def self.compact_offering(offering)
  {
    id:           offering[:offering_id] || offering[:id],
    model:        offering[:model].to_s,
    type:         offering[:type].to_s,
    model_family: offering[:model_family]&.to_s,
    capabilities: Array(offering[:capabilities]).map(&:to_s),
    limits:       offering[:limits] || {},
    enabled:      offering[:enabled] != false,
    cost:         offering[:cost] || {},
    health:       offering[:health] || {}
  }.compact
end

.group_offerings(offerings) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/legion/llm/api/native/offerings.rb', line 63

def self.group_offerings(offerings)
  grouped = {}

  offerings.each do |offering|
    tier = (offering[:tier] || :unknown).to_s
    provider = (offering[:provider_family] || :unknown).to_s
    instance = (offering[:instance_id] || offering[:provider_instance] || :default).to_s

    grouped[tier] ||= {}
    grouped[tier][provider] ||= {}
    grouped[tier][provider][instance] ||= []
    grouped[tier][provider][instance] << compact_offering(offering)
  end

  grouped
end

.registered(app) ⇒ Object



12
13
14
15
16
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
# File 'lib/legion/llm/api/native/offerings.rb', line 12

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

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

    filters = Legion::LLM::API::Native::Offerings.request_filters(params)
    raw_offerings = Legion::LLM::Inventory.offerings(filters)
    grouped = Legion::LLM::API::Native::Offerings.group_offerings(raw_offerings)

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

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

    offering = Legion::LLM::Inventory.offerings(offering_id: offering_id).first
    halt json_error('offering_not_found', "Offering '#{offering_id}' not found", status_code: 404) unless offering

    json_response({ offering: offering })
  rescue StandardError => e
    handle_exception(e, level: :error, handled: true, operation: 'llm.api.offerings.get')
    json_error('offering_inventory_error', e.message, status_code: 500)
  end

  log.debug('[llm][api][offerings] offering inventory routes registered')
end

.request_filters(params) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/legion/llm/api/native/offerings.rb', line 49

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

.summary(offerings) ⇒ Object



94
95
96
97
98
99
100
101
102
# File 'lib/legion/llm/api/native/offerings.rb', line 94

def self.summary(offerings)
  {
    total:     offerings.size,
    tiers:     offerings.map { |o| (o[:tier] || :unknown).to_s }.uniq.size,
    providers: offerings.map { |o| (o[:provider_family] || :unknown).to_s }.uniq.size,
    instances: offerings.map { |o| (o[:instance_id] || :default).to_s }.uniq.size,
    models:    offerings.map { |o| o[:model] }.uniq.size
  }
end