Class: Aoororachain::Embeddings::LocalPythonEmbedding

Inherits:
Object
  • Object
show all
Defined in:
lib/aoororachain/embeddings/local_python_embedding.rb

Constant Summary collapse

MODEL_INSTRUCTOR_L =
"hkunlp/instructor-large"
MODEL_INSTRUCTOR_XL =
"hkunlp/instructor-xl"
MODEL_ALL_MPNET =
"sentence-transformers/all-mpnet-base-v2"

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ LocalPythonEmbedding

Returns a new instance of LocalPythonEmbedding.



10
11
12
13
14
15
16
# File 'lib/aoororachain/embeddings/local_python_embedding.rb', line 10

def initialize(options = {})
  @model = options.delete(:model) || MODEL_ALL_MPNET
  @device = options.delete(:device) || "cpu"

  Aoororachain::Util.log_info("Using", data: {model: @model, device: @device})
  Aoororachain::Util.log_info("This embedding calls Python code using system call.")
end

Instance Method Details

#embed_documents(documents, include_metadata: false) ⇒ Object



18
19
20
21
22
23
# File 'lib/aoororachain/embeddings/local_python_embedding.rb', line 18

def embed_documents(documents, include_metadata: false)
  texts = documents.map { |document| "#{document.content} #{include_metadata ? document.metadata.to_json : ""}.strip" }

  embeddings = embed_texts(texts)
  embeddings || []
end

#embed_query(text) ⇒ Object



37
38
39
40
41
42
# File 'lib/aoororachain/embeddings/local_python_embedding.rb', line 37

def embed_query(text)
  return [] if text.nil? || text.strip == ""

  Aoororachain::Util.log_info("First time usage might take long time due to models download.")
  [embed_text_python(text)]
end

#embed_texts(texts) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/aoororachain/embeddings/local_python_embedding.rb', line 25

def embed_texts(texts)
  return if texts.empty?

  Aoororachain::Util.log_info("First time usage might take long time due to models download.")
  texts_file_path = save_texts_to_file(texts)

  embeddings_file_path = embed_texts_python(texts_file_path)

  embeddings = load_embeddings(embeddings_file_path) if !embeddings_file_path.nil?
  embeddings || []
end

#to_sObject



44
45
46
# File 'lib/aoororachain/embeddings/local_python_embedding.rb', line 44

def to_s
  "#{self.class} : #{@model} : #{@device}"
end