Class: DeepAgents::Tool

Inherits:
Object
  • Object
show all
Defined in:
lib/deepagents/tools.rb

Overview

Tool class for defining tools that can be used by the agent

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, description, parameters = [], &block) ⇒ Tool

Returns a new instance of Tool.

Raises:

  • (ArgumentError)


8
9
10
11
12
13
14
15
16
17
# File 'lib/deepagents/tools.rb', line 8

def initialize(name, description, parameters = [], &block)
  raise ArgumentError, "Tool name cannot be nil or empty" if name.nil? || name.empty?
  raise ArgumentError, "Tool description cannot be nil or empty" if description.nil? || description.empty?
  raise ArgumentError, "Tool function block must be provided" unless block_given?

  @name = name
  @description = description
  @parameters = parameters
  @function = block
end

Instance Attribute Details

#descriptionObject (readonly)

Returns the value of attribute description.



6
7
8
# File 'lib/deepagents/tools.rb', line 6

def description
  @description
end

#functionObject (readonly)

Returns the value of attribute function.



6
7
8
# File 'lib/deepagents/tools.rb', line 6

def function
  @function
end

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/deepagents/tools.rb', line 6

def name
  @name
end

#parametersObject (readonly)

Returns the value of attribute parameters.



6
7
8
# File 'lib/deepagents/tools.rb', line 6

def parameters
  @parameters
end

Instance Method Details

#call(*args, **kwargs) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/deepagents/tools.rb', line 19

def call(*args, **kwargs)
  begin
    @function.call(*args, **kwargs)
  rescue => e
    raise ToolError.new(@name, e.message)
  end
end

#to_hObject



27
28
29
30
31
32
33
# File 'lib/deepagents/tools.rb', line 27

def to_h
  {
    name: @name,
    description: @description,
    parameters: @parameters
  }
end