Class: Agentic::GenerationStats

Inherits:
Object
  • Object
show all
Defined in:
lib/agentic/generation_stats.rb

Overview

Value object representing statistics for an LLM generation

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id:, prompt_tokens:, completion_tokens:, total_tokens:, raw_stats: {}) ⇒ GenerationStats

Initializes a new generation statistics object



27
28
29
30
31
32
33
# File 'lib/agentic/generation_stats.rb', line 27

def initialize(id:, prompt_tokens:, completion_tokens:, total_tokens:, raw_stats: {})
  @id = id
  @prompt_tokens = prompt_tokens
  @completion_tokens = completion_tokens
  @total_tokens = total_tokens
  @raw_stats = raw_stats
end

Instance Attribute Details

#completion_tokensInteger (readonly)



13
14
15
# File 'lib/agentic/generation_stats.rb', line 13

def completion_tokens
  @completion_tokens
end

#idString (readonly)



7
8
9
# File 'lib/agentic/generation_stats.rb', line 7

def id
  @id
end

#prompt_tokensInteger (readonly)



10
11
12
# File 'lib/agentic/generation_stats.rb', line 10

def prompt_tokens
  @prompt_tokens
end

#raw_statsHash (readonly)



19
20
21
# File 'lib/agentic/generation_stats.rb', line 19

def raw_stats
  @raw_stats
end

#total_tokensInteger (readonly)



16
17
18
# File 'lib/agentic/generation_stats.rb', line 16

def total_tokens
  @total_tokens
end

Class Method Details

.from_response(response) ⇒ GenerationStats

Creates a GenerationStats object from an API response



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/agentic/generation_stats.rb', line 49

def self.from_response(response)
  usage = response&.dig("usage") || {}

  new(
    id: response&.dig("id") || "",
    prompt_tokens: usage["prompt_tokens"] || 0,
    completion_tokens: usage["completion_tokens"] || 0,
    total_tokens: usage["total_tokens"] || 0,
    raw_stats: response || {}
  )
end

Instance Method Details

#to_hHash

Returns a hash representation of the generation statistics



37
38
39
40
41
42
43
44
# File 'lib/agentic/generation_stats.rb', line 37

def to_h
  {
    id: @id,
    prompt_tokens: @prompt_tokens,
    completion_tokens: @completion_tokens,
    total_tokens: @total_tokens
  }
end