Module: OmniAI::Schema

Defined in:
lib/omniai/schema.rb,
lib/omniai/schema/array.rb,
lib/omniai/schema/format.rb,
lib/omniai/schema/object.rb,
lib/omniai/schema/scalar.rb

Overview

Examples:

format = OmniAI::Schema.format(name: "Contact", schema: OmniAI::Schema.object(
  description: "A contact with a name, relationship, and addresses.",
  properties: {
    name: OmniAI::Schema.string,
    relationship: OmniAI::Schema.string(enum: %w[friend family]),
    addresses: OmniAI::Schema.array(
      items: OmniAI::Schema.object(
        title: "Address",
        description: "An address with street, city, state, and zip code.",
        properties: {
          street: OmniAI::Schema.string,
          city: OmniAI::Schema.string,
          state: OmniAI::Schema.string,
          zip: OmniAI::Schema.string,
        },
        required: %i[street city state zip]
      )
    ),
  },
  required: %i[name]
))

Defined Under Namespace

Classes: Array, Format, Object, Scalar

Class Method Summary collapse

Class Method Details

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

Examples:

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

Parameters:

  • items (OmniAI::Schema::Scalar)

    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:



85
86
87
# File 'lib/omniai/schema.rb', line 85

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

.boolean(description: nil, enum: nil) ⇒ OmniAI::Schema::Scalar

Examples:

OmniAI::Schema.boolean(description: "Is the person employed?") #=> OmniAI::Schema::Scalar

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:



117
118
119
# File 'lib/omniai/schema.rb', line 117

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

.build(kind, **args) ⇒ OmniAI::Schema::Object, ...



60
61
62
63
64
65
66
67
68
69
# File 'lib/omniai/schema.rb', line 60

def self.build(kind, **args)
  case kind
  when :array then array(**args)
  when :object then object(**args)
  when :boolean then boolean(**args)
  when :integer then integer(**args)
  when :number then number(**args)
  when :string then string(**args)
  end
end

.deserialize(data) ⇒ Object

Examples:

OmniAI::Schema.deserialize({
  type: 'object',
  title: 'Person',
  properties: { name: { type: 'string' } },
  required: %i[name],
}) # => OmniAI::Schema::Object
OmniAI::Schema.deserialize({
  type: 'array',
  items: { type: 'string' },
}) # => OmniAI::Schema::Array
OmniAI::Schema.deserialize({
  type: 'string',
  description: '...',
  enum: ['...', ],
}) # => OmniAI::Schema::Scalar

Parameters:



49
50
51
52
53
54
55
# File 'lib/omniai/schema.rb', line 49

def self.deserialize(data)
  case data["type"] || data[:type]
  when OmniAI::Schema::Array::TYPE then OmniAI::Schema::Array.deserialize(data)
  when OmniAI::Schema::Object::TYPE then OmniAI::Schema::Object.deserialize(data)
  else OmniAI::Schema::Scalar.deserialize(data)
  end
end

.format(name:, schema:) ⇒ OmniAI::Schema::Format

Examples:

OmniAI::Schema.format(name: "Contact", schema: OmniAI::Schema.object(...)) #=> OmniAI::Schema::Format

Parameters:

Returns:



161
162
163
# File 'lib/omniai/schema.rb', line 161

def self.format(name:, schema:)
  OmniAI::Schema::Format.new(name:, schema:)
end

.integer(description: nil, enum: nil) ⇒ OmniAI::Schema::Scalar

Examples:

OmniAI::Schema.integer(description: "The age of the person") #=> OmniAI::Schema::Scalar

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:



128
129
130
# File 'lib/omniai/schema.rb', line 128

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

.number(description: nil, enum: nil) ⇒ OmniAI::Schema::Scalar

Examples:

OmniAI::Schema.number(description: "The current temperature.") #=> OmniAI::Schema::Scalar

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:



150
151
152
# File 'lib/omniai/schema.rb', line 150

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

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

Examples:

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

Parameters:

  • title (String) (defaults to: nil)

    optional - the title of the object

  • properties (Hash<String, OmniAI::Schema::Scalar>) (defaults to: {})

    required - the properties of the object

  • required (Array<Symbol>) (defaults to: [])

    optional - the required properties

  • description (String) (defaults to: nil)

    optional - a description of the object

Returns:



106
107
108
# File 'lib/omniai/schema.rb', line 106

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

.string(description: nil, enum: nil) ⇒ OmniAI::Schema::Scalar

Examples:

OmniAI::Schema.string(description: "The name of the person.") #=> OmniAI::Schema::Scalar

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:



139
140
141
# File 'lib/omniai/schema.rb', line 139

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