Class: Kernai::LlmResponse

Inherits:
Object
  • Object
show all
Defined in:
lib/kernai/llm_response.rb

Overview

Structured response returned by every Provider#call. Carries the model’s text output alongside deterministic observability metadata (latency, token usage). Providers that can’t report a field leave it nil — the Kernel and recorder handle that uniformly without any conditional branching at the call sites.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(content:, latency_ms:, prompt_tokens: nil, completion_tokens: nil, total_tokens: nil) ⇒ LlmResponse

Returns a new instance of LlmResponse.



12
13
14
15
16
17
18
# File 'lib/kernai/llm_response.rb', line 12

def initialize(content:, latency_ms:, prompt_tokens: nil, completion_tokens: nil, total_tokens: nil)
  @content = content.to_s
  @latency_ms = latency_ms.to_i
  @prompt_tokens = prompt_tokens
  @completion_tokens = completion_tokens
  @total_tokens = total_tokens || derived_total(prompt_tokens, completion_tokens)
end

Instance Attribute Details

#completion_tokensObject (readonly)

Returns the value of attribute completion_tokens.



10
11
12
# File 'lib/kernai/llm_response.rb', line 10

def completion_tokens
  @completion_tokens
end

#contentObject (readonly)

Returns the value of attribute content.



10
11
12
# File 'lib/kernai/llm_response.rb', line 10

def content
  @content
end

#latency_msObject (readonly)

Returns the value of attribute latency_ms.



10
11
12
# File 'lib/kernai/llm_response.rb', line 10

def latency_ms
  @latency_ms
end

#prompt_tokensObject (readonly)

Returns the value of attribute prompt_tokens.



10
11
12
# File 'lib/kernai/llm_response.rb', line 10

def prompt_tokens
  @prompt_tokens
end

#total_tokensObject (readonly)

Returns the value of attribute total_tokens.



10
11
12
# File 'lib/kernai/llm_response.rb', line 10

def total_tokens
  @total_tokens
end

Instance Method Details

#to_hObject



20
21
22
23
24
25
26
27
28
# File 'lib/kernai/llm_response.rb', line 20

def to_h
  {
    content: @content,
    latency_ms: @latency_ms,
    prompt_tokens: @prompt_tokens,
    completion_tokens: @completion_tokens,
    total_tokens: @total_tokens
  }
end