Class: Langchain::ToolDefinition::ParameterBuilder
- Inherits:
-
Object
- Object
- Langchain::ToolDefinition::ParameterBuilder
- Defined in:
- lib/langchain/tool_definition.rb
Overview
Builds parameter schemas for functions
Constant Summary collapse
- VALID_TYPES =
%w[object array string number integer boolean].freeze
Instance Method Summary collapse
-
#build { ... } ⇒ Hash
Builds the parameter schema.
-
#initialize(parent_type:) ⇒ ParameterBuilder
constructor
A new instance of ParameterBuilder.
-
#property(name = nil, type:, description: nil, enum: nil, required: false) {|Block| ... } ⇒ Object
(also: #item)
Defines a property in the schema.
Constructor Details
#initialize(parent_type:) ⇒ ParameterBuilder
Returns a new instance of ParameterBuilder.
129 130 131 132 |
# File 'lib/langchain/tool_definition.rb', line 129 def initialize(parent_type:) @schema = (parent_type == "object") ? {type: "object", properties: {}, required: []} : {} @parent_type = parent_type end |
Instance Method Details
#build { ... } ⇒ Hash
Builds the parameter schema
138 139 140 141 |
# File 'lib/langchain/tool_definition.rb', line 138 def build(&block) instance_eval(&block) @schema end |
#property(name = nil, type:, description: nil, enum: nil, required: false) {|Block| ... } ⇒ Object Also known as: item
Defines a property in the schema
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/langchain/tool_definition.rb', line 152 def property(name = nil, type:, description: nil, enum: nil, required: false, &block) validate_parameters(name:, type:, enum:, required:) prop = {type:, description:, enum:}.compact if block_given? # rubocop:disable Performance/BlockGivenWithExplicitBlock nested_schema = ParameterBuilder.new(parent_type: type).build(&block) case type when "object" if nested_schema[:properties].empty? raise ArgumentError, "Object properties must have at least one property defined within it" end prop = nested_schema when "array" if nested_schema.empty? raise ArgumentError, "Array properties must have at least one item defined within it" end prop[:items] = nested_schema end end if @parent_type == "object" @schema[:properties][name] = prop @schema[:required] << name.to_s if required else @schema = prop end end |