Class: Legion::LLM::Router::Rule

Inherits:
Object
  • Object
show all
Includes:
Legion::Logging::Helper
Defined in:
lib/legion/llm/router/rule.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, conditions:, target:, priority: 0, constraint: nil, fallback: nil, cost_multiplier: 1.0, schedule: nil, note: nil) ⇒ Rule

Returns a new instance of Rule.



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/legion/llm/router/rule.rb', line 30

def initialize(name:, conditions:, target:, priority: 0, constraint: nil, fallback: nil,
               cost_multiplier: 1.0, schedule: nil, note: nil)
  @name            = name
  @conditions      = conditions.transform_keys(&:to_sym)
  @target          = target.transform_keys(&:to_sym)
  @priority        = priority
  @constraint      = constraint
  @fallback        = fallback
  @cost_multiplier = cost_multiplier
  @schedule        = schedule
  @note            = note
end

Instance Attribute Details

#conditionsObject (readonly)

Returns the value of attribute conditions.



13
14
15
# File 'lib/legion/llm/router/rule.rb', line 13

def conditions
  @conditions
end

#constraintObject (readonly)

Returns the value of attribute constraint.



13
14
15
# File 'lib/legion/llm/router/rule.rb', line 13

def constraint
  @constraint
end

#cost_multiplierObject (readonly)

Returns the value of attribute cost_multiplier.



13
14
15
# File 'lib/legion/llm/router/rule.rb', line 13

def cost_multiplier
  @cost_multiplier
end

#fallbackObject (readonly)

Returns the value of attribute fallback.



13
14
15
# File 'lib/legion/llm/router/rule.rb', line 13

def fallback
  @fallback
end

#nameObject (readonly)

Returns the value of attribute name.



13
14
15
# File 'lib/legion/llm/router/rule.rb', line 13

def name
  @name
end

#noteObject (readonly)

Returns the value of attribute note.



13
14
15
# File 'lib/legion/llm/router/rule.rb', line 13

def note
  @note
end

#priorityObject (readonly)

Returns the value of attribute priority.



13
14
15
# File 'lib/legion/llm/router/rule.rb', line 13

def priority
  @priority
end

#scheduleObject (readonly)

Returns the value of attribute schedule.



13
14
15
# File 'lib/legion/llm/router/rule.rb', line 13

def schedule
  @schedule
end

#targetObject (readonly)

Returns the value of attribute target.



13
14
15
# File 'lib/legion/llm/router/rule.rb', line 13

def target
  @target
end

Class Method Details

.from_hash(hash) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/legion/llm/router/rule.rb', line 15

def self.from_hash(hash)
  h = hash.transform_keys(&:to_sym)
  new(
    name:            h[:name],
    conditions:      h[:when]  || {},
    target:          h[:then]  || {},
    priority:        h.fetch(:priority, 0),
    constraint:      h[:constraint],
    fallback:        h[:fallback].is_a?(Hash) ? h[:fallback].transform_keys(&:to_sym) : h[:fallback]&.to_sym,
    cost_multiplier: h.fetch(:cost_multiplier, 1.0),
    schedule:        h[:schedule],
    note:            h[:note]
  )
end

Instance Method Details

#matches_intent?(intent) ⇒ Boolean

Returns:

  • (Boolean)


43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/legion/llm/router/rule.rb', line 43

def matches_intent?(intent)
  @conditions.all? do |key, value|
    unless intent.key?(key)
      log.debug("Rule '#{@name}' rejected: missing intent key=#{key}")
      return false
    end

    unless intent[key].to_s == value.to_s
      log.debug("Rule '#{@name}' rejected: intent #{key}=#{intent[key]} != #{value}")
      return false
    end

    true
  end
end

#to_resolutionObject



59
60
61
62
63
64
65
66
67
# File 'lib/legion/llm/router/rule.rb', line 59

def to_resolution
  target_without_compress = @target.except(:compress_level)
  Resolution.new(
    **target_without_compress,
    rule:           @name,
    metadata:       { cost_multiplier: @cost_multiplier, fallback: @fallback }.compact,
    compress_level: @target.fetch(:compress_level, 0)
  )
end

#within_schedule?(now = Time.now) ⇒ Boolean

Returns:

  • (Boolean)


69
70
71
72
73
74
75
# File 'lib/legion/llm/router/rule.rb', line 69

def within_schedule?(now = Time.now)
  return true if @schedule.nil? || (@schedule.respond_to?(:empty?) && @schedule.empty?)

  sched = @schedule.transform_keys(&:to_s)
  now = localize(now, sched['timezone'])
  schedule_rejection(sched, now).nil?
end