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
48
49
50
51
52
53
54
55
56
|
# File 'lib/legion/llm/api/openai/models.rb', line 13
def self.registered(app)
log.debug('[llm][api][openai][models] registering GET /v1/models and GET /v1/models/:id')
app.get '/v1/models' do
log.debug('[llm][api][openai][models] action=list')
require_llm!
model_list = Legion::LLM::API::OpenAI::Models.build_model_list
log.debug("[llm][api][openai][models] action=listed count=#{model_list.size}")
content_type :json
Legion::JSON.dump({ object: 'list', data: model_list })
rescue StandardError => e
handle_exception(e, level: :error, handled: true, operation: 'llm.api.openai.models.list')
halt 500, { 'Content-Type' => 'application/json' },
Legion::JSON.dump({ error: { message: e.message, type: 'server_error' } })
end
app.get '/v1/models/:id' do
model_id = params[:id]
log.debug("[llm][api][openai][models] action=get id=#{model_id}")
require_llm!
model_list = Legion::LLM::API::OpenAI::Models.build_model_list
found = model_list.find { |m| m[:id] == model_id }
unless found
log.debug("[llm][api][openai][models] action=not_found id=#{model_id}")
halt 404, { 'Content-Type' => 'application/json' },
Legion::JSON.dump({ error: { message: "Model '#{model_id}' not found",
type: 'invalid_request_error', code: 'model_not_found' } })
end
log.debug("[llm][api][openai][models] action=found id=#{model_id}")
content_type :json
Legion::JSON.dump(found)
rescue StandardError => e
handle_exception(e, level: :error, handled: true, operation: 'llm.api.openai.models.get')
halt 500, { 'Content-Type' => 'application/json' },
Legion::JSON.dump({ error: { message: e.message, type: 'server_error' } })
end
log.debug('[llm][api][openai][models] GET /v1/models routes registered')
end
|