Sha256: 059e847d8d13c05dc600ab3e808b4fd7b232d7f459f43aa3c083ee0250d11941

Contents?: true

Size: 1.24 KB

Versions: 20

Compression:

Stored size: 1.24 KB

Contents

# frozen_string_literal: true

module OmniAI
  class Chat
    # A usage returned by the API.
    class Usage
      attr_accessor :input_tokens, :output_tokens, :total_tokens

      # @param data [Hash]
      # @return [OmniAI::Chat::Usage]
      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

      # @param input_tokens [Integer]
      # @param output_tokens [Integer]
      # @param total_tokens [Integer]
      def initialize(input_tokens:, output_tokens:, total_tokens:)
        @input_tokens = input_tokens
        @output_tokens = output_tokens
        @total_tokens = total_tokens
      end

      # @return [Integer]
      def completion_tokens
        @output_tokens
      end

      # @return [Integer]
      def prompt_tokens
        @input_tokens
      end

      # @return [String]
      def inspect
        "#<#{self.class.name} input_tokens=#{input_tokens} output_tokens=#{output_tokens} total_tokens=#{total_tokens}>"
      end
    end
  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
omniai-1.4.2 lib/omniai/chat/usage.rb
omniai-1.4.1 lib/omniai/chat/usage.rb
omniai-1.4.0 lib/omniai/chat/usage.rb
omniai-1.3.1 lib/omniai/chat/usage.rb
omniai-1.3.0 lib/omniai/chat/usage.rb
omniai-1.2.3 lib/omniai/chat/usage.rb
omniai-1.2.2 lib/omniai/chat/usage.rb
omniai-1.2.1 lib/omniai/chat/usage.rb
omniai-1.2.0 lib/omniai/chat/usage.rb
omniai-1.1.6 lib/omniai/chat/usage.rb
omniai-1.1.5 lib/omniai/chat/usage.rb
omniai-1.1.4 lib/omniai/chat/usage.rb
omniai-1.1.2 lib/omniai/chat/usage.rb
omniai-1.1.1 lib/omniai/chat/usage.rb
omniai-1.1.0 lib/omniai/chat/usage.rb
omniai-1.0.9 lib/omniai/chat/usage.rb
omniai-1.0.8 lib/omniai/chat/usage.rb
omniai-1.0.7 lib/omniai/chat/usage.rb
omniai-1.0.6 lib/omniai/chat/usage.rb
omniai-1.0.5 lib/omniai/chat/usage.rb