Class: RubyLLM::Contract::Step::Trace

Inherits:
Object
  • Object
show all
Includes:
Concerns::DeepFreeze, Concerns::TraceEquality
Defined in:
lib/ruby_llm/contract/step/trace.rb

Constant Summary collapse

KNOWN_KEYS =
%i[messages model latency_ms usage attempts cost].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Concerns::TraceEquality

#==

Constructor Details

#initialize(messages: nil, model: nil, latency_ms: nil, usage: nil, attempts: nil, cost: nil) ⇒ Trace

Returns a new instance of Trace.



12
13
14
15
16
17
18
19
20
# File 'lib/ruby_llm/contract/step/trace.rb', line 12

def initialize(messages: nil, model: nil, latency_ms: nil, usage: nil, attempts: nil, cost: nil)
  @messages = deep_dup_freeze(messages)
  @model = model.frozen? ? model : model&.dup&.freeze
  @latency_ms = latency_ms
  @usage = deep_dup_freeze(usage)
  @attempts = deep_dup_freeze(attempts)
  @cost = cost || CostCalculator.calculate(model_name: model, usage: usage)
  freeze
end

Instance Attribute Details

#attemptsObject (readonly)

Returns the value of attribute attempts.



10
11
12
# File 'lib/ruby_llm/contract/step/trace.rb', line 10

def attempts
  @attempts
end

#costObject (readonly)

Returns the value of attribute cost.



10
11
12
# File 'lib/ruby_llm/contract/step/trace.rb', line 10

def cost
  @cost
end

#latency_msObject (readonly)

Returns the value of attribute latency_ms.



10
11
12
# File 'lib/ruby_llm/contract/step/trace.rb', line 10

def latency_ms
  @latency_ms
end

#messagesObject (readonly)

Returns the value of attribute messages.



10
11
12
# File 'lib/ruby_llm/contract/step/trace.rb', line 10

def messages
  @messages
end

#modelObject (readonly)

Returns the value of attribute model.



10
11
12
# File 'lib/ruby_llm/contract/step/trace.rb', line 10

def model
  @model
end

#usageObject (readonly)

Returns the value of attribute usage.



10
11
12
# File 'lib/ruby_llm/contract/step/trace.rb', line 10

def usage
  @usage
end

Instance Method Details

#[](key) ⇒ Object



24
25
26
27
28
# File 'lib/ruby_llm/contract/step/trace.rb', line 24

def [](key)
  return nil unless KNOWN_KEYS.include?(key.to_sym)

  public_send(key)
end

#dig(key, *rest) ⇒ Object



30
31
32
33
34
35
# File 'lib/ruby_llm/contract/step/trace.rb', line 30

def dig(key, *rest)
  value = self[key]
  return value if rest.empty? || value.nil?

  value.dig(*rest)
end

#key?(key) ⇒ Boolean Also known as: has_key?

Returns:

  • (Boolean)


37
38
39
# File 'lib/ruby_llm/contract/step/trace.rb', line 37

def key?(key)
  KNOWN_KEYS.include?(key.to_sym) && !public_send(key).nil?
end

#merge(**overrides) ⇒ Object



42
43
44
45
46
47
48
49
50
51
# File 'lib/ruby_llm/contract/step/trace.rb', line 42

def merge(**overrides)
  self.class.new(
    messages: overrides.fetch(:messages, @messages),
    model: overrides.fetch(:model, @model),
    latency_ms: overrides.fetch(:latency_ms, @latency_ms),
    usage: overrides.fetch(:usage, @usage),
    attempts: overrides.fetch(:attempts, @attempts),
    cost: overrides.fetch(:cost, @cost)
  )
end

#to_hObject



53
54
55
56
# File 'lib/ruby_llm/contract/step/trace.rb', line 53

def to_h
  { messages: @messages, model: @model, latency_ms: @latency_ms,
    usage: @usage, attempts: @attempts, cost: @cost }.compact
end

#to_sObject



58
59
60
# File 'lib/ruby_llm/contract/step/trace.rb', line 58

def to_s
  build_summary_parts.join(" ")
end