Class: I18nContextGenerator::RunMetrics

Inherits:
Data
  • Object
show all
Defined in:
lib/i18n_context_generator/run_metrics.rb

Overview

Aggregates run-local provider and cache telemetry. Cost is an estimate based on documented standard list prices for the built-in default models.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#cache_hitsObject (readonly)

Returns the value of attribute cache_hits

Returns:

  • (Object)

    the current value of cache_hits



12
13
14
# File 'lib/i18n_context_generator/run_metrics.rb', line 12

def cache_hits
  @cache_hits
end

#cost_modelObject (readonly)

Returns the value of attribute cost_model

Returns:

  • (Object)

    the current value of cost_model



12
13
14
# File 'lib/i18n_context_generator/run_metrics.rb', line 12

def cost_model
  @cost_model
end

#cost_pricing_as_ofObject (readonly)

Returns the value of attribute cost_pricing_as_of

Returns:

  • (Object)

    the current value of cost_pricing_as_of



12
13
14
# File 'lib/i18n_context_generator/run_metrics.rb', line 12

def cost_pricing_as_of
  @cost_pricing_as_of
end

#estimated_cost_usdObject (readonly)

Returns the value of attribute estimated_cost_usd

Returns:

  • (Object)

    the current value of estimated_cost_usd



12
13
14
# File 'lib/i18n_context_generator/run_metrics.rb', line 12

def estimated_cost_usd
  @estimated_cost_usd
end

#input_tokensObject (readonly)

Returns the value of attribute input_tokens

Returns:

  • (Object)

    the current value of input_tokens



12
13
14
# File 'lib/i18n_context_generator/run_metrics.rb', line 12

def input_tokens
  @input_tokens
end

#output_tokensObject (readonly)

Returns the value of attribute output_tokens

Returns:

  • (Object)

    the current value of output_tokens



12
13
14
# File 'lib/i18n_context_generator/run_metrics.rb', line 12

def output_tokens
  @output_tokens
end

#request_countObject (readonly)

Returns the value of attribute request_count

Returns:

  • (Object)

    the current value of request_count



12
13
14
# File 'lib/i18n_context_generator/run_metrics.rb', line 12

def request_count
  @request_count
end

#retry_countObject (readonly)

Returns the value of attribute retry_count

Returns:

  • (Object)

    the current value of retry_count



12
13
14
# File 'lib/i18n_context_generator/run_metrics.rb', line 12

def retry_count
  @retry_count
end

Class Method Details

.from(results, provider:, model:) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/i18n_context_generator/run_metrics.rb', line 16

def self.from(results, provider:, model:)
  input_tokens = results.sum { |result| result.input_tokens.to_i }
  output_tokens = results.sum { |result| result.output_tokens.to_i }
  price = RUN_METRICS_PRICES_PER_MILLION[[provider.to_s, model.to_s]]
  estimated_cost = ((input_tokens * price[:input]) + (output_tokens * price[:output])) / 1_000_000.0 if price

  new(
    request_count: results.sum { |result| result.request_count.to_i },
    cache_hits: results.count(&:cache_hit),
    input_tokens: input_tokens,
    output_tokens: output_tokens,
    retry_count: results.sum { |result| result.retries.to_i },
    estimated_cost_usd: estimated_cost&.round(8),
    cost_model: price ? model.to_s : nil,
    cost_pricing_as_of: price ? RUN_METRICS_PRICING_AS_OF : nil
  )
end

Instance Method Details

#to_hObject



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/i18n_context_generator/run_metrics.rb', line 34

def to_h
  {
    request_count: request_count,
    cache_hits: cache_hits,
    input_tokens: input_tokens,
    output_tokens: output_tokens,
    retry_count: retry_count,
    estimated_cost_usd: estimated_cost_usd,
    cost_model: cost_model,
    cost_pricing_as_of: cost_pricing_as_of
  }
end