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



34
35
36
37
38
39
40
41
42
# File 'lib/chromable.rb', line 34

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



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

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

#chroma_delete_collectionObject



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

def chroma_delete_collection
  Chroma::Resources::Collection.delete(chromable_settings.collection_name)
rescue Chroma::InvalidRequestError
  Chroma::Util.log_debug("Collection #{chromable_settings.collection_name} does not exist.")
end

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



60
61
62
63
64
65
66
67
# File 'lib/chromable.rb', line 60

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



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

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

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