Class: RubyLLM::SemanticRouter::RoutingDecision

Inherits:
Object
  • Object
show all
Defined in:
lib/rubyllm/semantic_router/routing_decision.rb

Overview

Value object representing a routing decision

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(agent:, confidence: 0.0, matched_example: nil, reason: :semantic_match, inject_instruction: nil) ⇒ RoutingDecision

Returns a new instance of RoutingDecision.



22
23
24
25
26
27
28
# File 'lib/rubyllm/semantic_router/routing_decision.rb', line 22

def initialize(agent:, confidence: 0.0, matched_example: nil, reason: :semantic_match, inject_instruction: nil)
  @agent = agent&.to_sym
  @confidence = confidence.to_f
  @matched_example = matched_example
  @reason = reason
  @inject_instruction = inject_instruction
end

Instance Attribute Details

#agentObject (readonly)

The name of the agent to route to



8
9
10
# File 'lib/rubyllm/semantic_router/routing_decision.rb', line 8

def agent
  @agent
end

#confidenceObject (readonly)

Confidence score (0.0 - 1.0) of the routing decision



11
12
13
# File 'lib/rubyllm/semantic_router/routing_decision.rb', line 11

def confidence
  @confidence
end

#inject_instructionObject (readonly)

Optional instruction to inject (used for ask_clarification)



20
21
22
# File 'lib/rubyllm/semantic_router/routing_decision.rb', line 20

def inject_instruction
  @inject_instruction
end

#matched_exampleObject (readonly)

The example text that matched (for debugging)



14
15
16
# File 'lib/rubyllm/semantic_router/routing_decision.rb', line 14

def matched_example
  @matched_example
end

#reasonObject (readonly)

Reason for the decision (:semantic_match, :fallback, :kept_current, :needs_clarification)



17
18
19
# File 'lib/rubyllm/semantic_router/routing_decision.rb', line 17

def reason
  @reason
end

Instance Method Details

#==(other) ⇒ Object



55
56
57
58
59
60
61
# File 'lib/rubyllm/semantic_router/routing_decision.rb', line 55

def ==(other)
  return false unless other.is_a?(RoutingDecision)

  agent == other.agent &&
    confidence == other.confidence &&
    reason == other.reason
end

#confident?Boolean

Returns true if this was a confident semantic match

Returns:

  • (Boolean)


31
32
33
# File 'lib/rubyllm/semantic_router/routing_decision.rb', line 31

def confident?
  reason == :semantic_match && confidence > 0
end

#fallback?Boolean

Returns true if this decision used a fallback

Returns:

  • (Boolean)


36
37
38
# File 'lib/rubyllm/semantic_router/routing_decision.rb', line 36

def fallback?
  %i[fallback kept_current needs_clarification].include?(reason)
end

#inspectObject



63
64
65
# File 'lib/rubyllm/semantic_router/routing_decision.rb', line 63

def inspect
  "#<RoutingDecision agent=#{agent.inspect} confidence=#{confidence.round(3)} reason=#{reason.inspect}>"
end

#needs_clarification?Boolean

Returns true if clarification is needed

Returns:

  • (Boolean)


41
42
43
# File 'lib/rubyllm/semantic_router/routing_decision.rb', line 41

def needs_clarification?
  reason == :needs_clarification
end

#to_hObject



45
46
47
48
49
50
51
52
53
# File 'lib/rubyllm/semantic_router/routing_decision.rb', line 45

def to_h
  {
    agent: agent,
    confidence: confidence,
    matched_example: matched_example,
    reason: reason,
    inject_instruction: inject_instruction
  }.compact
end