Class: Kernai::GenerationOptions

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

Overview

Provider-agnostic knobs for how the LLM should generate its response. Carries the fields every major vendor exposes (temperature, max_tokens, top_p) plus a generic ‘thinking` hash for reasoning models (Anthropic’s extended thinking ‘budget`, OpenAI/Gemini-style `effort`).

The value object is intentionally dumb: it does not know which vendor honours which field. Each provider inspects the fields it supports and routes them into its vendor payload. Fields left nil are simply ignored.

Extra vendor-specific knobs pass through ‘extra:` (kwargs), so an application can still reach provider-specific params without having to subclass the value object. Providers that don’t recognise a key should silently drop it.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(temperature: nil, max_tokens: nil, top_p: nil, thinking: nil, **extra) ⇒ GenerationOptions

Returns a new instance of GenerationOptions.



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

def initialize(temperature: nil, max_tokens: nil, top_p: nil, thinking: nil, **extra)
  @temperature = temperature
  @max_tokens  = max_tokens
  @top_p       = top_p
  @thinking    = thinking
  @extra       = extra
end

Instance Attribute Details

#extraObject (readonly)

Returns the value of attribute extra.



19
20
21
# File 'lib/kernai/generation_options.rb', line 19

def extra
  @extra
end

#max_tokensObject (readonly)

Returns the value of attribute max_tokens.



19
20
21
# File 'lib/kernai/generation_options.rb', line 19

def max_tokens
  @max_tokens
end

#temperatureObject (readonly)

Returns the value of attribute temperature.



19
20
21
# File 'lib/kernai/generation_options.rb', line 19

def temperature
  @temperature
end

#thinkingObject (readonly)

Returns the value of attribute thinking.



19
20
21
# File 'lib/kernai/generation_options.rb', line 19

def thinking
  @thinking
end

#top_pObject (readonly)

Returns the value of attribute top_p.



19
20
21
# File 'lib/kernai/generation_options.rb', line 19

def top_p
  @top_p
end

Class Method Details

.coerce(value) ⇒ Object

Construct from any of: nil, Hash, or existing GenerationOptions.



63
64
65
66
67
68
69
70
71
# File 'lib/kernai/generation_options.rb', line 63

def self.coerce(value)
  case value
  when nil then new
  when GenerationOptions then value
  when Hash then new(**value)
  else
    raise ArgumentError, "Cannot coerce #{value.class} to GenerationOptions"
  end
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



53
54
55
# File 'lib/kernai/generation_options.rb', line 53

def ==(other)
  other.is_a?(GenerationOptions) && to_h == other.to_h
end

#empty?Boolean

An options object with no fields set. Providers can fast-path on ‘generation.empty?` to avoid building their vendor payload.

Returns:

  • (Boolean)


40
41
42
# File 'lib/kernai/generation_options.rb', line 40

def empty?
  to_h.empty?
end

#hashObject



58
59
60
# File 'lib/kernai/generation_options.rb', line 58

def hash
  to_h.hash
end

#merge(other) ⇒ Object

Returns a new GenerationOptions with ‘other`’s non-nil fields layered on top of this one. Accepts a Hash or another GenerationOptions. Nil inputs are a no-op.



47
48
49
50
51
# File 'lib/kernai/generation_options.rb', line 47

def merge(other)
  return self if other.nil?

  self.class.new(**to_h.merge(other.to_h))
end

#to_hObject



29
30
31
32
33
34
35
36
# File 'lib/kernai/generation_options.rb', line 29

def to_h
  {
    temperature: @temperature,
    max_tokens: @max_tokens,
    top_p: @top_p,
    thinking: @thinking
  }.compact.merge(@extra)
end