Class: Faq::ContentCondenser

Inherits:
Object
  • Object
show all
Defined in:
app/services/faq/content_condenser.rb

Defined Under Namespace

Classes: Result

Constant Summary collapse

MAX_WORDS =
ArticleFaq::FAQ_MAX_WORDS
TARGET_WORDS =
ArticleFaq::FAQ_TARGET_WORDS

Instance Method Summary collapse

Constructor Details

#initialize(article) ⇒ ContentCondenser

Returns a new instance of ContentCondenser.



14
15
16
# File 'app/services/faq/content_condenser.rb', line 14

def initialize(article)
  @article = article
end

Instance Method Details

#improveObject Also known as: condense



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'app/services/faq/content_condenser.rb', line 18

def improve
  return Result.new(success: false, error: 'Article is not a FAQ') unless @article.is_a?(ArticleFaq)
  return Result.new(success: false, error: 'FAQ has no answer content') if @article.solution.blank?

  original_html = @article.solution
  original_wc = word_count(original_html)

  improved = call_llm(original_html, @article.subject, original_wc)
  improved_wc = word_count(improved)

  Result.new(
    success: true,
    original_html: original_html,
    output_html: improved,
    original_word_count: original_wc,
    output_word_count: improved_wc
  )
rescue StandardError => e
  ErrorReporting.error(e, context: { article_id: @article.id, tool: 'faq_improver' })
  Result.new(success: false, error: "AI improvement failed: #{e.message}")
end