Class: Legion::LLM::ConfidenceScore

Inherits:
Data
  • Object
show all
Defined in:
lib/legion/llm/confidence_score.rb

Overview

Immutable value object representing a scored confidence level for an LLM response.

score - Float in [0.0, 1.0] band - Symbol: :very_low, :low, :medium, :high, :very_high source - Symbol: :heuristic, :logprobs, :caller_provided signals - Hash of contributing signals and their raw values (informational)

Constant Summary collapse

BAND_ORDER =

Band ordering from lowest to highest — defined outside the ::Data.define block so it is accessible as Legion::LLM::ConfidenceScore::BAND_ORDER.

%i[very_low low medium high very_high].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#bandObject (readonly)

Returns the value of attribute band

Returns:

  • (Object)

    the current value of band



11
12
13
# File 'lib/legion/llm/confidence_score.rb', line 11

def band
  @band
end

#scoreObject (readonly)

Returns the value of attribute score

Returns:

  • (Object)

    the current value of score



11
12
13
# File 'lib/legion/llm/confidence_score.rb', line 11

def score
  @score
end

#signalsObject (readonly)

Returns the value of attribute signals

Returns:

  • (Object)

    the current value of signals



11
12
13
# File 'lib/legion/llm/confidence_score.rb', line 11

def signals
  @signals
end

#sourceObject (readonly)

Returns the value of attribute source

Returns:

  • (Object)

    the current value of source



11
12
13
# File 'lib/legion/llm/confidence_score.rb', line 11

def source
  @source
end

Class Method Details

.build(score:, bands:, source: :heuristic, signals: {}) ⇒ Object



12
13
14
15
16
17
18
19
20
# File 'lib/legion/llm/confidence_score.rb', line 12

def self.build(score:, bands:, source: :heuristic, signals: {})
  clamped = score.to_f.clamp(0.0, 1.0)
  new(
    score:   clamped,
    band:    classify(clamped, bands),
    source:  source,
    signals: signals
  )
end

Instance Method Details

#at_least?(band_name) ⇒ Boolean

Returns true when the band is at or above the given band name.

Returns:

  • (Boolean)


23
24
25
# File 'lib/legion/llm/confidence_score.rb', line 23

def at_least?(band_name)
  Legion::LLM::ConfidenceScore::BAND_ORDER.index(band) >= Legion::LLM::ConfidenceScore::BAND_ORDER.index(band_name.to_sym)
end

#to_hObject



27
28
29
# File 'lib/legion/llm/confidence_score.rb', line 27

def to_h
  { score: score, band: band, source: source, signals: signals }
end