Class: Ragdoll::Content
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Ragdoll::Content
- Defined in:
- app/models/ragdoll/content.rb
Direct Known Subclasses
Class Method Summary collapse
-
.search_content(query, **options) ⇒ Object
Search within this content type.
Instance Method Summary collapse
- #character_count ⇒ Object
-
#content_for_embedding ⇒ Object
Content to use for embedding generation (overridden by subclasses).
- #embedding_count ⇒ Object
-
#generate_embeddings! ⇒ Object
Generate embeddings for this content.
-
#should_generate_embeddings? ⇒ Boolean
Whether this content should generate embeddings.
-
#word_count ⇒ Object
Statistics.
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, **) return none if query.blank? where( "to_tsvector('english', COALESCE(content, '')) @@ plainto_tsquery('english', ?)", query ).limit([:limit] || 20) end |
Instance Method Details
#character_count ⇒ Object
77 78 79 |
# File 'app/models/ragdoll/content.rb', line 77 def character_count content&.length || 0 end |
#content_for_embedding ⇒ Object
Content to use for embedding generation (overridden by subclasses)
62 63 64 |
# File 'app/models/ragdoll/content.rb', line 62 def content end |
#embedding_count ⇒ Object
81 82 83 |
# File 'app/models/ragdoll/content.rb', line 81 def .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 return unless = return if .blank? # Clear existing embeddings .destroy_all # Use TextChunker to split content into chunks chunks = Ragdoll::TextChunker.chunk() # Generate embeddings for each chunk = Ragdoll::EmbeddingService.new chunks.each_with_index do |chunk_text, index| begin vector = .(chunk_text) .create!( content: chunk_text, embedding_vector: vector, chunk_index: index ) rescue StandardError => e puts "Failed to generate embedding for chunk #{index}: #{e.}" end end update!(metadata: .merge("embeddings_generated_at" => Time.current)) end |
#should_generate_embeddings? ⇒ Boolean
Whether this content should generate embeddings
67 68 69 |
# File 'app/models/ragdoll/content.rb', line 67 def .present? && .empty? end |
#word_count ⇒ Object
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 |