Class: RubyLLM::Contract::Step::Trace
- Inherits:
-
Object
- Object
- RubyLLM::Contract::Step::Trace
- 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
-
#attempts ⇒ Object
readonly
Returns the value of attribute attempts.
-
#cost ⇒ Object
readonly
Returns the value of attribute cost.
-
#latency_ms ⇒ Object
readonly
Returns the value of attribute latency_ms.
-
#messages ⇒ Object
readonly
Returns the value of attribute messages.
-
#model ⇒ Object
readonly
Returns the value of attribute model.
-
#usage ⇒ Object
readonly
Returns the value of attribute usage.
Instance Method Summary collapse
- #[](key) ⇒ Object
- #dig(key, *rest) ⇒ Object
-
#initialize(messages: nil, model: nil, latency_ms: nil, usage: nil, attempts: nil, cost: nil) ⇒ Trace
constructor
A new instance of Trace.
- #key?(key) ⇒ Boolean (also: #has_key?)
- #merge(**overrides) ⇒ Object
- #to_h ⇒ Object
- #to_s ⇒ Object
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() @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
#attempts ⇒ Object (readonly)
Returns the value of attribute attempts.
10 11 12 |
# File 'lib/ruby_llm/contract/step/trace.rb', line 10 def attempts @attempts end |
#cost ⇒ Object (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_ms ⇒ Object (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 |
#messages ⇒ Object (readonly)
Returns the value of attribute messages.
10 11 12 |
# File 'lib/ruby_llm/contract/step/trace.rb', line 10 def @messages end |
#model ⇒ Object (readonly)
Returns the value of attribute model.
10 11 12 |
# File 'lib/ruby_llm/contract/step/trace.rb', line 10 def model @model end |
#usage ⇒ Object (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?
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_h ⇒ Object
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_s ⇒ Object
58 59 60 |
# File 'lib/ruby_llm/contract/step/trace.rb', line 58 def to_s build_summary_parts.join(" ") end |