Class: Ragnar::Embedder
- Inherits:
-
Object
- Object
- Ragnar::Embedder
- Defined in:
- lib/ragnar/embedder.rb
Instance Attribute Summary collapse
-
#model ⇒ Object
readonly
Returns the value of attribute model.
-
#model_name ⇒ Object
readonly
Returns the value of attribute model_name.
Class Method Summary collapse
Instance Method Summary collapse
- #embed_batch(texts, show_progress: true) ⇒ Object
- #embed_chunks(chunks, show_progress: true) ⇒ Object
- #embed_text(text) ⇒ Object
-
#initialize(model_name: Ragnar::DEFAULT_EMBEDDING_MODEL) ⇒ Embedder
constructor
A new instance of Embedder.
Constructor Details
#initialize(model_name: Ragnar::DEFAULT_EMBEDDING_MODEL) ⇒ Embedder
Returns a new instance of Embedder.
5 6 7 8 |
# File 'lib/ragnar/embedder.rb', line 5 def initialize(model_name: Ragnar::DEFAULT_EMBEDDING_MODEL) @model_name = model_name @model = load_model(model_name) end |
Instance Attribute Details
#model ⇒ Object (readonly)
Returns the value of attribute model.
3 4 5 |
# File 'lib/ragnar/embedder.rb', line 3 def model @model end |
#model_name ⇒ Object (readonly)
Returns the value of attribute model_name.
3 4 5 |
# File 'lib/ragnar/embedder.rb', line 3 def model_name @model_name end |
Class Method Details
.available_models ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/ragnar/embedder.rb', line 89 def self.available_models # List of commonly used embedding models # This could be expanded or made dynamic [ "BAAI/bge-small-en-v1.5", "BAAI/bge-base-en-v1.5", "BAAI/bge-large-en-v1.5", "sentence-transformers/all-MiniLM-L6-v2", "sentence-transformers/all-mpnet-base-v2", "thenlper/gte-small", "thenlper/gte-base", "thenlper/gte-large" ] end |
.model_info(model_name) ⇒ Object
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/ragnar/embedder.rb', line 104 def self.model_info(model_name) # Provide information about embedding models info = { "BAAI/bge-small-en-v1.5" => { dimensions: 384, max_tokens: 512, description: "Small, fast, good quality embeddings" }, "BAAI/bge-base-en-v1.5" => { dimensions: 768, max_tokens: 512, description: "Balanced size and quality" }, "BAAI/bge-large-en-v1.5" => { dimensions: 1024, max_tokens: 512, description: "Large, highest quality embeddings" }, "sentence-transformers/all-MiniLM-L6-v2" => { dimensions: 384, max_tokens: 256, description: "Fast, lightweight model" }, "sentence-transformers/all-mpnet-base-v2" => { dimensions: 768, max_tokens: 384, description: "High quality general purpose embeddings" } } info[model_name] || { description: "Model information not available" } end |
Instance Method Details
#embed_batch(texts, show_progress: true) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/ragnar/embedder.rb', line 34 def (texts, show_progress: true) = [] if show_progress && $stdout.respond_to?(:ioctl) = TTY::ProgressBar.new( "Generating embeddings [:bar] :percent :current/:total", total: texts.size, bar_format: :block, width: 30 ) end texts.each do |text| = (text) << .advance if show_progress end end |
#embed_chunks(chunks, show_progress: true) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/ragnar/embedder.rb', line 55 def (chunks, show_progress: true) texts = chunks.map do |chunk| if chunk.is_a?(Hash) chunk[:text] || chunk["text"] else chunk.to_s end end (texts, show_progress: show_progress) end |
#embed_text(text) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/ragnar/embedder.rb', line 10 def (text) return nil if text.nil? || text.empty? || (text.respond_to?(:strip) && text.strip.empty?) # Use Candle to generate embeddings # The embedding method returns a tensor, we need to convert to array = @model.(text) # Convert tensor to array - Candle tensors need double to_a # First to_a gives [tensor], second to_a on the tensor gives the float array if .respond_to?(:to_a) result = .to_a if result.is_a?(Array) && result.first.respond_to?(:to_a) result.first.to_a else result end else end rescue => e puts "Error generating embedding: #{e.}" nil end |