Class: DSPy::Memory::LocalEmbeddingEngine

Inherits:
EmbeddingEngine show all
Extended by:
T::Sig
Defined in:
lib/dspy/memory/local_embedding_engine.rb

Overview

Local embedding engine using ankane/informers for privacy-preserving embeddings

Constant Summary collapse

DEFAULT_MODEL =

Default models supported by informers

'Xenova/all-MiniLM-L6-v2'
SUPPORTED_MODELS =
[
  'Xenova/all-MiniLM-L6-v2',
  'Xenova/all-MiniLM-L12-v2',
  'Xenova/multi-qa-MiniLM-L6-cos-v1',
  'Xenova/paraphrase-MiniLM-L6-v2'
].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from EmbeddingEngine

#cosine_similarity, #normalize_vector

Constructor Details

#initialize(model_name = DEFAULT_MODEL) ⇒ LocalEmbeddingEngine

Returns a new instance of LocalEmbeddingEngine.



28
29
30
31
32
33
34
35
# File 'lib/dspy/memory/local_embedding_engine.rb', line 28

def initialize(model_name = DEFAULT_MODEL)
  @model_name = model_name
  @model = T.let(nil, T.nilable(T.untyped))
  @embedding_dim = T.let(nil, T.nilable(Integer))
  @ready = T.let(false, T::Boolean)
  
  load_model!
end

Instance Attribute Details

#model_nameObject (readonly)

Returns the value of attribute model_name.



25
26
27
# File 'lib/dspy/memory/local_embedding_engine.rb', line 25

def model_name
  @model_name
end

Class Method Details

.model_supported?(model_name) ⇒ Boolean

Returns:

  • (Boolean)


90
91
92
# File 'lib/dspy/memory/local_embedding_engine.rb', line 90

def self.model_supported?(model_name)
  SUPPORTED_MODELS.include?(model_name)
end

.supported_modelsObject



96
97
98
# File 'lib/dspy/memory/local_embedding_engine.rb', line 96

def self.supported_models
  SUPPORTED_MODELS
end

Instance Method Details

#embed(text) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/dspy/memory/local_embedding_engine.rb', line 38

def embed(text)
  ensure_ready!
  
  # Preprocess text
  cleaned_text = preprocess_text(text)
  
  # Generate embedding
  result = @model.call(cleaned_text)
  
  # Extract embedding array and normalize
  embedding = result.first.to_a
  normalize_vector(embedding)
end

#embed_batch(texts) ⇒ Object



53
54
55
56
57
58
59
60
# File 'lib/dspy/memory/local_embedding_engine.rb', line 53

def embed_batch(texts)
  ensure_ready!
  
  # Generate embeddings one by one (informers doesn't support true batch processing)
  texts.map do |text|
    embed(text)
  end
end

#embedding_dimensionObject



63
64
65
# File 'lib/dspy/memory/local_embedding_engine.rb', line 63

def embedding_dimension
  @embedding_dim || load_model_info!
end

#ready?Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/dspy/memory/local_embedding_engine.rb', line 73

def ready?
  @ready
end

#statsObject



78
79
80
81
82
83
84
85
86
# File 'lib/dspy/memory/local_embedding_engine.rb', line 78

def stats
  {
    model_name: @model_name,
    embedding_dimension: embedding_dimension,
    ready: ready?,
    supported_models: SUPPORTED_MODELS,
    backend: 'informers'
  }
end