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
78
79
80
81
|
# File 'lib/legion/llm/api/namespaces/openai/models.rb', line 18
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
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
|