Class: ActivePinecone::Base
- Inherits:
-
Object
- Object
- ActivePinecone::Base
- Defined in:
- lib/active_pinecone/base.rb
Instance Attribute Summary collapse
-
#attributes ⇒ Object
readonly
Returns the value of attribute attributes.
Class Method Summary collapse
- .assistant(instruction = 'Context:') ⇒ Object
- .create(**attributes) ⇒ Object
- .delete(id) ⇒ Object
- .describe ⇒ Object
- .embed(input) ⇒ Object
- .find(id) ⇒ Object
- .index ⇒ Object
- .index_name ⇒ Object
- .init ⇒ Object
- .openai ⇒ Object
- .pinecone ⇒ Object
- .search(text, filter: {}) ⇒ Object
- .vectorized_attributes ⇒ Object
- .vectorizes(*attr_names) ⇒ Object
Instance Method Summary collapse
-
#initialize(attributes = {}) ⇒ Base
constructor
A new instance of Base.
- #update(**attributes) ⇒ Object
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
#attributes ⇒ Object (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: (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 |
.describe ⇒ Object
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.(input) = openai.(parameters: { model: ActivePinecone.configuration., input: input }) ['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 |
.index ⇒ Object
44 45 46 |
# File 'lib/active_pinecone/base.rb', line 44 def self.index pinecone.index(index_name) end |
.index_name ⇒ Object
19 20 21 |
# File 'lib/active_pinecone/base.rb', line 19 def self.index_name to_s.downcase.pluralize end |
.init ⇒ Object
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 |
.openai ⇒ Object
27 28 29 |
# File 'lib/active_pinecone/base.rb', line 27 def self.openai @openai ||= ActivePinecone::Openai.new end |
.pinecone ⇒ Object
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: (text), filter: filter) searched['matches'].map do |match| new(match['metadata'].merge(id: match['id']).symbolize_keys) end end |
.vectorized_attributes ⇒ Object
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.(attributes.slice(self.class.vectorized_attributes).to_json) }]) @attributes = attributes end |