Class: Ragdoll::MetadataGenerator

Inherits:
Object
  • Object
show all
Defined in:
app/services/ragdoll/metadata_generator.rb

Overview

Service for generating structured metadata using LLM providers Leverages structured output capabilities to ensure consistent metadata schemas

Instance Method Summary collapse

Constructor Details

#initialize(llm_client: nil) ⇒ MetadataGenerator

Returns a new instance of MetadataGenerator.



7
8
9
# File 'app/services/ragdoll/metadata_generator.rb', line 7

def initialize(llm_client: nil)
  @llm_client = llm_client || default_llm_client
end

Instance Method Details

#generate_audio_metadata(document) ⇒ Object

Generate metadata for audio content



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'app/services/ragdoll/metadata_generator.rb', line 55

def (document)
  audio_content = document.audio_contents.first
  return {} unless audio_content

  schema = Ragdoll::MetadataSchemas::AUDIO_SCHEMA

  # Use transcript if available, otherwise analyze audio directly
  prompt = if audio_content.transcript.present?
             build_audio_transcript_analysis_prompt(audio_content.transcript, audio_content.duration)
           else
             # This would require audio-capable models or speech-to-text preprocessing
             build_audio_analysis_prompt(audio_content)
           end

  (prompt, schema)
end

#generate_for_document(document) ⇒ Object

Generate metadata for a document based on its content and type



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'app/services/ragdoll/metadata_generator.rb', line 12

def generate_for_document(document)
  case document.document_type
  when "text", "markdown", "html"
    (document)
  when "image"
    (document)
  when "audio"
    (document)
  when "pdf", "docx"
    (document)
  when "mixed"
    (document)
  else
    (document) # fallback
  end
end

#generate_image_metadata(document) ⇒ Object

Generate metadata for image content



42
43
44
45
46
47
48
49
50
51
52
# File 'app/services/ragdoll/metadata_generator.rb', line 42

def (document)
  # For images, we need to use vision-capable models
  image_content = document.image_contents.first
  return {} unless image_content&.image_attached?

  schema = Ragdoll::MetadataSchemas::IMAGE_SCHEMA
  prompt = build_image_analysis_prompt(image_content)

  # This would use a vision model like GPT-4V, Claude 3, etc.
  (prompt, schema, content_type: "image", image: image_content.image)
end

#generate_mixed_metadata(document) ⇒ Object

Generate metadata for mixed/multi-modal content



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'app/services/ragdoll/metadata_generator.rb', line 84

def (document)
  schema = Ragdoll::MetadataSchemas::MIXED_SCHEMA

  # Combine analysis from all content types
  content_summaries = []

  document.text_contents.each do |text|
    content_summaries << { type: "text", content: text.content[0..500] }
  end

  document.image_contents.each do |image|
    content_summaries << { type: "image", description: image.description || "Image content" }
  end

  document.audio_contents.each do |audio|
    content_summaries << { type: "audio", transcript: audio.transcript || "Audio content" }
  end

  prompt = build_mixed_analysis_prompt(content_summaries)
  (prompt, schema)
end

#generate_pdf_metadata(document) ⇒ Object

Generate metadata for PDF content



73
74
75
76
77
78
79
80
81
# File 'app/services/ragdoll/metadata_generator.rb', line 73

def (document)
  text_content = document.text_contents.map(&:content).join("\n\n")
  return {} if text_content.blank?

  schema = Ragdoll::MetadataSchemas::PDF_SCHEMA
  prompt = build_pdf_analysis_prompt(text_content, document.)

  (prompt, schema)
end

#generate_text_metadata(document) ⇒ Object

Generate metadata for text content



30
31
32
33
34
35
36
37
38
39
# File 'app/services/ragdoll/metadata_generator.rb', line 30

def (document)
  # Combine all text content from the document
  text_content = document.text_contents.map(&:content).join("\n\n")
  return {} if text_content.blank?

  schema = Ragdoll::MetadataSchemas::TEXT_SCHEMA
  prompt = build_text_analysis_prompt(text_content)

  (prompt, schema)
end