Class: TurnKit::Usage

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input_tokens: 0, output_tokens: 0, cached_tokens: 0, cache_write_tokens: 0, cost: nil) ⇒ Usage

Returns a new instance of Usage.



36
37
38
39
40
41
42
# File 'lib/turnkit/usage.rb', line 36

def initialize(input_tokens: 0, output_tokens: 0, cached_tokens: 0, cache_write_tokens: 0, cost: nil)
  @input_tokens = input_tokens.to_i
  @output_tokens = output_tokens.to_i
  @cached_tokens = cached_tokens.to_i
  @cache_write_tokens = cache_write_tokens.to_i
  @cost = cost
end

Instance Attribute Details

#cache_write_tokensObject (readonly)

Returns the value of attribute cache_write_tokens.



5
6
7
# File 'lib/turnkit/usage.rb', line 5

def cache_write_tokens
  @cache_write_tokens
end

#cached_tokensObject (readonly)

Returns the value of attribute cached_tokens.



5
6
7
# File 'lib/turnkit/usage.rb', line 5

def cached_tokens
  @cached_tokens
end

#costObject (readonly)

Returns the value of attribute cost.



5
6
7
# File 'lib/turnkit/usage.rb', line 5

def cost
  @cost
end

#input_tokensObject (readonly)

Returns the value of attribute input_tokens.



5
6
7
# File 'lib/turnkit/usage.rb', line 5

def input_tokens
  @input_tokens
end

#output_tokensObject (readonly)

Returns the value of attribute output_tokens.



5
6
7
# File 'lib/turnkit/usage.rb', line 5

def output_tokens
  @output_tokens
end

Class Method Details

.aggregate(usages) ⇒ Object



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

def self.aggregate(usages)
  usages = usages.compact
  costs = usages.map(&:cost).compact
  cost = costs.sum if costs.any?
  new(
    input_tokens: usages.sum(&:input_tokens),
    output_tokens: usages.sum(&:output_tokens),
    cached_tokens: usages.sum(&:cached_tokens),
    cache_write_tokens: usages.sum(&:cache_write_tokens),
    cost: cost
  )
end

.from_h(hash) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/turnkit/usage.rb', line 24

def self.from_h(hash)
  attrs = hash.transform_keys(&:to_s)
  cost = attrs["cost"] unless attrs["cost"].is_a?(Hash)
  new(
    input_tokens: attrs["input_tokens"],
    output_tokens: attrs["output_tokens"],
    cached_tokens: attrs["cached_tokens"],
    cache_write_tokens: attrs["cache_write_tokens"],
    cost: cost
  )
end

.from_records(records) ⇒ Object



20
21
22
# File 'lib/turnkit/usage.rb', line 20

def self.from_records(records)
  aggregate(records.map { |record| from_h(record.fetch("usage", {})) })
end

Instance Method Details

#to_hObject



48
49
50
51
52
53
54
55
56
57
# File 'lib/turnkit/usage.rb', line 48

def to_h
  {
    "input_tokens" => input_tokens,
    "output_tokens" => output_tokens,
    "cached_tokens" => cached_tokens,
    "cache_write_tokens" => cache_write_tokens,
    "total_tokens" => total_tokens,
    "cost" => cost
  }.compact
end

#total_tokensObject



44
45
46
# File 'lib/turnkit/usage.rb', line 44

def total_tokens
  input_tokens + output_tokens + cached_tokens + cache_write_tokens
end