Class: Langchain::LLM::UnifiedParameters

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/langchain/llm/unified_parameters.rb

Direct Known Subclasses

Null

Defined Under Namespace

Classes: Null

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(schema:, parameters: {}) ⇒ UnifiedParameters

Returns a new instance of UnifiedParameters.



15
16
17
18
19
20
21
22
23
24
# File 'lib/langchain/llm/unified_parameters.rb', line 15

def initialize(schema:, parameters: {})
  @schema = schema || {}
  @aliases = {}
  @remapped = {}
  @ignored = Set.new
  @schema.each do |name, param|
    @aliases[name] = Set.new(Array(param[:aliases])) if param[:aliases]
  end
  @parameters = to_params(parameters.to_h) if !parameters.to_h.empty?
end

Instance Attribute Details

#aliasesObject (readonly)

Returns the value of attribute aliases.



7
8
9
# File 'lib/langchain/llm/unified_parameters.rb', line 7

def aliases
  @aliases
end

#ignoredObject (readonly)

Returns the value of attribute ignored.



7
8
9
# File 'lib/langchain/llm/unified_parameters.rb', line 7

def ignored
  @ignored
end

#parametersObject (readonly)

Returns the value of attribute parameters.



7
8
9
# File 'lib/langchain/llm/unified_parameters.rb', line 7

def parameters
  @parameters
end

#remappedObject (readonly)

Returns the value of attribute remapped.



7
8
9
# File 'lib/langchain/llm/unified_parameters.rb', line 7

def remapped
  @remapped
end

#schemaObject (readonly)

Returns the value of attribute schema.



7
8
9
# File 'lib/langchain/llm/unified_parameters.rb', line 7

def schema
  @schema
end

Instance Method Details

#<=>(other) ⇒ Object



84
85
86
# File 'lib/langchain/llm/unified_parameters.rb', line 84

def <=>(other)
  to_params.<=>(other.to_params)
end

#[](key) ⇒ Object



88
89
90
# File 'lib/langchain/llm/unified_parameters.rb', line 88

def [](key)
  to_params[key]
end

#alias_field(field_name, as:) ⇒ Object



71
72
73
74
# File 'lib/langchain/llm/unified_parameters.rb', line 71

def alias_field(field_name, as:)
  @aliases[field_name] ||= Set.new
  @aliases[field_name] << as
end

#each(&block) ⇒ Object



80
81
82
# File 'lib/langchain/llm/unified_parameters.rb', line 80

def each(&block)
  to_params.each(&block)
end

#ignore(*field_names) ⇒ Object



67
68
69
# File 'lib/langchain/llm/unified_parameters.rb', line 67

def ignore(*field_names)
  @ignored.merge(field_names)
end

#remap(field_map) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/langchain/llm/unified_parameters.rb', line 48

def remap(field_map)
  @remapped ||= {}
  @remapped.merge!(field_map)
  field_map.each do |field, renamed_field|
    @schema[renamed_field] = @schema[field]
  end
end

#to_hObject



76
77
78
# File 'lib/langchain/llm/unified_parameters.rb', line 76

def to_h
  @parameters.to_h
end

#to_params(params = {}) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/langchain/llm/unified_parameters.rb', line 26

def to_params(params = {})
  # if params are provided, reset any previously initialized
  @parameters = params if !params.empty?
  @parameters = (@parameters || {}).merge!(params).slice(*schema.keys)
  @aliases.each do |field, aliased_keys|
    # favor existing keys in case of conflicts,
    # and check for multiples
    aliased_keys.each do |alias_key|
      @parameters[field] ||= params[alias_key] if value_present?(params[alias_key])
    end
  end
  @schema.each do |field, param_options|
    param_options ||= {}
    default = param_options[:default]
    @parameters[field] ||= default if value_present?(default)
  end
  @remapped.each do |field, renamed_field|
    @parameters[renamed_field] = @parameters[field] if value_present?(@parameters[field])
  end
  @parameters = @parameters.except(*@ignored + @remapped.keys)
end

#update(schema = {}) ⇒ Object



56
57
58
59
60
61
62
63
64
65
# File 'lib/langchain/llm/unified_parameters.rb', line 56

def update(schema = {})
  @schema.merge!(schema)
  schema.each do |name, param|
    if param[:aliases]
      @aliases[name] ||= Set.new
      @aliases[name] << param[:aliases]
    end
  end
  self
end