Class: Legion::LLM::Router::Rule
- Inherits:
-
Object
- Object
- Legion::LLM::Router::Rule
- Includes:
- Legion::Logging::Helper
- Defined in:
- lib/legion/llm/router/rule.rb
Instance Attribute Summary collapse
-
#conditions ⇒ Object
readonly
Returns the value of attribute conditions.
-
#constraint ⇒ Object
readonly
Returns the value of attribute constraint.
-
#cost_multiplier ⇒ Object
readonly
Returns the value of attribute cost_multiplier.
-
#fallback ⇒ Object
readonly
Returns the value of attribute fallback.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#note ⇒ Object
readonly
Returns the value of attribute note.
-
#priority ⇒ Object
readonly
Returns the value of attribute priority.
-
#schedule ⇒ Object
readonly
Returns the value of attribute schedule.
-
#target ⇒ Object
readonly
Returns the value of attribute target.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(name:, conditions:, target:, priority: 0, constraint: nil, fallback: nil, cost_multiplier: 1.0, schedule: nil, note: nil) ⇒ Rule
constructor
A new instance of Rule.
- #matches_intent?(intent) ⇒ Boolean
- #to_resolution ⇒ Object
- #within_schedule?(now = Time.now) ⇒ Boolean
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
#conditions ⇒ Object (readonly)
Returns the value of attribute conditions.
13 14 15 |
# File 'lib/legion/llm/router/rule.rb', line 13 def conditions @conditions end |
#constraint ⇒ Object (readonly)
Returns the value of attribute constraint.
13 14 15 |
# File 'lib/legion/llm/router/rule.rb', line 13 def constraint @constraint end |
#cost_multiplier ⇒ Object (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 |
#fallback ⇒ Object (readonly)
Returns the value of attribute fallback.
13 14 15 |
# File 'lib/legion/llm/router/rule.rb', line 13 def fallback @fallback end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
13 14 15 |
# File 'lib/legion/llm/router/rule.rb', line 13 def name @name end |
#note ⇒ Object (readonly)
Returns the value of attribute note.
13 14 15 |
# File 'lib/legion/llm/router/rule.rb', line 13 def note @note end |
#priority ⇒ Object (readonly)
Returns the value of attribute priority.
13 14 15 |
# File 'lib/legion/llm/router/rule.rb', line 13 def priority @priority end |
#schedule ⇒ Object (readonly)
Returns the value of attribute schedule.
13 14 15 |
# File 'lib/legion/llm/router/rule.rb', line 13 def schedule @schedule end |
#target ⇒ Object (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
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_resolution ⇒ Object
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
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 |