Class: OmniAI::Chat::Usage

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

Overview

A usage returned by the API.

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)


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

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_tokensObject

Returns the value of attribute input_tokens.



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

def input_tokens
  @input_tokens
end

#output_tokensObject

Returns the value of attribute output_tokens.



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

def output_tokens
  @output_tokens
end

#total_tokensObject

Returns the value of attribute total_tokens.



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

def total_tokens
  @total_tokens
end

Class Method Details

.for(data:) ⇒ OmniAI::Chat::Usage

Parameters:

  • data (Hash)

Returns:



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/omniai/chat/usage.rb', line 11

def self.for(data:)
  input_tokens = data['input_tokens'] || data['prompt_tokens']
  output_tokens = data['output_tokens'] || data['completion_tokens']
  total_tokens = data['total_tokens'] || (input_tokens + output_tokens)

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

Instance Method Details

#completion_tokensInteger

Returns:

  • (Integer)


33
34
35
# File 'lib/omniai/chat/usage.rb', line 33

def completion_tokens
  @output_tokens
end

#inspectString

Returns:

  • (String)


43
44
45
# File 'lib/omniai/chat/usage.rb', line 43

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

#prompt_tokensInteger

Returns:

  • (Integer)


38
39
40
# File 'lib/omniai/chat/usage.rb', line 38

def prompt_tokens
  @input_tokens
end