Class: OmniAI::Chat::Usage

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

Overview

The usage of a chat in terms of tokens (input / output / total).

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input_tokens:, output_tokens:, total_tokens:) ⇒ Usage

Returns a new instance of Usage.

Parameters:

  • input_tokens (Integer)
  • output_tokens (Integer)
  • total_tokens (Integer)


19
20
21
22
23
# File 'lib/omniai/chat/usage.rb', line 19

def initialize(input_tokens:, output_tokens:, total_tokens:)
  @input_tokens = input_tokens
  @output_tokens = output_tokens
  @total_tokens = total_tokens
end

Instance Attribute Details

#input_tokensInteger

Returns:

  • (Integer)


8
9
10
# File 'lib/omniai/chat/usage.rb', line 8

def input_tokens
  @input_tokens
end

#output_tokensInteger

Returns:

  • (Integer)


11
12
13
# File 'lib/omniai/chat/usage.rb', line 11

def output_tokens
  @output_tokens
end

#total_tokensInteger

Returns:

  • (Integer)


14
15
16
# File 'lib/omniai/chat/usage.rb', line 14

def total_tokens
  @total_tokens
end

Class Method Details

.deserialize(data, context: nil) ⇒ OmniAI::Chat::Usage

Parameters:

Returns:



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

def self.deserialize(data, context: nil)
  deserialize = context&.deserializer(:usage)
  return deserialize.call(data, context:) if deserialize

  input_tokens = data['input_tokens']
  output_tokens = data['output_tokens']
  total_tokens = data['total_tokens']

  new(input_tokens:, output_tokens:, total_tokens:)
end

Instance Method Details

#inspectString

Returns:

  • (String)


26
27
28
# File 'lib/omniai/chat/usage.rb', line 26

def inspect
  "#<#{self.class.name} input_tokens=#{input_tokens} output_tokens=#{output_tokens} total_tokens=#{total_tokens}>"
end

#serialize(context: nil) ⇒ Hash

Parameters:

Returns:

  • (Hash)


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

def serialize(context: nil)
  serialize = context&.serializer(:usage)
  return serialize.call(self, context:) if serialize

  {
    input_tokens:,
    output_tokens:,
    total_tokens:,
  }
end