Class: Vecsearch

Inherits:
Object
  • Object
show all
Defined in:
lib/vecsearch/gte_tiny.rb,
lib/vecsearch.rb,
lib/vecsearch/version.rb

Overview

require ‘narray’

Defined Under Namespace

Classes: GTETiny

Constant Summary collapse

VERSION =
"0.2.0"

Instance Method Summary collapse

Constructor Details

#initialize(*records) ⇒ Vecsearch

Returns a new instance of Vecsearch.



7
8
9
10
11
12
# File 'lib/vecsearch.rb', line 7

def initialize(*records)
  @faiss = Faiss::IndexFlatL2.new(384)
  @gte = GTETiny.new
  @texts = []
  records.each { |rec| self << rec }
end

Instance Method Details

#<<(str) ⇒ Object



14
15
16
17
18
# File 'lib/vecsearch.rb', line 14

def <<(str)
  emb = @gte.encode(str)
  @texts << str
  @faiss.add(emb)
end

#nearest(str) ⇒ Object



20
# File 'lib/vecsearch.rb', line 20

def nearest(str) = query(str, 1)[0]

#query(str, n) ⇒ Object



22
23
24
25
26
# File 'lib/vecsearch.rb', line 22

def query(str, n)
  emb = @gte.encode(str)
  _scores, indexes = @faiss.search(emb, 2)
  [].tap { |res| indexes.each { |idx| res << @texts[idx] } }
end