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:

class Weather < OmniAI::Tool
  description 'Find the weather for a location'

  parameter :location, :string, description: 'The location to find the weather for (e.g. "Toronto, Canada").'
  parameter :unit, :string, description: 'The unit of measurement (e.g. "Celcius" or "Fahrenheit").'
  required %i[location]

  def execute!(location:)
    # ...
  end
end

Defined Under Namespace

Classes: Array, Object, Parameters, Property

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(function = method(:execute), name: self.class.namify, description: self.class.description, parameters: self.class.parameters) ⇒ Tool

Returns a new instance of Tool.

Parameters:

  • function (Proc) (defaults to: method(:execute))
  • name (String) (defaults to: self.class.namify)
  • description (String) (defaults to: self.class.description)
  • parameters (OmniAI::Tool::Parameters) (defaults to: self.class.parameters)


76
77
78
79
80
81
82
83
84
85
86
# File 'lib/omniai/tool.rb', line 76

def initialize(
  function = method(:execute),
  name: self.class.namify,
  description: self.class.description,
  parameters: self.class.parameters
)
  @function = function
  @name = name
  @description = description
  @parameters = parameters
end

Instance Attribute Details

#functionProc

Returns:

  • (Proc)


58
59
60
# File 'lib/omniai/tool.rb', line 58

def function
  @function
end

#nameString

Returns:

  • (String)


62
63
64
# File 'lib/omniai/tool.rb', line 62

def name
  @name
end

#parametersObject

@!attribute

@return [Hash, nil]


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

def parameters
  @parameters
end

Class Method Details

.description(description = nil) ⇒ Object

Parameters:

  • description (String) (defaults to: nil)


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

def description(description = nil)
  return @description if description.nil?

  @description = description
end

.namifyString

Converts a class name to a tool:

- e.g. "IBM::Watson::SearchTool" => "ibm_watson_search"

Returns:

  • (String)


46
47
48
49
50
51
52
53
# File 'lib/omniai/tool.rb', line 46

def namify
  name
    .gsub("::", "_")
    .gsub(/(?<prefix>[A-Z+])(?<suffix>[A-Z][a-z])/, '\k<prefix>_\k<suffix>')
    .gsub(/(?<prefix>[a-z])(?<suffix>[A-Z])/, '\k<prefix>_\k<suffix>')
    .gsub(/_tool$/i, "")
    .downcase
end

.parameter(name, kind) ⇒ Object

Parameters:

  • name (Symbol)
  • kind (Symbol)


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

def parameter(name, kind, **)
  parameters.properties[name] = Property.build(kind, **)
end

.parametersOmniAI::Tool::Parameters



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

def parameters
  @parameters ||= Parameters.new
end

.required(names) ⇒ Object

Parameters:



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

def required(names)
  parameters.required = names
end

Instance Method Details

#call(args = {}) ⇒ String

Examples:

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

Parameters:

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

Returns:

  • (String)


134
135
136
# File 'lib/omniai/tool.rb', line 134

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

#descriptionObject



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

def description
  @description
end

#executeObject

Raises:

  • (NotImplementedError)


124
125
126
# File 'lib/omniai/tool.rb', line 124

def execute(...)
  raise NotImplementedError, "#{self.class}#execute undefined"
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)


110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/omniai/tool.rb', line 110

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