Class: RubyLLM::Accounting::Usage::Entry
- Inherits:
-
Object
- Object
- RubyLLM::Accounting::Usage::Entry
- 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
-
#cost ⇒ Object
readonly
Returns the value of attribute cost.
-
#message ⇒ Object
Returns the value of attribute message.
-
#model ⇒ Object
readonly
Returns the value of attribute model.
-
#operation ⇒ Object
readonly
Returns the value of attribute operation.
-
#provider ⇒ Object
readonly
Returns the value of attribute provider.
-
#status ⇒ Object
readonly
Returns the value of attribute status.
-
#tokens ⇒ Object
readonly
Returns the value of attribute tokens.
Instance Method Summary collapse
- #cancelled? ⇒ Boolean
- #cost_available? ⇒ Boolean
- #failed? ⇒ Boolean
-
#finish(status:, tokens: nil, cost: nil) ⇒ Object
:nodoc:.
-
#initialize(operation:, provider:, model:, status: :pending, tokens: nil, cost: nil, message: nil) ⇒ Entry
constructor
A new instance of Entry.
-
#inspect_attributes ⇒ Object
:nodoc:.
- #pending? ⇒ Boolean
- #succeeded? ⇒ Boolean
- #to_h ⇒ Object
- #usage_available? ⇒ Boolean
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 = validate! end |
Instance Attribute Details
#cost ⇒ Object (readonly)
Returns the value of attribute cost.
54 55 56 |
# File 'lib/ruby_llm/accounting/usage.rb', line 54 def cost @cost end |
#message ⇒ Object
Returns the value of attribute message.
55 56 57 |
# File 'lib/ruby_llm/accounting/usage.rb', line 55 def @message end |
#model ⇒ Object (readonly)
Returns the value of attribute model.
54 55 56 |
# File 'lib/ruby_llm/accounting/usage.rb', line 54 def model @model end |
#operation ⇒ Object (readonly)
Returns the value of attribute operation.
54 55 56 |
# File 'lib/ruby_llm/accounting/usage.rb', line 54 def operation @operation end |
#provider ⇒ Object (readonly)
Returns the value of attribute provider.
54 55 56 |
# File 'lib/ruby_llm/accounting/usage.rb', line 54 def provider @provider end |
#status ⇒ Object (readonly)
Returns the value of attribute status.
54 55 56 |
# File 'lib/ruby_llm/accounting/usage.rb', line 54 def status @status end |
#tokens ⇒ Object (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
77 |
# File 'lib/ruby_llm/accounting/usage.rb', line 77 def cancelled? = status == :cancelled |
#cost_available? ⇒ Boolean
79 |
# File 'lib/ruby_llm/accounting/usage.rb', line 79 def cost_available? = !cost.total.nil? |
#failed? ⇒ 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_attributes ⇒ Object
: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
74 |
# File 'lib/ruby_llm/accounting/usage.rb', line 74 def pending? = status == :pending |
#succeeded? ⇒ Boolean
75 |
# File 'lib/ruby_llm/accounting/usage.rb', line 75 def succeeded? = status == :succeeded |
#to_h ⇒ Object
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
78 |
# File 'lib/ruby_llm/accounting/usage.rb', line 78 def usage_available? = tokens.to_h.any? |