Sha256: f0543916dd6ac02aee54c49ef94c6186168a2fdacb2302ca6131a76893903ef8

Contents?: true

Size: 941 Bytes

Versions: 8

Compression:

Stored size: 941 Bytes

Contents

# frozen_string_literal: true

module GenAI
  class Result
    attr_reader :raw, :provider, :model, :values

    def initialize(provider:, model:, raw:, values:)
      @raw = raw
      @provider = provider
      @model = model
      @values = values
    end

    def value(format = :raw)
      case format
      when :raw
        values.first
      when :base64
        Base64.encode64(values.first)
      else
        raise "Unsupported format: #{format}"
      end
    end

    def prompt_tokens
      usage['prompt_tokens']
    end

    def completion_tokens
      if usage['completion_tokens'] ||
         (total_tokens && prompt_tokens)
        total_tokens.to_i - prompt_tokens.to_i
      end
    end

    def total_tokens
      usage['total_tokens']
    end

    private

    def usage
      raw['usage'] || {
        'prompt_tokens' => nil,
        'completion_tokens' => nil,
        'total_tokens' => nil
      }
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
gen-ai-0.4.1 lib/gen_ai/result.rb
gen-ai-0.4.0 lib/gen_ai/result.rb
gen-ai-0.4.0.alpha.3 lib/gen_ai/result.rb
gen-ai-0.4.0.alpha.2 lib/gen_ai/result.rb
gen-ai-0.4.0.alpha.1 lib/gen_ai/result.rb
gen-ai-0.3.2 lib/gen_ai/result.rb
gen-ai-0.3.1 lib/gen_ai/result.rb
gen-ai-0.3.0 lib/gen_ai/result.rb