Class: RubyLlmMesh::Rag::Embeddings
- Inherits:
-
Object
- Object
- RubyLlmMesh::Rag::Embeddings
- Defined in:
- lib/ruby_llm_mesh/rag/embeddings.rb
Overview
Zero-dependency bag-of-words style embeddings for local prototyping. Swap for a real embedding provider in production.
Instance Method Summary collapse
- #cosine_similarity(a, b) ⇒ Object
- #embed(text) ⇒ Object
- #embed_many(texts) ⇒ Object
-
#initialize(dimensions: 256) ⇒ Embeddings
constructor
A new instance of Embeddings.
- #top_k(query, documents, k: 3) ⇒ Object
Constructor Details
#initialize(dimensions: 256) ⇒ Embeddings
Returns a new instance of Embeddings.
8 9 10 |
# File 'lib/ruby_llm_mesh/rag/embeddings.rb', line 8 def initialize(dimensions: 256) @dimensions = dimensions end |
Instance Method Details
#cosine_similarity(a, b) ⇒ Object
26 27 28 29 30 31 32 |
# File 'lib/ruby_llm_mesh/rag/embeddings.rb', line 26 def cosine_similarity(a, b) raise ArgumentError, "vectors must match length" unless a.length == b.length dot = 0.0 a.each_index { |i| dot += a[i] * b[i] } dot end |
#embed(text) ⇒ Object
12 13 14 15 16 17 18 19 20 |
# File 'lib/ruby_llm_mesh/rag/embeddings.rb', line 12 def (text) vector = Array.new(@dimensions, 0.0) tokenize(text).each do |token| index = stable_hash(token) % @dimensions vector[index] += 1.0 end normalize!(vector) vector end |
#embed_many(texts) ⇒ Object
22 23 24 |
# File 'lib/ruby_llm_mesh/rag/embeddings.rb', line 22 def (texts) Array(texts).map { |t| (t) } end |
#top_k(query, documents, k: 3) ⇒ Object
34 35 36 37 38 39 40 41 42 |
# File 'lib/ruby_llm_mesh/rag/embeddings.rb', line 34 def top_k(query, documents, k: 3) query_vec = (query) scored = documents.map do |doc| text = doc.is_a?(Hash) ? doc[:text] || doc["text"] : doc.to_s score = cosine_similarity(query_vec, (text)) { document: doc, score: score } end scored.sort_by { |row| -row[:score] }.first(k) end |