Class: Hyrum::Validators::SemanticSimilarity

Inherits:
Object
  • Object
show all
Defined in:
lib/hyrum/validators/semantic_similarity.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(original_message, variations, ai_service, ai_model) ⇒ SemanticSimilarity

Returns a new instance of SemanticSimilarity.



11
12
13
14
15
16
# File 'lib/hyrum/validators/semantic_similarity.rb', line 11

def initialize(original_message, variations, ai_service, ai_model)
  @original_message = original_message
  @variations = variations
  @ai_service = ai_service
  @ai_model = ai_model
end

Instance Attribute Details

#ai_modelObject (readonly)

Returns the value of attribute ai_model.



9
10
11
# File 'lib/hyrum/validators/semantic_similarity.rb', line 9

def ai_model
  @ai_model
end

#ai_serviceObject (readonly)

Returns the value of attribute ai_service.



9
10
11
# File 'lib/hyrum/validators/semantic_similarity.rb', line 9

def ai_service
  @ai_service
end

#original_messageObject (readonly)

Returns the value of attribute original_message.



9
10
11
# File 'lib/hyrum/validators/semantic_similarity.rb', line 9

def original_message
  @original_message
end

#variationsObject (readonly)

Returns the value of attribute variations.



9
10
11
# File 'lib/hyrum/validators/semantic_similarity.rb', line 9

def variations
  @variations
end

Instance Method Details

#calculateObject



18
19
20
21
22
23
24
25
26
# File 'lib/hyrum/validators/semantic_similarity.rb', line 18

def calculate
  return 100.0 if variations.empty?

  if supports_embeddings?
    calculate_with_embeddings
  else
    calculate_with_fallback
  end
end

#supports_embeddings?Boolean

Returns:

  • (Boolean)


28
29
30
31
32
33
34
35
# File 'lib/hyrum/validators/semantic_similarity.rb', line 28

def supports_embeddings?
  # Check if RubyLLM has any embedding models available in the current registry
  # User is responsible for calling RubyLLM.models.refresh! if needed
  RubyLLM.models.embedding_models.any?
rescue StandardError
  # If we can't check the registry, assume embeddings aren't available
  false
end