Module: Chromable::InstanceMethods

Defined in:
lib/chromable.rb

Overview

Methods to be added to the model instances.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



66
67
68
69
70
71
72
73
74
75
# File 'lib/chromable.rb', line 66

def self.included(base)
  base.instance_eval do
    # rubocop:disable Style/Alias
    alias_method :embedding, :chroma_embedding unless method_defined? :embedding
    alias_method :upsert_embedding, :chroma_upsert_embedding unless method_defined? :upsert_embedding
    alias_method :destroy_embedding, :chroma_destroy_embedding unless method_defined? :destroy_embedding
    alias_method :neighbors, :chroma_neighbors unless method_defined? :neighbors
    # rubocop:enable Style/Alias
  end
end

Instance Method Details

#chroma_destroy_embeddingObject



85
86
87
# File 'lib/chromable.rb', line 85

def chroma_destroy_embedding
  self.class.chroma_collection.delete(ids: [id])
end

#chroma_embeddingObject



77
78
79
# File 'lib/chromable.rb', line 77

def chroma_embedding
  self.class.chroma_collection.get(ids: [id])[0]
end

#chroma_neighbors(results: 10, where: {}, where_document: {}) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/chromable.rb', line 89

def chroma_neighbors(results: 10, where: {}, where_document: {})
  collection = self.class.chroma_collection

  embedding = collection.get(ids: [id], include: [:embeddings])[0].embedding

  self.class.find(collection.query(
    query_embeddings: [embedding],
    results: results,
    where: where,
    where_document: where_document
  ).map(&:id))
end

#chroma_upsert_embeddingObject



81
82
83
# File 'lib/chromable.rb', line 81

def chroma_upsert_embedding
  self.class.chroma_collection.upsert(build_embedding)
end