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



72
73
74
75
76
77
78
79
80
81
# File 'lib/chromable.rb', line 72

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



91
92
93
# File 'lib/chromable.rb', line 91

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

#chroma_embeddingObject



83
84
85
# File 'lib/chromable.rb', line 83

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

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



95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/chromable.rb', line 95

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

  embedding = collection.get(ids: [id.to_s], 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



87
88
89
# File 'lib/chromable.rb', line 87

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