Class: RagRuby::Stores::Zvec

Inherits:
Base
  • Object
show all
Defined in:
lib/rag_ruby/stores/zvec.rb

Instance Method Summary collapse

Constructor Details

#initialize(path:, dimension:) ⇒ Zvec

Returns a new instance of Zvec.



6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/rag_ruby/stores/zvec.rb', line 6

def initialize(path:, dimension:)
  @path = path
  @dimension = dimension

  begin
    require "zvec"
  rescue LoadError
    raise LoadError, "zvec-ruby gem is required for Zvec store. Add `gem 'zvec-ruby'` to your Gemfile."
  end

  @index = ::Zvec::Index.new(path: path, dimension: dimension)
end

Instance Method Details

#add(id, embedding:, metadata: {}, chunk: nil) ⇒ Object



19
20
21
# File 'lib/rag_ruby/stores/zvec.rb', line 19

def add(id, embedding:, metadata: {}, chunk: nil)
  @index.add(id, embedding: embedding, metadata: )
end

#count ⇒ Object



39
40
41
# File 'lib/rag_ruby/stores/zvec.rb', line 39

def count
  @index.count
end

#delete(id) ⇒ Object



35
36
37
# File 'lib/rag_ruby/stores/zvec.rb', line 35

def delete(id)
  @index.delete(id)
end

#search(embedding, top_k: 5, filter: nil) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/rag_ruby/stores/zvec.rb', line 23

def search(embedding, top_k: 5, filter: nil)
  results = @index.search(embedding, top_k: top_k)

  if filter
    results = results.select do |r|
      filter.all? { |k, v| r[:metadata][k] == v }
    end
  end

  results
end