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

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

Overview

Canonical sampling and limit parameters for a request. Per G18: all standard/useful params are first-class, mapped per provider by translators. Canonical keys only (O03a): provider spellings are translated at the edges.

Constant Summary collapse

BUILD_SITE =
'Canonical::Params.build'
FROM_HASH_SITE =
'Canonical::Params.from_hash'
NEW_SITE =
'Canonical::Params.new'

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



13
14
15
# File 'lib/legion/extensions/llm/canonical/params.rb', line 13

def frequency_penalty
  @frequency_penalty
end

#max_tokensObject (readonly)

Returns the value of attribute max_tokens

Returns:

  • (Object)

    the current value of max_tokens



13
14
15
# File 'lib/legion/extensions/llm/canonical/params.rb', line 13

def max_tokens
  @max_tokens
end

#metadataObject (readonly)

Returns the value of attribute metadata

Returns:

  • (Object)

    the current value of metadata



13
14
15
# File 'lib/legion/extensions/llm/canonical/params.rb', line 13

def 
  @metadata
end

#presence_penaltyObject (readonly)

Returns the value of attribute presence_penalty

Returns:

  • (Object)

    the current value of presence_penalty



13
14
15
# File 'lib/legion/extensions/llm/canonical/params.rb', line 13

def presence_penalty
  @presence_penalty
end

#response_formatObject (readonly)

Returns the value of attribute response_format

Returns:

  • (Object)

    the current value of response_format



13
14
15
# File 'lib/legion/extensions/llm/canonical/params.rb', line 13

def response_format
  @response_format
end

#seedObject (readonly)

Returns the value of attribute seed

Returns:

  • (Object)

    the current value of seed



13
14
15
# File 'lib/legion/extensions/llm/canonical/params.rb', line 13

def seed
  @seed
end

#stop_sequencesObject (readonly)

Returns the value of attribute stop_sequences

Returns:

  • (Object)

    the current value of stop_sequences



13
14
15
# File 'lib/legion/extensions/llm/canonical/params.rb', line 13

def stop_sequences
  @stop_sequences
end

#temperatureObject (readonly)

Returns the value of attribute temperature

Returns:

  • (Object)

    the current value of temperature



13
14
15
# File 'lib/legion/extensions/llm/canonical/params.rb', line 13

def temperature
  @temperature
end

#top_kObject (readonly)

Returns the value of attribute top_k

Returns:

  • (Object)

    the current value of top_k



13
14
15
# File 'lib/legion/extensions/llm/canonical/params.rb', line 13

def top_k
  @top_k
end

#top_pObject (readonly)

Returns the value of attribute top_p

Returns:

  • (Object)

    the current value of top_p



13
14
15
# File 'lib/legion/extensions/llm/canonical/params.rb', line 13

def top_p
  @top_p
end

Class Method Details

.build(max_tokens: nil, temperature: nil, top_p: nil, top_k: nil, stop_sequences: nil, seed: nil, frequency_penalty: nil, presence_penalty: nil, response_format: nil, metadata: {}) ⇒ Object

rubocop:disable Metrics/ParameterLists -- factory methods have many params Build from keyword args (primary constructor).



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/legion/extensions/llm/canonical/params.rb', line 20

def self.build(
  max_tokens: nil, temperature: nil, top_p: nil, top_k: nil,
  stop_sequences: nil, seed: nil, frequency_penalty: nil, presence_penalty: nil,
  response_format: nil, metadata: {}
)
  new(
    max_tokens:, temperature:, top_p:, top_k:,
    stop_sequences:, seed:, frequency_penalty:, presence_penalty:,
    response_format:, metadata: Strict.metadata!(, self::BUILD_SITE)
  )
end

.from_hash(source) ⇒ Object

Build from a Hash (raw client request or deserialized wire payload). Canonical member keys only; unknown keys fold into metadata (04 L5). from_hash({}) is a valid all-nil Params (the no-params object), never nil.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/legion/extensions/llm/canonical/params.rb', line 36

def self.from_hash(source)
  Strict.require_hash!(source, self::FROM_HASH_SITE)
  hash = Strict.symbolize_keys(source)
   = Strict.fold_unknowns!(self, self::FROM_HASH_SITE, hash)
  build(
    max_tokens: hash[:max_tokens],
    temperature: hash[:temperature],
    top_p: hash[:top_p],
    top_k: hash[:top_k],
    stop_sequences: hash[:stop_sequences],
    seed: hash[:seed],
    frequency_penalty: hash[:frequency_penalty],
    presence_penalty: hash[:presence_penalty],
    response_format: hash[:response_format],
    metadata:
  )
end

Instance Method Details

#as_jsonObject

MultiJson/Oj/::JSON callback — prevents Data.define #inspect leak into JSON.



60
61
62
# File 'lib/legion/extensions/llm/canonical/params.rb', line 60

def as_json(*)
  to_h
end

#to_hObject

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



55
56
57
# File 'lib/legion/extensions/llm/canonical/params.rb', line 55

def to_h
  super.compact
end

#to_jsonObject



64
65
66
# File 'lib/legion/extensions/llm/canonical/params.rb', line 64

def to_json(*)
  to_h.to_json(*)
end