Module: RailsAi::Embeddable

Extended by:
ActiveSupport::Concern
Defined in:
app/models/concerns/ai/embeddable.rb

Instance Method Summary collapse

Instance Method Details

#generate_embedding!(field: :content, model: nil) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'app/models/concerns/ai/embeddable.rb', line 11

def generate_embedding!(field: :content, model: nil)
  text = send(field)
  return if text.blank?

  model ||= RailsAi.config.default_model

  embedding = RailsAi.provider.embed!(
    texts: [text],
    model: model
  ).first

  ai_embeddings.create!(
    content: text,
    embedding: embedding,
    model: model,
    field: field.to_s
  )
end

#similar_records(limit: 5, threshold: 0.8) ⇒ Object



30
31
32
33
34
35
36
# File 'app/models/concerns/ai/embeddable.rb', line 30

def similar_records(limit: 5, threshold: 0.8)
  return [] unless ai_embeddings.any?

  # This would need to be implemented based on your vector database
  # For now, return empty array
  []
end