Class: Raif::Llms::SyntheticJsonResponseToolInputNormalizer

Inherits:
Object
  • Object
show all
Defined in:
app/models/raif/llms/synthetic_json_response_tool_input_normalizer.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input:, schema:) ⇒ SyntheticJsonResponseToolInputNormalizer

Returns a new instance of SyntheticJsonResponseToolInputNormalizer.



8
9
10
11
# File 'app/models/raif/llms/synthetic_json_response_tool_input_normalizer.rb', line 8

def initialize(input:, schema:)
  @input = input
  @schema = schema
end

Class Method Details

.call(input:, schema:) ⇒ Object



4
5
6
# File 'app/models/raif/llms/synthetic_json_response_tool_input_normalizer.rb', line 4

def self.call(input:, schema:)
  new(input:, schema:).call
end

Instance Method Details

#callObject



13
14
15
16
17
18
19
20
21
# File 'app/models/raif/llms/synthetic_json_response_tool_input_normalizer.rb', line 13

def call
  return @input unless @input.is_a?(Hash)
  return @input if expected_keys.empty?

  normalized_input = @input.deep_stringify_keys
  return normalized_input if JSON::Validator.validate(@schema, normalized_input)

  valid_candidates.max_by { |candidate| matching_key_count(candidate) }
end