Class: OmniAI::Tool

Inherits:
Object
  • Object
show all
Defined in:
lib/omniai/tool.rb,
lib/omniai/tool/array.rb,
lib/omniai/tool/object.rb,
lib/omniai/tool/property.rb,
lib/omniai/tool/parameters.rb

Overview

Usage:

fibonacci = proc do |n:|
  next(0) if n == 0
  next(1) if n == 1
  fibonacci.call(n: n - 1) + fibonacci.call(n: n - 2)
end

tool = OmniAI::Tool.new(fibonacci,
  name: 'Fibonacci',
  description: 'Cacluate the nth Fibonacci',
  parameters: OmniAI::Tool::Parameters.new(
    properties: {
      n: OmniAI::Tool::Property.integer(description: 'The nth Fibonacci number to calculate')
    },
    required: %i[n],
   )
)

Defined Under Namespace

Classes: Array, Object, Parameters, Property

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(function, name:, description: nil, parameters: nil) ⇒ Tool

Returns a new instance of Tool.

Parameters:

  • function (Proc)
  • name (String)
  • description (String) (defaults to: nil)
  • parameters (Hash) (defaults to: nil)


39
40
41
42
43
44
# File 'lib/omniai/tool.rb', line 39

def initialize(function, name:, description: nil, parameters: nil)
  @function = function
  @name = name
  @description = description
  @parameters = parameters
end

Instance Attribute Details

#descriptionString?

Returns:

  • (String, nil)


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

def description
  @description
end

#functionProc

Returns:

  • (Proc)


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

def function
  @function
end

#nameString

Returns:

  • (String)


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

def name
  @name
end

#parametersHash?

Returns:

  • (Hash, nil)


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

def parameters
  @parameters
end

Instance Method Details

#call(args = {}) ⇒ String

Examples:

tool.call({ "n" => 6 })
#=> 8

Parameters:

  • args (Hash) (defaults to: {})

Returns:

  • (String)


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

def call(args = {})
  @function.call(**(@parameters.is_a?(Tool::Parameters) ? @parameters.parse(args) : args))
end

#serialize(context: nil) ⇒ Hash

Examples:

tool.serialize
# {
#   type: 'function',
#   function: {
#     name: 'Fibonacci',
#     description: 'Calculate the nth Fibonacci number',
#     parameters: {
#       type: 'object',
#       properties: {
#         n: {
#           description: 'The nth Fibonacci number to calculate')
#           type: 'integer'
#         }
#       },
#       required: ['n']
#     }
#   }
# }

Parameters:

  • context (Context) (defaults to: nil)

    optional

Returns:

  • (Hash)


68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/omniai/tool.rb', line 68

def serialize(context: nil)
  serialize = context&.serializer(:tool)
  return serialize.call(self, context:) if serialize

  {
    type: 'function',
    function: {
      name: @name,
      description: @description,
      parameters: @parameters.is_a?(Tool::Parameters) ? @parameters.serialize : @parameters,
    }.compact,
  }
end