Class: Vapey::Search

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

Instance Method Summary collapse

Instance Method Details

#create_indexObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/vapey.rb', line 31

def create_index
  schema = {
    id: "TAG",
    page_id: "TAG",
    section_id: "TAG",
    file: "TEXT",
    title: "TEXT",
    content: "TEXT",
    checksum: "TEXT",
    token_count: "NUMERIC",
    embedding: "VECTOR FLAT 6 DIM 1536 DISTANCE_METRIC COSINE TYPE FLOAT64",
  }
  preamble = "FT.CREATE index ON JSON PREFIX 1 item: SCHEMA "
  command = (preamble + schema.map{|name,type| "$.#{name} AS #{name} #{type}"}.join(" ")).split(" ")
  redis.call(command)
end

#find_by_embedding(embedding) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/vapey.rb', line 7

def find_by_embedding(embedding)
  results = redis.call([
    "FT.SEARCH", "index", "@embedding:[VECTOR_RANGE $r $BLOB]=>{$YIELD_DISTANCE_AS: my_scores}",
    "PARAMS", "4", "BLOB", embedding.pack("E*"), "r", "5",
    "LIMIT", "0", "10", "SORTBY", "my_scores", "DIALECT", "2"
  ])
  count = results[0]

  output = []
  results[1..].each_slice(2) do |key,value|
    data = JSON.parse(value[3])
    result = { key: key }.merge(data)
    result.delete("embedding")
    output << result
  end

  output
end

#recreate_indexObject



26
27
28
29
# File 'lib/vapey.rb', line 26

def recreate_index
  redis.call(["FT.DROP", "index"])
  create_index
end

#redisObject



48
49
50
# File 'lib/vapey.rb', line 48

def redis
  @redis ||= Redis.new(host: ENV.fetch("REDIS_HOST") { "127.0.0.1" }, port: ENV.fetch("REDIS_PORT") { "6379" }.to_i)
end