Class: OmniAI::Tool::Object

Inherits:
Object
  • Object
show all
Defined in:
lib/omniai/tool/object.rb

Overview

Represents a schema object.

Examples:

object = OmniAI::Tool::Object.new(
  properties: {
    name: OmniAI::Tool::Property.string(description: 'The name of the person.'),
    age: OmniAI::Tool::Property.integer(description: 'The age of the person.'),
  },
  required: %i[name]
})

Direct Known Subclasses

Parameters

Constant Summary collapse

TYPE =
'object'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(properties: {}, required: [], description: nil) ⇒ OmniAI::Tool::Parameters

Parameters:

  • properties (Hash) (defaults to: {})
  • required (Array<String>) (defaults to: [])


33
34
35
36
37
# File 'lib/omniai/tool/object.rb', line 33

def initialize(properties: {}, required: [], description: nil)
  @properties = properties
  @required = required
  @description = description
end

Instance Attribute Details

#descriptionString?

Returns:

  • (String, nil)


28
29
30
# File 'lib/omniai/tool/object.rb', line 28

def description
  @description
end

#propertiesHash

Returns:

  • (Hash)


20
21
22
# File 'lib/omniai/tool/object.rb', line 20

def properties
  @properties
end

#requiredArray<String>

Returns:



24
25
26
# File 'lib/omniai/tool/object.rb', line 24

def required
  @required
end

Instance Method Details

#parse(args) ⇒ Hash

Parameters:

  • args (Hash)

Returns:

  • (Hash)


52
53
54
55
56
57
58
59
# File 'lib/omniai/tool/object.rb', line 52

def parse(args)
  result = {}
  @properties.each do |name, property|
    value = args[String(name)]
    result[name.intern] = property.parse(value) unless value.nil?
  end
  result
end

#serializeHash

Returns:

  • (Hash)


40
41
42
43
44
45
46
47
# File 'lib/omniai/tool/object.rb', line 40

def serialize
  {
    type: TYPE,
    description: @description,
    properties: @properties.transform_values(&:serialize),
    required: @required,
  }.compact
end