Class: EmbeddingWorker
- Inherits:
-
Object
- Object
- EmbeddingWorker
- Includes:
- Sidekiq::Worker
- Defined in:
- app/workers/embedding_worker.rb
Overview
Background worker for generating content embeddings.
Uses RubyLLM to generate vector embeddings for semantic search.
Instance Method Summary collapse
Instance Method Details
#perform(class_name, id, options = {}) ⇒ Object
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 |
# File 'app/workers/embedding_worker.rb', line 32 def perform(class_name, id, = {}) = .with_indifferent_access record = find_record(class_name, id) return unless record content_types = [:content_types]&.map(&:to_sym) || record.class. force = [:force].to_b chunked = [:chunked].to_b # Models that define embeddable_locales (e.g. Post → ['en-US', 'en-CA']) will have # a separate embedding generated for each locale. All other models fall back to a # single locale via locale_for_embedding. locales = [:locales]&.map(&:to_s).presence || record. log_info "Generating embeddings for #{class_name}##{id} " \ "(types: #{content_types.join(', ')}, locales: #{locales.join(', ')}, chunked: #{chunked})" results = content_types.flat_map do |content_type| locales.map do |locale| if chunked && record.respond_to?(:generate_chunked_embeddings!) generate_chunked_with_rate_limit(record, content_type, force, locale) else generate_with_rate_limit(record, content_type, force, locale) end end end.flatten successful = results.count { |r| r.is_a?(ContentEmbedding) } log_info "Completed #{class_name}##{id}: #{successful} embeddings generated" rescue ActiveRecord::RecordNotFound log_info "Record not found: #{class_name}##{id} - skipping" rescue RubyLLM::Error => e log_error "RubyLLM error for #{class_name}##{id}: #{e.}" raise # Re-raise to trigger retry rescue StandardError => e log_error "Unexpected error for #{class_name}##{id}: #{e.}" ErrorReporting.error(e) raise end |