Module: Legion::LLM::API::Namespaces::OpenAI::Files
- Extended by:
- Legion::Logging::Helper
- Defined in:
- lib/legion/llm/api/namespaces/openai/files.rb
Constant Summary collapse
- FILE_STORE =
rubocop:disable Style/MutableConstant
{}
- FILE_MUTEX =
Mutex.new
- CONTENT_TYPE_MAP =
{ '.jsonl' => 'application/jsonl', '.json' => 'application/json', '.txt' => 'text/plain', '.csv' => 'text/csv', '.pdf' => 'application/pdf' }.freeze
Class Method Summary collapse
- .files_dir ⇒ Object
-
.registered(app) ⇒ Object
rubocop:disable Metrics/AbcSize,Metrics/MethodLength.
- .serialize_file(entry) ⇒ Object
Class Method Details
.files_dir ⇒ Object
27 28 29 |
# File 'lib/legion/llm/api/namespaces/openai/files.rb', line 27 def self.files_dir ::File.join(::Dir.home, '.legionio', 'data', 'files') end |
.registered(app) ⇒ Object
rubocop:disable Metrics/AbcSize,Metrics/MethodLength
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 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 |
# File 'lib/legion/llm/api/namespaces/openai/files.rb', line 31 def self.registered(app) # rubocop:disable Metrics/AbcSize,Metrics/MethodLength log.debug('[llm][api][namespaces][openai][files] registering routes') # POST /v1/files (multipart/form-data) app.post '/v1/files' do # rubocop:disable Metrics/BlockLength require_llm! uploaded = params[:file] purpose = params[:purpose] unless uploaded halt 400, { 'Content-Type' => 'application/json' }, Legion::JSON.dump({ error: { message: 'file is required', type: 'invalid_request_error', code: 'missing_file' } }) end unless purpose halt 400, { 'Content-Type' => 'application/json' }, Legion::JSON.dump({ error: { message: 'purpose is required', type: 'invalid_request_error', code: 'missing_purpose' } }) end file_id = "file-#{SecureRandom.hex(16)}" filename = begin uploaded[:filename] || uploaded.original_filename rescue StandardError 'upload.bin' end data = begin uploaded[:tempfile]&.read || uploaded.read rescue StandardError '' end ::FileUtils.mkdir_p(Files.files_dir) dest = ::File.join(Files.files_dir, file_id) ::File.binwrite(dest, data) entry = { id: file_id, filename: filename.to_s, purpose: purpose.to_s, bytes: data.bytesize, created_at: Time.now, status: 'uploaded' } FILE_MUTEX.synchronize { FILE_STORE[file_id] = entry } log.debug("[llm][api][openai][files] action=upload file_id=#{file_id} filename=#{filename} bytes=#{data.bytesize}") content_type :json status 200 Legion::JSON.dump(Files.serialize_file(entry)) rescue StandardError => e handle_exception(e, level: :error, handled: false, operation: 'llm.api.openai.files.upload') openai_error(e., type: 'server_error', code: 'internal_error', status_code: 500) end # GET /v1/files (list) app.get '/v1/files' do require_llm! purpose = params[:purpose] limit = params[:limit]&.to_i || 20 after = params[:after] entries = FILE_MUTEX.synchronize { FILE_STORE.values.dup } entries.select! { |f| f[:purpose] == purpose } if purpose entries.sort_by! { |f| -f[:created_at].to_i } if after idx = entries.index { |f| f[:id] == after } entries = entries[(idx + 1)..] if idx end entries = entries.first(limit) log.debug("[llm][api][openai][files] action=list count=#{entries.size}") content_type :json status 200 Legion::JSON.dump({ object: 'list', data: entries.map { |f| Files.serialize_file(f) }, has_more: false }) rescue StandardError => e handle_exception(e, level: :error, handled: false, operation: 'llm.api.openai.files.list') openai_error(e., type: 'server_error', code: 'internal_error', status_code: 500) end # GET /v1/files/:id app.get '/v1/files/:id' do require_llm! file_id = params[:id] entry = FILE_MUTEX.synchronize { FILE_STORE[file_id] } unless entry halt 404, { 'Content-Type' => 'application/json' }, Legion::JSON.dump({ error: { message: "File '#{file_id}' not found.", type: 'invalid_request_error', code: 'file_not_found' } }) end log.debug("[llm][api][openai][files] action=get file_id=#{file_id}") content_type :json status 200 Legion::JSON.dump(Files.serialize_file(entry)) rescue StandardError => e handle_exception(e, level: :error, handled: false, operation: 'llm.api.openai.files.get') openai_error(e., type: 'server_error', code: 'internal_error', status_code: 500) end # DELETE /v1/files/:id app.delete '/v1/files/:id' do require_llm! file_id = params[:id] entry = FILE_MUTEX.synchronize { FILE_STORE[file_id] } unless entry halt 404, { 'Content-Type' => 'application/json' }, Legion::JSON.dump({ error: { message: "File '#{file_id}' not found.", type: 'invalid_request_error', code: 'file_not_found' } }) end disk_path = ::File.join(Files.files_dir, file_id) ::FileUtils.rm_f(disk_path) FILE_MUTEX.synchronize { FILE_STORE.delete(file_id) } log.debug("[llm][api][openai][files] action=delete file_id=#{file_id}") content_type :json status 200 Legion::JSON.dump({ id: file_id, object: 'file', deleted: true }) rescue StandardError => e handle_exception(e, level: :error, handled: false, operation: 'llm.api.openai.files.delete') openai_error(e., type: 'server_error', code: 'internal_error', status_code: 500) end # GET /v1/files/:id/content app.get '/v1/files/:id/content' do require_llm! file_id = params[:id] entry = FILE_MUTEX.synchronize { FILE_STORE[file_id] } unless entry halt 404, { 'Content-Type' => 'application/json' }, Legion::JSON.dump({ error: { message: "File '#{file_id}' not found.", type: 'invalid_request_error', code: 'file_not_found' } }) end disk_path = ::File.join(Files.files_dir, file_id) unless ::File.exist?(disk_path) halt 404, { 'Content-Type' => 'application/json' }, Legion::JSON.dump({ error: { message: "File content for '#{file_id}' is missing from disk.", type: 'invalid_request_error', code: 'file_content_missing' } }) end ext = ::File.extname(entry[:filename].to_s).downcase content_mime = CONTENT_TYPE_MAP[ext] || 'application/octet-stream' raw = ::File.binread(disk_path) log.debug("[llm][api][openai][files] action=content file_id=#{file_id} bytes=#{raw.bytesize}") content_type content_mime status 200 raw rescue StandardError => e handle_exception(e, level: :error, handled: false, operation: 'llm.api.openai.files.content') openai_error(e., type: 'server_error', code: 'internal_error', status_code: 500) end log.debug('[llm][api][namespaces][openai][files] routes registered') rescue StandardError => e handle_exception(e, level: :error, handled: false, operation: 'llm.api.openai.files.register') end |
.serialize_file(entry) ⇒ Object
203 204 205 206 207 208 209 210 211 212 213 |
# File 'lib/legion/llm/api/namespaces/openai/files.rb', line 203 def self.serialize_file(entry) { id: entry[:id], object: 'file', bytes: entry[:bytes], created_at: entry[:created_at]&.to_i, filename: entry[:filename], purpose: entry[:purpose], status: entry[:status] } end |