Class: ActivePinecone::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/active_pinecone/base.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Base

Returns a new instance of Base.



5
6
7
8
9
10
11
12
13
# File 'lib/active_pinecone/base.rb', line 5

def initialize(attributes = {})
  attributes.symbolize_keys!
  @attributes = attributes
  attributes.dup.each do |key, value|
    define_singleton_method("#{key}=") { |value| @attributes[key] = value }
    define_singleton_method(key) { @attributes[key] }
    send "#{key}=", value
  end
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



3
4
5
# File 'lib/active_pinecone/base.rb', line 3

def attributes
  @attributes
end

Class Method Details

.assistant(instruction = 'Context:') ⇒ Object



15
16
17
# File 'lib/active_pinecone/base.rb', line 15

def self.assistant(instruction = 'Context:')
  ActivePinecone::Assistant.new(self, instruction: instruction)
end

.create(**attributes) ⇒ Object



57
58
59
60
61
# File 'lib/active_pinecone/base.rb', line 57

def self.create(**attributes)
  id = attributes[:id] || SecureRandom.uuid
  index.upsert(vectors: [{ id: id, metadata: attributes, values: embed(attributes.slice(vectorized_attributes).to_json) }])
  new(attributes.merge(id: id))
end

.delete(id) ⇒ Object



63
64
65
# File 'lib/active_pinecone/base.rb', line 63

def self.delete(id)
  index.delete(ids: [id])
end

.describeObject



31
32
33
# File 'lib/active_pinecone/base.rb', line 31

def self.describe
  pinecone.describe_index(index_name)
end

.embed(input) ⇒ Object



35
36
37
38
# File 'lib/active_pinecone/base.rb', line 35

def self.embed(input)
  embeded = openai.embeddings(parameters: { model: ActivePinecone.configuration.embedding_model, input: input })
  embeded['data'][0]['embedding']
end

.find(id) ⇒ Object



67
68
69
70
# File 'lib/active_pinecone/base.rb', line 67

def self.find(id)
  fetched = index.fetch(ids: [id])
  new(fetched['vectors'][id]['metadata'].merge(id: id).symbolize_keys)
end

.indexObject



44
45
46
# File 'lib/active_pinecone/base.rb', line 44

def self.index
  pinecone.index(index_name)
end

.index_nameObject



19
20
21
# File 'lib/active_pinecone/base.rb', line 19

def self.index_name
  to_s.downcase.pluralize
end

.initObject



53
54
55
# File 'lib/active_pinecone/base.rb', line 53

def self.init
  pinecone.create_index({ name: index_name, dimension: ActivePinecone.configuration.dimension, metrics: ActivePinecone.configuration.metrics })
end

.openaiObject



27
28
29
# File 'lib/active_pinecone/base.rb', line 27

def self.openai
  @openai ||= ActivePinecone::Openai.new
end

.pineconeObject



23
24
25
# File 'lib/active_pinecone/base.rb', line 23

def self.pinecone
  @pinecone ||= ActivePinecone::Pinecone.new
end

.search(text, filter: {}) ⇒ Object



72
73
74
75
76
77
# File 'lib/active_pinecone/base.rb', line 72

def self.search(text, filter: {})
  searched = index.query(vector: embed(text), filter: filter)
  searched['matches'].map do |match|
    new(match['metadata'].merge(id: match['id']).symbolize_keys)
  end
end

.vectorized_attributesObject



40
41
42
# File 'lib/active_pinecone/base.rb', line 40

def self.vectorized_attributes
  @@vectorized_attributes
end

.vectorizes(*attr_names) ⇒ Object



48
49
50
51
# File 'lib/active_pinecone/base.rb', line 48

def self.vectorizes(*attr_names)
  @@vectorized_attributes ||= []
  @@vectorized_attributes += attr_names
end

Instance Method Details

#update(**attributes) ⇒ Object



79
80
81
82
# File 'lib/active_pinecone/base.rb', line 79

def update(**attributes)
  self.class.index.upsert(vectors: [{ id: id, metadata: attributes, values: self.class.embed(attributes.slice(self.class.vectorized_attributes).to_json) }])
  @attributes = attributes
end