Class: ImageGeneration::TitleSuggester
- Inherits:
-
Object
- Object
- ImageGeneration::TitleSuggester
- Defined in:
- app/services/image_generation/title_suggester.rb
Overview
Generates a concise, SEO-aware image title from the generation prompt and
optional source image metadata using a cheap fast-text model via RubyLLM.
Returns a +Result+ value object with:
- +title+ [String] The suggested title (nil on failure)
- +input_tokens+ [Integer] Tokens consumed (nil if unavailable)
- +output_tokens+ [Integer]
- +model_id+ [String] Model that was used
- +error+ [String] Error message on failure (nil on success)
Defined Under Namespace
Classes: Result
Constant Summary collapse
- PREFERRED_MODELS =
Cheap, fast, adequate-quality models, tried in order of availability.
All ids come from the central registry (AiModelConstants) so they track
the current snapshots rather than pinning stale ones here. [ *AiModelConstants.candidates(:image_suggestion), AiModelConstants.id(:anthropic_haiku) ].freeze
Class Method Summary collapse
Instance Method Summary collapse
- #call ⇒ Object
-
#initialize(prompt:, source_title: nil, source_tags: []) ⇒ TitleSuggester
constructor
A new instance of TitleSuggester.
Constructor Details
#initialize(prompt:, source_title: nil, source_tags: []) ⇒ TitleSuggester
Returns a new instance of TitleSuggester.
47 48 49 50 51 |
# File 'app/services/image_generation/title_suggester.rb', line 47 def initialize(prompt:, source_title: nil, source_tags: []) @prompt = prompt.to_s.strip @source_title = source_title.to_s.presence @source_tags = Array().compact_blank - Image::PROVENANCE_TAGS end |
Class Method Details
.call(prompt:, source_title: nil, source_tags: []) ⇒ Object
43 44 45 |
# File 'app/services/image_generation/title_suggester.rb', line 43 def self.call(prompt:, source_title: nil, source_tags: []) new(prompt: prompt, source_title: source_title, source_tags: ).call end |
Instance Method Details
#call ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'app/services/image_generation/title_suggester.rb', line 53 def call model = select_model return Result.new(error: 'No LLM model available for title suggestion') unless model = generate_title(model) title = clean_title(.content.to_s) Result.new( title: title, input_tokens: .input_tokens, output_tokens: .output_tokens, model_id: model ) rescue RubyLLM::RateLimitError => e Rails.logger.warn "[TitleSuggester] Rate limited: #{e.}" Result.new(error: "Rate limited — please retry shortly") rescue RubyLLM::Error => e Rails.logger.warn "[TitleSuggester] RubyLLM error: #{e.}" Result.new(error: e.) rescue StandardError => e Rails.logger.warn "[TitleSuggester] Failed: #{e.}" Result.new(error: e.) end |