Class: SimpleRag::Index

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

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ Index

Returns a new instance of Index.



3
4
5
6
# File 'lib/simple_rag/index.rb', line 3

def initialize(client)
  @text = nil
  @client = client
end

Instance Method Details

#chunk(text) ⇒ Object



16
17
18
19
# File 'lib/simple_rag/index.rb', line 16

def chunk(text)
  chunk_size = 2048
  text.chars.each_slice(chunk_size).map(&:join)
end

#embed_chunks(chunks) ⇒ Object



21
22
23
# File 'lib/simple_rag/index.rb', line 21

def embed_chunks(chunks)
  SimpleRag::Embed.embed_chunks(@client, chunks)
end

#load(url) ⇒ Object



8
9
10
11
12
13
14
# File 'lib/simple_rag/index.rb', line 8

def load(url)
  response = HTTParty.get(url)
  text = response.body
  File.write("data/essay.txt", text)
  @text = text
  text
end

#save(text_embeddings) ⇒ Object



25
26
27
28
29
30
# File 'lib/simple_rag/index.rb', line 25

def save(text_embeddings)
  d = text_embeddings.shape[1]
  index = Faiss::IndexFlatL2.new(d)
  index.add(text_embeddings)
  index
end