Class: Botiasloop::AutoLabel

Inherits:
Object
  • Object
show all
Defined in:
lib/botiasloop/auto_label.rb

Overview

Service class for automatically generating conversation labels Triggered after 3rd user message if no label is set

Constant Summary collapse

MIN_MESSAGES_FOR_AUTO_LABEL =

3 user + 3 assistant messages

6

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeAutoLabel

Returns a new instance of AutoLabel.



38
39
40
# File 'lib/botiasloop/auto_label.rb', line 38

def initialize
  @config = Config.instance
end

Class Method Details

.generate(conversation) ⇒ String?

Generate a label for the conversation if conditions are met

Parameters:

Returns:

  • (String, nil)

    The generated label or nil if not applicable



16
17
18
19
20
21
22
23
24
# File 'lib/botiasloop/auto_label.rb', line 16

def self.generate(conversation)
  return nil unless should_generate?(conversation)

  label = new.generate_label(conversation)

  Logger.info "[AutoLabel] Generated label '#{label}' for conversation #{conversation.uuid}" if label

  label
end

.should_generate?(conversation) ⇒ Boolean

Check if auto-labelling should run

Parameters:

Returns:

  • (Boolean)

    True if conditions are met



30
31
32
33
34
35
36
# File 'lib/botiasloop/auto_label.rb', line 30

def self.should_generate?(conversation)
  return false unless Config.instance.features&.dig("auto_labelling", "enabled") != false
  return false if conversation.label?
  return false if conversation.message_count < MIN_MESSAGES_FOR_AUTO_LABEL

  true
end

Instance Method Details

#generate_label(conversation) ⇒ String?

Generate a label based on conversation content

Parameters:

Returns:

  • (String, nil)

    The generated and formatted label



46
47
48
49
50
51
52
53
54
55
# File 'lib/botiasloop/auto_label.rb', line 46

def generate_label(conversation)
  messages = conversation.history
  raw_label = generate_label_text(messages)
  return nil unless raw_label

  formatted_label = format_label(raw_label)
  return nil unless valid_label?(formatted_label)

  formatted_label
end