Class: Aoororachain::Embeddings::LocalPythonEmbedding
- Inherits:
-
Object
- Object
- Aoororachain::Embeddings::LocalPythonEmbedding
- 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
- #embed_documents(documents, include_metadata: false) ⇒ Object
- #embed_query(text) ⇒ Object
- #embed_texts(texts) ⇒ Object
-
#initialize(options = {}) ⇒ LocalPythonEmbedding
constructor
A new instance of LocalPythonEmbedding.
- #to_s ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ LocalPythonEmbedding
Returns a new instance of LocalPythonEmbedding.
10 11 12 13 14 15 16 17 18 |
# File 'lib/aoororachain/embeddings/local_python_embedding.rb', line 10 def initialize( = {}) @model = .delete(:model) || MODEL_ALL_MPNET @device = .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. First time initialization might take long due to Python dependencies installation.") install_python_dependencies end |
Instance Method Details
#embed_documents(documents, include_metadata: false) ⇒ Object
20 21 22 23 24 25 |
# File 'lib/aoororachain/embeddings/local_python_embedding.rb', line 20 def (documents, include_metadata: false) texts = documents.map { |document| "#{document.content} #{ ? document..to_json : ""}.strip" } = (texts) || [] end |
#embed_query(text) ⇒ Object
39 40 41 42 43 44 |
# File 'lib/aoororachain/embeddings/local_python_embedding.rb', line 39 def (text) return [] if text.nil? || text.strip == "" Aoororachain::Util.log_info("First time usage might take long time due to models download.") [(text)] end |
#embed_texts(texts) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/aoororachain/embeddings/local_python_embedding.rb', line 27 def (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) = (texts_file_path) = () if !.nil? || [] end |
#to_s ⇒ Object
46 47 48 |
# File 'lib/aoororachain/embeddings/local_python_embedding.rb', line 46 def to_s "#{self.class} : #{@model} : #{@device}" end |