Class: RubyLLM::Tool::SchemaDefinition
- Inherits:
-
Object
- Object
- RubyLLM::Tool::SchemaDefinition
- Defined in:
- lib/ruby_llm/tool.rb
Overview
:nodoc:
Class Method Summary collapse
- .default_items_schema ⇒ Object
- .from_parameters(parameters, allow_empty: false) ⇒ Object
- .map_type(type) ⇒ Object
Instance Method Summary collapse
-
#initialize(schema: nil, block: nil) ⇒ SchemaDefinition
constructor
A new instance of SchemaDefinition.
- #json_schema ⇒ Object
- #present? ⇒ Boolean
Constructor Details
#initialize(schema: nil, block: nil) ⇒ SchemaDefinition
Returns a new instance of SchemaDefinition.
399 400 401 402 |
# File 'lib/ruby_llm/tool.rb', line 399 def initialize(schema: nil, block: nil) @schema = schema @block = block end |
Class Method Details
.default_items_schema ⇒ Object
395 396 397 |
# File 'lib/ruby_llm/tool.rb', line 395 def self.default_items_schema { type: 'string' } end |
.from_parameters(parameters, allow_empty: false) ⇒ Object
356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 |
# File 'lib/ruby_llm/tool.rb', line 356 def self.from_parameters(parameters, allow_empty: false) return nil if parameters.nil? || (parameters.empty? && !allow_empty) properties = parameters.to_h do |name, param| schema = { type: map_type(param.type), description: param.description }.compact schema[:items] = default_items_schema if schema[:type] == 'array' [name.to_s, schema] end required = parameters.select { |_, param| param.required }.keys.map(&:to_s) json_schema = { type: 'object', properties: properties, required: required, additionalProperties: false, strict: true } new(schema: json_schema) end |
.map_type(type) ⇒ Object
383 384 385 386 387 388 389 390 391 392 393 |
# File 'lib/ruby_llm/tool.rb', line 383 def self.map_type(type) case type.to_s when 'integer', 'int' then 'integer' when 'number', 'float', 'double' then 'number' when 'boolean' then 'boolean' when 'array' then 'array' when 'object' then 'object' else 'string' end end |
Instance Method Details
#json_schema ⇒ Object
408 409 410 411 412 |
# File 'lib/ruby_llm/tool.rb', line 408 def json_schema @json_schema ||= RubyLLM::Support::Utils.( RubyLLM::Support::Utils.deep_stringify_keys(resolve_schema) ) end |
#present? ⇒ Boolean
404 405 406 |
# File 'lib/ruby_llm/tool.rb', line 404 def present? @schema || @block end |