Class: OmniAI::Tool
- Inherits:
-
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.
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
#description ⇒ String?
30
31
32
|
# File 'lib/omniai/tool.rb', line 30
def description
@description
end
|
#function ⇒ Proc
24
25
26
|
# File 'lib/omniai/tool.rb', line 24
def function
@function
end
|
#name ⇒ String
27
28
29
|
# File 'lib/omniai/tool.rb', line 27
def name
@name
end
|
#parameters ⇒ Hash?
33
34
35
|
# File 'lib/omniai/tool.rb', line 33
def parameters
@parameters
end
|
Instance Method Details
#call(args = {}) ⇒ 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
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
|