Class: OmniAI::Tool::Property

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

Overview

A property used for a tool parameter.

Examples:

OmniAI::Tool::Property.array(description: '...', items: ...)
OmniAI::Tool::Property.object(description: '...', properties: { ... }, required: %i[...])
OmniAI::Tool::Property.string(description: '...')
OmniAI::Tool::Property.integer(description: '...')
OmniAI::Tool::Property.number(description: '...')
OmniAI::Tool::Property.boolean(description: '...')

Defined Under Namespace

Modules: Type

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type:, description: nil, enum: nil) ⇒ OmniAI::Tool::Property

Parameters:

  • type (String)

    required - the type of the property

  • description (String) (defaults to: nil)

    optional - a description of the property

  • enum (Array) (defaults to: nil)

    optional - the possible values of the property



106
107
108
109
110
# File 'lib/omniai/tool/property.rb', line 106

def initialize(type:, description: nil, enum: nil)
  @type = type
  @description = description
  @enum = enum
end

Instance Attribute Details

#descriptionString? (readonly)

Returns:

  • (String, nil)


26
27
28
# File 'lib/omniai/tool/property.rb', line 26

def description
  @description
end

#enumArray<String>? (readonly)

Returns:



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

def enum
  @enum
end

#typeString (readonly)

Returns:

  • (String)


23
24
25
# File 'lib/omniai/tool/property.rb', line 23

def type
  @type
end

Class Method Details

.array(items:, min_items: nil, max_items: nil, description: nil) ⇒ OmniAI::Tool::Array

Examples:

property = OmniAI::Tool::Property.array(
  items: OmniAI::Tool::Property.string(description: 'The name of the person.'),
  description: 'A list of names.'
  min_items: 1,
  max_items: 5,
)

Parameters:

  • items (OmniAI::Tool::Property)

    required - the items in the array

  • min_items (Integer) (defaults to: nil)

    optional - the minimum number of items

  • max_items (Integer) (defaults to: nil)

    optional - the maximum number of items

  • description (String) (defaults to: nil)

    optional - a description of the array

Returns:



45
46
47
# File 'lib/omniai/tool/property.rb', line 45

def self.array(items:, min_items: nil, max_items: nil, description: nil)
  OmniAI::Tool::Array.new(items:, description:, min_items:, max_items:)
end

.boolean(description: nil, enum: nil) ⇒ OmniAI::Tool::Property

Parameters:

  • description (String) (defaults to: nil)

    optional - a description of the property

  • enum (Array<Boolean>) (defaults to: nil)

    optional - the possible values of the property

Returns:



73
74
75
# File 'lib/omniai/tool/property.rb', line 73

def self.boolean(description: nil, enum: nil)
  new(type: Type::BOOLEAN, description:, enum:)
end

.integer(description: nil, enum: nil) ⇒ OmniAI::Tool::Property

Parameters:

  • description (String) (defaults to: nil)

    optional - a description of the property

  • enum (Array<Integer>) (defaults to: nil)

    optinoal - the possible values of the property

Returns:



81
82
83
# File 'lib/omniai/tool/property.rb', line 81

def self.integer(description: nil, enum: nil)
  new(type: Type::INTEGER, description:, enum:)
end

.number(description: nil, enum: nil) ⇒ OmniAI::Tool::Property

Parameters:

  • description (String) (defaults to: nil)

    optional - a description of the property

  • enum (Array<Number>) (defaults to: nil)

    optional - the possible values of the property

Returns:



97
98
99
# File 'lib/omniai/tool/property.rb', line 97

def self.number(description: nil, enum: nil)
  new(type: Type::NUMBER, description:, enum:)
end

.object(properties: {}, required: [], description: nil) ⇒ OmniAI::Tool::Array

Examples:

property = OmniAI::Tool::Property.object(
  properties: {
    name: OmniAI::Tool::Property.string(description: 'The name of the person.'),
    age: OmniAI::Tool::Property.integer(description: 'The age of the person.'),
    employeed: OmniAI::Tool::Property.boolean(description: 'Is the person employeed?'),
  },
  description: 'A person.'
  required: %i[name]
)

Parameters:

  • properties (Hash<String, OmniAI::Tool::Property>) (defaults to: {})

    required - the properties of the object

  • requird (Array<Symbol>)

    optional - the required properties

  • description (String) (defaults to: nil)

    optional - a description of the object

Returns:



65
66
67
# File 'lib/omniai/tool/property.rb', line 65

def self.object(properties: {}, required: [], description: nil)
  OmniAI::Tool::Object.new(properties:, required:, description:)
end

.string(description: nil, enum: nil) ⇒ OmniAI::Tool::Property

Parameters:

  • description (String) (defaults to: nil)

    optional - a description of the property

  • enum (Array<String>) (defaults to: nil)

    optional - the possible values of the property

Returns:



89
90
91
# File 'lib/omniai/tool/property.rb', line 89

def self.string(description: nil, enum: nil)
  new(type: Type::STRING, description:, enum:)
end

Instance Method Details

#parse(value) ⇒ String, ...

Examples:

property.parse('123') #=> 123

Returns:

  • (String, Integer, Float, Boolean, Object)


128
129
130
131
132
133
134
135
# File 'lib/omniai/tool/property.rb', line 128

def parse(value)
  case @type
  when Type::INTEGER then Integer(value)
  when Type::STRING then String(value)
  when Type::NUMBER then Float(value)
  else value
  end
end

#serializeHash

Examples:

property.serialize #=> { type: 'string' }

Returns:

  • (Hash)


116
117
118
119
120
121
122
# File 'lib/omniai/tool/property.rb', line 116

def serialize
  {
    type: @type,
    description: @description,
    enum: @enum,
  }.compact
end