Class: LLMs::Usage::UsageData

Inherits:
Object
  • Object
show all
Defined in:
lib/llms/usage/usage_data.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input_tokens: 0, output_tokens: 0, cache_read_tokens: 0, cache_write_tokens: 0, execution_time: 0, model_name: nil) ⇒ UsageData

Returns a new instance of UsageData.



7
8
9
10
11
12
13
14
15
16
# File 'lib/llms/usage/usage_data.rb', line 7

def initialize(input_tokens: 0, output_tokens: 0, 
               cache_read_tokens: 0, cache_write_tokens: 0,
               execution_time: 0, model_name: nil)
  @input_tokens = input_tokens.to_i
  @output_tokens = output_tokens.to_i
  @cache_read_tokens = cache_read_tokens.to_i
  @cache_write_tokens = cache_write_tokens.to_i
  @execution_time = execution_time.to_f
  @model_name = model_name
end

Instance Attribute Details

#cache_read_tokensObject (readonly)

Returns the value of attribute cache_read_tokens.



4
5
6
# File 'lib/llms/usage/usage_data.rb', line 4

def cache_read_tokens
  @cache_read_tokens
end

#cache_write_tokensObject (readonly)

Returns the value of attribute cache_write_tokens.



4
5
6
# File 'lib/llms/usage/usage_data.rb', line 4

def cache_write_tokens
  @cache_write_tokens
end

#execution_timeObject (readonly)

Returns the value of attribute execution_time.



4
5
6
# File 'lib/llms/usage/usage_data.rb', line 4

def execution_time
  @execution_time
end

#input_tokensObject (readonly)

Returns the value of attribute input_tokens.



4
5
6
# File 'lib/llms/usage/usage_data.rb', line 4

def input_tokens
  @input_tokens
end

#model_nameObject (readonly)

Returns the value of attribute model_name.



4
5
6
# File 'lib/llms/usage/usage_data.rb', line 4

def model_name
  @model_name
end

#output_tokensObject (readonly)

Returns the value of attribute output_tokens.



4
5
6
# File 'lib/llms/usage/usage_data.rb', line 4

def output_tokens
  @output_tokens
end

Instance Method Details

#to_hObject



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/llms/usage/usage_data.rb', line 22

def to_h
  {
    input_tokens: @input_tokens,
    output_tokens: @output_tokens,
    cache_read_tokens: @cache_read_tokens,
    cache_write_tokens: @cache_write_tokens,
    total_tokens: total_tokens,
    execution_time: @execution_time,
    model_name: @model_name
  }
end

#to_sObject



34
35
36
37
38
39
40
41
42
43
# File 'lib/llms/usage/usage_data.rb', line 34

def to_s
  if total_tokens == 0
    "Usage: 0 tokens () in #{@execution_time.round(2)}s"
  else
    "Usage: #{total_tokens} tokens (#{@input_tokens} input, #{@output_tokens} output" +
    "#{@cache_read_tokens > 0 ? ", #{@cache_read_tokens} cache_read" : ""}" +
    "#{@cache_write_tokens > 0 ? ", #{@cache_write_tokens} cache_write" : ""}" +
    ") in #{@execution_time.round(2)}s"
  end
end

#total_tokensObject



18
19
20
# File 'lib/llms/usage/usage_data.rb', line 18

def total_tokens
  @input_tokens + @output_tokens + @cache_read_tokens + @cache_write_tokens
end