Class: Ragdoll::TextGenerationService

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

Defined Under Namespace

Classes: GenerationError

Instance Method Summary collapse

Constructor Details

#initialize(client: nil, config_service: nil, model_resolver: nil) ⇒ TextGenerationService

Returns a new instance of TextGenerationService.



9
10
11
12
13
14
# File 'app/services/ragdoll/text_generation_service.rb', line 9

def initialize(client: nil, config_service: nil, model_resolver: nil)
  @config_service = config_service || Ragdoll::ConfigurationService.new
  @model_resolver = model_resolver || Ragdoll::ModelResolver.new(@config_service)
  @client = client
  configure_ruby_llm_if_possible unless @client
end

Instance Method Details

#extract_keywords(text, max_keywords: 20) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'app/services/ragdoll/text_generation_service.rb', line 93

def extract_keywords(text, max_keywords: 20)
  return [] if text.nil? || text.strip.empty?

  # Clean and prepare text
  cleaned_text = clean_text(text)

  # Create keyword extraction prompt
  prompt = build_keyword_prompt(cleaned_text, max_keywords)

  begin
    if @client == :ruby_llm_configured
      # Use RubyLLM for keyword extraction
      # Use keywords model from models config, fallback to default
      model_obj = @model_resolver.resolve_for_task(:keywords)
      model = model_obj.model

      chat = RubyLLM.chat.with_model(model).with_temperature(0.1)
      chat.add_message(role: "user", content: prompt)
      response = chat.complete

      if response.respond_to?(:content)
        content = response.content.strip
        parse_keywords_response(content)
      elsif response.respond_to?(:message) && response.message.respond_to?(:content)
        content = response.message.content.strip
        parse_keywords_response(content)
      elsif response && response["choices"]&.first
        content = response["choices"].first["message"]["content"].strip
        parse_keywords_response(content)
      elsif response && response["content"]
        content = response["content"].strip
        parse_keywords_response(content)
      else
        raise GenerationError, "Invalid response format from text generation API"
      end
    elsif @client
      # Use custom client for testing
      model_obj = @model_resolver.resolve_for_task(:keywords)
      model = model_obj.model

      response = @client.chat(
        model: model,
        messages: [
          { role: "user", content: prompt }
        ],
        max_tokens: 200,
        temperature: 0.1
      )

      if response && response["choices"]&.first
        content = response["choices"].first["message"]["content"].strip
        parse_keywords_response(content)
      elsif response && response["content"]
        content = response["content"].strip
        parse_keywords_response(content)
      else
        raise GenerationError, "Invalid response format from text generation API"
      end
    else
      # Fallback to basic keyword extraction for testing/dev environments
      puts "⚠️  No LLM client configured, using fallback keyword extraction"
      extract_basic_keywords(cleaned_text, max_keywords)
    end
  rescue StandardError => e
    # Fall back to basic keyword extraction if API fails
    puts "❌ LLM keyword extraction failed, using fallback: #{e.message}"
    puts "Error class: #{e.class}"
    puts "Backtrace: #{e.backtrace.first(3).join(', ')}"
    extract_basic_keywords(cleaned_text, max_keywords)
  end
end

#generate_summary(text, max_length: nil) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'app/services/ragdoll/text_generation_service.rb', line 16

def generate_summary(text, max_length: nil)
  return "" if text.nil? || text.strip.empty?

  # Skip summarization if not enabled
  unless @config_service.config.summarization[:enable]
    puts "⚠️  LLM summarization disabled, using fallback (first 500 chars)"
    return text[0..500]
  end

  # Skip if content is too short
  min_length = @config_service.config.summarization[:min_content_length]
  return text if text.length < min_length

  max_length ||= @config_service.config.summarization[:max_length]

  # Clean and prepare text
  cleaned_text = clean_text(text)

  # Create summarization prompt
  prompt = build_summary_prompt(cleaned_text, max_length)

  begin
    if @client == :ruby_llm_configured
      # Use RubyLLM for text generation
      # Use model resolver to get summary model with inheritance
      model_obj = @model_resolver.resolve_for_task(:summary)
      model = model_obj.model

      chat = RubyLLM.chat.with_model(model)
                    .with_temperature(0.3)
      chat.add_message(role: "user", content: prompt)
      response = chat.complete

      if response.respond_to?(:content)
        response.content.strip
      elsif response.respond_to?(:message) && response.message.respond_to?(:content)
        response.message.content.strip
      elsif response && response["choices"]&.first
        response["choices"].first["message"]["content"].strip
      elsif response && response["content"]
        response["content"].strip
      else
        raise GenerationError, "Invalid response format from text generation API"
      end
    elsif @client
      # Use custom client for testing
      model_obj = @model_resolver.resolve_for_task(:summary)
      model = model_obj.model

      response = @client.chat(
        model: model,
        messages: [
          { role: "user", content: prompt }
        ],
        max_tokens: max_length + 50,
        temperature: 0.3
      )

      if response && response["choices"]&.first
        response["choices"].first["message"]["content"].strip
      elsif response && response["content"]
        response["content"].strip
      else
        raise GenerationError, "Invalid response format from text generation API"
      end
    else
      # Fallback to basic summarization for testing/dev environments
      puts "⚠️  No LLM client configured, using fallback summarization"
      generate_basic_summary(cleaned_text, max_length)
    end
  rescue StandardError => e
    # Fall back to basic summarization if API fails
    puts "❌ LLM summary generation failed, using fallback: #{e.message}"
    generate_basic_summary(cleaned_text, max_length)
  end
end