Class: ActiveAgent::Tool

Inherits:
Object
  • Object
show all
Defined in:
lib/active_agent/tool.rb

Overview

Abstract base class for tools that Agents can execute.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.description(desc = nil) ⇒ Object



7
8
9
10
11
12
13
# File 'lib/active_agent/tool.rb', line 7

def description(desc = nil)
  if desc
    @description = desc
  else
    @description || "Tool: #{name}"
  end
end

.param(name, type: :string, desc: '', required: true) ⇒ Object



15
16
17
# File 'lib/active_agent/tool.rb', line 15

def param(name, type: :string, desc: '', required: true)
  params_schema[name] = { type: type, description: desc, required: required }
end

.params_schemaObject



19
20
21
# File 'lib/active_agent/tool.rb', line 19

def params_schema
  @params_schema ||= {}
end

.to_openai_functionObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/active_agent/tool.rb', line 23

def to_openai_function
  properties = {}
  required_fields = []

  params_schema.each do |param_name, options|
    properties[param_name] = {
      type: options[:type].to_s,
      description: options[:description]
    }
    required_fields << param_name.to_s if options[:required]
  end

  {
    name: name.split('::').last.downcase,
    description: description,
    parameters: {
      type: 'object',
      properties: properties,
      required: required_fields
    }
  }
end

Instance Method Details

#perform(**_args) ⇒ Object

Raises:

  • (NotImplementedError)


51
52
53
# File 'lib/active_agent/tool.rb', line 51

def perform(**_args)
  raise NotImplementedError, "#{self.class.name}#perform must be implemented"
end

#to_openai_functionObject



47
48
49
# File 'lib/active_agent/tool.rb', line 47

def to_openai_function
  self.class.to_openai_function
end