Class: PineconeService

Inherits:
Object
  • Object
show all
Defined in:
lib/vector_stores/pinecone.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pinecone_key: ENV['PINECONE_API_KEY'], pinecone_env: ENV['PINECONE_ENV'], index_name:) ⇒ PineconeService

Returns a new instance of PineconeService.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/vector_stores/pinecone.rb', line 9

def initialize(pinecone_key: ENV['PINECONE_API_KEY'], pinecone_env: ENV['PINECONE_ENV'], index_name:)
  raise 'API key not found. Please set the PINECONE_API_KEY environment variable.' if pinecone_key.nil? || pinecone_key.empty?
  raise 'ENV not found. Please set the PINECONE_ENV environment variable.' if pinecone_env.nil? || pinecone_env.empty?
  @pinecone_key = pinecone_key
  @pinecone_env = pinecone_env
  @index_name = index_name


  Pinecone.configure do |config|
    config.api_key  = @pinecone_key
    config.environment = @pinecone_env
  end

  if @pinecone_key && @pinecone_env
    @pinecone = Pinecone::Client.new
  else
    Rails.logger.error "Set the PINECONE_API_KEY and PINECONE_ENV in the ENV variables"
  end
  @index = @pinecone.index(@index_name)
end

Instance Attribute Details

#indexObject (readonly)

Returns the value of attribute index.



8
9
10
# File 'lib/vector_stores/pinecone.rb', line 8

def index
  @index
end

Instance Method Details

#compute_hash(text) ⇒ Object



30
31
32
# File 'lib/vector_stores/pinecone.rb', line 30

def compute_hash(text)
  Digest::SHA256.hexdigest(text)[0...44]
end

#store(embeddings, text) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/vector_stores/pinecone.rb', line 34

def store(embeddings, text)
  namespace = compute_hash(text)

  upsert_with_retry(@index, namespace, embeddings)

  namespace
end