Class: Legion::Extensions::Llm::Canonical::Params

Inherits:
Data
  • Object
show all
Defined in:
lib/legion/extensions/llm/canonical/params.rb

Overview

rubocop:disable Lint/ConstantDefinitionInBlock – required for Data.define block scope Canonical sampling and limit parameters for a request. Per G18: all standard/useful params are first-class, mapped per provider by translators.

Constant Summary collapse

PARAMS_KNOWN_KEYS =
%i[max_tokens max_thinking_tokens temperature top_p top_k
stop_sequences seed frequency_penalty presence_penalty
response_format].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#frequency_penaltyObject (readonly)

Returns the value of attribute frequency_penalty

Returns:

  • (Object)

    the current value of frequency_penalty



11
12
13
# File 'lib/legion/extensions/llm/canonical/params.rb', line 11

def frequency_penalty
  @frequency_penalty
end

#max_thinking_tokensObject (readonly)

Returns the value of attribute max_thinking_tokens

Returns:

  • (Object)

    the current value of max_thinking_tokens



11
12
13
# File 'lib/legion/extensions/llm/canonical/params.rb', line 11

def max_thinking_tokens
  @max_thinking_tokens
end

#max_tokensObject (readonly)

Returns the value of attribute max_tokens

Returns:

  • (Object)

    the current value of max_tokens



11
12
13
# File 'lib/legion/extensions/llm/canonical/params.rb', line 11

def max_tokens
  @max_tokens
end

#presence_penaltyObject (readonly)

Returns the value of attribute presence_penalty

Returns:

  • (Object)

    the current value of presence_penalty



11
12
13
# File 'lib/legion/extensions/llm/canonical/params.rb', line 11

def presence_penalty
  @presence_penalty
end

#response_formatObject (readonly)

Returns the value of attribute response_format

Returns:

  • (Object)

    the current value of response_format



11
12
13
# File 'lib/legion/extensions/llm/canonical/params.rb', line 11

def response_format
  @response_format
end

#seedObject (readonly)

Returns the value of attribute seed

Returns:

  • (Object)

    the current value of seed



11
12
13
# File 'lib/legion/extensions/llm/canonical/params.rb', line 11

def seed
  @seed
end

#stop_sequencesObject (readonly)

Returns the value of attribute stop_sequences

Returns:

  • (Object)

    the current value of stop_sequences



11
12
13
# File 'lib/legion/extensions/llm/canonical/params.rb', line 11

def stop_sequences
  @stop_sequences
end

#temperatureObject (readonly)

Returns the value of attribute temperature

Returns:

  • (Object)

    the current value of temperature



11
12
13
# File 'lib/legion/extensions/llm/canonical/params.rb', line 11

def temperature
  @temperature
end

#top_kObject (readonly)

Returns the value of attribute top_k

Returns:

  • (Object)

    the current value of top_k



11
12
13
# File 'lib/legion/extensions/llm/canonical/params.rb', line 11

def top_k
  @top_k
end

#top_pObject (readonly)

Returns the value of attribute top_p

Returns:

  • (Object)

    the current value of top_p



11
12
13
# File 'lib/legion/extensions/llm/canonical/params.rb', line 11

def top_p
  @top_p
end

Class Method Details

.from_hash(source) ⇒ Object

Build from a Hash (raw client request or deserialized wire payload). Accepts both canonical key names and common provider spellings.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/legion/extensions/llm/canonical/params.rb', line 22

def self.from_hash(source)
  return nil if source.nil? || source.empty?

  h = source.transform_keys(&:to_sym)

  # Normalize common provider key variations
  h[:max_tokens] ||= h.delete(:max_output_tokens) || h.delete(:num_predict)
  h[:max_thinking_tokens] ||= h.delete(:budget_tokens) || h.delete(:thinking_budget)
  h[:stop_sequences] ||= h.delete(:stop)

  # Filter to known keys only
  filtered = h.slice(*PARAMS_KNOWN_KEYS)

  # Return nil if all known values are nil
  return nil if filtered.all? { |_, v| v.nil? }

  new(
    max_tokens: filtered[:max_tokens],
    max_thinking_tokens: filtered[:max_thinking_tokens],
    temperature: filtered[:temperature],
    top_p: filtered[:top_p],
    top_k: filtered[:top_k],
    stop_sequences: filtered[:stop_sequences],
    seed: filtered[:seed],
    frequency_penalty: filtered[:frequency_penalty],
    presence_penalty: filtered[:presence_penalty],
    response_format: filtered[:response_format]
  )
end

Instance Method Details

#to_hObject

Serialize to a Hash for AMQP/fleet/wire transport.



53
54
55
# File 'lib/legion/extensions/llm/canonical/params.rb', line 53

def to_h
  super.compact
end