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
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
|
# File 'lib/legion/llm/api/namespaces/openai/conversations/items.rb', line 17
def self.registered(app) log.debug('[llm][api][namespaces][openai][items] registering routes')
app.post '/v1/conversations/:id/items' do
require_llm!
conv_id = params[:id]
body = parse_request_body
validate_required!(body, :role, :content)
unless Inference::Conversation.conversation_exists?(conv_id)
halt 404, { 'Content-Type' => 'application/json' },
Legion::JSON.dump({ error: { message: "Conversation '#{conv_id}' not found.",
type: 'invalid_request_error', code: 'conversation_not_found' } })
end
role = body[:role].to_sym
content = body[:content]
msg = Inference::Conversation.append(conv_id, role: role, content: content)
log.debug("[llm][api][openai][items] action=create conv_id=#{conv_id} item_id=#{msg[:id]} role=#{role}")
content_type :json
status 200
Legion::JSON.dump({
id: msg[:id],
object: 'conversation.item',
conv_id: conv_id,
role: role.to_s,
content: content,
created_at: msg[:created_at].to_i
})
rescue StandardError => e
handle_exception(e, level: :error, handled: false, operation: 'llm.api.openai.items.create')
openai_error(e.message, type: 'server_error', code: 'internal_error', status_code: 500)
end
app.get '/v1/conversations/:id/items' do
require_llm!
conv_id = params[:id]
limit = params[:limit]&.to_i || 100
after = params[:after]
unless Inference::Conversation.conversation_exists?(conv_id)
halt 404, { 'Content-Type' => 'application/json' },
Legion::JSON.dump({ error: { message: "Conversation '#{conv_id}' not found.",
type: 'invalid_request_error', code: 'conversation_not_found' } })
end
conv_meta = Inference::Conversation.read_metadata(conv_id) || {}
if conv_meta[:title] == '__deleted__'
halt 404, { 'Content-Type' => 'application/json' },
Legion::JSON.dump({ error: { message: "Conversation '#{conv_id}' not found.",
type: 'invalid_request_error', code: 'conversation_not_found' } })
end
msgs = Inference::Conversation.messages(conv_id)
msgs = msgs.reject { |m| m[:__deleted__] }
if after
idx = msgs.index { |m| m[:id] == after }
msgs = msgs[(idx + 1)..] if idx
end
msgs = msgs.first(limit)
log.debug("[llm][api][openai][items] action=list conv_id=#{conv_id} count=#{msgs.size}")
content_type :json
status 200
Legion::JSON.dump({
object: 'list',
data: msgs.map { |m| Items.serialize_item(m, conv_id) },
has_more: false,
first_id: msgs.first&.dig(:id),
last_id: msgs.last&.dig(:id)
})
rescue StandardError => e
handle_exception(e, level: :error, handled: false, operation: 'llm.api.openai.items.list')
openai_error(e.message, type: 'server_error', code: 'internal_error', status_code: 500)
end
app.get '/v1/conversations/:id/items/:item_id' do
require_llm!
conv_id = params[:id]
item_id = params[:item_id]
unless Inference::Conversation.conversation_exists?(conv_id)
halt 404, { 'Content-Type' => 'application/json' },
Legion::JSON.dump({ error: { message: "Conversation '#{conv_id}' not found.",
type: 'invalid_request_error', code: 'conversation_not_found' } })
end
msgs = Inference::Conversation.raw_messages(conv_id)
msg = msgs.find { |m| m[:id] == item_id }
unless msg
halt 404, { 'Content-Type' => 'application/json' },
Legion::JSON.dump({ error: { message: "Item '#{item_id}' not found.",
type: 'invalid_request_error', code: 'item_not_found' } })
end
log.debug("[llm][api][openai][items] action=get item_id=#{item_id}")
content_type :json
status 200
Legion::JSON.dump(Items.serialize_item(msg, conv_id))
rescue StandardError => e
handle_exception(e, level: :error, handled: false, operation: 'llm.api.openai.items.get')
openai_error(e.message, type: 'server_error', code: 'internal_error', status_code: 500)
end
app.delete '/v1/conversations/:id/items/:item_id' do
require_llm!
conv_id = params[:id]
item_id = params[:item_id]
unless Inference::Conversation.conversation_exists?(conv_id)
halt 404, { 'Content-Type' => 'application/json' },
Legion::JSON.dump({ error: { message: "Conversation '#{conv_id}' not found.",
type: 'invalid_request_error', code: 'conversation_not_found' } })
end
msgs = Inference::Conversation.raw_messages(conv_id)
msg = msgs.find { |m| m[:id] == item_id }
unless msg
halt 404, { 'Content-Type' => 'application/json' },
Legion::JSON.dump({ error: { message: "Item '#{item_id}' not found.",
type: 'invalid_request_error', code: 'item_not_found' } })
end
msg[:__deleted__] = true
log.debug("[llm][api][openai][items] action=delete item_id=#{item_id}")
content_type :json
status 200
Legion::JSON.dump({ id: item_id, object: 'conversation.item', deleted: true })
rescue StandardError => e
handle_exception(e, level: :error, handled: false, operation: 'llm.api.openai.items.delete')
openai_error(e.message, type: 'server_error', code: 'internal_error', status_code: 500)
end
log.debug('[llm][api][namespaces][openai][items] routes registered')
rescue StandardError => e
handle_exception(e, level: :error, handled: false, operation: 'llm.api.openai.items.register')
end
|