Class: OmniAI::Tool::Array

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

Overview

Represents a schema object.

Examples:

array = OmniAI::Tool::Array.new(
  description: 'A list of people.',
  items: 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]
  ),
  min_items: 1,
  max_items: 5,
})

Constant Summary collapse

TYPE =
'array'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(items:, max_items: nil, min_items: nil, description: nil) ⇒ Array

Returns a new instance of Array.

Parameters:



43
44
45
46
47
48
# File 'lib/omniai/tool/array.rb', line 43

def initialize(items:, max_items: nil, min_items: nil, description: nil)
  @items = items
  @description = description
  @max_items = max_items
  @min_items = min_items
end

Instance Attribute Details

#descriptionString?

Returns:

  • (String, nil)


37
38
39
# File 'lib/omniai/tool/array.rb', line 37

def description
  @description
end

#itemsOmniAI::Tool::Object, ...



25
26
27
# File 'lib/omniai/tool/array.rb', line 25

def items
  @items
end

#max_itemsInteger?

Returns:

  • (Integer, nil)


29
30
31
# File 'lib/omniai/tool/array.rb', line 29

def max_items
  @max_items
end

#min_itemsInteger?

Returns:

  • (Integer, nil)


33
34
35
# File 'lib/omniai/tool/array.rb', line 33

def min_items
  @min_items
end

Instance Method Details

#parse(args) ⇒ Array

Examples:

array.parse(['1', '2', '3']) # => [1, 2, 3]

Parameters:

Returns:



69
70
71
# File 'lib/omniai/tool/array.rb', line 69

def parse(args)
  args.map { |arg| @items.parse(arg) }
end

#serializeHash

Examples:

array.serialize # => { type: 'array', items: { type: 'string' } }

Returns:

  • (Hash)


54
55
56
57
58
59
60
61
62
# File 'lib/omniai/tool/array.rb', line 54

def serialize
  {
    type: TYPE,
    description: @description,
    items: @items.serialize,
    maxItems: @max_items,
    minItems: @min_items,
  }.compact
end