Module: Spectre::Embeddable::ClassMethods

Includes:
Logging
Defined in:
lib/spectre/embeddable.rb

Instance Method Summary collapse

Methods included from Logging

#log_debug, #log_error, #log_info, #logger

Instance Method Details

#embed_all!(scope: nil, validation: nil, embedding_field: :embedding, timestamp_field: :embedded_at) ⇒ Object

Embeds the vectorized content for all records that match the optional scope and pass the validation check. Saves the embedding and timestamp to the specified fields. Also counts the number of successful and failed embeddings.

Examples:

embed_all!(
  scope: -> { where(:response.exists => true, :response.ne => nil) },
  validation: ->(record) { !record.response.nil? },
  embedding_field: :custom_embedding,
  timestamp_field: :custom_embedded_at
)

Parameters:

  • scope (Proc, nil) (defaults to: nil)

    A scope or query to filter records (default: all records).

  • validation (Proc, nil) (defaults to: nil)

    A validation block that returns true if the embedding should proceed for a record.

  • embedding_field (Symbol) (defaults to: :embedding)

    The field in which to store the generated embedding (default: :embedding).

  • timestamp_field (Symbol) (defaults to: :embedded_at)

    The field in which to store the embedding timestamp (default: :embedded_at).



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
# File 'lib/spectre/embeddable.rb', line 80

def embed_all!(scope: nil, validation: nil, embedding_field: :embedding, timestamp_field: :embedded_at)
  records = scope ? instance_exec(&scope) : all

  success_count = 0
  failure_count = 0

  records.each do |record|
    begin
      record.embed!(
        validation: validation,
        embedding_field: embedding_field,
        timestamp_field: timestamp_field
      )
      success_count += 1
    rescue EmbeddingValidationError => e
      log_error("Failed to embed record #{record.id}: #{e.message}")
      failure_count += 1
    rescue => e
      log_error("Unexpected error embedding record #{record.id}: #{e.message}")
      failure_count += 1
    end
  end

  puts "Successfully embedded #{success_count} records."
  puts "Failed to embed #{failure_count} records."
end

#embeddable_field(*fields) ⇒ Object



55
56
57
# File 'lib/spectre/embeddable.rb', line 55

def embeddable_field(*fields)
  @embeddable_fields = fields
end

#embeddable_fieldsObject



59
60
61
# File 'lib/spectre/embeddable.rb', line 59

def embeddable_fields
  @embeddable_fields ||= []
end