Class: LightGptProxy::SchemaDefaultsApplier

Inherits:
Object
  • Object
show all
Defined in:
lib/light_gpt_proxy/schema_defaults_applier.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(schema) ⇒ SchemaDefaultsApplier

Returns a new instance of SchemaDefaultsApplier.



7
8
9
# File 'lib/light_gpt_proxy/schema_defaults_applier.rb', line 7

def initialize(schema)
  @schema = schema
end

Instance Attribute Details

#payloadObject (readonly)

Returns the value of attribute payload.



5
6
7
# File 'lib/light_gpt_proxy/schema_defaults_applier.rb', line 5

def payload
  @payload
end

#schemaObject (readonly)

Returns the value of attribute schema.



5
6
7
# File 'lib/light_gpt_proxy/schema_defaults_applier.rb', line 5

def schema
  @schema
end

Class Method Details

.ensure_system_presence(messages, defaults) ⇒ Object

helper method to ensure that the system message is present in the messages array only for completions endpoint

Raises:

  • (TypeError)


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

def self.ensure_system_presence(messages, defaults)
  raise TypeError, "messages cannot be #{messages.class.name}" unless messages.is_a?(Array)
  return messages if defaults.nil?
  raise TypeError, "defaults cannot be #{defaults.class.name}" unless defaults.is_a?(Hash)

  return messages if messages.any? { |message| message['role'] == 'system' }

  messages.unshift(defaults)
end

Instance Method Details

#perform(payload) ⇒ Object Also known as: []



11
12
13
14
15
# File 'lib/light_gpt_proxy/schema_defaults_applier.rb', line 11

def perform(payload)
  @payload = payload
  fill_defaults_recursive(schema, payload)
  payload
end