Module: Legion::LLM::API::Namespaces::OpenAI::VectorStores::Files

Extended by:
Legion::Logging::Helper, Sinatra::Extension
Defined in:
lib/legion/llm/api/namespaces/openai/vector_stores/files.rb

Instance Method Summary collapse

Instance Method Details

#fetch_file_content(file_id) ⇒ Object



241
242
243
244
245
246
247
248
249
# File 'lib/legion/llm/api/namespaces/openai/vector_stores/files.rb', line 241

def fetch_file_content(file_id)
  return nil unless Legion::LLM::VectorStore::Storage.data_available?
  return nil unless Legion::LLM::VectorStore::Storage.db.table_exists?(:llm_files)

  Legion::LLM::VectorStore::Storage.db[:llm_files].where(id: file_id).first&.dig(:content)
rescue StandardError => e
  handle_exception(e, level: :warn, handled: true, operation: 'llm.api.vector_stores.files.fetch_content')
  nil
end

#format_vsf(row) ⇒ Object



269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# File 'lib/legion/llm/api/namespaces/openai/vector_stores/files.rb', line 269

def format_vsf(row)
  return nil unless row

  {
    id:                row[:id],
    object:            'vector_store.file',
    vector_store_id:   row[:vector_store_id],
    file_id:           row[:file_id],
    status:            row[:status].to_s,
    usage_bytes:       row[:usage_bytes].to_i,
    created_at:        row[:created_at].to_i,
    chunking_strategy: Legion::JSON.load(row[:chunking_strategy_json] || '{"type":"auto"}'),
    last_error:        row[:last_error_json] ? Legion::JSON.load(row[:last_error_json]) : nil,
    attributes:        Legion::JSON.load(row[:attributes_json] || '{}')
  }
end

#store_chunks(vsf_id, store_id, file_id, chunks, embed_results) ⇒ Object



251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# File 'lib/legion/llm/api/namespaces/openai/vector_stores/files.rb', line 251

def store_chunks(vsf_id, store_id, file_id, chunks, embed_results)
  ts = Legion::LLM::VectorStore::Storage.now_ts
  chunks.each_with_index do |chunk, index|
    result = embed_results[index] || {}
    vec    = result[:vector]
    Legion::LLM::VectorStore::Storage.db[:llm_vector_store_chunks].insert(
      id:                   Legion::LLM::VectorStore::Storage.generate_id('vsc'),
      vector_store_id:      store_id,
      vector_store_file_id: vsf_id,
      file_id:              file_id,
      chunk_index:          index,
      text:                 chunk,
      embedding_json:       vec ? Legion::JSON.dump(vec) : nil,
      created_at:           ts
    )
  end
end