Module: Legion::LLM::API::Namespaces::OpenAI::Models

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

Overview

Single namespace serving /v1/models for BOTH OpenAI and Anthropic clients. Uses detect_client(env) to branch between formats.

Class Method Summary collapse

Class Method Details

.build_openai_model_listObject



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/legion/llm/api/namespaces/openai/models.rb', line 104

def self.build_openai_model_list
  offerings = Legion::LLM::Inventory.offerings(type: :inference)
  offerings = Legion::LLM::API::Native::Models.with_auto_routing_offering(offerings, {})

  models = offerings.map do |offering|
    Legion::LLM::API::Translators::OpenAIResponse.format_model_object(
      offering[:model],
      owned_by: offering[:provider_family],
      limits:   offering[:limits]
    )
  end
  seen = {}
  models.select { |m| seen[m[:id]] ? false : (seen[m[:id]] = true) }
rescue StandardError => e
  log.warn("[llm][api][namespaces][openai][models] inventory_error=#{e.message}")
  []
end

.openai_to_anthropic_model(openai_model) ⇒ Object



143
144
145
146
147
148
149
150
151
152
153
# File 'lib/legion/llm/api/namespaces/openai/models.rb', line 143

def self.openai_to_anthropic_model(openai_model)
  model = {
    type:         'model',
    id:           openai_model[:id],
    display_name: openai_model[:id],
    created_at:   Time.at(openai_model[:created] || Time.now.to_i).utc.strftime('%Y-%m-%dT%H:%M:%SZ')
  }
  model[:max_input_tokens] = openai_model[:context_window] if openai_model[:context_window]
  model[:max_tokens] = openai_model[:max_output_tokens] if openai_model[:max_output_tokens]
  model
end

.passthrough_model_idsObject



122
123
124
# File 'lib/legion/llm/api/namespaces/openai/models.rb', line 122

def self.passthrough_model_ids
  Legion::Settings[:llm][:routing][:model_passthrough_ids]
end

.registered(app) ⇒ Object



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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/legion/llm/api/namespaces/openai/models.rb', line 19

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

  app.get '/v1/models' do
    require_llm!
    client = detect_client(env)
    log.debug("[llm][api][namespaces][openai][models] action=list client=#{client}")

    model_list = Models.build_openai_model_list
    log.debug("[llm][api][namespaces][openai][models] action=listed count=#{model_list.size}")

    content_type :json
    if client == :anthropic
      anthropic_list = Models.to_anthropic_model_list(model_list)
      Legion::JSON.dump({
                          data:     anthropic_list,
                          has_more: false,
                          first_id: anthropic_list.first&.dig(:id),
                          last_id:  anthropic_list.last&.dig(:id)
                        })
    else
      Legion::JSON.dump({ object: 'list', data: model_list })
    end
  rescue StandardError => e
    handle_exception(e, level: :error, handled: true, operation: 'llm.api.namespaces.openai.models.list')
    openai_error(e.message, type: 'server_error', status_code: 500)
  end

  Models.passthrough_model_ids.each do |passthrough_id|
    app.get "/v1/models/#{passthrough_id}" do
      require_llm!
      log.debug("[llm][api][namespaces][openai][models] action=passthrough_model id=#{passthrough_id}")
      found = Models.synthetic_model_for_auto_route(passthrough_id)

      unless found
        return openai_error("Model '#{passthrough_id}' not found",
                            type: 'invalid_request_error', code: 'model_not_found', status_code: 404)
      end

      content_type :json
      Legion::JSON.dump(found)
    rescue StandardError => e
      handle_exception(e, level: :error, handled: true,
                          operation: 'llm.api.namespaces.openai.models.passthrough')
      openai_error(e.message, type: 'server_error', status_code: 500)
    end
  end

  app.get '/v1/models/:id' do
    require_llm!
    model_id = params[:id]
    client   = detect_client(env)
    log.debug("[llm][api][namespaces][openai][models] action=get id=#{model_id} client=#{client}")

    model_list = Models.build_openai_model_list
    found = model_list.find { |m| m[:id] == model_id }

    unless found
      return openai_error("Model '#{model_id}' not found",
                          type: 'invalid_request_error', code: 'model_not_found', status_code: 404)
    end

    content_type :json
    if client == :anthropic
      Legion::JSON.dump(Models.openai_to_anthropic_model(found))
    else
      Legion::JSON.dump(found)
    end
  rescue StandardError => e
    handle_exception(e, level: :error, handled: true, operation: 'llm.api.namespaces.openai.models.get')
    openai_error(e.message, type: 'server_error', status_code: 500)
  end

  app.delete '/v1/models/:id' do
    model_id = params[:id]
    log.debug("[llm][api][namespaces][openai][models] action=delete id=#{model_id}")
    content_type :json
    Legion::JSON.dump({ id: model_id, object: 'model', deleted: true })
  end

  log.debug('[llm][api][namespaces][openai][models] routes registered')
rescue StandardError => e
  handle_exception(e, level: :error, handled: false, operation: 'llm.api.namespaces.openai.models.register')
end

.synthetic_model_for_auto_route(model_id) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/legion/llm/api/namespaces/openai/models.rb', line 126

def self.synthetic_model_for_auto_route(model_id)
  return nil unless Legion::LLM::Inventory.lanes.any?

  best_lane = Legion::LLM::Inventory.lanes.max_by { |l| l[:lane_weight].to_i }
  return nil unless best_lane

  Legion::LLM::API::Translators::OpenAIResponse.format_model_object(
    model_id,
    owned_by: 'legionio',
    limits:   best_lane[:limits]
  )
end

.to_anthropic_model_list(openai_list) ⇒ Object



139
140
141
# File 'lib/legion/llm/api/namespaces/openai/models.rb', line 139

def self.to_anthropic_model_list(openai_list)
  openai_list.map { |m| openai_to_anthropic_model(m) }
end