Module: Spectre::Embeddable
- Includes:
- Logging
- Defined in:
- lib/spectre/embeddable.rb
Defined Under Namespace
Modules: ClassMethods Classes: EmbeddingValidationError, NoEmbeddableFieldsError
Class Method Summary collapse
Instance Method Summary collapse
-
#as_vector ⇒ String
Converts the specified fields into a JSON representation suitable for embedding.
-
#embed!(validation: nil, embedding_field: :embedding, timestamp_field: :embedded_at) ⇒ Object
Embeds the vectorized content and saves it to the specified fields.
Methods included from Logging
#log_debug, #log_error, #log_info, #logger
Class Method Details
.included(base) ⇒ Object
13 14 15 |
# File 'lib/spectre/embeddable.rb', line 13 def self.included(base) base.extend ClassMethods end |
Instance Method Details
#as_vector ⇒ String
Converts the specified fields into a JSON representation suitable for embedding.
23 24 25 26 27 28 |
# File 'lib/spectre/embeddable.rb', line 23 def as_vector raise NoEmbeddableFieldsError, "Embeddable fields are not defined" if self.class..empty? vector_data = self.class..map { |field| [field, send(field)] }.to_h vector_data.to_json end |
#embed!(validation: nil, embedding_field: :embedding, timestamp_field: :embedded_at) ⇒ Object
Embeds the vectorized content and saves it to the specified fields.
41 42 43 44 45 46 47 48 49 50 |
# File 'lib/spectre/embeddable.rb', line 41 def (validation: nil, embedding_field: :embedding, timestamp_field: :embedded_at) if validation && !validation.call(self) raise EmbeddingValidationError, "Validation failed for embedding" end = Spectre.provider_module::Embeddings.create(as_vector) send("#{}=", ) send("#{}=", Time.now) save! end |