Class: RubyLLM::Accounting::Usage::Entry

Inherits:
Object
  • Object
show all
Includes:
Support::Inspectable
Defined in:
lib/ruby_llm/accounting/usage.rb

Overview

One physical provider request attempt and its accounting facts.

Constant Summary collapse

OPERATIONS =
%i[chat embedding moderation image speech transcription ocr rerank].freeze
STATUSES =
%i[pending succeeded failed cancelled].freeze

Constants included from Support::Inspectable

Support::Inspectable::TRUNCATE_AT

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Support::Inspectable

#full_inspect, #inspect, #pretty_print

Constructor Details

#initialize(operation:, provider:, model:, status: :pending, tokens: nil, cost: nil, message: nil) ⇒ Entry

Returns a new instance of Entry.



63
64
65
66
67
68
69
70
71
72
# File 'lib/ruby_llm/accounting/usage.rb', line 63

def initialize(operation:, provider:, model:, status: :pending, tokens: nil, cost: nil, message: nil)
  @operation = operation.to_sym
  @provider = provider.to_s
  @model = model&.to_s
  @status = status.to_sym
  @tokens = tokens || Tokens.new
  @cost = cost || Cost.new(tokens: @tokens)
  @message = message
  validate!
end

Instance Attribute Details

#costObject (readonly)

Returns the value of attribute cost.



54
55
56
# File 'lib/ruby_llm/accounting/usage.rb', line 54

def cost
  @cost
end

#messageObject

Returns the value of attribute message.



55
56
57
# File 'lib/ruby_llm/accounting/usage.rb', line 55

def message
  @message
end

#modelObject (readonly)

Returns the value of attribute model.



54
55
56
# File 'lib/ruby_llm/accounting/usage.rb', line 54

def model
  @model
end

#operationObject (readonly)

Returns the value of attribute operation.



54
55
56
# File 'lib/ruby_llm/accounting/usage.rb', line 54

def operation
  @operation
end

#providerObject (readonly)

Returns the value of attribute provider.



54
55
56
# File 'lib/ruby_llm/accounting/usage.rb', line 54

def provider
  @provider
end

#statusObject (readonly)

Returns the value of attribute status.



54
55
56
# File 'lib/ruby_llm/accounting/usage.rb', line 54

def status
  @status
end

#tokensObject (readonly)

Returns the value of attribute tokens.



54
55
56
# File 'lib/ruby_llm/accounting/usage.rb', line 54

def tokens
  @tokens
end

Instance Method Details

#cancelled?Boolean

Returns:

  • (Boolean)


77
# File 'lib/ruby_llm/accounting/usage.rb', line 77

def cancelled? = status == :cancelled

#cost_available?Boolean

Returns:

  • (Boolean)


79
# File 'lib/ruby_llm/accounting/usage.rb', line 79

def cost_available? = !cost.total.nil?

#failed?Boolean

Returns:

  • (Boolean)


76
# File 'lib/ruby_llm/accounting/usage.rb', line 76

def failed? = status == :failed

#finish(status:, tokens: nil, cost: nil) ⇒ Object

:nodoc:



92
93
94
95
96
97
98
# File 'lib/ruby_llm/accounting/usage.rb', line 92

def finish(status:, tokens: nil, cost: nil) # :nodoc:
  @status = status.to_sym
  @tokens = tokens || Tokens.new
  @cost = cost || Cost.new(tokens: @tokens)
  validate!
  self
end

#inspect_attributesObject

:nodoc:



59
60
61
# File 'lib/ruby_llm/accounting/usage.rb', line 59

def inspect_attributes # :nodoc:
  { operation: operation, provider: provider, model: model, status: status, tokens: tokens }
end

#pending?Boolean

Returns:

  • (Boolean)


74
# File 'lib/ruby_llm/accounting/usage.rb', line 74

def pending? = status == :pending

#succeeded?Boolean

Returns:

  • (Boolean)


75
# File 'lib/ruby_llm/accounting/usage.rb', line 75

def succeeded? = status == :succeeded

#to_hObject



81
82
83
84
85
86
87
88
89
90
# File 'lib/ruby_llm/accounting/usage.rb', line 81

def to_h
  {
    operation: operation,
    provider: provider,
    model: model,
    status: status,
    tokens: tokens.to_h,
    cost: cost.to_h
  }
end

#usage_available?Boolean

Returns:

  • (Boolean)


78
# File 'lib/ruby_llm/accounting/usage.rb', line 78

def usage_available? = tokens.to_h.any?