Class: LLM::RAG

Inherits:
Object
  • Object
show all
Defined in:
lib/scout/llm/rag.rb

Class Method Summary collapse

Class Method Details

.index(data) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
# File 'lib/scout/llm/rag.rb', line 3

def self.index(data)
  require 'hnswlib'

  dim = data.first.length
  t = Hnswlib::HierarchicalNSW.new(space: 'l2', dim: dim)
  t.init_index(max_elements: data.length)

  data.each_with_index do |vector,i|
    t.add_point vector, i
  end
  t
end

.load(path, dim) ⇒ Object



16
17
18
19
20
21
22
23
# File 'lib/scout/llm/rag.rb', line 16

def self.load(path, dim)
  require 'hnswlib'

  u = Hnswlib::HierarchicalNSW.new(space: 'l2', dim: dim)
  u.load_index(path)

  u
end

.top(texts, prompt, num = 10) ⇒ Object



25
26
27
28
29
30
31
32
# File 'lib/scout/llm/rag.rb', line 25

def self.top(texts, prompt, num = 10)
  data = texts.collect{|text| LLM.embed(text) }

  i = LLM::RAG.index(data)
  pos, scores = i.search_knn LLM.embed(prompt), num

  texts.values_at *pos.reverse
end