Class: OxAiWorkers::ToolDefinition::ParameterBuilder
- Inherits:
-
Object
- Object
- OxAiWorkers::ToolDefinition::ParameterBuilder
- Defined in:
- lib/oxaiworkers/tool_definition.rb
Overview
Builds parameter schemas for functions
Constant Summary collapse
- VALID_TYPES =
%w[object array string number integer boolean null].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: true) {|Block| ... } ⇒ Object
(also: #item)
Defines a property in the schema.
Constructor Details
#initialize(parent_type:) ⇒ ParameterBuilder
Returns a new instance of ParameterBuilder.
184 185 186 187 |
# File 'lib/oxaiworkers/tool_definition.rb', line 184 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
193 194 195 196 |
# File 'lib/oxaiworkers/tool_definition.rb', line 193 def build(&) instance_eval(&) @schema end |
#property(name = nil, type:, description: nil, enum: nil, required: true) {|Block| ... } ⇒ Object Also known as: item
Defines a property in the schema
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 |
# File 'lib/oxaiworkers/tool_definition.rb', line 207 def property(name = nil, type:, description: nil, enum: nil, required: true, &) validate_parameters(name:, type:, enum:, required:) prop = { type:, description:, enum: }.compact if block_given? nested_schema = ParameterBuilder.new(parent_type: type).build(&) 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 # @schema[:additionalProperties] = false if @strict else @schema = prop end end |