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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
|
# File 'lib/legion/llm/api/namespaces/openai/models.rb', line 21
def self.registered(app)
log.debug('[llm][api][namespaces][openai][models] registering routes')
app.get '/v1/models' do
require_llm!
dialect = detect_client(env) == :anthropic ? :anthropic : :openai
log.debug("[llm][api][namespaces][openai][models] action=list dialect=#{dialect}")
snapshot = Legion::Extensions::Llm::Inventory::Registry.snapshot
settings_snapshot = Legion::LLM::Router::SettingsState.current
entries = Legion::LLM::API::ModelCatalog.list(
snapshot: snapshot, settings_snapshot: settings_snapshot, dialect: dialect
)
log.debug("[llm][api][namespaces][openai][models] action=listed dialect=#{dialect} count=#{entries.size}")
content_type :json
if dialect == :anthropic
Legion::JSON.dump({
data: entries,
has_more: false,
first_id: entries.first&.dig(:id),
last_id: entries.last&.dig(:id)
})
else
Legion::JSON.dump({ object: 'list', data: entries })
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!
dialect = detect_client(env) == :anthropic ? :anthropic : :openai
log.debug("[llm][api][namespaces][openai][models] action=passthrough_model id=#{passthrough_id} dialect=#{dialect}")
snapshot = Legion::Extensions::Llm::Inventory::Registry.snapshot
settings_snapshot = Legion::LLM::Router::SettingsState.current
found = Legion::LLM::API::ModelCatalog.fetch(
id: passthrough_id, snapshot: snapshot, settings_snapshot: settings_snapshot, dialect: dialect
)
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]
dialect = detect_client(env) == :anthropic ? :anthropic : :openai
log.debug("[llm][api][namespaces][openai][models] action=get id=#{model_id} dialect=#{dialect}")
snapshot = Legion::Extensions::Llm::Inventory::Registry.snapshot
settings_snapshot = Legion::LLM::Router::SettingsState.current
found = Legion::LLM::API::ModelCatalog.fetch(
id: model_id, snapshot: snapshot, settings_snapshot: settings_snapshot, dialect: dialect
)
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
Legion::JSON.dump(found)
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
|