Class: Ragdoll::Content

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/ragdoll/content.rb

Direct Known Subclasses

AudioContent, ImageContent, TextContent

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.search_content(query, **options) ⇒ Object

Search within this content type



86
87
88
89
90
91
92
93
# File 'app/models/ragdoll/content.rb', line 86

def self.search_content(query, **options)
  return none if query.blank?

  where(
    "to_tsvector('english', COALESCE(content, '')) @@ plainto_tsquery('english', ?)",
    query
  ).limit(options[:limit] || 20)
end

Instance Method Details

#character_countObject



77
78
79
# File 'app/models/ragdoll/content.rb', line 77

def character_count
  content&.length || 0
end

#content_for_embeddingObject

Content to use for embedding generation (overridden by subclasses)



62
63
64
# File 'app/models/ragdoll/content.rb', line 62

def content_for_embedding
  content
end

#embedding_countObject



81
82
83
# File 'app/models/ragdoll/content.rb', line 81

def embedding_count
  embeddings.count
end

#generate_embeddings!Object

Generate embeddings for this content



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'app/models/ragdoll/content.rb', line 29

def generate_embeddings!
  return unless should_generate_embeddings?

  embedding_content = content_for_embedding
  return if embedding_content.blank?

  # Clear existing embeddings
  embeddings.destroy_all

  # Use TextChunker to split content into chunks
  chunks = Ragdoll::TextChunker.chunk(embedding_content)

  # Generate embeddings for each chunk
  embedding_service = Ragdoll::EmbeddingService.new

  chunks.each_with_index do |chunk_text, index|
    begin
      vector = embedding_service.generate_embedding(chunk_text)

      embeddings.create!(
        content: chunk_text,
        embedding_vector: vector,
        chunk_index: index
      )
    rescue StandardError => e
      puts "Failed to generate embedding for chunk #{index}: #{e.message}"
    end
  end

  update!(metadata: .merge("embeddings_generated_at" => Time.current))
end

#should_generate_embeddings?Boolean

Whether this content should generate embeddings

Returns:

  • (Boolean)


67
68
69
# File 'app/models/ragdoll/content.rb', line 67

def should_generate_embeddings?
  content_for_embedding.present? && embeddings.empty?
end

#word_countObject

Statistics



72
73
74
75
# File 'app/models/ragdoll/content.rb', line 72

def word_count
  return 0 unless content.present?
  content.split(/\s+/).length
end