Module: Chromable::ClassMethods

Defined in:
lib/chromable.rb

Overview

Methods to be added to the model class.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(base) ⇒ Object



30
31
32
33
34
35
36
37
38
# File 'lib/chromable.rb', line 30

def self.extended(base)
  class << base
    alias_method :collection, :chroma_collection unless method_defined? :collection
    alias_method :delete_collection, :chroma_delete_collection unless method_defined? :delete_collection
    alias_method :query, :chroma_query unless method_defined? :query
  end

  base.cattr_accessor :chromable_settings
end

Instance Method Details

#chroma_collectionObject



46
47
48
# File 'lib/chromable.rb', line 46

def chroma_collection
  Chroma::Resources::Collection.get_or_create(chromable_settings.collection_name)
end

#chroma_delete_collectionObject



50
51
52
# File 'lib/chromable.rb', line 50

def chroma_delete_collection
  Chroma::Resources::Collection.delete(chromable_settings.collection_name)
end

#chroma_query(text:, results: 10, where: {}, where_document: {}, **embedder_options) ⇒ Object



54
55
56
57
58
59
60
61
# File 'lib/chromable.rb', line 54

def chroma_query(text:, results: 10, where: {}, where_document: {}, **embedder_options)
  find(chroma_collection.query(
    query_embeddings: [send(chromable_settings.embedder, text, **embedder_options)],
    results: results,
    where: where,
    where_document: where_document
  ).map(&:id))
end

#chromable(**options) ⇒ Object



40
41
42
43
44
# File 'lib/chromable.rb', line 40

def chromable(**options)
  options[:collection_name] ||= name.underscore.pluralize

  self.chromable_settings = Settings.new(**options)
end